0.0.4 • Published 6 years ago

fastify-accept-version-header v0.0.4

Weekly downloads
1
License
MIT
Repository
github
Last release
6 years ago

fastify-accept-version-header

A plugin for Fastify to get the Accept-Version header assigned to the request object as apiVersion.
The default value is 1.0.0 and will be overwritten when an Accept-Version header is send.
Routes need a version property, the plugin will check and route the request when the version is supported. The version can be a string or an array of strings.

NPM

How to install

npm i --save fastify-accept-version-header

How to use

const fastify = require('fastify')();

fastify.register(require('fastify-accept-version-header'));

fastify.route({
  method: 'GET',
  url: '/',
  version: '1.0.0',
  beforeHandler: function (request, reply, done) {
    console.log('version', request.apiVersion);
    done()
  },
  handler: (request, reply) => {
    reply.send({ version: request.apiVersion });
  }
});

fastify.get('/', {version: ['1.0.0', '2.0.0']}, (request, reply) => {
  reply.send({ version: request.apiVersion });
});

fastify.listen(3000, (err) => {
  if (err) throw err
  console.log(`server listening on ${fastify.server.address().port}`)
});

Test

$ npm i && npm test

Security

Node Security CLI to identify known vulnerabilities in the project.
Audits an the project using auditjs to identify known vulnerabilities and outdated package versions.

$ npm run scan

Feedback

Star this repo if you found it useful. Use the github issue tracker to give feedback on this repo.