fastify-accepts
Advanced tools
Comparing version 2.2.0 to 2.3.0
63
index.js
'use strict' | ||
const accepts = require('accepts') | ||
const warning = require('process-warning')() | ||
warning.create('FastifyWarning.fastify-accepts', 'FST_MODULE_DEP_fastify-accepts'.toUpperCase(), 'fastify-accepts has been deprecated. Use @fastify/accepts@3.0.0 instead.') | ||
warning.emit('FST_MODULE_DEP_fastify-accepts'.toUpperCase()) | ||
const fp = require('fastify-plugin') | ||
const acceptsObjectSymbol = Symbol('acceptsObject') | ||
const methodNames = [ | ||
'charset', 'charsets', | ||
'encoding', 'encodings', | ||
'language', 'languages', | ||
'type', 'types' | ||
] | ||
function acceptsMethod () { | ||
if (!this.raw[acceptsObjectSymbol]) { | ||
this.raw[acceptsObjectSymbol] = accepts(this.raw) | ||
} | ||
return this.raw[acceptsObjectSymbol] | ||
} | ||
function replyAcceptMethod () { | ||
if (!this.request[acceptsObjectSymbol]) { | ||
this.request[acceptsObjectSymbol] = accepts(this.request.raw) | ||
} | ||
return this.request[acceptsObjectSymbol] | ||
} | ||
function fastifyAcceptHeader (fastify, options, done) { | ||
const decorateReplyToo = options.decorateReply | ||
fastify.decorateRequest('accepts', acceptsMethod) | ||
methodNames.forEach(methodName => { | ||
fastify.decorateRequest(methodName, function (arr) { | ||
const acceptsObject = this.accepts() | ||
if (arguments.length === 0) return acceptsObject[methodName]() | ||
return acceptsObject[methodName](arr) | ||
}) | ||
}) | ||
if (decorateReplyToo) { | ||
fastify.decorateReply('requestAccepts', replyAcceptMethod) | ||
methodNames.forEach(methodName => { | ||
const capitalizedMethodName = methodName.replace(/(?:^|\s)\S/g, a => a.toUpperCase()) | ||
fastify.decorateReply('request' + capitalizedMethodName, function (arr) { | ||
const acceptsObject = this.requestAccepts() | ||
if (arguments.length === 0) return acceptsObject[methodName]() | ||
return acceptsObject[methodName](arr) | ||
}) | ||
}) | ||
} | ||
done() | ||
} | ||
module.exports = fp(fastifyAcceptHeader, { | ||
fastify: '3.x', | ||
name: 'fastify-accepts' | ||
}) | ||
module.exports = require('fastify-accepts-deprecated') |
{ | ||
"name": "fastify-accepts", | ||
"version": "2.2.0", | ||
"description": "Add accept parser to fastify", | ||
"version": "2.3.0", | ||
"main": "index.js", | ||
"devDependencies": { | ||
"@types/accepts": "^1.3.5", | ||
"@types/node": "^17.0.0", | ||
"fastify": "^3.0.0-rc.4", | ||
"pre-commit": "^1.2.2", | ||
"request": "^2.88.0", | ||
"snazzy": "^9.0.0", | ||
"standard": "^16.0.0", | ||
"tap": "^16.0.0", | ||
"typescript": "^4.0.2", | ||
"tsd": "^0.19.0" | ||
"license": "MIT", | ||
"repository": { | ||
"url": "git://github.com/fastify/fastify-accepts.git" | ||
}, | ||
"engines": { | ||
"node": ">=10" | ||
}, | ||
"homepage": "https://github.com/fastify/fastify-accepts", | ||
"dependencies": { | ||
"accepts": "^1.3.5", | ||
"fastify-plugin": "^3.0.0" | ||
}, | ||
"scripts": { | ||
"lint": "standard | snazzy", | ||
"test": "npm run lint && npm run unit && npm run typescript", | ||
"test:ci": "npm run lint && tap test.js --coverage-report=lcovonly && npm run typescript", | ||
"typescript": "tsd", | ||
"unit": "tap --100 test.js" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/fastify/fastify-accepts.git" | ||
}, | ||
"author": "allevo", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/fastify/fastify-accepts/issues" | ||
}, | ||
"homepage": "https://github.com/fastify/fastify-accepts#readme" | ||
"process-warning": "^1.0.0", | ||
"fastify-accepts-deprecated": "npm:fastify-accepts@2.2.0" | ||
} | ||
} |
# fastify-accepts | ||
![CI](https://github.com/fastify/fastify-accepts/workflows/CI/badge.svg) | ||
[![NPM version](https://img.shields.io/npm/v/fastify-accepts.svg?style=flat)](https://www.npmjs.com/package/fastify-accepts) | ||
[![Known Vulnerabilities](https://snyk.io/test/github/fastify/fastify-accepts/badge.svg)](https://snyk.io/test/github/fastify/fastify-accepts) | ||
[![Coverage Status](https://coveralls.io/repos/github/fastify/fastify-accepts/badge.svg?branch=master)](https://coveralls.io/github/fastify/fastify-accepts?branch=master) | ||
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](https://standardjs.com/) | ||
Add accepts parser to fastify | ||
## Install | ||
`npm install --save fastify-accepts` | ||
## Usage | ||
```js | ||
const fastify = require('fastify') | ||
const Boom = require('boom') | ||
fastify.register(require('fastify-accepts')) | ||
fastify.post('/', function (req, reply) { | ||
const accept = req.accepts() // Accepts object | ||
switch(accept.type(['json', 'html'])) { | ||
case 'json': | ||
reply.type('application/json').send({hello: 'world'}) | ||
break | ||
case 'html': | ||
reply.type('text/html').send('<b>hello, world!</b>') | ||
break | ||
default: | ||
reply.send(Boom.notAcceptable('unacceptable')) | ||
break | ||
} | ||
}) | ||
``` | ||
See [accepts package](https://www.npmjs.com/package/accepts) for all available APIs. | ||
This plugin adds to `Request` object all `Accepts` object methods. | ||
```js | ||
fastify.post('/', function (req, reply) { | ||
req.charset(['utf-8']) | ||
req.charsets() | ||
req.encoding(['gzip', 'compress']) | ||
req.encodings() | ||
req.language(['es', 'en']) | ||
req.languages() | ||
req.type(['image/png', 'image/tiff']) | ||
req.types() | ||
}) | ||
``` | ||
## License | ||
Licensed under [MIT](./LICENSE) | ||
`fastify-accepts@2.3.0` has been deprecated. Please use | ||
`@fastify/accepts@3.0.0` instead. |
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
Trivial Package
Supply chain riskPackages less than 10 lines of code are easily copied into your own project and may not warrant the additional supply chain risk of an external dependency.
Found 1 instance in 1 package
No contributors or author data
MaintenancePackage does not specify a list of contributors or an author in package.json.
Found 1 instance in 1 package
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
0
815
3
5
2
1
5
1
3
+ Addedprocess-warning@^1.0.0
+ Addedfastify-accepts@2.2.0(transitive)
+ Addedprocess-warning@1.0.0(transitive)
- Removedaccepts@^1.3.5
- Removedfastify-plugin@^3.0.0