0.3.0 • Published 6 years ago
fastify-utils v0.3.0
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();