UNPKG

2.24 kBMarkdownView Raw
1# fastify-plugin
2
3[![Greenkeeper badge](https://badges.greenkeeper.io/fastify/fastify-plugin.svg)](https://greenkeeper.io/)
4
5[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat)](http://standardjs.com/)
6[![Build Status](https://travis-ci.org/fastify/fastify-plugin.svg?branch=master)](https://travis-ci.org/fastify/fastify-plugin)
7
8`fastify-plugin` is a plugin helper for [Fastify](https://github.com/fastify/fastify).
9
10When you build plugins for Fastify and you want that them to be accessible in the same context where you require them, you have two ways:
111. Use the `skip-override` hidden property
122. Use this module
13
14In addition if you use this module when creating new plugins, you can declare the dependencies, the name and the expected Fastify version that your plugin needs.
15
16#### Usage
17`fastify-plugin` can do three things for you:
18- Add the `skip-override` hidden property
19- Check the bare-minimum version of Fastify
20- Pass some custom metadata of the plugin to Fastify
21
22Example:
23```js
24const fp = require('fastify-plugin')
25
26module.exports = fp(function (fastify, opts, next) {
27 // your plugin code
28 next()
29})
30```
31
32If you need to set a bare-minimum version of Fastify for your plugin, just add the [semver](http://semver.org/) range that you need:
33```js
34const fp = require('fastify-plugin')
35
36module.exports = fp(function (fastify, opts, next) {
37 // your plugin code
38 next()
39}, { fastify: '0.x' })
40```
41
42If you need to check the Fastify version only, you can pass just the version string.
43
44You can check [here](https://github.com/npm/node-semver#ranges) how to define a `semver` range.
45
46You can also pass some metadata that will be handled by Fastify, such as the dependencies of your plugin.
47```js
48const fp = require('fastify-plugin')
49
50function plugin (fastify, opts, next) {
51 // your plugin code
52 next()
53}
54
55module.exports = fp(plugin, {
56 fastify: '0.x',
57 decorators: {
58 fastify: ['plugin1', 'plugin2'],
59 reply: ['compress']
60 },
61 dependencies: ['plugin1-name', 'plugin2-name']
62})
63```
64
65## Acknowledgements
66
67This project is kindly sponsored by:
68- [nearForm](http://nearform.com)
69- [LetzDoIt](http://www.letzdoitapp.com/)
70
71## License
72
73Licensed under [MIT](./LICENSE).