@nestjs/platform-express 12.1.1 → 12.1.2

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.
@@ -5,9 +5,16 @@ import * as http from 'http';
5
5
  import * as https from 'https';
6
6
  import { pathToRegexp } from 'path-to-regexp';
7
7
  import { getBodyParserOptions } from './utils/get-body-parser-options.util.js';
8
+ import { getMediaTypeVersion } from './utils/get-media-type-version.util.js';
8
9
  import { addLeadingSlash, isFunction, isNil, isObject, isString, isUndefined, stripEndSlash, } from '@nestjs/common/internal';
9
10
  import { AbstractHttpAdapter } from '@nestjs/core';
10
11
  import { RouterMethodFactory, LegacyRouteConverter, } from '@nestjs/core/internal';
12
+ // Covers "application/json" and the "+json" structured syntax suffix (RFC 6839),
13
+ // e.g. "application/problem+json", with or without parameters.
14
+ function isJsonContentType(contentType) {
15
+ const mediaType = contentType.split(';')[0].trim().toLowerCase();
16
+ return (mediaType.startsWith('application/json') || mediaType.endsWith('+json'));
17
+ }
11
18
  /**
12
19
  * @publicApi
13
20
  */
@@ -60,7 +67,7 @@ export class ExpressAdapter extends AbstractHttpAdapter {
60
67
  }
61
68
  const responseContentType = response.getHeader('Content-Type');
62
69
  if (typeof responseContentType === 'string' &&
63
- !responseContentType.startsWith('application/json') &&
70
+ !isJsonContentType(responseContentType) &&
64
71
  body?.statusCode >= HttpStatus.BAD_REQUEST) {
65
72
  this.logger.warn("Content-Type doesn't match Reply body, you might need a custom ExceptionFilter for non-JSON responses");
66
73
  response.setHeader('Content-Type', 'application/json');
@@ -308,11 +315,9 @@ export class ExpressAdapter extends AbstractHttpAdapter {
308
315
  const MEDIA_TYPE_HEADER = 'Accept';
309
316
  const acceptHeaderValue = req.headers?.[MEDIA_TYPE_HEADER] ||
310
317
  req.headers?.[MEDIA_TYPE_HEADER.toLowerCase()];
311
- const acceptHeaderVersionParameter = acceptHeaderValue
312
- ? acceptHeaderValue.split(';')[1]
313
- : undefined;
318
+ const headerVersion = getMediaTypeVersion(acceptHeaderValue, versioningOptions.key);
314
319
  // No version was supplied
315
- if (isUndefined(acceptHeaderVersionParameter)) {
320
+ if (isUndefined(headerVersion)) {
316
321
  if (Array.isArray(version)) {
317
322
  if (version.includes(VERSION_NEUTRAL)) {
318
323
  return handler(req, res, next);
@@ -320,7 +325,6 @@ export class ExpressAdapter extends AbstractHttpAdapter {
320
325
  }
321
326
  }
322
327
  else {
323
- const headerVersion = acceptHeaderVersionParameter.split(versioningOptions.key)[1];
324
328
  if (Array.isArray(version)) {
325
329
  if (version.includes(headerVersion)) {
326
330
  return handler(req, res, next);
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Returns the version carried by an `Accept` header for media type versioning,
3
+ * e.g. `2` for `application/json;v=2` with the `v=` key.
4
+ *
5
+ * The version parameter may follow other parameters and may belong to any
6
+ * media range of a list (`text/html, application/json;v=2`); the first match
7
+ * wins. Returns `undefined` when no parameter starts with the key.
8
+ *
9
+ * Duplicated in each platform package so that a platform package never
10
+ * depends on a newer `@nestjs/core` than the one installed.
11
+ */
12
+ export declare function getMediaTypeVersion(acceptHeader: string | string[] | undefined, key: string): string | undefined;
@@ -0,0 +1,26 @@
1
+ /**
2
+ * Returns the version carried by an `Accept` header for media type versioning,
3
+ * e.g. `2` for `application/json;v=2` with the `v=` key.
4
+ *
5
+ * The version parameter may follow other parameters and may belong to any
6
+ * media range of a list (`text/html, application/json;v=2`); the first match
7
+ * wins. Returns `undefined` when no parameter starts with the key.
8
+ *
9
+ * Duplicated in each platform package so that a platform package never
10
+ * depends on a newer `@nestjs/core` than the one installed.
11
+ */
12
+ export function getMediaTypeVersion(acceptHeader, key) {
13
+ const mediaRanges = []
14
+ .concat(acceptHeader ?? [])
15
+ .flatMap(value => value.split(','));
16
+ for (const mediaRange of mediaRanges) {
17
+ const [, ...parameters] = mediaRange.split(';');
18
+ for (const parameter of parameters) {
19
+ const trimmedParameter = parameter.trim();
20
+ if (trimmedParameter.startsWith(key)) {
21
+ return trimmedParameter.slice(key.length);
22
+ }
23
+ }
24
+ }
25
+ return undefined;
26
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nestjs/platform-express",
3
- "version": "12.1.1",
3
+ "version": "12.1.2",
4
4
  "description": "Nest - modern, fast, powerful node.js web framework (@platform-express)",
5
5
  "author": "Kamil Mysliwiec",
6
6
  "license": "MIT",
@@ -32,12 +32,12 @@
32
32
  "tslib": "2.8.1"
33
33
  },
34
34
  "devDependencies": {
35
- "@nestjs/common": "12.1.1",
36
- "@nestjs/core": "12.1.1"
35
+ "@nestjs/common": "12.1.2",
36
+ "@nestjs/core": "12.1.2"
37
37
  },
38
38
  "peerDependencies": {
39
39
  "@nestjs/common": "^12.0.0",
40
40
  "@nestjs/core": "^12.0.0"
41
41
  },
42
- "gitHead": "9feedffde8ef8c86f0cd521153740575dc579d88"
42
+ "gitHead": "8ab60aa1b2927fbe0e23135ffc65d610c623236a"
43
43
  }