@autofleet/fastify-boilerplate 1.0.19 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +46 -0
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -10,6 +10,7 @@ Fastify Boilerplate is a chunk of code that is likely to be required in any `Fas
|
|
|
10
10
|
- **Health Checks**: An endpoint to check if the server is `/alive`.
|
|
11
11
|
- **Stats Endpoint**: Provides server and application base data.
|
|
12
12
|
- **Tracing**: Adds HTTP request tracing.
|
|
13
|
+
- **Request Cancellation**: Provides `AbortSignal` on each request for operation cancellation, if request is aborted.
|
|
13
14
|
- **SWAGGER**: Optionally, adds a `/documentation` endpoint for the API documentation.
|
|
14
15
|
|
|
15
16
|
## Usage
|
|
@@ -36,3 +37,48 @@ await app.register(fastifyBoilerplatePlugin, {
|
|
|
36
37
|
|
|
37
38
|
});
|
|
38
39
|
```
|
|
40
|
+
|
|
41
|
+
## Request Cancellation
|
|
42
|
+
|
|
43
|
+
The boilerplate automatically provides an `AbortSignal` on each request through `req.signal`. This signal is automatically aborted when the client disconnects or the request encounters an error, allowing you to cancel ongoing operations gracefully.
|
|
44
|
+
|
|
45
|
+
### Usage Example
|
|
46
|
+
|
|
47
|
+
```javascript
|
|
48
|
+
app.route({
|
|
49
|
+
method: 'GET',
|
|
50
|
+
url: '/long-running-task',
|
|
51
|
+
handler: async (req, reply) => {
|
|
52
|
+
const { signal } = req;
|
|
53
|
+
|
|
54
|
+
try {
|
|
55
|
+
// Use the signal with fetch or other abortable operations
|
|
56
|
+
const response = await fetch('https://api.example.com/data', { signal });
|
|
57
|
+
const data = await response.json();
|
|
58
|
+
|
|
59
|
+
// Or check the signal manually during long operations
|
|
60
|
+
for (let i = 0; i < 1000; i++) {
|
|
61
|
+
if (signal.aborted) {
|
|
62
|
+
throw new Error('Operation was cancelled');
|
|
63
|
+
}
|
|
64
|
+
// Perform work...
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return { data };
|
|
68
|
+
} catch (error) {
|
|
69
|
+
if (signal.aborted) {
|
|
70
|
+
reply.code(499); // Client Closed Request
|
|
71
|
+
return { error: 'Request cancelled' };
|
|
72
|
+
}
|
|
73
|
+
throw error;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
### Best Practices
|
|
80
|
+
|
|
81
|
+
- Always check `signal.aborted` before performing expensive operations
|
|
82
|
+
- Use the signal with fetch requests, DB queries and other abortable APIs
|
|
83
|
+
- Handle cancellation gracefully by cleaning up resources
|
|
84
|
+
- Consider using Node.js `addAbortListener` for attaching listeners to the signal's abort event, as it handles them safely, and returns a disposer to cleanup the listener when no longer needed.
|
package/dist/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import*as e from"zod/v4";import t from"@fastify/helmet";import n from"fastify-plugin";import r from"@fastify/compress";import{handleErrorFastify as i}from"@autofleet/errors";import{systemAliveCheck as a}from"@autofleet/nitur/fastify";import{authFromUserIdHeaderPlugin as o}from"@autofleet/zehut";var
|
|
1
|
+
import*as e from"zod/v4";import t from"@fastify/helmet";import n from"fastify-plugin";import r from"@fastify/compress";import{handleErrorFastify as i}from"@autofleet/errors";import{systemAliveCheck as a}from"@autofleet/nitur/fastify";import{authFromUserIdHeaderPlugin as o}from"@autofleet/zehut";const s=(e,t,n)=>{let r=new WeakMap;function i(e){if(!r.has(e))return;let{boundOnceRequestClosed:t,boundOnceRequestError:n}=r.get(e);t&&e.raw.removeListener(`close`,t),n&&e.raw.removeListener(`error`,n)}function a(){if(i(this),r.has(this)&&this.raw.destroyed){let{controller:e}=r.get(this);if(e.signal.aborted)return;e.abort()}}function o(e){if(i(this),r.has(this)){let{controller:t}=r.get(this);t.abort(e)}}e.decorateRequest(`signal`,{getter(){let{raw:e,id:t}=this;if(e.socket.destroyed===!0)throw Error(`Socket for request with ID '${t}' already closed`);if(r.has(this))return r.get(this).controller.signal;let n=a.bind(this),i=o.bind(this),s=new AbortController;return r.set(this,{controller:s,boundOnceRequestClosed:n,boundOnceRequestError:i}),e.once(`close`,n),e.once(`error`,i),s.signal}}),e.addHook(`onResponse`,(e,t,n)=>{r.has(e)&&(i(e),r.delete(e)),n()}),n()},c=n(s);var l=`@autofleet/fastify-boilerplate`,u=`1.1.0`,d=`module`,f=`./dist/index.js`,p=`./dist/index.js`,m=`./dist/index.d.ts`,exports={".":{import:{types:`./dist/index.d.ts`,default:`./dist/index.js`}}},g={build:`tsdown`,prepublish:`node --run build`,test:`vitest`,coverage:`vitest --coverage`},_={type:`git`,url:`git+https://github.com/Autofleet/autorepo.git`},v=``,y=`Proprietary`,b={url:`https://github.com/Autofleet/autorepo/issues`},x=`https://github.com/Autofleet/autorepo/tree/master/packages/fastify-boilerplate#readme`,S={node:`>=22`},C={"@autofleet/logger":`*`},w={"@autofleet/logger":`*`,fastify:`^5.3.2`,"fastify-plugin":`^5.0.1`,"zod-openapi":`^5.0.1`},T=[`dist`],E={"@autofleet/errors":`*`,"@autofleet/nitur":`*`,"@autofleet/zehut":`^4.1.1`,"@fastify/compress":`^8.0.1`,"@fastify/helmet":`^13.0.1`,"@fastify/swagger":`^9.5.1`,"@fastify/swagger-ui":`^5.2.2`,"fastify-zod-openapi":`^5.0.2`,zod:`^3.25.75`},D={name:l,version:u,type:d,main:f,module:p,types:m,exports,scripts:g,repository:_,author:v,license:y,bugs:b,homepage:x,engines:S,peerDependencies:C,devDependencies:w,files:T,dependencies:E};const{version:O}=D,k=async(n,s)=>{let{logger:l,name:u,openApiName:d,version:f,tags:p,aliveEndpointOptions:m,eagerLoadUserPermissions:h=!0,customPermissionLoader:g}=s;if(n.decorateRequest(`logger`,{getter:()=>l}),n.register(i,{fallbackMsg:`Unhandled error caught`}),n.register(t),n.register(o,{eagerLoadUserPermissions:h,customPermissionLoader:g}),n.register(r,{global:!0,threshold:1024}),p?.length){let[e,{default:t},{default:r}]=await Promise.all([import(`fastify-zod-openapi`),import(`@fastify/swagger`),import(`@fastify/swagger-ui`)]),{validatorCompiler:i,serializerCompiler:a,fastifyZodOpenApiTransform:o,fastifyZodOpenApiTransformObject:s,fastifyZodOpenApiPlugin:c}=e;n.setValidatorCompiler(i),n.setSerializerCompiler(a),await n.register(c),await n.register(t,{openapi:{openapi:`3.1.1`,info:{title:d||`${u} API`,version:f||`1.0.0`},tags:[{name:`Server status`,description:`General endpoints indicating server's status`},...p]},transform:o,transformObject:s}),await n.register(r)}let _=new Date;n.withTypeProvider().route({method:`GET`,url:`/`,logLevel:`warn`,...p?.length&&{schema:{tags:[`Server status`],description:`Get server status`,response:{200:{content:{"application/json":{schema:e.object({name:e.literal(u).optional().meta({description:`The name of ${u}`}),version:e.literal(f).optional().meta({description:`The version in package.json file of ${u}`}),boilerPlateVersion:e.literal(O).optional().meta({description:`The version in package.json file of the fastify boilerplate`}),serverRunningSince:e.date().optional().meta({description:`The date when the server started running`}),commit:e.string().meta({description:`The commit SHA of the current deployment`})})}}}}}},handler:()=>({name:u,version:f,serverRunningSince:_,boilerPlateVersion:O,commit:process.env.DD_GIT_COMMIT_SHA||`unknown`})}),n.withTypeProvider().route({method:`GET`,url:`/alive`,logLevel:`warn`,...p?.length&&{schema:{tags:[`Server status`],description:`Server health status`,response:{200:{content:{"application/json":{schema:e.object({status:e.literal(`ok`)})}}}}}},handler:()=>a({logger:l,...m})}),n.register(c)},A=n(k);export{A as fastifyBoilerplatePlugin};
|
|
2
2
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":["pkgJson","fastifyBoilerplate: FastifyPluginAsync<FastifyBoilerPlateOptions>","name","version","fastifyBoilerplatePlugin: FastifyPluginAsync<FastifyBoilerPlateOptions>"],"sources":["../package.json","../src/index.ts"],"sourcesContent":["{\n \"name\": \"@autofleet/fastify-boilerplate\",\n \"version\": \"1.0.19\",\n \"type\": \"module\",\n \"main\": \"./dist/index.js\",\n \"module\": \"./dist/index.js\",\n \"types\": \"./dist/index.d.ts\",\n \"exports\": {\n \".\": {\n \"import\": {\n \"types\": \"./dist/index.d.ts\",\n \"default\": \"./dist/index.js\"\n }\n }\n },\n \"scripts\": {\n \"build\": \"tsdown\",\n \"prepublish\": \"node --run build\",\n \"test\": \"vitest\",\n \"coverage\": \"vitest --coverage\"\n },\n \"repository\": {\n \"type\": \"git\",\n \"url\": \"git+https://github.com/Autofleet/autorepo.git\"\n },\n \"author\": \"\",\n \"license\": \"Proprietary\",\n \"bugs\": {\n \"url\": \"https://github.com/Autofleet/autorepo/issues\"\n },\n \"homepage\": \"https://github.com/Autofleet/autorepo/tree/master/packages/fastify-boilerplate#readme\",\n \"engines\": {\n \"node\": \">=22\"\n },\n \"peerDependencies\": {\n \"@autofleet/logger\": \"*\"\n },\n \"devDependencies\": {\n \"@autofleet/logger\": \"*\",\n \"fastify\": \"^5.3.2\",\n \"fastify-plugin\": \"^5.0.1\",\n \"zod-openapi\": \"^5.0.1\"\n },\n \"files\": [\n \"dist\"\n ],\n \"dependencies\": {\n \"@autofleet/errors\": \"*\",\n \"@autofleet/nitur\": \"*\",\n \"@autofleet/zehut\": \"^4.1.1\",\n \"@fastify/compress\": \"^8.0.1\",\n \"@fastify/helmet\": \"^13.0.1\",\n \"@fastify/swagger\": \"^9.5.1\",\n \"@fastify/swagger-ui\": \"^5.2.2\",\n \"fastify-zod-openapi\": \"^5.0.2\",\n \"zod\": \"^3.25.75\"\n }\n}\n","import * as z from 'zod/v4';\nimport helmet from '@fastify/helmet';\nimport fastifyPlugin from 'fastify-plugin';\nimport type { FastifyPluginAsync } from 'fastify';\nimport compression from '@fastify/compress';\nimport type { ZodOpenApiVersion } from 'zod-openapi';\nimport { handleErrorFastify } from '@autofleet/errors';\nimport { systemAliveCheck } from '@autofleet/nitur/fastify';\nimport { authFromUserIdHeaderPlugin } from '@autofleet/zehut';\nimport type { LoggerInstanceManager } from '@autofleet/logger';\nimport type { FastifyDynamicSwaggerOptions } from '@fastify/swagger';\nimport type { FastifyZodOpenApiSchema, FastifyZodOpenApiTypeProvider } from 'fastify-zod-openapi';\nimport pkgJson from '../package.json' with { type: 'json' };\n\nconst { version: boilerPlateVersion } = pkgJson;\n\ndeclare module 'fastify' {\n interface FastifyRequest {\n logger: LoggerInstanceManager;\n }\n}\n\nexport interface FastifyBoilerPlateOptions {\n /** The name of the Fastify application. */\n name?: string;\n /** The name to show in the swagger UI. @default `${name} API` */\n openApiName?: string;\n /** The version of the MS. */\n version?: string;\n /** The logger instance to use across the application. */\n logger: LoggerInstanceManager;\n /** The options for the alive endpoint. */\n aliveEndpointOptions?: Omit<Parameters<typeof systemAliveCheck>[0], 'logger'>;\n /** When tags are provided, a few plugins will be added for fastify to include OpenAPI docs, and a SwaggerUI. */\n tags?: NonNullable<FastifyDynamicSwaggerOptions['openapi']>['tags'];\n /** Should user permissions be loaded eagerly. @default true */\n eagerLoadUserPermissions?: boolean;\n /** Custom permission loader function to be used in the `zehut` authorization plugin. */\n customPermissionLoader?: Parameters<typeof authFromUserIdHeaderPlugin>[1]['customPermissionLoader'];\n}\n\nconst fastifyBoilerplate: FastifyPluginAsync<FastifyBoilerPlateOptions> = async (fastify, options) => {\n const { logger, name, openApiName, version, tags, aliveEndpointOptions, eagerLoadUserPermissions = true, customPermissionLoader } = options;\n fastify.decorateRequest('logger', { getter: () => logger });\n\n fastify.register(handleErrorFastify, { fallbackMsg: 'Unhandled error caught' });\n\n fastify.register(helmet);\n\n fastify.register(authFromUserIdHeaderPlugin, { eagerLoadUserPermissions, customPermissionLoader });\n\n fastify.register(compression, { global: true, threshold: 1024 });\n\n // #region zod + swagger\n if (tags?.length) {\n const [fastifyZodOpenAPI, { default: fastifySwagger }, { default: fastifySwaggerUI }] = await Promise.all([\n import('fastify-zod-openapi'),\n import('@fastify/swagger'),\n import('@fastify/swagger-ui'),\n ]);\n const { validatorCompiler, serializerCompiler, fastifyZodOpenApiTransform, fastifyZodOpenApiTransformObject, fastifyZodOpenApiPlugin } = fastifyZodOpenAPI;\n fastify.setValidatorCompiler(validatorCompiler);\n fastify.setSerializerCompiler(serializerCompiler);\n await fastify.register(fastifyZodOpenApiPlugin);\n await fastify.register(fastifySwagger, {\n openapi: {\n openapi: '3.1.1' satisfies ZodOpenApiVersion,\n info: {\n title: openApiName || `${name} API`,\n /* v8 ignore next */\n version: version || '1.0.0',\n },\n tags: [\n { name: 'Server status', description: 'General endpoints indicating server\\'s status' },\n ...tags,\n ],\n },\n transform: fastifyZodOpenApiTransform,\n transformObject: fastifyZodOpenApiTransformObject,\n });\n await fastify.register(fastifySwaggerUI);\n }\n // #endregion\n\n const serverRunningSince = new Date();\n fastify.withTypeProvider<FastifyZodOpenApiTypeProvider>().route({\n method: 'GET',\n url: '/',\n logLevel: 'warn',\n ...(tags?.length && {\n schema: {\n tags: ['Server status'],\n description: 'Get server status',\n response: {\n 200: {\n content: {\n 'application/json': {\n schema: z.object({\n name: z.literal(name).optional().meta({ description: `The name of ${name}` }),\n version: z.literal(version).optional().meta({ description: `The version in package.json file of ${name}` }),\n boilerPlateVersion: z.literal(boilerPlateVersion).optional().meta({ description: 'The version in package.json file of the fastify boilerplate' }),\n serverRunningSince: z.date().optional().meta({ description: 'The date when the server started running' }),\n commit: z.string().meta({ description: 'The commit SHA of the current deployment' }),\n }),\n },\n },\n },\n },\n } satisfies FastifyZodOpenApiSchema,\n }),\n handler: () => ({\n name,\n version,\n serverRunningSince,\n boilerPlateVersion,\n commit: process.env.DD_GIT_COMMIT_SHA || 'unknown',\n }),\n });\n\n fastify.withTypeProvider<FastifyZodOpenApiTypeProvider>().route({\n method: 'GET',\n url: '/alive',\n logLevel: 'warn',\n ...(tags?.length && {\n schema: {\n tags: ['Server status'],\n description: 'Server health status',\n response: {\n 200: {\n content: {\n 'application/json': {\n schema: z.object({ status: z.literal('ok') }),\n },\n },\n },\n },\n } satisfies FastifyZodOpenApiSchema,\n }),\n handler: () => systemAliveCheck({ logger, ...aliveEndpointOptions }),\n });\n};\n\nexport const fastifyBoilerplatePlugin: FastifyPluginAsync<FastifyBoilerPlateOptions> = fastifyPlugin(fastifyBoilerplate);\n"],"mappings":"8SACU,mCACG,WACH,WACA,oBACE,oBACD,4BACE,CACT,IAAK,CACH,OAAU,CACR,MAAS,oBACT,QAAW,iBACZ,CACF,CACF,IACU,CACT,MAAS,SACT,WAAc,mBACd,KAAQ,SACR,SAAY,mBACb,IACa,CACZ,KAAQ,MACR,IAAO,+CACR,IACS,KACC,gBACH,CACN,IAAO,8CACR,IACW,0FACD,CACT,KAAQ,MACT,IACmB,CAClB,oBAAqB,GACtB,IACkB,CACjB,oBAAqB,IACrB,QAAW,SACX,iBAAkB,SAClB,cAAe,QAChB,IACQ,CACP,MACD,IACe,CACd,oBAAqB,IACrB,mBAAoB,IACpB,mBAAoB,SACpB,oBAAqB,SACrB,kBAAmB,UACnB,mBAAoB,SACpB,sBAAuB,SACvB,sBAAuB,SACvB,IAAO,UACR,IAxDH,0LAyDC,EC3CD,KAAM,CAAE,QAAS,EAAoB,CAAGA,EA2BlCC,EAAoE,MAAO,EAAS,IAAY,CACpG,GAAM,CAAE,SAAQ,KAAA,EAAM,cAAa,QAAA,EAAS,OAAM,uBAAsB,2BAA2B,GAAM,yBAAwB,CAAG,EAYpI,GAXA,EAAQ,gBAAgB,SAAU,CAAE,OAAQ,IAAM,CAAQ,EAAC,CAE3D,EAAQ,SAAS,EAAoB,CAAE,YAAa,wBAA0B,EAAC,CAE/E,EAAQ,SAAS,EAAO,CAExB,EAAQ,SAAS,EAA4B,CAAE,2BAA0B,wBAAwB,EAAC,CAElG,EAAQ,SAAS,EAAa,CAAE,OAAQ,GAAM,UAAW,IAAM,EAAC,CAG5D,GAAM,OAAQ,CAChB,GAAM,CAAC,EAAmB,CAAE,QAAS,EAAgB,CAAE,CAAE,QAAS,EAAkB,CAAC,CAAG,MAAM,QAAQ,IAAI,CACxG,OAAO,uB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A,qBACA,qBACA,OAAQ,QAAQ,IAAI,mBAAqB,SAC1C,EACF,EAAC,CAEF,EAAQ,kBAAiD,CAAC,MAAM,CAC9D,OAAQ,MACR,IAAK,SACL,SAAU,OACV,GAAI,GAAM,QAAU,CAClB,OAAQ,CACN,KAAM,CAAC,eAAgB,EACvB,YAAa,uBACb,SAAU,CACR,IAAK,CACH,QAAS,CACP,mBAAoB,CAClB,OAAQ,EAAE,OAAO,CAAE,OAAQ,EAAE,QAAQ,KAAK,AAAE,EAAC,AAC9C,CACF,CACF,CACF,CACF,CACF,EACD,QAAS,IAAM,EAAiB,CAAE,SAAQ,GAAG,CAAsB,EAAC,AACrE,EAAC,AACH,EAEYE,EAA0E,EAAc,EAAmB"}
|
|
1
|
+
{"version":3,"file":"index.js","names":["fastifyRequestSignal: FastifyPluginCallback","req: FastifyRequest","err: Error","fastifyRequestSignalPlugin: FastifyPluginCallback","pkgJson","fastifyBoilerplate: FastifyPluginAsync<FastifyBoilerPlateOptions>","name","version","fastifyBoilerplatePlugin: FastifyPluginAsync<FastifyBoilerPlateOptions>"],"sources":["../src/cancelSignalProvider.ts","../package.json","../src/index.ts"],"sourcesContent":["import fastifyPlugin from 'fastify-plugin';\nimport type { FastifyRequest, FastifyPluginCallback } from 'fastify';\n\ndeclare module 'fastify' {\n interface FastifyRequest {\n /**\n * The AbortSignal associated with the request.\n * This signal can be used to abort operations related to the request.\n * It is automatically aborted when the request is closed or destroyed.\n *\n * This signal is created lazily, meaning it is only created if this field is accessed.\n *\n * Make sure to handle the signal carefully in your request handlers to avoid memory leaks or unexpected behavior.\n * If you add an event listener, make sure to remove it when the request is done.\n * Consider using the disposable {@link https://nodejs.org/docs/latest/api/events.html#eventsaddabortlistenersignal-listener `addAbortListener`} method.\n */\n signal: AbortSignal;\n }\n}\n\nconst fastifyRequestSignal: FastifyPluginCallback = (fastify, _options, next) => {\n const controllers = new WeakMap<FastifyRequest, { controller: AbortController; boundOnceRequestClosed: typeof onceRequestClosed; boundOnceRequestError: typeof onceRequestError; }>();\n\n function removeRequestListeners(req: FastifyRequest): void {\n if (!controllers.has(req)) {\n return;\n }\n const { boundOnceRequestClosed, boundOnceRequestError } = controllers.get(req)!;\n if (boundOnceRequestClosed) {\n req.raw.removeListener('close', boundOnceRequestClosed);\n }\n if (boundOnceRequestError) {\n req.raw.removeListener('error', boundOnceRequestError);\n }\n }\n\n function onceRequestClosed(this: FastifyRequest) {\n removeRequestListeners(this);\n if (controllers.has(this) && this.raw.destroyed) {\n const { controller } = controllers.get(this)!;\n if (controller.signal.aborted) {\n return;\n }\n controller.abort();\n }\n }\n function onceRequestError(this: FastifyRequest, err: Error) {\n removeRequestListeners(this);\n if (controllers.has(this)) {\n const { controller } = controllers.get(this)!;\n controller.abort(err);\n }\n }\n\n fastify.decorateRequest('signal', {\n getter() {\n const { raw, id } = this;\n if (raw.socket.destroyed === true) {\n throw new Error(`Socket for request with ID '${id}' already closed`);\n }\n if (controllers.has(this)) {\n // Since this is a getter, we can return the existing controller's signal, if the signal was already created.\n return controllers.get(this)!.controller.signal;\n }\n const boundOnceRequestClosed = onceRequestClosed.bind(this);\n const boundOnceRequestError = onceRequestError.bind(this);\n const controller = new AbortController();\n controllers.set(this, { controller, boundOnceRequestClosed, boundOnceRequestError });\n\n raw.once('close', boundOnceRequestClosed);\n raw.once('error', boundOnceRequestError);\n return controller.signal;\n },\n });\n\n fastify.addHook('onResponse', (request, _reply, done) => {\n if (controllers.has(request)) {\n removeRequestListeners(request);\n controllers.delete(request);\n }\n done();\n });\n\n next();\n};\n\nexport const fastifyRequestSignalPlugin: FastifyPluginCallback = fastifyPlugin(fastifyRequestSignal);\n","{\n \"name\": \"@autofleet/fastify-boilerplate\",\n \"version\": \"1.1.0\",\n \"type\": \"module\",\n \"main\": \"./dist/index.js\",\n \"module\": \"./dist/index.js\",\n \"types\": \"./dist/index.d.ts\",\n \"exports\": {\n \".\": {\n \"import\": {\n \"types\": \"./dist/index.d.ts\",\n \"default\": \"./dist/index.js\"\n }\n }\n },\n \"scripts\": {\n \"build\": \"tsdown\",\n \"prepublish\": \"node --run build\",\n \"test\": \"vitest\",\n \"coverage\": \"vitest --coverage\"\n },\n \"repository\": {\n \"type\": \"git\",\n \"url\": \"git+https://github.com/Autofleet/autorepo.git\"\n },\n \"author\": \"\",\n \"license\": \"Proprietary\",\n \"bugs\": {\n \"url\": \"https://github.com/Autofleet/autorepo/issues\"\n },\n \"homepage\": \"https://github.com/Autofleet/autorepo/tree/master/packages/fastify-boilerplate#readme\",\n \"engines\": {\n \"node\": \">=22\"\n },\n \"peerDependencies\": {\n \"@autofleet/logger\": \"*\"\n },\n \"devDependencies\": {\n \"@autofleet/logger\": \"*\",\n \"fastify\": \"^5.3.2\",\n \"fastify-plugin\": \"^5.0.1\",\n \"zod-openapi\": \"^5.0.1\"\n },\n \"files\": [\n \"dist\"\n ],\n \"dependencies\": {\n \"@autofleet/errors\": \"*\",\n \"@autofleet/nitur\": \"*\",\n \"@autofleet/zehut\": \"^4.1.1\",\n \"@fastify/compress\": \"^8.0.1\",\n \"@fastify/helmet\": \"^13.0.1\",\n \"@fastify/swagger\": \"^9.5.1\",\n \"@fastify/swagger-ui\": \"^5.2.2\",\n \"fastify-zod-openapi\": \"^5.0.2\",\n \"zod\": \"^3.25.75\"\n }\n}\n","import * as z from 'zod/v4';\nimport helmet from '@fastify/helmet';\nimport fastifyPlugin from 'fastify-plugin';\nimport type { FastifyPluginAsync } from 'fastify';\nimport compression from '@fastify/compress';\nimport type { ZodOpenApiVersion } from 'zod-openapi';\nimport { handleErrorFastify } from '@autofleet/errors';\nimport { systemAliveCheck } from '@autofleet/nitur/fastify';\nimport { authFromUserIdHeaderPlugin } from '@autofleet/zehut';\nimport type { LoggerInstanceManager } from '@autofleet/logger';\nimport type { FastifyDynamicSwaggerOptions } from '@fastify/swagger';\nimport type { FastifyZodOpenApiSchema, FastifyZodOpenApiTypeProvider } from 'fastify-zod-openapi';\nimport { fastifyRequestSignalPlugin } from './cancelSignalProvider.js';\nimport pkgJson from '../package.json' with { type: 'json' };\n\nconst { version: boilerPlateVersion } = pkgJson;\n\ndeclare module 'fastify' {\n interface FastifyRequest {\n logger: LoggerInstanceManager;\n }\n}\n\nexport interface FastifyBoilerPlateOptions {\n /** The name of the Fastify application. */\n name?: string;\n /** The name to show in the swagger UI. @default `${name} API` */\n openApiName?: string;\n /** The version of the MS. */\n version?: string;\n /** The logger instance to use across the application. */\n logger: LoggerInstanceManager;\n /** The options for the alive endpoint. */\n aliveEndpointOptions?: Omit<Parameters<typeof systemAliveCheck>[0], 'logger'>;\n /** When tags are provided, a few plugins will be added for fastify to include OpenAPI docs, and a SwaggerUI. */\n tags?: NonNullable<FastifyDynamicSwaggerOptions['openapi']>['tags'];\n /** Should user permissions be loaded eagerly. @default true */\n eagerLoadUserPermissions?: boolean;\n /** Custom permission loader function to be used in the `zehut` authorization plugin. */\n customPermissionLoader?: Parameters<typeof authFromUserIdHeaderPlugin>[1]['customPermissionLoader'];\n}\n\nconst fastifyBoilerplate: FastifyPluginAsync<FastifyBoilerPlateOptions> = async (fastify, options) => {\n const { logger, name, openApiName, version, tags, aliveEndpointOptions, eagerLoadUserPermissions = true, customPermissionLoader } = options;\n fastify.decorateRequest('logger', { getter: () => logger });\n\n fastify.register(handleErrorFastify, { fallbackMsg: 'Unhandled error caught' });\n\n fastify.register(helmet);\n\n fastify.register(authFromUserIdHeaderPlugin, { eagerLoadUserPermissions, customPermissionLoader });\n\n fastify.register(compression, { global: true, threshold: 1024 });\n\n // #region zod + swagger\n if (tags?.length) {\n const [fastifyZodOpenAPI, { default: fastifySwagger }, { default: fastifySwaggerUI }] = await Promise.all([\n import('fastify-zod-openapi'),\n import('@fastify/swagger'),\n import('@fastify/swagger-ui'),\n ]);\n const { validatorCompiler, serializerCompiler, fastifyZodOpenApiTransform, fastifyZodOpenApiTransformObject, fastifyZodOpenApiPlugin } = fastifyZodOpenAPI;\n fastify.setValidatorCompiler(validatorCompiler);\n fastify.setSerializerCompiler(serializerCompiler);\n await fastify.register(fastifyZodOpenApiPlugin);\n await fastify.register(fastifySwagger, {\n openapi: {\n openapi: '3.1.1' satisfies ZodOpenApiVersion,\n info: {\n title: openApiName || `${name} API`,\n /* v8 ignore next */\n version: version || '1.0.0',\n },\n tags: [\n { name: 'Server status', description: 'General endpoints indicating server\\'s status' },\n ...tags,\n ],\n },\n transform: fastifyZodOpenApiTransform,\n transformObject: fastifyZodOpenApiTransformObject,\n });\n await fastify.register(fastifySwaggerUI);\n }\n // #endregion\n\n const serverRunningSince = new Date();\n fastify.withTypeProvider<FastifyZodOpenApiTypeProvider>().route({\n method: 'GET',\n url: '/',\n logLevel: 'warn',\n ...(tags?.length && {\n schema: {\n tags: ['Server status'],\n description: 'Get server status',\n response: {\n 200: {\n content: {\n 'application/json': {\n schema: z.object({\n name: z.literal(name).optional().meta({ description: `The name of ${name}` }),\n version: z.literal(version).optional().meta({ description: `The version in package.json file of ${name}` }),\n boilerPlateVersion: z.literal(boilerPlateVersion).optional().meta({ description: 'The version in package.json file of the fastify boilerplate' }),\n serverRunningSince: z.date().optional().meta({ description: 'The date when the server started running' }),\n commit: z.string().meta({ description: 'The commit SHA of the current deployment' }),\n }),\n },\n },\n },\n },\n } satisfies FastifyZodOpenApiSchema,\n }),\n handler: () => ({\n name,\n version,\n serverRunningSince,\n boilerPlateVersion,\n commit: process.env.DD_GIT_COMMIT_SHA || 'unknown',\n }),\n });\n\n fastify.withTypeProvider<FastifyZodOpenApiTypeProvider>().route({\n method: 'GET',\n url: '/alive',\n logLevel: 'warn',\n ...(tags?.length && {\n schema: {\n tags: ['Server status'],\n description: 'Server health status',\n response: {\n 200: {\n content: {\n 'application/json': {\n schema: z.object({ status: z.literal('ok') }),\n },\n },\n },\n },\n } satisfies FastifyZodOpenApiSchema,\n }),\n handler: () => systemAliveCheck({ logger, ...aliveEndpointOptions }),\n });\n\n fastify.register(fastifyRequestSignalPlugin);\n};\n\nexport const fastifyBoilerplatePlugin: FastifyPluginAsync<Fastif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