@nestjs/platform-fastify 11.1.3 → 11.1.5

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.
@@ -47,11 +47,16 @@ export declare class FastifyAdapter<TServer extends RawServerBase = RawServerDef
47
47
  protected readonly instance: TInstance;
48
48
  protected _pathPrefix?: string;
49
49
  private _isParserRegistered;
50
+ private onRequestHook?;
51
+ private onResponseHook?;
50
52
  private isMiddieRegistered;
53
+ private pendingMiddlewares;
51
54
  private versioningOptions?;
52
55
  private readonly versionConstraint;
53
56
  get isParserRegistered(): boolean;
54
57
  constructor(instanceOrOptions?: TInstance | FastifyHttp2Options<any> | FastifyHttp2SecureOptions<any> | FastifyHttpsOptions<any> | FastifyHttpOptions<any> | FastifyAdapterBaseOptions<TServer>);
58
+ setOnRequestHook(hook: (request: TRequest, reply: TReply, done: (err?: Error) => void) => void | Promise<void>): void;
59
+ setOnResponseHook(hook: (request: TRequest, reply: TReply, done: (err?: Error) => void) => void | Promise<void>): void;
55
60
  init(): Promise<void>;
56
61
  listen(port: string | number, callback?: () => void): void;
57
62
  listen(port: string | number, hostname: string, callback?: () => void): void;
@@ -102,6 +107,7 @@ export declare class FastifyAdapter<TServer extends RawServerBase = RawServerDef
102
107
  useBodyParser(type: string | string[] | RegExp, rawBody: boolean, options?: NestFastifyBodyParserOptions, parser?: FastifyBodyParser<Buffer, TServer>): void;
103
108
  createMiddlewareFactory(requestMethod: RequestMethod): Promise<(path: string, callback: Function) => any>;
104
109
  getType(): string;
110
+ use(...args: any[]): any;
105
111
  protected registerWithPrefix(factory: FastifyPluginCallback<any> | FastifyPluginAsync<any> | Promise<{
106
112
  default: FastifyPluginCallback<any>;
107
113
  }> | Promise<{
@@ -23,6 +23,7 @@ class FastifyAdapter extends http_adapter_1.AbstractHttpAdapter {
23
23
  constructor(instanceOrOptions) {
24
24
  super();
25
25
  this.logger = new common_1.Logger(FastifyAdapter.name);
26
+ this.pendingMiddlewares = [];
26
27
  this.versionConstraint = {
27
28
  name: 'version',
28
29
  validate(value) {
@@ -99,12 +100,41 @@ class FastifyAdapter extends http_adapter_1.AbstractHttpAdapter {
99
100
  if (instanceOrOptions?.skipMiddie) {
100
101
  this.isMiddieRegistered = true;
101
102
  }
103
+ this.instance.addHook('onRequest', (request, reply, done) => {
104
+ if (this.onRequestHook) {
105
+ this.onRequestHook(request, reply, done);
106
+ }
107
+ else {
108
+ done();
109
+ }
110
+ });
111
+ this.instance.addHook('onResponse', (request, reply, done) => {
112
+ if (this.onResponseHook) {
113
+ this.onResponseHook(request, reply, done);
114
+ }
115
+ else {
116
+ done();
117
+ }
118
+ });
119
+ }
120
+ setOnRequestHook(hook) {
121
+ this.onRequestHook = hook;
122
+ }
123
+ setOnResponseHook(hook) {
124
+ this.onResponseHook = hook;
102
125
  }
103
126
  async init() {
104
127
  if (this.isMiddieRegistered) {
105
128
  return;
106
129
  }
107
130
  await this.registerMiddie();
131
+ // Register any pending middlewares that were added before init
132
+ if (this.pendingMiddlewares.length > 0) {
133
+ for (const { args } of this.pendingMiddlewares) {
134
+ this.instance.use(...args);
135
+ }
136
+ this.pendingMiddlewares = [];
137
+ }
108
138
  }
109
139
  listen(listenOptions, ...args) {
110
140
  const isFirstArgTypeofFunction = typeof args[0] === 'function';
@@ -381,6 +411,15 @@ class FastifyAdapter extends http_adapter_1.AbstractHttpAdapter {
381
411
  getType() {
382
412
  return 'fastify';
383
413
  }
414
+ use(...args) {
415
+ // Fastify requires @fastify/middie plugin to be registered before middleware can be used.
416
+ // If middie is not registered yet, we queue the middleware and register it later during init.
417
+ if (!this.isMiddieRegistered) {
418
+ this.pendingMiddlewares.push({ args });
419
+ return this;
420
+ }
421
+ return this.instance.use(...args);
422
+ }
384
423
  registerWithPrefix(factory, prefix = '/') {
385
424
  return this.instance.register(factory, { prefix });
386
425
  }
@@ -418,8 +457,10 @@ class FastifyAdapter extends http_adapter_1.AbstractHttpAdapter {
418
457
  handlerRef.version !== common_1.VERSION_NEUTRAL;
419
458
  const routeConfig = Reflect.getMetadata(constants_1.FASTIFY_ROUTE_CONFIG_METADATA, handlerRef);
420
459
  const routeConstraints = Reflect.getMetadata(constants_1.FASTIFY_ROUTE_CONSTRAINTS_METADATA, handlerRef);
460
+ const routeSchema = Reflect.getMetadata(constants_1.FASTIFY_ROUTE_SCHEMA_METADATA, handlerRef);
421
461
  const hasConfig = !(0, shared_utils_1.isUndefined)(routeConfig);
422
462
  const hasConstraints = !(0, shared_utils_1.isUndefined)(routeConstraints);
463
+ const hasSchema = !(0, shared_utils_1.isUndefined)(routeSchema);
423
464
  const routeToInject = {
424
465
  method: routerMethodKey,
425
466
  url: args[0],
@@ -428,7 +469,7 @@ class FastifyAdapter extends http_adapter_1.AbstractHttpAdapter {
428
469
  if (this.instance.supportedMethods.indexOf(routerMethodKey) === -1) {
429
470
  this.instance.addHttpMethod(routerMethodKey, { hasBody: true });
430
471
  }
431
- if (isVersioned || hasConstraints || hasConfig) {
472
+ if (isVersioned || hasConstraints || hasConfig || hasSchema) {
432
473
  const isPathAndRouteTuple = args.length === 2;
433
474
  if (isPathAndRouteTuple) {
434
475
  const constraints = {
@@ -444,6 +485,9 @@ class FastifyAdapter extends http_adapter_1.AbstractHttpAdapter {
444
485
  ...routeConfig,
445
486
  },
446
487
  }),
488
+ ...(hasSchema && {
489
+ schema: routeSchema,
490
+ }),
447
491
  };
448
492
  const routeToInjectWithOptions = { ...routeToInject, ...options };
449
493
  return this.instance.route(routeToInjectWithOptions);
package/constants.d.ts CHANGED
@@ -1,2 +1,3 @@
1
1
  export declare const FASTIFY_ROUTE_CONFIG_METADATA = "__fastify_route_config__";
2
2
  export declare const FASTIFY_ROUTE_CONSTRAINTS_METADATA = "__fastify_route_constraints__";
3
+ export declare const FASTIFY_ROUTE_SCHEMA_METADATA = "__fastify_route_schema__";
package/constants.js CHANGED
@@ -1,5 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.FASTIFY_ROUTE_CONSTRAINTS_METADATA = exports.FASTIFY_ROUTE_CONFIG_METADATA = void 0;
3
+ exports.FASTIFY_ROUTE_SCHEMA_METADATA = exports.FASTIFY_ROUTE_CONSTRAINTS_METADATA = exports.FASTIFY_ROUTE_CONFIG_METADATA = void 0;
4
4
  exports.FASTIFY_ROUTE_CONFIG_METADATA = '__fastify_route_config__';
5
5
  exports.FASTIFY_ROUTE_CONSTRAINTS_METADATA = '__fastify_route_constraints__';
6
+ exports.FASTIFY_ROUTE_SCHEMA_METADATA = '__fastify_route_schema__';
@@ -1,2 +1,3 @@
1
1
  export * from './route-config.decorator';
2
2
  export * from './route-constraints.decorator';
3
+ export * from './route-schema.decorator';
@@ -3,3 +3,4 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const tslib_1 = require("tslib");
4
4
  tslib_1.__exportStar(require("./route-config.decorator"), exports);
5
5
  tslib_1.__exportStar(require("./route-constraints.decorator"), exports);
6
+ tslib_1.__exportStar(require("./route-schema.decorator"), exports);
@@ -0,0 +1,11 @@
1
+ import { FastifySchema } from 'fastify';
2
+ /**
3
+ * @publicApi
4
+ * Allows setting the schema for the route. Schema is an object that can contain the following properties:
5
+ * - body: JsonSchema
6
+ * - querystring or query: JsonSchema
7
+ * - params: JsonSchema
8
+ * - response: Record<HttpStatusCode, JsonSchema>
9
+ * @param schema See {@link https://fastify.dev/docs/latest/Reference/Routes/#routes-options}
10
+ */
11
+ export declare const RouteSchema: (schema: FastifySchema) => import("@nestjs/common").CustomDecorator<string>;
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RouteSchema = void 0;
4
+ const common_1 = require("@nestjs/common");
5
+ const constants_1 = require("../constants");
6
+ /**
7
+ * @publicApi
8
+ * Allows setting the schema for the route. Schema is an object that can contain the following properties:
9
+ * - body: JsonSchema
10
+ * - querystring or query: JsonSchema
11
+ * - params: JsonSchema
12
+ * - response: Record<HttpStatusCode, JsonSchema>
13
+ * @param schema See {@link https://fastify.dev/docs/latest/Reference/Routes/#routes-options}
14
+ */
15
+ const RouteSchema = (schema) => (0, common_1.SetMetadata)(constants_1.FASTIFY_ROUTE_SCHEMA_METADATA, schema);
16
+ exports.RouteSchema = RouteSchema;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nestjs/platform-fastify",
3
- "version": "11.1.3",
3
+ "version": "11.1.5",
4
4
  "description": "Nest - modern, fast, powerful node.js web framework (@platform-fastify)",
5
5
  "author": "Kamil Mysliwiec",
6
6
  "license": "MIT",
@@ -22,7 +22,7 @@
22
22
  "@fastify/formbody": "8.0.2",
23
23
  "@fastify/middie": "9.0.3",
24
24
  "fast-querystring": "1.1.2",
25
- "fastify": "5.3.3",
25
+ "fastify": "5.4.0",
26
26
  "light-my-request": "6.6.0",
27
27
  "path-to-regexp": "8.2.0",
28
28
  "tslib": "2.8.1"