New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

fastify-accepts

Package Overview
Dependencies
Maintainers
17
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fastify-accepts - npm Package Compare versions

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.
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc