0.3.0 • Published 6 years ago

fastify-utils v0.3.0

Weekly downloads
-
License
MIT
Repository
-
Last release
6 years ago

fastify-utils

Install

npm install fastify-utils --save

Usage

Example:

makePlugin

const fastify = require('fastify')();
const { makePlugin } = require('fastify-utils');

const plugin1 = makePlugin({
  decorators: {
    getName: function(fastify, options) {
      return () => options.option1;
    },
  },

  requestDecorators: {
    getName: function(fastify, options) {
      return () => options.option1;
    },
  },

  routes: [
    {
      method: 'GET',
      url: '/foo',
      handler: async function() {
        return `namespace: ${this.options.namespace}, \ngetName(): ${this.decorators.getName()}`;
      },
    },
  ],

  hooks: {
    async onRequest(req, reply) {
      console.log('onRequest');
      return;
    },
  },

  middlewares: [
    function(req, reply, next) {
      console.log('middle');
      next();
    },
  ],
});

fastify.register(plugin1, { option1: 'value 1' });
fastify.register(plugin1, { namespace: 'ok', option1: 'value 2' });
fastify.register(plugin1, { namespace: 'o98k', routePrefix: '/888', option1: 'value 3' });

fastify.get('/', async (request, reply) => {
  const getName1 = fastify.getDecorator('o98k', 'getName');
  const getName2 = request.getDecorator('ok', 'getName');
  return getName1() + ',' + getName2();
});

const start = async () => {
  try {
    await fastify.listen(3000);
  } catch (err) {
    fastify.log.error(err);
    process.exit(1);
  }
};

start();
0.3.0

6 years ago

0.2.1

6 years ago

0.1.0

6 years ago