UNPKG

opinionated-machine

Version:

Very opinionated DI framework for fastify, built on top of awilix

36 lines 1.12 kB
import fp from 'fastify-plugin'; /** * Optional Fastify plugin that decorates `app.buildGatewayManifest()` and * (optionally) exposes the manifest over HTTP. * * @example * ```ts * await app.register(fastifyGatewayPlugin, { * context, * defaults: { service: 'users-api' }, * }) * * // From anywhere in the app: * const manifest = app.buildGatewayManifest() * * // Or fetched over HTTP (default route): * // GET /_gateway/manifest * ``` */ const fastifyGatewayPluginInner = (app, opts, done) => { const buildManifest = (overrides) => opts.context.buildGatewayManifest({ ...opts.defaults, ...(overrides ?? {}) }); app.decorate('buildGatewayManifest', buildManifest); if (typeof opts.exposeRoute === 'string' && opts.exposeRoute.length > 0) { app.route({ method: 'GET', url: opts.exposeRoute, handler: async () => buildManifest(), }); } done(); }; export const fastifyGatewayPlugin = fp(fastifyGatewayPluginInner, { name: '@opinionated-machine/gateway', fastify: '5.x', }); //# sourceMappingURL=fastifyGatewayPlugin.js.map