{"id":21522213,"url":"https://github.com/greguz/fastify-dynareg","last_synced_at":"2025-03-17T17:23:17.539Z","repository":{"id":57233285,"uuid":"289301662","full_name":"greguz/fastify-dynareg","owner":"greguz","description":"Dynamic plugin register for Fastify","archived":false,"fork":false,"pushed_at":"2020-08-27T08:46:16.000Z","size":248,"stargazers_count":0,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-04-29T23:14:38.172Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/greguz.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-08-21T15:22:12.000Z","updated_at":"2020-08-27T08:46:19.000Z","dependencies_parsed_at":"2022-08-31T14:11:26.808Z","dependency_job_id":null,"html_url":"https://github.com/greguz/fastify-dynareg","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/greguz%2Ffastify-dynareg","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/greguz%2Ffastify-dynareg/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/greguz%2Ffastify-dynareg/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/greguz%2Ffastify-dynareg/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/greguz","download_url":"https://codeload.github.com/greguz/fastify-dynareg/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244075659,"owners_count":20393980,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-24T01:09:36.885Z","updated_at":"2025-03-17T17:23:17.504Z","avatar_url":"https://github.com/greguz.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# fastify-dynareg\n\n[![npm version](https://badge.fury.io/js/fastify-dynareg.svg)](https://badge.fury.io/js/fastify-dynareg) [![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com) [![Build Status](https://travis-ci.com/greguz/fastify-dynareg.svg?branch=master)](https://travis-ci.com/greguz/fastify-dynareg) [![Coverage Status](https://coveralls.io/repos/github/greguz/fastify-dynareg/badge.svg?branch=master)](https://coveralls.io/github/greguz/fastify-dynareg?branch=master)\n\nThis package is technically a plugin builder for Fastify. It exports a single function that accepts two arguments, a package name and a boolean indicating if that package is required, and returns a valid Fastify plugin. This library can be used to define both **optional** or **parametric** plugins.\n\n## dynareg(packageName[, isRequired])\n\n- `packageName` `\u003cString\u003e` The plugin's package name to `require()`.\n- `isRequired` `\u003cBoolean\u003e` Defaults to `false`.\n- Returns: `\u003cFastifyPlugin\u003e`\n\n### Optional plugin presence\n\nIn the next example, this library is used to make a plugin (`fastify-blipp`) optional. This behavior can be helpful for packages only used during development. If the plugin is not present, a nice debug log is written, and no errors are thrown.\n\n```javascript\nconst fastify = require('fastify')\nconst dynareg = require('fastify-dynareg')\n\nconst app = fastify()\n\n// This plugin will try to register fastify-blipp if It's installed\nconst optionalBlipp = dynareg('fastify-blipp')\n\n// You can still pass plugin options as usual\napp.register(optionalBlipp, {\n  my: 'options'\n})\n\napp.addHook('onReady', async () =\u003e {\n  // Because fastify-blipp is now optional\n  if (app.blipp) {\n    app.blipp()\n  }\n})\n\n// TODO: Setup and start you application...\n```\n\n### Parametric plugin presence\n\nThe plugin presence can also be parametric. Passing `true` as second argument will require the plugin presence.\n\n```javascript\nconst fastify = require('fastify')\nconst dynareg = require('fastify-dynareg')\n\nconst app = fastify()\n\n// Force fastify-blipp presence during development\nconst developmentBlipp = dynareg(\n  'fastify-blipp',\n  process.env.NODE_ENV !== 'production'\n)\n\napp.register(developmentBlipp)\n\napp.addHook('onReady', async () =\u003e {\n  // Because fastify-blipp is now optional\n  if (app.blipp) {\n    app.blipp()\n  }\n})\n\n// TODO: Setup and start you application...\n```\n\n### Fully parametric plugin\n\nIn the last example, both plugin's name and presence are parametric.\n\n```javascript\nconst fastify = require('fastify')\nconst dynareg = require('fastify-dynareg')\n\nconst app = fastify()\n\n// Declare a dynamic production plugin\nconst myDynamicPlugin = dynareg(\n  process.env.FASTIFY_PLUGIN,\n  process.env.NODE_ENV === 'production'\n)\n\napp.register(myDynamicPlugin, {\n  my: 'options'\n})\n\n// TODO: Setup and start you application...\n```\n\n## Example\n\n```javascript\nconst fastify = require('fastify')\nconst dynareg = require('fastify-dynareg')\n\nconst app = fastify({\n  logger: true\n})\n\n// Force fastify-blipp to be registered when env is not production\napp.register(\n  dynareg(\n    'fastify-blipp',\n    process.env.NODE_ENV !== 'production'\n  )\n)\n\napp.get('/', async (request, reply) =\u003e {\n  return { hello: 'world' }\n})\n\nconst start = async () =\u003e {\n  try {\n    await app.listen(3000)\n    // Use conditional blipp\n    if (app.blipp) {\n      app.blipp()\n    }\n    app.log.info(`server listening on ${app.server.address().port}`)\n  } catch (err) {\n    app.log.error(err)\n    process.exit(1)\n  }\n}\n\nstart()\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgreguz%2Ffastify-dynareg","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgreguz%2Ffastify-dynareg","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgreguz%2Ffastify-dynareg/lists"}