{"name":"fastify-plugin","version":"6.0.0","keywords":["plugin","helper","fastify"],"author":{"url":"http://delved.org","name":"Tomas Della Vedova - @delvedor"},"license":"MIT","_id":"fastify-plugin@6.0.0","maintainers":[{"name":"jsumners","email":"james.sumners@gmail.com"},{"name":"delvedor","email":"tommydelved@gmail.com"},{"name":"matteo.collina","email":"hello@matteocollina.com"},{"name":"eomm","email":"behemoth89@gmail.com"},{"name":"climba03003","email":"kaka@kakawebsitedemo.com"},{"name":"zekth","email":"vince.legoff@gmail.com"},{"name":"gurgunday","email":"hey@gurgun.day"},{"name":"ivan-tymoshenko","email":"ivan@tymoshenko.me"},{"name":"simoneb","email":"simone.busoli@gmail.com"},{"name":"fdawgs","email":"frazer.dev@icloud.com"},{"name":"metcoder95","email":"me@metcoder.dev"}],"contributors":[{"name":"Matteo Collina","email":"hello@matteocollina.com"},{"name":"Manuel Spigolon","email":"behemoth89@gmail.com"},{"name":"Aras Abbasi","email":"aras.abbasi@gmail.com"},{"url":"https://github.com/fdawgs","name":"Frazer Smith","email":"frazer.dev@icloud.com"}],"homepage":"https://github.com/fastify/fastify-plugin#readme","bugs":{"url":"https://github.com/fastify/fastify-plugin/issues"},"dist":{"shasum":"7cd659c6aad5f3bf07b4c59205412351a47d39c8","tarball":"http://registry-npm.ywwl.com/fastify-plugin/download/fastify-plugin-6.0.0.tgz","fileCount":24,"integrity":"sha512-fZOty7z3O7vOliF6d8bHE3wiEh1KcNnKEQensSgTk9C1DvN6nRLS++XVd86v33Hw/8u9Un8A1zDrQ8ujcQDHEg==","signatures":[{"sig":"MEQCICSSKg1s+yyHvziNf4d4Ty2Zj43ppgpOscjwykMkEEHjAiB9F0m5DKOqdRxmwVUpOOzXvUdVf4xvdCDM9Lk8Moi20A==","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":43496,"size":10350,"noattachment":false,"key":"/fastify-plugin/-/fastify-plugin-6.0.0.tgz"},"main":"index.js","type":"commonjs","types":"types/index.d.ts","funding":[{"url":"https://github.com/sponsors/fastify","type":"github"},{"url":"https://opencollective.com/fastify","type":"opencollective"}],"gitHead":"05c1c32b3cbfc86390243a0366e3288762c43bfc","scripts":{"lint":"eslint","test":"npm run test:unit && npm run test:typescript","lint:fix":"eslint --fix","test:unit":"c8 --100 node --test","test:coverage":"c8 node --test && c8 report --reporter=html","test:typescript":"tstyche"},"_npmUser":{"name":"fdawgs","email":"frazer.dev@icloud.com"},"repository":{"url":"git+https://github.com/fastify/fastify-plugin.git","type":"git"},"_npmVersion":"11.12.1","description":"Plugin helper for Fastify","directories":{},"_nodeVersion":"24.15.0","_hasShrinkwrap":false,"devDependencies":{"c8":"^11.0.0","eslint":"^9.17.0","fastify":"^5.0.0","tstyche":"^7.0.0","proxyquire":"^2.1.3","@types/node":"^25.0.3","neostandard":"^0.13.0","@fastify/type-provider-typebox":"^6.0.0"},"_npmOperationalInternal":{"tmp":"tmp/fastify-plugin_6.0.0_1781007209312_0.49541764546508316","host":"s3://npm-registry-packages-npm-production"},"readme":"# fastify-plugin\n\n[![CI](https://github.com/fastify/fastify-plugin/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/fastify/fastify-plugin/actions/workflows/ci.yml)\n[![NPM version](https://img.shields.io/npm/v/fastify-plugin.svg?style=flat)](https://www.npmjs.com/package/fastify-plugin)\n[![neostandard javascript style](https://img.shields.io/badge/code_style-neostandard-brightgreen?style=flat)](https://github.com/neostandard/neostandard)\n\n`fastify-plugin` is a plugin helper for [Fastify](https://github.com/fastify/fastify).\n\nWhen you build plugins for Fastify and you want them to be accessible in the same context where you require them, you have two ways:\n1. Use the `skip-override` hidden property\n2. Use this module\n\n__Note: the v4.x series of this module covers Fastify v4__\n__Note: the v2.x & v3.x series of this module covers Fastify v3. For Fastify v2 support, refer to the v1.x series.__\n\n## Install\n\n```sh\nnpm i fastify-plugin\n```\n\n## Usage\n`fastify-plugin` can do three things for you:\n- Add the `skip-override` hidden property\n- Check the bare-minimum version of Fastify\n- Pass some custom metadata of the plugin to Fastify\n\nExample using a callback:\n```js\nconst fp = require('fastify-plugin')\n\nmodule.exports = fp(function (fastify, opts, done) {\n  // your plugin code\n  done()\n})\n```\n\nExample using an [async](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function) function:\n```js\nconst fp = require('fastify-plugin')\n\n// A callback function param is not required for async functions\nmodule.exports = fp(async function (fastify, opts) {\n  // Wait for an async function to fulfill promise before proceeding\n  await exampleAsyncFunction()\n})\n```\n\n## Metadata\nIn 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.\n\n#### Fastify version\nIf you need to set a bare-minimum version of Fastify for your plugin, just add the [semver](https://semver.org/) range that you need:\n```js\nconst fp = require('fastify-plugin')\n\nmodule.exports = fp(function (fastify, opts, done) {\n  // your plugin code\n  done()\n}, { fastify: '5.x' })\n```\n\nIf you need to check the Fastify version only, you can pass just the version string.\n\nYou can check [here](https://github.com/npm/node-semver#ranges) how to define a `semver` range.\n\n#### Name\nFastify uses this option to validate the dependency graph, allowing it to ensure that no name collisions occur and making it possible to perform [dependency checks](https://github.com/fastify/fastify-plugin#dependencies).\n\n```js\nconst fp = require('fastify-plugin')\n\nfunction plugin (fastify, opts, done) {\n  // your plugin code\n  done()\n}\n\nmodule.exports = fp(plugin, {\n  fastify: '5.x',\n  name: 'your-plugin-name'\n})\n```\n\n#### Dependencies\nYou can also check if the `plugins` and `decorators` that your plugin intend to use are present in the dependency graph.\n> *Note:* This is the point where registering `name` of the plugins become important, because you can reference `plugin` dependencies by their [name](https://github.com/fastify/fastify-plugin#name).\n```js\nconst fp = require('fastify-plugin')\n\nfunction plugin (fastify, opts, done) {\n  // your plugin code\n  done()\n}\n\nmodule.exports = fp(plugin, {\n  fastify: '5.x',\n  decorators: {\n    fastify: ['plugin1', 'plugin2'],\n    reply: ['compress']\n  },\n  dependencies: ['plugin1-name', 'plugin2-name']\n})\n```\n\n#### Encapsulate\n\nBy default, `fastify-plugin` breaks the [encapsulation](https://github.com/fastify/fastify/blob/HEAD/docs/Reference/Encapsulation.md) but you can optionally keep the plugin encapsulated.\nThis allows you to set the plugin's name and validate its dependencies without making the plugin accessible.\n```js\nconst fp = require('fastify-plugin')\n\nfunction plugin (fastify, opts, done) {\n  // the decorator is not accessible outside this plugin\n  fastify.decorate('util', function() {})\n  done()\n}\n\nmodule.exports = fp(plugin, {\n  name: 'my-encapsulated-plugin',\n  fastify: '5.x',\n  decorators: {\n    fastify: ['plugin1', 'plugin2'],\n    reply: ['compress']\n  },\n  dependencies: ['plugin1-name', 'plugin2-name'],\n  encapsulate: true\n})\n```\n\n#### Bundlers and Typescript\n`fastify-plugin` adds a `.default` and `[name]` property to the passed in function.\nThe type definition would have to be updated to leverage this.\n\n## Known Issue: TypeScript Contextual Inference\n\n[Documentation Reference](https://www.typescriptlang.org/docs/handbook/functions.html#inferring-the-types)\n\nIt is common for developers to inline their plugin with fastify-plugin such as:\n\n```js\nfp((fastify, opts, done) => { done() })\nfp(async (fastify, opts) => { return })\n```\n\nTypeScript can sometimes infer the types of the arguments for these functions. Plugins in Fastify are recommended to be typed using either `FastifyPluginCallback` or `FastifyPluginAsync`. These two definitions only differ in two ways:\n\n1. The third argument `done` (the callback part)\n2. The return type `FastifyPluginCallback` or `FastifyPluginAsync`\n\nAt this time, TypeScript inference is not smart enough to differentiate by definition argument length alone.\n\nThus, if you are a TypeScript developer please use on the following patterns instead:\n\n```ts\n// Callback\n\n// Assign type directly\nconst pluginCallback: FastifyPluginCallback = (fastify, options, done) => { }\nfp(pluginCallback)\n\n// or define your own function declaration that satisfies the existing definitions\nconst pluginCallbackWithTypes = (fastify: FastifyInstance, options: FastifyPluginOptions, done: (error?: FastifyError) => void): void => { }\nfp(pluginCallbackWithTypes)\n// or inline\nfp((fastify: FastifyInstance, options: FastifyPluginOptions, done: (error?: FastifyError) => void): void => { })\n\n// Async\n\n// Assign type directly\nconst pluginAsync: FastifyPluginAsync = async (fastify, options) => { }\nfp(pluginAsync)\n\n// or define your own function declaration that satisfies the existing definitions\nconst pluginAsyncWithTypes = async (fastify: FastifyInstance, options: FastifyPluginOptions): Promise<void> => { }\nfp(pluginAsyncWithTypes)\n// or inline\nfp(async (fastify: FastifyInstance, options: FastifyPluginOptions): Promise<void> => { })\n```\n\n## Acknowledgments\n\nThis project is kindly sponsored by:\n- [nearForm](https://nearform.com)\n- [LetzDoIt](https://www.letzdoitapp.com/)\n\n## License\n\nLicensed under [MIT](./LICENSE).\n","publish_time":1781007209560,"_cnpm_publish_time":1781007209560,"dist-tags":{"latest":"6.0.1","next":"5.0.0"}}