@nestjs/platform-fastify 9.3.0-beta.3 → 9.3.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/adapters/fastify-adapter.d.ts +22 -19
- package/adapters/fastify-adapter.js +41 -16
- package/interfaces/index.d.ts +1 -0
- package/interfaces/index.js +1 -0
- package/interfaces/nest-fastify-application.interface.d.ts +19 -1
- package/interfaces/nest-fastify-body-parser-options.interface.d.ts +2 -0
- package/interfaces/nest-fastify-body-parser-options.interface.js +2 -0
- package/package.json +2 -2
|
@@ -1,26 +1,28 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
/// <reference types="node" />
|
|
3
|
+
/// <reference types="node" />
|
|
3
4
|
import { RequestMethod, VersioningOptions } from '@nestjs/common';
|
|
4
5
|
import { VersionValue } from '@nestjs/common/interfaces';
|
|
5
6
|
import { CorsOptions, CorsOptionsDelegate } from '@nestjs/common/interfaces/external/cors-options.interface';
|
|
6
7
|
import { AbstractHttpAdapter } from '@nestjs/core/adapters/http-adapter';
|
|
7
|
-
import {
|
|
8
|
+
import { FastifyBaseLogger, FastifyBodyParser, FastifyInstance, FastifyPluginAsync, FastifyPluginCallback, FastifyRegister, FastifyReply, FastifyRequest, FastifyServerOptions, RawReplyDefaultExpression, RawRequestDefaultExpression, RawServerBase, RawServerDefault, RequestGenericInterface } from 'fastify';
|
|
8
9
|
import * as http2 from 'http2';
|
|
9
10
|
import * as https from 'https';
|
|
10
11
|
import { Chain as LightMyRequestChain, InjectOptions, Response as LightMyRequestResponse } from 'light-my-request';
|
|
12
|
+
import { NestFastifyBodyParserOptions } from '../interfaces';
|
|
11
13
|
import { FastifyStaticOptions, FastifyViewOptions } from '../interfaces/external';
|
|
12
|
-
|
|
14
|
+
type FastifyHttp2SecureOptions<Server extends http2.Http2SecureServer, Logger extends FastifyBaseLogger = FastifyBaseLogger> = FastifyServerOptions<Server, Logger> & {
|
|
13
15
|
http2: true;
|
|
14
16
|
https: http2.SecureServerOptions;
|
|
15
17
|
};
|
|
16
|
-
|
|
18
|
+
type FastifyHttp2Options<Server extends http2.Http2Server, Logger extends FastifyBaseLogger = FastifyBaseLogger> = FastifyServerOptions<Server, Logger> & {
|
|
17
19
|
http2: true;
|
|
18
20
|
http2SessionTimeout?: number;
|
|
19
21
|
};
|
|
20
|
-
|
|
22
|
+
type FastifyHttpsOptions<Server extends https.Server, Logger extends FastifyBaseLogger = FastifyBaseLogger> = FastifyServerOptions<Server, Logger> & {
|
|
21
23
|
https: https.ServerOptions;
|
|
22
24
|
};
|
|
23
|
-
|
|
25
|
+
type VersionedRoute<TRequest, TResponse> = ((req: TRequest, res: TResponse, next: Function) => Function) & {
|
|
24
26
|
version: VersionValue;
|
|
25
27
|
versioningOptions: VersioningOptions;
|
|
26
28
|
};
|
|
@@ -30,7 +32,7 @@ declare type VersionedRoute<TRequest, TResponse> = ((req: TRequest, res: TRespon
|
|
|
30
32
|
* ref https://github.com/fastify/middie/pull/16
|
|
31
33
|
* ref https://github.com/fastify/fastify/pull/559
|
|
32
34
|
*/
|
|
33
|
-
|
|
35
|
+
type FastifyRawRequest<TServer extends RawServerBase> = RawRequestDefaultExpression<TServer> & {
|
|
34
36
|
originalUrl?: string;
|
|
35
37
|
};
|
|
36
38
|
export declare class FastifyAdapter<TServer extends RawServerBase = RawServerDefault, TRawRequest extends FastifyRawRequest<TServer> = FastifyRawRequest<TServer>, TRawResponse extends RawReplyDefaultExpression<TServer> = RawReplyDefaultExpression<TServer>, TRequest extends FastifyRequest<RequestGenericInterface, TServer, TRawRequest> = FastifyRequest<RequestGenericInterface, TServer, TRawRequest>, TReply extends FastifyReply<TServer, TRawRequest, TRawResponse> = FastifyReply<TServer, TRawRequest, TRawResponse>, TInstance extends FastifyInstance<TServer, TRawRequest, TRawResponse> = FastifyInstance<TServer, TRawRequest, TRawResponse>> extends AbstractHttpAdapter<TServer, TRequest, TReply> {
|
|
@@ -44,13 +46,13 @@ export declare class FastifyAdapter<TServer extends RawServerBase = RawServerDef
|
|
|
44
46
|
init(): Promise<void>;
|
|
45
47
|
listen(port: string | number, callback?: () => void): void;
|
|
46
48
|
listen(port: string | number, hostname: string, callback?: () => void): void;
|
|
47
|
-
get(...args: any[]): FastifyInstance<TServer, TRawRequest, TRawResponse,
|
|
48
|
-
post(...args: any[]): FastifyInstance<TServer, TRawRequest, TRawResponse,
|
|
49
|
-
head(...args: any[]): FastifyInstance<TServer, TRawRequest, TRawResponse,
|
|
50
|
-
delete(...args: any[]): FastifyInstance<TServer, TRawRequest, TRawResponse,
|
|
51
|
-
put(...args: any[]): FastifyInstance<TServer, TRawRequest, TRawResponse,
|
|
52
|
-
patch(...args: any[]): FastifyInstance<TServer, TRawRequest, TRawResponse,
|
|
53
|
-
options(...args: any[]): FastifyInstance<TServer, TRawRequest, TRawResponse,
|
|
49
|
+
get(...args: any[]): FastifyInstance<TServer, TRawRequest, TRawResponse, FastifyBaseLogger, import("fastify").FastifyTypeProviderDefault>;
|
|
50
|
+
post(...args: any[]): FastifyInstance<TServer, TRawRequest, TRawResponse, FastifyBaseLogger, import("fastify").FastifyTypeProviderDefault>;
|
|
51
|
+
head(...args: any[]): FastifyInstance<TServer, TRawRequest, TRawResponse, FastifyBaseLogger, import("fastify").FastifyTypeProviderDefault>;
|
|
52
|
+
delete(...args: any[]): FastifyInstance<TServer, TRawRequest, TRawResponse, FastifyBaseLogger, import("fastify").FastifyTypeProviderDefault>;
|
|
53
|
+
put(...args: any[]): FastifyInstance<TServer, TRawRequest, TRawResponse, FastifyBaseLogger, import("fastify").FastifyTypeProviderDefault>;
|
|
54
|
+
patch(...args: any[]): FastifyInstance<TServer, TRawRequest, TRawResponse, FastifyBaseLogger, import("fastify").FastifyTypeProviderDefault>;
|
|
55
|
+
options(...args: any[]): FastifyInstance<TServer, TRawRequest, TRawResponse, FastifyBaseLogger, import("fastify").FastifyTypeProviderDefault>;
|
|
54
56
|
applyVersionFilter(handler: Function, version: VersionValue, versioningOptions: VersioningOptions): VersionedRoute<TRequest, TReply>;
|
|
55
57
|
reply(response: TRawResponse | TReply, body: any, statusCode?: number): FastifyReply<TServer, TRawRequest, TRawResponse, import("fastify").RouteGenericInterface, unknown, import("fastify").FastifySchema, import("fastify").FastifyTypeProviderDefault, unknown>;
|
|
56
58
|
status(response: TRawResponse | TReply, statusCode: number): TRawResponse | FastifyReply<TServer, TRawRequest, TRawResponse, import("fastify").RouteGenericInterface, unknown, import("fastify").FastifySchema, import("fastify").FastifyTypeProviderDefault, unknown>;
|
|
@@ -59,17 +61,17 @@ export declare class FastifyAdapter<TServer extends RawServerBase = RawServerDef
|
|
|
59
61
|
view: Function;
|
|
60
62
|
}, view: string, options: any): any;
|
|
61
63
|
redirect(response: TReply, statusCode: number, url: string): FastifyReply<TServer, TRawRequest, TRawResponse, import("fastify").RouteGenericInterface, unknown, import("fastify").FastifySchema, import("fastify").FastifyTypeProviderDefault, unknown>;
|
|
62
|
-
setErrorHandler(handler: Parameters<TInstance['setErrorHandler']>[0]): FastifyInstance<TServer, TRawRequest, TRawResponse,
|
|
63
|
-
setNotFoundHandler(handler: Function): FastifyInstance<TServer, TRawRequest, TRawResponse,
|
|
64
|
+
setErrorHandler(handler: Parameters<TInstance['setErrorHandler']>[0]): FastifyInstance<TServer, TRawRequest, TRawResponse, FastifyBaseLogger, import("fastify").FastifyTypeProvider>;
|
|
65
|
+
setNotFoundHandler(handler: Function): FastifyInstance<TServer, TRawRequest, TRawResponse, FastifyBaseLogger, import("fastify").FastifyTypeProviderDefault>;
|
|
64
66
|
getHttpServer<T = TServer>(): T;
|
|
65
67
|
getInstance<T = TInstance>(): T;
|
|
66
|
-
register<TRegister extends Parameters<FastifyRegister<TInstance>>>(plugin: TRegister['0'], opts?: TRegister['1']): FastifyInstance<TServer, TRawRequest, TRawResponse,
|
|
68
|
+
register<TRegister extends Parameters<FastifyRegister<TInstance>>>(plugin: TRegister['0'], opts?: TRegister['1']): FastifyInstance<TServer, TRawRequest, TRawResponse, FastifyBaseLogger, import("fastify").FastifyTypeProviderDefault> & PromiseLike<undefined>;
|
|
67
69
|
inject(): LightMyRequestChain;
|
|
68
70
|
inject(opts: InjectOptions | string): Promise<LightMyRequestResponse>;
|
|
69
71
|
close(): Promise<undefined>;
|
|
70
72
|
initHttpServer(): void;
|
|
71
|
-
useStaticAssets(options: FastifyStaticOptions): FastifyInstance<TServer, TRawRequest, TRawResponse,
|
|
72
|
-
setViewEngine(options: FastifyViewOptions | string): FastifyInstance<TServer, TRawRequest, TRawResponse,
|
|
73
|
+
useStaticAssets(options: FastifyStaticOptions): FastifyInstance<TServer, TRawRequest, TRawResponse, FastifyBaseLogger, import("fastify").FastifyTypeProviderDefault> & PromiseLike<undefined>;
|
|
74
|
+
setViewEngine(options: FastifyViewOptions | string): FastifyInstance<TServer, TRawRequest, TRawResponse, FastifyBaseLogger, import("fastify").FastifyTypeProviderDefault> & PromiseLike<undefined>;
|
|
73
75
|
isHeadersSent(response: TReply): boolean;
|
|
74
76
|
setHeader(response: TReply, name: string, value: string): FastifyReply<TServer, TRawRequest, TRawResponse, import("fastify").RouteGenericInterface, unknown, import("fastify").FastifySchema, import("fastify").FastifyTypeProviderDefault, unknown>;
|
|
75
77
|
getRequestHostname(request: TRequest): string;
|
|
@@ -78,13 +80,14 @@ export declare class FastifyAdapter<TServer extends RawServerBase = RawServerDef
|
|
|
78
80
|
getRequestUrl(request: TRawRequest): string;
|
|
79
81
|
enableCors(options: CorsOptions | CorsOptionsDelegate<TRequest>): void;
|
|
80
82
|
registerParserMiddleware(prefix?: string, rawBody?: boolean): void;
|
|
83
|
+
useBodyParser(type: string | string[] | RegExp, rawBody: boolean, options?: NestFastifyBodyParserOptions, parser?: FastifyBodyParser<Buffer, TServer>): void;
|
|
81
84
|
createMiddlewareFactory(requestMethod: RequestMethod): Promise<(path: string, callback: Function) => any>;
|
|
82
85
|
getType(): string;
|
|
83
86
|
protected registerWithPrefix(factory: FastifyPluginCallback<any> | FastifyPluginAsync<any> | Promise<{
|
|
84
87
|
default: FastifyPluginCallback<any>;
|
|
85
88
|
}> | Promise<{
|
|
86
89
|
default: FastifyPluginAsync<any>;
|
|
87
|
-
}>, prefix?: string): FastifyInstance<TServer, TRawRequest, TRawResponse,
|
|
90
|
+
}>, prefix?: string): FastifyInstance<TServer, TRawRequest, TRawResponse, FastifyBaseLogger, import("fastify").FastifyTypeProviderDefault> & PromiseLike<undefined>;
|
|
88
91
|
private isNativeResponse;
|
|
89
92
|
private registerJsonContentParser;
|
|
90
93
|
private registerUrlencodedContentParser;
|
|
@@ -7,9 +7,13 @@ const shared_utils_1 = require("@nestjs/common/utils/shared.utils");
|
|
|
7
7
|
const http_adapter_1 = require("@nestjs/core/adapters/http-adapter");
|
|
8
8
|
const fastify_1 = require("fastify");
|
|
9
9
|
const Reply = require("fastify/lib/reply");
|
|
10
|
+
const symbols_1 = require("fastify/lib/symbols");
|
|
10
11
|
// `querystring` is used internally in fastify for registering urlencoded body parser.
|
|
11
12
|
const querystring_1 = require("querystring");
|
|
12
13
|
class FastifyAdapter extends http_adapter_1.AbstractHttpAdapter {
|
|
14
|
+
get isParserRegistered() {
|
|
15
|
+
return !!this._isParserRegistered;
|
|
16
|
+
}
|
|
13
17
|
constructor(instanceOrOptions) {
|
|
14
18
|
super();
|
|
15
19
|
this.versionConstraint = {
|
|
@@ -84,21 +88,28 @@ class FastifyAdapter extends http_adapter_1.AbstractHttpAdapter {
|
|
|
84
88
|
} }, instanceOrOptions));
|
|
85
89
|
this.setInstance(instance);
|
|
86
90
|
}
|
|
87
|
-
get isParserRegistered() {
|
|
88
|
-
return !!this._isParserRegistered;
|
|
89
|
-
}
|
|
90
91
|
async init() {
|
|
91
92
|
if (this.isMiddieRegistered) {
|
|
92
93
|
return;
|
|
93
94
|
}
|
|
94
95
|
await this.registerMiddie();
|
|
95
96
|
}
|
|
96
|
-
listen(
|
|
97
|
+
listen(listenOptions, ...args) {
|
|
97
98
|
const isFirstArgTypeofFunction = typeof args[0] === 'function';
|
|
98
99
|
const callback = isFirstArgTypeofFunction ? args[0] : args[1];
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
100
|
+
let options;
|
|
101
|
+
if (typeof listenOptions === 'object' &&
|
|
102
|
+
(listenOptions.host !== undefined ||
|
|
103
|
+
listenOptions.port !== undefined ||
|
|
104
|
+
listenOptions.path !== undefined)) {
|
|
105
|
+
// First parameter is an object with a path, port and/or host attributes
|
|
106
|
+
options = listenOptions;
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
options = {
|
|
110
|
+
port: +listenOptions,
|
|
111
|
+
};
|
|
112
|
+
}
|
|
102
113
|
if (!isFirstArgTypeofFunction) {
|
|
103
114
|
options.host = args[0];
|
|
104
115
|
}
|
|
@@ -136,7 +147,7 @@ class FastifyAdapter extends http_adapter_1.AbstractHttpAdapter {
|
|
|
136
147
|
reply(response, body, statusCode) {
|
|
137
148
|
const fastifyReply = this.isNativeResponse(response)
|
|
138
149
|
? new Reply(response, {
|
|
139
|
-
|
|
150
|
+
[symbols_1.kRouteContext]: {
|
|
140
151
|
preSerialization: null,
|
|
141
152
|
preValidation: [],
|
|
142
153
|
preHandler: [],
|
|
@@ -252,6 +263,22 @@ class FastifyAdapter extends http_adapter_1.AbstractHttpAdapter {
|
|
|
252
263
|
this.registerJsonContentParser(rawBody);
|
|
253
264
|
this._isParserRegistered = true;
|
|
254
265
|
}
|
|
266
|
+
useBodyParser(type, rawBody, options, parser) {
|
|
267
|
+
const parserOptions = Object.assign(Object.assign({}, (options || {})), { parseAs: 'buffer' });
|
|
268
|
+
this.getInstance().addContentTypeParser(type, parserOptions, (req, body, done) => {
|
|
269
|
+
if (rawBody === true && Buffer.isBuffer(body)) {
|
|
270
|
+
req.rawBody = body;
|
|
271
|
+
}
|
|
272
|
+
if (parser) {
|
|
273
|
+
parser(req, body, done);
|
|
274
|
+
return;
|
|
275
|
+
}
|
|
276
|
+
done(null, body);
|
|
277
|
+
});
|
|
278
|
+
// To avoid the Nest application init to override our custom
|
|
279
|
+
// body parser, we mark the parsers as registered.
|
|
280
|
+
this._isParserRegistered = true;
|
|
281
|
+
}
|
|
255
282
|
async createMiddlewareFactory(requestMethod) {
|
|
256
283
|
if (!this.isMiddieRegistered) {
|
|
257
284
|
await this.registerMiddie();
|
|
@@ -277,22 +304,20 @@ class FastifyAdapter extends http_adapter_1.AbstractHttpAdapter {
|
|
|
277
304
|
return !('status' in response);
|
|
278
305
|
}
|
|
279
306
|
registerJsonContentParser(rawBody) {
|
|
307
|
+
const contentType = 'application/json';
|
|
308
|
+
const withRawBody = !!rawBody;
|
|
280
309
|
const { bodyLimit } = this.getInstance().initialConfig;
|
|
281
|
-
this.
|
|
282
|
-
if (rawBody === true && Buffer.isBuffer(body)) {
|
|
283
|
-
req.rawBody = body;
|
|
284
|
-
}
|
|
310
|
+
this.useBodyParser(contentType, withRawBody, { bodyLimit }, (req, body, done) => {
|
|
285
311
|
const { onProtoPoisoning, onConstructorPoisoning } = this.instance.initialConfig;
|
|
286
312
|
const defaultJsonParser = this.instance.getDefaultJsonParser(onProtoPoisoning || 'error', onConstructorPoisoning || 'error');
|
|
287
313
|
defaultJsonParser(req, body, done);
|
|
288
314
|
});
|
|
289
315
|
}
|
|
290
316
|
registerUrlencodedContentParser(rawBody) {
|
|
317
|
+
const contentType = 'application/x-www-form-urlencoded';
|
|
318
|
+
const withRawBody = !!rawBody;
|
|
291
319
|
const { bodyLimit } = this.getInstance().initialConfig;
|
|
292
|
-
this.
|
|
293
|
-
if (rawBody === true && Buffer.isBuffer(body)) {
|
|
294
|
-
req.rawBody = body;
|
|
295
|
-
}
|
|
320
|
+
this.useBodyParser(contentType, withRawBody, { bodyLimit }, (_req, body, done) => {
|
|
296
321
|
done(null, (0, querystring_1.parse)(body.toString()));
|
|
297
322
|
});
|
|
298
323
|
}
|
package/interfaces/index.d.ts
CHANGED
package/interfaces/index.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
1
2
|
import { INestApplication } from '@nestjs/common';
|
|
2
|
-
import { FastifyInstance, FastifyPluginAsync, FastifyPluginCallback, FastifyPluginOptions, FastifyRegisterOptions } from 'fastify';
|
|
3
|
+
import { FastifyBodyParser, FastifyInstance, FastifyPluginAsync, FastifyPluginCallback, FastifyPluginOptions, FastifyRegisterOptions, RawServerBase } from 'fastify';
|
|
3
4
|
import { Chain as LightMyRequestChain, InjectOptions, Response as LightMyRequestResponse } from 'light-my-request';
|
|
4
5
|
import { FastifyStaticOptions, FastifyViewOptions } from './external';
|
|
6
|
+
import { NestFastifyBodyParserOptions } from './nest-fastify-body-parser-options.interface';
|
|
5
7
|
export interface NestFastifyApplication extends INestApplication {
|
|
6
8
|
/**
|
|
7
9
|
* A wrapper function around native `fastify.register()` method.
|
|
@@ -13,6 +15,22 @@ export interface NestFastifyApplication extends INestApplication {
|
|
|
13
15
|
}> | Promise<{
|
|
14
16
|
default: FastifyPluginAsync<Options>;
|
|
15
17
|
}>, opts?: FastifyRegisterOptions<Options>): Promise<FastifyInstance>;
|
|
18
|
+
/**
|
|
19
|
+
* Register Fastify body parsers on the fly. Will respect
|
|
20
|
+
* the application's `rawBody` option.
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* const app = await NestFactory.create<NestFastifyApplication>(
|
|
24
|
+
* AppModule,
|
|
25
|
+
* new FastifyAdapter(),
|
|
26
|
+
* { rawBody: true }
|
|
27
|
+
* );
|
|
28
|
+
* // enable the json parser with a parser limit of 50mb
|
|
29
|
+
* app.useBodyParser('application/json', { bodyLimit: 50 * 1000 * 1024 });
|
|
30
|
+
*
|
|
31
|
+
* @returns {this}
|
|
32
|
+
*/
|
|
33
|
+
useBodyParser<TServer extends RawServerBase = RawServerBase>(type: string | string[] | RegExp, options?: NestFastifyBodyParserOptions, parser?: FastifyBodyParser<Buffer, TServer>): this;
|
|
16
34
|
/**
|
|
17
35
|
* Sets a base directory for public assets.
|
|
18
36
|
* Example `app.useStaticAssets({ root: 'public' })`
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nestjs/platform-fastify",
|
|
3
|
-
"version": "9.3.0
|
|
3
|
+
"version": "9.3.0",
|
|
4
4
|
"description": "Nest - modern, fast, powerful node.js web framework (@platform-fastify)",
|
|
5
5
|
"author": "Kamil Mysliwiec",
|
|
6
6
|
"license": "MIT",
|
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
"@fastify/cors": "8.2.0",
|
|
21
21
|
"@fastify/formbody": "7.4.0",
|
|
22
22
|
"@fastify/middie": "8.1.0",
|
|
23
|
-
"fastify": "4.
|
|
23
|
+
"fastify": "4.12.0",
|
|
24
24
|
"light-my-request": "5.8.0",
|
|
25
25
|
"path-to-regexp": "3.2.0",
|
|
26
26
|
"tslib": "2.4.1"
|