@nestjs/platform-fastify 9.4.2 → 10.0.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.
|
@@ -8,7 +8,7 @@ import { AbstractHttpAdapter } from '@nestjs/core/adapters/http-adapter';
|
|
|
8
8
|
import { FastifyBaseLogger, FastifyBodyParser, FastifyInstance, FastifyPluginAsync, FastifyPluginCallback, FastifyRegister, FastifyReply, FastifyRequest, FastifyServerOptions, RawReplyDefaultExpression, RawRequestDefaultExpression, RawServerBase, RawServerDefault, RequestGenericInterface } from 'fastify';
|
|
9
9
|
import * as http2 from 'http2';
|
|
10
10
|
import * as https from 'https';
|
|
11
|
-
import { Chain as LightMyRequestChain,
|
|
11
|
+
import { InjectOptions, Chain as LightMyRequestChain, Response as LightMyRequestResponse } from 'light-my-request';
|
|
12
12
|
import { NestFastifyBodyParserOptions } from '../interfaces';
|
|
13
13
|
import { FastifyStaticOptions, FastifyViewOptions } from '../interfaces/external';
|
|
14
14
|
type FastifyHttp2SecureOptions<Server extends http2.Http2SecureServer, Logger extends FastifyBaseLogger = FastifyBaseLogger> = FastifyServerOptions<Server, Logger> & {
|
|
@@ -8,6 +8,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
10
|
const symbols_1 = require("fastify/lib/symbols");
|
|
11
|
+
const pathToRegexp = require("path-to-regexp");
|
|
11
12
|
// `querystring` is used internally in fastify for registering urlencoded body parser.
|
|
12
13
|
const querystring_1 = require("querystring");
|
|
13
14
|
/**
|
|
@@ -56,11 +57,10 @@ class FastifyAdapter extends http_adapter_1.AbstractHttpAdapter {
|
|
|
56
57
|
};
|
|
57
58
|
},
|
|
58
59
|
deriveConstraint: (req) => {
|
|
59
|
-
var _a, _b, _c, _d;
|
|
60
60
|
// Media Type (Accept Header) Versioning Handler
|
|
61
61
|
if (this.versioningOptions.type === common_1.VersioningType.MEDIA_TYPE) {
|
|
62
62
|
const MEDIA_TYPE_HEADER = 'Accept';
|
|
63
|
-
const acceptHeaderValue = (
|
|
63
|
+
const acceptHeaderValue = (req.headers?.[MEDIA_TYPE_HEADER] || req.headers?.[MEDIA_TYPE_HEADER.toLowerCase()]);
|
|
64
64
|
const acceptHeaderVersionParameter = acceptHeaderValue
|
|
65
65
|
? acceptHeaderValue.split(';')[1]
|
|
66
66
|
: '';
|
|
@@ -70,8 +70,8 @@ class FastifyAdapter extends http_adapter_1.AbstractHttpAdapter {
|
|
|
70
70
|
}
|
|
71
71
|
// Header Versioning Handler
|
|
72
72
|
else if (this.versioningOptions.type === common_1.VersioningType.HEADER) {
|
|
73
|
-
const customHeaderVersionParameter =
|
|
74
|
-
|
|
73
|
+
const customHeaderVersionParameter = req.headers?.[this.versioningOptions.header] ||
|
|
74
|
+
req.headers?.[this.versioningOptions.header.toLowerCase()];
|
|
75
75
|
return (0, shared_utils_1.isUndefined)(customHeaderVersionParameter)
|
|
76
76
|
? common_1.VERSION_NEUTRAL // No version was supplied
|
|
77
77
|
: customHeaderVersionParameter;
|
|
@@ -86,9 +86,12 @@ class FastifyAdapter extends http_adapter_1.AbstractHttpAdapter {
|
|
|
86
86
|
};
|
|
87
87
|
const instance = instanceOrOptions && instanceOrOptions.server
|
|
88
88
|
? instanceOrOptions
|
|
89
|
-
: (0, fastify_1.fastify)(
|
|
89
|
+
: (0, fastify_1.fastify)({
|
|
90
|
+
constraints: {
|
|
90
91
|
version: this.versionConstraint,
|
|
91
|
-
}
|
|
92
|
+
},
|
|
93
|
+
...instanceOrOptions,
|
|
94
|
+
});
|
|
92
95
|
this.setInstance(instance);
|
|
93
96
|
}
|
|
94
97
|
async init() {
|
|
@@ -180,7 +183,7 @@ class FastifyAdapter extends http_adapter_1.AbstractHttpAdapter {
|
|
|
180
183
|
}
|
|
181
184
|
if (fastifyReply.getHeader('Content-Type') !== undefined &&
|
|
182
185
|
fastifyReply.getHeader('Content-Type') !== 'application/json' &&
|
|
183
|
-
|
|
186
|
+
body?.statusCode >= common_1.HttpStatus.BAD_REQUEST) {
|
|
184
187
|
common_1.Logger.warn("Content-Type doesn't match Reply body, you might need a custom ExceptionFilter for non-JSON responses", FastifyAdapter.name);
|
|
185
188
|
fastifyReply.header('Content-Type', 'application/json');
|
|
186
189
|
}
|
|
@@ -200,7 +203,7 @@ class FastifyAdapter extends http_adapter_1.AbstractHttpAdapter {
|
|
|
200
203
|
return response && response.view(view, options);
|
|
201
204
|
}
|
|
202
205
|
redirect(response, statusCode, url) {
|
|
203
|
-
const code = statusCode
|
|
206
|
+
const code = statusCode ?? common_1.HttpStatus.FOUND;
|
|
204
207
|
return response.status(code).redirect(url);
|
|
205
208
|
}
|
|
206
209
|
setErrorHandler(handler) {
|
|
@@ -273,7 +276,10 @@ class FastifyAdapter extends http_adapter_1.AbstractHttpAdapter {
|
|
|
273
276
|
this._isParserRegistered = true;
|
|
274
277
|
}
|
|
275
278
|
useBodyParser(type, rawBody, options, parser) {
|
|
276
|
-
const parserOptions =
|
|
279
|
+
const parserOptions = {
|
|
280
|
+
...(options || {}),
|
|
281
|
+
parseAs: 'buffer',
|
|
282
|
+
};
|
|
277
283
|
this.getInstance().addContentTypeParser(type, parserOptions, (req, body, done) => {
|
|
278
284
|
if (rawBody === true && Buffer.isBuffer(body)) {
|
|
279
285
|
req.rawBody = body;
|
|
@@ -298,9 +304,19 @@ class FastifyAdapter extends http_adapter_1.AbstractHttpAdapter {
|
|
|
298
304
|
: path;
|
|
299
305
|
// Fallback to "(.*)" to support plugins like GraphQL
|
|
300
306
|
normalizedPath = normalizedPath === '/(.*)' ? '(.*)' : normalizedPath;
|
|
307
|
+
const re = pathToRegexp(normalizedPath);
|
|
301
308
|
// The following type assertion is valid as we use import('@fastify/middie') rather than require('@fastify/middie')
|
|
302
309
|
// ref https://github.com/fastify/middie/pull/55
|
|
303
|
-
this.instance.use(normalizedPath,
|
|
310
|
+
this.instance.use(normalizedPath, (req, res, next) => {
|
|
311
|
+
const queryParamsIndex = req.originalUrl.indexOf('?');
|
|
312
|
+
const pathname = queryParamsIndex >= 0
|
|
313
|
+
? req.originalUrl.slice(0, queryParamsIndex)
|
|
314
|
+
: req.originalUrl;
|
|
315
|
+
if (!re.exec(pathname + '/') && normalizedPath) {
|
|
316
|
+
return next();
|
|
317
|
+
}
|
|
318
|
+
return callback(req, res, next);
|
|
319
|
+
});
|
|
304
320
|
};
|
|
305
321
|
}
|
|
306
322
|
getType() {
|
|
@@ -1,13 +1,19 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
|
-
import { INestApplication } from '@nestjs/common';
|
|
3
|
-
import { FastifyBodyParser, FastifyInstance, FastifyPluginAsync, FastifyPluginCallback, FastifyPluginOptions, FastifyRegisterOptions, RawServerBase } from 'fastify';
|
|
2
|
+
import { INestApplication, HttpServer } from '@nestjs/common';
|
|
3
|
+
import { FastifyBodyParser, FastifyInstance, FastifyPluginAsync, FastifyPluginCallback, FastifyPluginOptions, FastifyRegisterOptions, FastifyRequest, FastifyReply, RawServerBase, RawServerDefault } from 'fastify';
|
|
4
4
|
import { Chain as LightMyRequestChain, InjectOptions, Response as LightMyRequestResponse } from 'light-my-request';
|
|
5
5
|
import { FastifyStaticOptions, FastifyViewOptions } from './external';
|
|
6
6
|
import { NestFastifyBodyParserOptions } from './nest-fastify-body-parser-options.interface';
|
|
7
7
|
/**
|
|
8
8
|
* @publicApi
|
|
9
9
|
*/
|
|
10
|
-
export interface NestFastifyApplication extends INestApplication {
|
|
10
|
+
export interface NestFastifyApplication<TServer extends RawServerBase = RawServerDefault> extends INestApplication<TServer> {
|
|
11
|
+
/**
|
|
12
|
+
* Returns the underlying HTTP adapter bounded to a Fastify app.
|
|
13
|
+
*
|
|
14
|
+
* @returns {HttpServer}
|
|
15
|
+
*/
|
|
16
|
+
getHttpAdapter(): HttpServer<FastifyRequest, FastifyReply, FastifyInstance>;
|
|
11
17
|
/**
|
|
12
18
|
* A wrapper function around native `fastify.register()` method.
|
|
13
19
|
* Example `app.register(require('@fastify/formbody'))
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nestjs/platform-fastify",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "10.0.0",
|
|
4
4
|
"description": "Nest - modern, fast, powerful node.js web framework (@platform-fastify)",
|
|
5
5
|
"author": "Kamil Mysliwiec",
|
|
6
6
|
"license": "MIT",
|
|
@@ -18,19 +18,19 @@
|
|
|
18
18
|
"access": "public"
|
|
19
19
|
},
|
|
20
20
|
"dependencies": {
|
|
21
|
-
"@fastify/cors": "8.
|
|
21
|
+
"@fastify/cors": "8.3.0",
|
|
22
22
|
"@fastify/formbody": "7.4.0",
|
|
23
23
|
"@fastify/middie": "8.3.0",
|
|
24
|
-
"fastify": "4.
|
|
25
|
-
"light-my-request": "5.
|
|
24
|
+
"fastify": "4.18.0",
|
|
25
|
+
"light-my-request": "5.10.0",
|
|
26
26
|
"path-to-regexp": "3.2.0",
|
|
27
|
-
"tslib": "2.5.
|
|
27
|
+
"tslib": "2.5.3"
|
|
28
28
|
},
|
|
29
29
|
"peerDependencies": {
|
|
30
30
|
"@fastify/static": "^6.0.0",
|
|
31
31
|
"@fastify/view": "^7.0.0",
|
|
32
|
-
"@nestjs/common": "^
|
|
33
|
-
"@nestjs/core": "^
|
|
32
|
+
"@nestjs/common": "^10.0.0",
|
|
33
|
+
"@nestjs/core": "^10.0.0"
|
|
34
34
|
},
|
|
35
35
|
"peerDependenciesMeta": {
|
|
36
36
|
"@fastify/static": {
|