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 |
|
10 | When 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:
|
11 | 1. Use the `skip-override` hidden property
|
12 | 2. Use this module
|
13 |
|
14 | In 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 |
|
22 | Example:
|
23 | ```js
|
24 | const fp = require('fastify-plugin')
|
25 |
|
26 | module.exports = fp(function (fastify, opts, next) {
|
27 | // your plugin code
|
28 | next()
|
29 | })
|
30 | ```
|
31 |
|
32 | If 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
|
34 | const fp = require('fastify-plugin')
|
35 |
|
36 | module.exports = fp(function (fastify, opts, next) {
|
37 | // your plugin code
|
38 | next()
|
39 | }, { fastify: '0.x' })
|
40 | ```
|
41 |
|
42 | If you need to check the Fastify version only, you can pass just the version string.
|
43 |
|
44 | You can check [here](https://github.com/npm/node-semver#ranges) how to define a `semver` range.
|
45 |
|
46 | You can also pass some metadata that will be handled by Fastify, such as the dependencies of your plugin.
|
47 | ```js
|
48 | const fp = require('fastify-plugin')
|
49 |
|
50 | function plugin (fastify, opts, next) {
|
51 | // your plugin code
|
52 | next()
|
53 | }
|
54 |
|
55 | module.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 |
|
67 | This project is kindly sponsored by:
|
68 | - [nearForm](http://nearform.com)
|
69 | - [LetzDoIt](http://www.letzdoitapp.com/)
|
70 |
|
71 | ## License
|
72 |
|
73 | Licensed under [MIT](./LICENSE).
|