@nestjs/common 10.4.10 → 11.0.0-next.1

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.
Files changed (77) hide show
  1. package/decorators/core/exception-filters.decorator.js +1 -3
  2. package/decorators/core/inject.decorator.d.ts +2 -1
  3. package/decorators/core/use-guards.decorator.js +1 -3
  4. package/decorators/core/use-interceptors.decorator.js +1 -2
  5. package/decorators/core/use-pipes.decorator.js +1 -2
  6. package/decorators/http/create-route-param-metadata.decorator.d.ts +1 -1
  7. package/decorators/http/create-route-param-metadata.decorator.js +2 -1
  8. package/decorators/http/request-mapping.decorator.d.ts +56 -0
  9. package/decorators/http/request-mapping.decorator.js +57 -1
  10. package/decorators/http/route-params.decorator.d.ts +1 -1
  11. package/decorators/http/route-params.decorator.js +2 -1
  12. package/decorators/modules/module.decorator.js +1 -1
  13. package/enums/http-status.enum.d.ts +8 -1
  14. package/enums/http-status.enum.js +7 -0
  15. package/enums/request-method.enum.d.ts +8 -1
  16. package/enums/request-method.enum.js +7 -0
  17. package/exceptions/bad-gateway.exception.d.ts +1 -1
  18. package/exceptions/bad-request.exception.d.ts +1 -1
  19. package/exceptions/conflict.exception.d.ts +1 -1
  20. package/exceptions/forbidden.exception.d.ts +1 -1
  21. package/exceptions/gateway-timeout.exception.d.ts +1 -1
  22. package/exceptions/gone.exception.d.ts +1 -1
  23. package/exceptions/http-version-not-supported.exception.d.ts +1 -1
  24. package/exceptions/http.exception.d.ts +8 -3
  25. package/exceptions/http.exception.js +4 -4
  26. package/exceptions/im-a-teapot.exception.d.ts +1 -1
  27. package/exceptions/index.d.ts +17 -16
  28. package/exceptions/index.js +17 -16
  29. package/exceptions/internal-server-error.exception.d.ts +1 -1
  30. package/exceptions/method-not-allowed.exception.d.ts +1 -1
  31. package/exceptions/misdirected.exception.d.ts +1 -1
  32. package/exceptions/not-acceptable.exception.d.ts +1 -1
  33. package/exceptions/not-found.exception.d.ts +1 -1
  34. package/exceptions/not-implemented.exception.d.ts +1 -1
  35. package/exceptions/payload-too-large.exception.d.ts +1 -1
  36. package/exceptions/precondition-failed.exception.d.ts +1 -1
  37. package/exceptions/request-timeout.exception.d.ts +1 -1
  38. package/exceptions/service-unavailable.exception.d.ts +1 -1
  39. package/exceptions/unauthorized.exception.d.ts +1 -1
  40. package/exceptions/unprocessable-entity.exception.d.ts +1 -1
  41. package/exceptions/unsupported-media-type.exception.d.ts +1 -1
  42. package/file-stream/streamable-file.d.ts +3 -3
  43. package/file-stream/streamable-file.js +2 -2
  44. package/interfaces/external/transformer-package.interface.d.ts +1 -1
  45. package/interfaces/features/custom-route-param-factory.interface.d.ts +2 -1
  46. package/interfaces/http/http-exception-body.interface.d.ts +1 -1
  47. package/interfaces/http/http-server.interface.d.ts +14 -0
  48. package/interfaces/middleware/nest-middleware.interface.d.ts +1 -1
  49. package/interfaces/modules/module-metadata.interface.d.ts +1 -1
  50. package/interfaces/nest-application-context-options.interface.d.ts +8 -0
  51. package/interfaces/nest-application-context.interface.d.ts +3 -1
  52. package/interfaces/nest-microservice.interface.d.ts +25 -7
  53. package/module-utils/configurable-module.builder.js +4 -3
  54. package/module-utils/utils/get-injection-providers.util.d.ts +1 -1
  55. package/package.json +1 -1
  56. package/pipes/file/file-type.validator.d.ts +1 -1
  57. package/pipes/file/file-type.validator.js +4 -1
  58. package/pipes/file/max-file-size.validator.d.ts +1 -1
  59. package/pipes/file/max-file-size.validator.js +4 -1
  60. package/pipes/index.d.ts +4 -3
  61. package/pipes/index.js +4 -3
  62. package/pipes/parse-array.pipe.d.ts +17 -0
  63. package/pipes/parse-array.pipe.js +3 -1
  64. package/pipes/parse-bool.pipe.d.ts +15 -2
  65. package/pipes/parse-enum.pipe.d.ts +15 -2
  66. package/pipes/parse-float.pipe.d.ts +15 -2
  67. package/pipes/parse-int.pipe.d.ts +15 -2
  68. package/pipes/parse-uuid.pipe.d.ts +18 -2
  69. package/pipes/validation.pipe.d.ts +3 -3
  70. package/pipes/validation.pipe.js +7 -1
  71. package/serializer/class-serializer.interceptor.js +1 -1
  72. package/services/console-logger.service.d.ts +95 -4
  73. package/services/console-logger.service.js +111 -16
  74. package/services/logger.service.d.ts +1 -1
  75. package/services/logger.service.js +2 -2
  76. package/utils/merge-with-values.util.js +1 -0
  77. package/utils/shared.utils.js +0 -1
@@ -1,3 +1,4 @@
1
+ import { InspectOptions } from 'util';
1
2
  import { LoggerService, LogLevel } from './logger.service';
2
3
  export interface ConsoleLoggerOptions {
3
4
  /**
@@ -6,16 +7,98 @@ export interface ConsoleLoggerOptions {
6
7
  logLevels?: LogLevel[];
7
8
  /**
8
9
  * If enabled, will print timestamp (time difference) between current and previous log message.
10
+ * Note: This option is not used when `json` is enabled.
9
11
  */
10
12
  timestamp?: boolean;
13
+ /**
14
+ * A prefix to be used for each log message.
15
+ * Note: This option is not used when `json` is enabled.
16
+ */
17
+ prefix?: string;
18
+ /**
19
+ * If enabled, will print the log message in JSON format.
20
+ */
21
+ json?: boolean;
22
+ /**
23
+ * If enabled, will print the log message in color.
24
+ * Default true if json is disabled, false otherwise
25
+ */
26
+ colors?: boolean;
27
+ /**
28
+ * The context of the logger.
29
+ */
30
+ context?: string;
31
+ /**
32
+ * If enabled, will print the log message in a single line, even if it is an object with multiple properties.
33
+ * If set to a number, the most n inner elements are united on a single line as long as all properties fit into breakLength. Short array elements are also grouped together.
34
+ * Default true when `json` is enabled, false otherwise.
35
+ */
36
+ compact?: boolean | number;
37
+ /**
38
+ * Specifies the maximum number of Array, TypedArray, Map, Set, WeakMap, and WeakSet elements to include when formatting.
39
+ * Set to null or Infinity to show all elements. Set to 0 or negative to show no elements.
40
+ * Ignored when `json` is enabled, colors are disabled, and `compact` is set to true as it produces a parseable JSON output.
41
+ * @default 100
42
+ */
43
+ maxArrayLength?: number;
44
+ /**
45
+ * Specifies the maximum number of characters to include when formatting.
46
+ * Set to null or Infinity to show all elements. Set to 0 or negative to show no characters.
47
+ * Ignored when `json` is enabled, colors are disabled, and `compact` is set to true as it produces a parseable JSON output.
48
+ * @default 10000.
49
+ */
50
+ maxStringLength?: number;
51
+ /**
52
+ * If enabled, will sort keys while formatting objects.
53
+ * Can also be a custom sorting function.
54
+ * Ignored when `json` is enabled, colors are disabled, and `compact` is set to true as it produces a parseable JSON output.
55
+ * @default false
56
+ */
57
+ sorted?: boolean | ((a: string, b: string) => number);
58
+ /**
59
+ * Specifies the number of times to recurse while formatting object. T
60
+ * This is useful for inspecting large objects. To recurse up to the maximum call stack size pass Infinity or null.
61
+ * Ignored when `json` is enabled, colors are disabled, and `compact` is set to true as it produces a parseable JSON output.
62
+ * @default 5
63
+ */
64
+ depth?: number;
65
+ /**
66
+ * If true, object's non-enumerable symbols and properties are included in the formatted result.
67
+ * WeakMap and WeakSet entries are also included as well as user defined prototype properties
68
+ * @default false
69
+ */
70
+ showHidden?: boolean;
71
+ /**
72
+ * The length at which input values are split across multiple lines. Set to Infinity to format the input as a single line (in combination with "compact" set to true).
73
+ * Default Infinity when "compact" is true, 80 otherwise.
74
+ * Ignored when `json` is enabled, colors are disabled, and `compact` is set to true as it produces a parseable JSON output.
75
+ */
76
+ breakLength?: number;
11
77
  }
12
78
  export declare class ConsoleLogger implements LoggerService {
13
- protected context?: string;
79
+ /**
80
+ * The options of the logger.
81
+ */
14
82
  protected options: ConsoleLoggerOptions;
15
- private static lastTimestampAt?;
16
- private originalContext?;
83
+ /**
84
+ * The context of the logger (can be set manually or automatically inferred).
85
+ */
86
+ protected context?: string;
87
+ /**
88
+ * The original context of the logger (set in the constructor).
89
+ */
90
+ protected originalContext?: string;
91
+ /**
92
+ * The options used for the "inspect" method.
93
+ */
94
+ protected inspectOptions: InspectOptions;
95
+ /**
96
+ * The last timestamp at which the log message was printed.
97
+ */
98
+ protected static lastTimestampAt?: number;
17
99
  constructor();
18
100
  constructor(context: string);
101
+ constructor(options: ConsoleLoggerOptions);
19
102
  constructor(context: string, options: ConsoleLoggerOptions);
20
103
  /**
21
104
  * Write a 'log' level log, if the configured level allows for it.
@@ -70,7 +153,13 @@ export declare class ConsoleLogger implements LoggerService {
70
153
  resetContext(): void;
71
154
  isLevelEnabled(level: LogLevel): boolean;
72
155
  protected getTimestamp(): string;
73
- protected printMessages(messages: unknown[], context?: string, logLevel?: LogLevel, writeStreamType?: 'stdout' | 'stderr'): void;
156
+ protected printMessages(messages: unknown[], context?: string, logLevel?: LogLevel, writeStreamType?: 'stdout' | 'stderr', errorStack?: unknown): void;
157
+ protected printAsJson(message: unknown, options: {
158
+ context: string;
159
+ logLevel: LogLevel;
160
+ writeStreamType?: 'stdout' | 'stderr';
161
+ errorStack?: unknown;
162
+ }): void;
74
163
  protected formatPid(pid: number): string;
75
164
  protected formatContext(context: string): string;
76
165
  protected formatMessage(logLevel: LogLevel, message: unknown, pidMessage: string, formattedLogLevel: string, contextMessage: string, timestampDiff: string): string;
@@ -79,6 +168,8 @@ export declare class ConsoleLogger implements LoggerService {
79
168
  protected printStackTrace(stack: string): void;
80
169
  protected updateAndGetTimestampDiff(): string;
81
170
  protected formatTimestampDiff(timestampDiff: number): string;
171
+ protected getInspectOptions(): InspectOptions;
172
+ protected stringifyReplacer(key: string, value: unknown): unknown;
82
173
  private getContextAndMessagesToPrint;
83
174
  private getContextAndStackAndMessagesToPrint;
84
175
  private isStackFormat;
@@ -3,10 +3,12 @@ var ConsoleLogger_1;
3
3
  Object.defineProperty(exports, "__esModule", { value: true });
4
4
  exports.ConsoleLogger = void 0;
5
5
  const tslib_1 = require("tslib");
6
+ const util_1 = require("util");
6
7
  const core_1 = require("../decorators/core");
7
8
  const cli_colors_util_1 = require("../utils/cli-colors.util");
8
9
  const shared_utils_1 = require("../utils/shared.utils");
9
10
  const utils_1 = require("./utils");
11
+ const DEFAULT_DEPTH = 5;
10
12
  const DEFAULT_LOG_LEVELS = [
11
13
  'log',
12
14
  'error',
@@ -24,13 +26,21 @@ const dateTimeFormatter = new Intl.DateTimeFormat(undefined, {
24
26
  month: '2-digit',
25
27
  });
26
28
  let ConsoleLogger = ConsoleLogger_1 = class ConsoleLogger {
27
- constructor(context, options = {}) {
28
- this.context = context;
29
- this.options = options;
30
- if (!options.logLevels) {
31
- options.logLevels = DEFAULT_LOG_LEVELS;
32
- }
29
+ constructor(contextOrOptions, options) {
30
+ // eslint-disable-next-line prefer-const
31
+ let [context, opts] = (0, shared_utils_1.isString)(contextOrOptions)
32
+ ? [contextOrOptions, options]
33
+ : options
34
+ ? [undefined, options]
35
+ : [contextOrOptions?.context, contextOrOptions];
36
+ opts = opts ?? {};
37
+ opts.logLevels ??= DEFAULT_LOG_LEVELS;
38
+ opts.colors ??= opts.colors ?? (opts.json ? false : true);
39
+ opts.prefix ??= 'Nest';
40
+ this.options = opts;
41
+ this.inspectOptions = this.getInspectOptions();
33
42
  if (context) {
43
+ this.context = context;
34
44
  this.originalContext = context;
35
45
  }
36
46
  }
@@ -49,7 +59,7 @@ let ConsoleLogger = ConsoleLogger_1 = class ConsoleLogger {
49
59
  return;
50
60
  }
51
61
  const { messages, context, stack } = this.getContextAndStackAndMessagesToPrint([message, ...optionalParams]);
52
- this.printMessages(messages, context, 'error', 'stderr');
62
+ this.printMessages(messages, context, 'error', 'stderr', stack);
53
63
  this.printStackTrace(stack);
54
64
  }
55
65
  warn(message, ...optionalParams) {
@@ -122,8 +132,17 @@ let ConsoleLogger = ConsoleLogger_1 = class ConsoleLogger {
122
132
  getTimestamp() {
123
133
  return dateTimeFormatter.format(Date.now());
124
134
  }
125
- printMessages(messages, context = '', logLevel = 'log', writeStreamType) {
135
+ printMessages(messages, context = '', logLevel = 'log', writeStreamType, errorStack) {
126
136
  messages.forEach(message => {
137
+ if (this.options.json) {
138
+ this.printAsJson(message, {
139
+ context,
140
+ logLevel,
141
+ writeStreamType,
142
+ errorStack,
143
+ });
144
+ return;
145
+ }
127
146
  const pidMessage = this.formatPid(process.pid);
128
147
  const contextMessage = this.formatContext(context);
129
148
  const timestampDiff = this.updateAndGetTimestampDiff();
@@ -132,11 +151,33 @@ let ConsoleLogger = ConsoleLogger_1 = class ConsoleLogger {
132
151
  process[writeStreamType ?? 'stdout'].write(formattedMessage);
133
152
  });
134
153
  }
154
+ printAsJson(message, options) {
155
+ const logObject = {
156
+ level: options.logLevel,
157
+ pid: process.pid,
158
+ timestamp: Date.now(),
159
+ message,
160
+ };
161
+ if (options.context) {
162
+ logObject.context = options.context;
163
+ }
164
+ if (options.errorStack) {
165
+ logObject.stack = options.errorStack;
166
+ }
167
+ const formattedMessage = !this.options.colors && this.inspectOptions.compact === true
168
+ ? JSON.stringify(logObject, this.stringifyReplacer)
169
+ : (0, util_1.inspect)(logObject, this.inspectOptions);
170
+ process[options.writeStreamType ?? 'stdout'].write(`${formattedMessage}\n`);
171
+ }
135
172
  formatPid(pid) {
136
- return `[Nest] ${pid} - `;
173
+ return `[${this.options.prefix}] ${pid} - `;
137
174
  }
138
175
  formatContext(context) {
139
- return context ? (0, cli_colors_util_1.yellow)(`[${context}] `) : '';
176
+ if (!context) {
177
+ return '';
178
+ }
179
+ context = `[${context}] `;
180
+ return this.options.colors ? (0, cli_colors_util_1.yellow)(context) : context;
140
181
  }
141
182
  formatMessage(logLevel, message, pidMessage, formattedLogLevel, contextMessage, timestampDiff) {
142
183
  const output = this.stringifyMessage(message, logLevel);
@@ -155,16 +196,27 @@ let ConsoleLogger = ConsoleLogger_1 = class ConsoleLogger {
155
196
  // If the message is a non-class function, call it and re-resolve its value.
156
197
  return this.stringifyMessage(message(), logLevel);
157
198
  }
158
- return (0, shared_utils_1.isPlainObject)(message) || Array.isArray(message)
159
- ? `${this.colorize('Object:', logLevel)}\n${JSON.stringify(message, (key, value) => typeof value === 'bigint' ? value.toString() : value, 2)}\n`
160
- : this.colorize(message, logLevel);
199
+ if (typeof message === 'string') {
200
+ return this.colorize(message, logLevel);
201
+ }
202
+ const outputText = (0, util_1.inspect)(message, this.inspectOptions);
203
+ if ((0, shared_utils_1.isPlainObject)(message)) {
204
+ return `Object(${Object.keys(message).length}) ${outputText}`;
205
+ }
206
+ if (Array.isArray(message)) {
207
+ return `Array(${message.length}) ${outputText}`;
208
+ }
209
+ return outputText;
161
210
  }
162
211
  colorize(message, logLevel) {
212
+ if (!this.options.colors || this.options.json) {
213
+ return message;
214
+ }
163
215
  const color = this.getColorByLogLevel(logLevel);
164
216
  return color(message);
165
217
  }
166
218
  printStackTrace(stack) {
167
- if (!stack) {
219
+ if (!stack || this.options.json) {
168
220
  return;
169
221
  }
170
222
  process.stderr.write(`${stack}\n`);
@@ -178,7 +230,50 @@ let ConsoleLogger = ConsoleLogger_1 = class ConsoleLogger {
178
230
  return result;
179
231
  }
180
232
  formatTimestampDiff(timestampDiff) {
181
- return (0, cli_colors_util_1.yellow)(` +${timestampDiff}ms`);
233
+ const formattedDiff = ` +${timestampDiff}ms`;
234
+ return this.options.colors ? (0, cli_colors_util_1.yellow)(formattedDiff) : formattedDiff;
235
+ }
236
+ getInspectOptions() {
237
+ let breakLength = this.options.breakLength;
238
+ if (typeof breakLength === 'undefined') {
239
+ breakLength = this.options.colors
240
+ ? this.options.compact
241
+ ? Infinity
242
+ : undefined
243
+ : this.options.compact === false
244
+ ? undefined
245
+ : Infinity; // default breakLength to Infinity if inline is not set and colors is false
246
+ }
247
+ const inspectOptions = {
248
+ depth: this.options.depth ?? DEFAULT_DEPTH,
249
+ sorted: this.options.sorted,
250
+ showHidden: this.options.showHidden,
251
+ compact: this.options.compact ?? (this.options.json ? true : false),
252
+ colors: this.options.colors,
253
+ breakLength,
254
+ };
255
+ if (this.options.maxArrayLength) {
256
+ inspectOptions.maxArrayLength = this.options.maxArrayLength;
257
+ }
258
+ if (this.options.maxStringLength) {
259
+ inspectOptions.maxStringLength = this.options.maxStringLength;
260
+ }
261
+ return inspectOptions;
262
+ }
263
+ stringifyReplacer(key, value) {
264
+ // Mimic util.inspect behavior for JSON logger with compact on and colors off
265
+ if (typeof value === 'bigint') {
266
+ return value.toString();
267
+ }
268
+ if (typeof value === 'symbol') {
269
+ return value.toString();
270
+ }
271
+ if (value instanceof Map ||
272
+ value instanceof Set ||
273
+ value instanceof Error) {
274
+ return `${(0, util_1.inspect)(value, this.inspectOptions)}`;
275
+ }
276
+ return value;
182
277
  }
183
278
  getContextAndMessagesToPrint(args) {
184
279
  if (args?.length <= 1) {
@@ -251,5 +346,5 @@ exports.ConsoleLogger = ConsoleLogger = ConsoleLogger_1 = tslib_1.__decorate([
251
346
  (0, core_1.Injectable)(),
252
347
  tslib_1.__param(0, (0, core_1.Optional)()),
253
348
  tslib_1.__param(1, (0, core_1.Optional)()),
254
- tslib_1.__metadata("design:paramtypes", [String, Object])
349
+ tslib_1.__metadata("design:paramtypes", [Object, Object])
255
350
  ], ConsoleLogger);
@@ -50,7 +50,7 @@ interface LogBufferRecord {
50
50
  * @publicApi
51
51
  */
52
52
  export declare class Logger implements LoggerService {
53
- protected context?: string;
53
+ protected context?: string | undefined;
54
54
  protected options: {
55
55
  timestamp?: boolean;
56
56
  };
@@ -118,12 +118,12 @@ let Logger = Logger_1 = class Logger {
118
118
  static overrideLogger(logger) {
119
119
  if (Array.isArray(logger)) {
120
120
  Logger_1.logLevels = logger;
121
- return this.staticInstanceRef?.setLogLevels(logger);
121
+ return this.staticInstanceRef?.setLogLevels?.(logger);
122
122
  }
123
123
  if ((0, shared_utils_1.isObject)(logger)) {
124
124
  if (logger instanceof Logger_1 && logger.constructor !== Logger_1) {
125
125
  const errorMessage = `Using the "extends Logger" instruction is not allowed in Nest v9. Please, use "extends ConsoleLogger" instead.`;
126
- this.staticInstanceRef.error(errorMessage);
126
+ this.staticInstanceRef?.error(errorMessage);
127
127
  throw new Error(errorMessage);
128
128
  }
129
129
  this.staticInstanceRef = logger;
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.MergeWithValues = void 0;
4
+ /* eslint-disable @typescript-eslint/no-empty-object-type */
4
5
  const MergeWithValues = (data) => {
5
6
  return (Metatype) => {
6
7
  const Type = class extends Metatype {
@@ -1,7 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.isSymbol = exports.isEmpty = exports.isNil = exports.isConstructor = exports.isNumber = exports.isString = exports.isFunction = exports.stripEndSlash = exports.normalizePath = exports.addLeadingSlash = exports.isPlainObject = exports.isObject = exports.isUndefined = void 0;
4
- /* eslint-disable @typescript-eslint/no-use-before-define */
5
4
  const isUndefined = (obj) => typeof obj === 'undefined';
6
5
  exports.isUndefined = isUndefined;
7
6
  const isObject = (fn) => !(0, exports.isNil)(fn) && typeof fn === 'object';