@nestjs/platform-express 8.4.5 → 8.4.6

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.
@@ -4,6 +4,7 @@ import { CorsOptions, CorsOptionsDelegate } from '@nestjs/common/interfaces/exte
4
4
  import { NestApplicationOptions } from '@nestjs/common/interfaces/nest-application-options.interface';
5
5
  import { AbstractHttpAdapter } from '@nestjs/core/adapters/http-adapter';
6
6
  import { ServeStaticOptions } from '../interfaces/serve-static-options.interface';
7
+ declare type VersionedRoute = <TRequest extends Record<string, any> = any, TResponse = any>(req: TRequest, res: TResponse, next: () => void) => any;
7
8
  export declare class ExpressAdapter extends AbstractHttpAdapter {
8
9
  private readonly routerMethodFactory;
9
10
  constructor(instance?: any);
@@ -30,9 +31,10 @@ export declare class ExpressAdapter extends AbstractHttpAdapter {
30
31
  enableCors(options: CorsOptions | CorsOptionsDelegate<any>): any;
31
32
  createMiddlewareFactory(requestMethod: RequestMethod): (path: string, callback: Function) => any;
32
33
  initHttpServer(options: NestApplicationOptions): void;
33
- registerParserMiddleware(): void;
34
+ registerParserMiddleware(prefix?: string, rawBody?: boolean): void;
34
35
  setLocal(key: string, value: any): this;
35
36
  getType(): string;
36
- applyVersionFilter(handler: Function, version: VersionValue, versioningOptions: VersioningOptions): <TRequest extends Record<string, any> = any, TResponse = any>(req: TRequest, res: TResponse, next: () => void) => any;
37
+ applyVersionFilter(handler: Function, version: VersionValue, versioningOptions: VersioningOptions): VersionedRoute;
37
38
  private isMiddlewareApplied;
38
39
  }
40
+ export {};
@@ -117,9 +117,20 @@ class ExpressAdapter extends http_adapter_1.AbstractHttpAdapter {
117
117
  }
118
118
  this.httpServer = http.createServer(this.getInstance());
119
119
  }
120
- registerParserMiddleware() {
120
+ registerParserMiddleware(prefix, rawBody) {
121
+ let bodyParserJsonOptions;
122
+ if (rawBody === true) {
123
+ bodyParserJsonOptions = {
124
+ verify: (req, _res, buffer) => {
125
+ if (Buffer.isBuffer(buffer)) {
126
+ req.rawBody = buffer;
127
+ }
128
+ return true;
129
+ },
130
+ };
131
+ }
121
132
  const parserMiddleware = {
122
- jsonParser: (0, body_parser_1.json)(),
133
+ jsonParser: (0, body_parser_1.json)(bodyParserJsonOptions),
123
134
  urlencodedParser: (0, body_parser_1.urlencoded)({ extended: true }),
124
135
  };
125
136
  Object.keys(parserMiddleware)
@@ -134,24 +145,28 @@ class ExpressAdapter extends http_adapter_1.AbstractHttpAdapter {
134
145
  return 'express';
135
146
  }
136
147
  applyVersionFilter(handler, version, versioningOptions) {
137
- return (req, res, next) => {
138
- var _a, _b, _c, _d;
139
- if (version === interfaces_1.VERSION_NEUTRAL) {
140
- return handler(req, res, next);
148
+ const callNextHandler = (req, res, next) => {
149
+ if (!next) {
150
+ throw new common_1.InternalServerErrorException('HTTP adapter does not support filtering on version');
141
151
  }
152
+ return next();
153
+ };
154
+ if (version === interfaces_1.VERSION_NEUTRAL ||
142
155
  // URL Versioning is done via the path, so the filter continues forward
143
- if (versioningOptions.type === common_1.VersioningType.URI) {
144
- return handler(req, res, next);
145
- }
146
- // Custom Extractor Versioning Handler
147
- if (versioningOptions.type === common_1.VersioningType.CUSTOM) {
156
+ versioningOptions.type === common_1.VersioningType.URI) {
157
+ const handlerForNoVersioning = (req, res, next) => handler(req, res, next);
158
+ return handlerForNoVersioning;
159
+ }
160
+ // Custom Extractor Versioning Handler
161
+ if (versioningOptions.type === common_1.VersioningType.CUSTOM) {
162
+ const handlerForCustomVersioning = (req, res, next) => {
148
163
  const extractedVersion = versioningOptions.extractor(req);
149
164
  if (Array.isArray(version)) {
150
165
  if (Array.isArray(extractedVersion) &&
151
166
  version.filter(v => extractedVersion.includes(v)).length) {
152
167
  return handler(req, res, next);
153
168
  }
154
- else if ((0, shared_utils_1.isString)(extractedVersion) &&
169
+ if ((0, shared_utils_1.isString)(extractedVersion) &&
155
170
  version.includes(extractedVersion)) {
156
171
  return handler(req, res, next);
157
172
  }
@@ -165,21 +180,33 @@ class ExpressAdapter extends http_adapter_1.AbstractHttpAdapter {
165
180
  extractedVersion.includes(version)) {
166
181
  return handler(req, res, next);
167
182
  }
168
- else if ((0, shared_utils_1.isString)(extractedVersion) &&
169
- version === extractedVersion) {
183
+ if ((0, shared_utils_1.isString)(extractedVersion) && version === extractedVersion) {
170
184
  return handler(req, res, next);
171
185
  }
172
186
  }
173
- }
174
- // Media Type (Accept Header) Versioning Handler
175
- if (versioningOptions.type === common_1.VersioningType.MEDIA_TYPE) {
187
+ return callNextHandler(req, res, next);
188
+ };
189
+ return handlerForCustomVersioning;
190
+ }
191
+ // Media Type (Accept Header) Versioning Handler
192
+ if (versioningOptions.type === common_1.VersioningType.MEDIA_TYPE) {
193
+ const handlerForMediaTypeVersioning = (req, res, next) => {
194
+ var _a, _b;
176
195
  const MEDIA_TYPE_HEADER = 'Accept';
177
196
  const acceptHeaderValue = ((_a = req.headers) === null || _a === void 0 ? void 0 : _a[MEDIA_TYPE_HEADER]) ||
178
197
  ((_b = req.headers) === null || _b === void 0 ? void 0 : _b[MEDIA_TYPE_HEADER.toLowerCase()]);
179
198
  const acceptHeaderVersionParameter = acceptHeaderValue
180
199
  ? acceptHeaderValue.split(';')[1]
181
- : '';
182
- if (acceptHeaderVersionParameter) {
200
+ : undefined;
201
+ // No version was supplied
202
+ if ((0, shared_utils_1.isUndefined)(acceptHeaderVersionParameter)) {
203
+ if (Array.isArray(version)) {
204
+ if (version.includes(interfaces_1.VERSION_NEUTRAL)) {
205
+ return handler(req, res, next);
206
+ }
207
+ }
208
+ }
209
+ else {
183
210
  const headerVersion = acceptHeaderVersionParameter.split(versioningOptions.key)[1];
184
211
  if (Array.isArray(version)) {
185
212
  if (version.includes(headerVersion)) {
@@ -192,12 +219,25 @@ class ExpressAdapter extends http_adapter_1.AbstractHttpAdapter {
192
219
  }
193
220
  }
194
221
  }
195
- }
196
- // Header Versioning Handler
197
- else if (versioningOptions.type === common_1.VersioningType.HEADER) {
198
- const customHeaderVersionParameter = ((_c = req.headers) === null || _c === void 0 ? void 0 : _c[versioningOptions.header]) ||
199
- ((_d = req.headers) === null || _d === void 0 ? void 0 : _d[versioningOptions.header.toLowerCase()]);
200
- if (customHeaderVersionParameter) {
222
+ return callNextHandler(req, res, next);
223
+ };
224
+ return handlerForMediaTypeVersioning;
225
+ }
226
+ // Header Versioning Handler
227
+ if (versioningOptions.type === common_1.VersioningType.HEADER) {
228
+ const handlerForHeaderVersioning = (req, res, next) => {
229
+ var _a, _b;
230
+ const customHeaderVersionParameter = ((_a = req.headers) === null || _a === void 0 ? void 0 : _a[versioningOptions.header]) ||
231
+ ((_b = req.headers) === null || _b === void 0 ? void 0 : _b[versioningOptions.header.toLowerCase()]);
232
+ // No version was supplied
233
+ if ((0, shared_utils_1.isUndefined)(customHeaderVersionParameter)) {
234
+ if (Array.isArray(version)) {
235
+ if (version.includes(interfaces_1.VERSION_NEUTRAL)) {
236
+ return handler(req, res, next);
237
+ }
238
+ }
239
+ }
240
+ else {
201
241
  if (Array.isArray(version)) {
202
242
  if (version.includes(customHeaderVersionParameter)) {
203
243
  return handler(req, res, next);
@@ -209,12 +249,10 @@ class ExpressAdapter extends http_adapter_1.AbstractHttpAdapter {
209
249
  }
210
250
  }
211
251
  }
212
- }
213
- if (!next) {
214
- throw new common_1.InternalServerErrorException('HTTP adapter does not support filtering on version');
215
- }
216
- return next();
217
- };
252
+ return callNextHandler(req, res, next);
253
+ };
254
+ return handlerForHeaderVersioning;
255
+ }
218
256
  }
219
257
  isMiddlewareApplied(name) {
220
258
  const app = this.getInstance();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nestjs/platform-express",
3
- "version": "8.4.5",
3
+ "version": "8.4.6",
4
4
  "description": "Nest - modern, fast, powerful node.js web framework (@platform-express)",
5
5
  "author": "Kamil Mysliwiec",
6
6
  "license": "MIT",
@@ -24,8 +24,8 @@
24
24
  "tslib": "2.4.0"
25
25
  },
26
26
  "devDependencies": {
27
- "@nestjs/common": "8.4.5",
28
- "@nestjs/core": "8.4.5"
27
+ "@nestjs/common": "8.4.6",
28
+ "@nestjs/core": "8.4.6"
29
29
  },
30
30
  "peerDependencies": {
31
31
  "@nestjs/common": "^8.0.0",