@nestjs/common 9.0.3 → 9.0.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.
package/constants.d.ts CHANGED
@@ -15,6 +15,10 @@ export declare const OPTIONAL_PROPERTY_DEPS_METADATA = "optional:properties_meta
15
15
  export declare const SCOPE_OPTIONS_METADATA = "scope:options";
16
16
  export declare const METHOD_METADATA = "method";
17
17
  export declare const ROUTE_ARGS_METADATA = "__routeArguments__";
18
+ export declare const CUSTOM_ROUTE_ARGS_METADATA = "__customRouteArgs__";
19
+ /**
20
+ * @deprecated Use `CUSTOM_ROUTE_ARGS_METADATA` instead
21
+ */
18
22
  export declare const CUSTOM_ROUTE_AGRS_METADATA = "__customRouteArgs__";
19
23
  export declare const EXCEPTION_FILTERS_METADATA = "__exceptionFilters__";
20
24
  export declare const FILTER_CATCH_EXCEPTIONS = "__filterCatchExceptions__";
package/constants.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.CATCH_WATERMARK = exports.CONTROLLER_WATERMARK = exports.INJECTABLE_WATERMARK = exports.VERSION_METADATA = exports.SSE_METADATA = exports.RESPONSE_PASSTHROUGH_METADATA = exports.REDIRECT_METADATA = exports.HEADERS_METADATA = exports.MODULE_PATH = exports.HTTP_CODE_METADATA = exports.INTERCEPTORS_METADATA = exports.RENDER_METADATA = exports.GUARDS_METADATA = exports.PIPES_METADATA = exports.FILTER_CATCH_EXCEPTIONS = exports.EXCEPTION_FILTERS_METADATA = exports.CUSTOM_ROUTE_AGRS_METADATA = exports.ROUTE_ARGS_METADATA = exports.METHOD_METADATA = exports.SCOPE_OPTIONS_METADATA = exports.OPTIONAL_PROPERTY_DEPS_METADATA = exports.PROPERTY_DEPS_METADATA = exports.OPTIONAL_DEPS_METADATA = exports.SELF_DECLARED_DEPS_METADATA = exports.PARAMTYPES_METADATA = exports.PATH_METADATA = exports.HOST_METADATA = exports.GLOBAL_MODULE_METADATA = exports.MODULE_METADATA = void 0;
3
+ exports.CATCH_WATERMARK = exports.CONTROLLER_WATERMARK = exports.INJECTABLE_WATERMARK = exports.VERSION_METADATA = exports.SSE_METADATA = exports.RESPONSE_PASSTHROUGH_METADATA = exports.REDIRECT_METADATA = exports.HEADERS_METADATA = exports.MODULE_PATH = exports.HTTP_CODE_METADATA = exports.INTERCEPTORS_METADATA = exports.RENDER_METADATA = exports.GUARDS_METADATA = exports.PIPES_METADATA = exports.FILTER_CATCH_EXCEPTIONS = exports.EXCEPTION_FILTERS_METADATA = exports.CUSTOM_ROUTE_AGRS_METADATA = exports.CUSTOM_ROUTE_ARGS_METADATA = exports.ROUTE_ARGS_METADATA = exports.METHOD_METADATA = exports.SCOPE_OPTIONS_METADATA = exports.OPTIONAL_PROPERTY_DEPS_METADATA = exports.PROPERTY_DEPS_METADATA = exports.OPTIONAL_DEPS_METADATA = exports.SELF_DECLARED_DEPS_METADATA = exports.PARAMTYPES_METADATA = exports.PATH_METADATA = exports.HOST_METADATA = exports.GLOBAL_MODULE_METADATA = exports.MODULE_METADATA = void 0;
4
4
  exports.MODULE_METADATA = {
5
5
  IMPORTS: 'imports',
6
6
  PROVIDERS: 'providers',
@@ -18,7 +18,11 @@ exports.OPTIONAL_PROPERTY_DEPS_METADATA = 'optional:properties_metadata';
18
18
  exports.SCOPE_OPTIONS_METADATA = 'scope:options';
19
19
  exports.METHOD_METADATA = 'method';
20
20
  exports.ROUTE_ARGS_METADATA = '__routeArguments__';
21
- exports.CUSTOM_ROUTE_AGRS_METADATA = '__customRouteArgs__';
21
+ exports.CUSTOM_ROUTE_ARGS_METADATA = '__customRouteArgs__';
22
+ /**
23
+ * @deprecated Use `CUSTOM_ROUTE_ARGS_METADATA` instead
24
+ */
25
+ exports.CUSTOM_ROUTE_AGRS_METADATA = exports.CUSTOM_ROUTE_ARGS_METADATA;
22
26
  exports.EXCEPTION_FILTERS_METADATA = '__exceptionFilters__';
23
27
  exports.FILTER_CATCH_EXCEPTIONS = '__filterCatchExceptions__';
24
28
  exports.PIPES_METADATA = '__pipes__';
@@ -1,9 +1,14 @@
1
1
  /// <reference types="node" />
2
2
  import { Readable } from 'stream';
3
3
  import { StreamableFileOptions } from './streamable-options.interface';
4
+ export interface StreamableHandlerResponse {
5
+ statusCode: number;
6
+ send: (msg: string) => void;
7
+ }
4
8
  export declare class StreamableFile {
5
9
  readonly options: StreamableFileOptions;
6
10
  private readonly stream;
11
+ protected handleError: (err: Error, response: StreamableHandlerResponse) => void;
7
12
  constructor(buffer: Uint8Array, options?: StreamableFileOptions);
8
13
  constructor(readable: Readable, options?: StreamableFileOptions);
9
14
  getStream(): Readable;
@@ -12,4 +17,6 @@ export declare class StreamableFile {
12
17
  disposition: string;
13
18
  length: number;
14
19
  };
20
+ get errorHandler(): (err: Error, response: StreamableHandlerResponse) => void;
21
+ setErrorHandler(handler: (err: Error, response: StreamableHandlerResponse) => void): this;
15
22
  }
@@ -9,6 +9,10 @@ class StreamableFile {
9
9
  var _a;
10
10
  var _b;
11
11
  this.options = options;
12
+ this.handleError = (err, res) => {
13
+ res.statusCode = 400;
14
+ res.send(err.message);
15
+ };
12
16
  if (util_1.types.isUint8Array(bufferOrReadStream)) {
13
17
  this.stream = new stream_1.Readable();
14
18
  this.stream.push(bufferOrReadStream);
@@ -30,5 +34,12 @@ class StreamableFile {
30
34
  length,
31
35
  };
32
36
  }
37
+ get errorHandler() {
38
+ return this.handleError;
39
+ }
40
+ setErrorHandler(handler) {
41
+ this.handleError = handler;
42
+ return this;
43
+ }
33
44
  }
34
45
  exports.StreamableFile = StreamableFile;
@@ -5,6 +5,7 @@ const logger_service_1 = require("../services/logger.service");
5
5
  const random_string_generator_util_1 = require("../utils/random-string-generator.util");
6
6
  const constants_1 = require("./constants");
7
7
  const generate_options_injection_token_util_1 = require("./utils/generate-options-injection-token.util");
8
+ const get_injection_providers_util_1 = require("./utils/get-injection-providers.util");
8
9
  /**
9
10
  * Factory that lets you create configurable modules and
10
11
  * provides a way to reduce the majority of dynamic module boilerplate.
@@ -156,6 +157,12 @@ class ConfigurableModuleBuilder {
156
157
  }
157
158
  static createAsyncProviders(options) {
158
159
  if (options.useExisting || options.useFactory) {
160
+ if (options.inject && options.provideInjectionTokensFrom) {
161
+ return [
162
+ this.createAsyncOptionsProvider(options),
163
+ ...(0, get_injection_providers_util_1.getInjectionProviders)(options.provideInjectionTokensFrom, options.inject),
164
+ ];
165
+ }
159
166
  return [this.createAsyncOptionsProvider(options)];
160
167
  }
161
168
  return [
@@ -1,4 +1,4 @@
1
- import { FactoryProvider, ModuleMetadata, Type } from '../../interfaces';
1
+ import { FactoryProvider, ModuleMetadata, Provider, Type } from '../../interfaces';
2
2
  import { DEFAULT_FACTORY_CLASS_METHOD_KEY } from '../constants';
3
3
  /**
4
4
  * Interface that must be implemented by the module options factory class.
@@ -28,9 +28,15 @@ export interface ConfigurableModuleAsyncOptions<ModuleOptions, FactoryClassMetho
28
28
  * Function returning options (or a Promise resolving to options) to configure the
29
29
  * cache module.
30
30
  */
31
- useFactory?: (...args: unknown[]) => Promise<ModuleOptions> | ModuleOptions;
31
+ useFactory?: (...args: any[]) => Promise<ModuleOptions> | ModuleOptions;
32
32
  /**
33
33
  * Dependencies that a Factory may inject.
34
34
  */
35
35
  inject?: FactoryProvider['inject'];
36
+ /**
37
+ * List of parent module's providers that will be filtered to only provide necessary
38
+ * providers for the 'inject' array
39
+ * useful to pass options to nested async modules
40
+ */
41
+ provideInjectionTokensFrom?: Provider[];
36
42
  }
@@ -0,0 +1,8 @@
1
+ import { Provider, FactoryProvider } from '../../interfaces';
2
+ /**
3
+ *
4
+ * @param providers List of a module's providers
5
+ * @param tokens Injection tokens needed for a useFactory function (usually the module's options' token)
6
+ * @returns All the providers needed for the tokens' injection (searched recursively)
7
+ */
8
+ export declare function getInjectionProviders(providers: Provider[], tokens: FactoryProvider['inject']): Provider[];
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getInjectionProviders = void 0;
4
+ /**
5
+ * check if x is OptionalFactoryDependency, based on prototype presence
6
+ * (to avoid classes with a static 'token' field)
7
+ * @param x
8
+ * @returns x is OptionalFactoryDependency
9
+ */
10
+ function isOptionalFactoryDependency(x) {
11
+ return !!((x === null || x === void 0 ? void 0 : x.token) && !(x === null || x === void 0 ? void 0 : x.prototype));
12
+ }
13
+ const mapInjectToTokens = (t) => isOptionalFactoryDependency(t) ? t.token : t;
14
+ /**
15
+ *
16
+ * @param providers List of a module's providers
17
+ * @param tokens Injection tokens needed for a useFactory function (usually the module's options' token)
18
+ * @returns All the providers needed for the tokens' injection (searched recursively)
19
+ */
20
+ function getInjectionProviders(providers, tokens) {
21
+ const result = [];
22
+ let search = tokens.map(mapInjectToTokens);
23
+ while (search.length > 0) {
24
+ const match = (providers !== null && providers !== void 0 ? providers : []).filter(p => !result.includes(p) && // this prevents circular loops and duplication
25
+ (search.includes(p) || search.includes(p === null || p === void 0 ? void 0 : p.provide)));
26
+ result.push(...match);
27
+ // get injection tokens of the matched providers, if any
28
+ search = match
29
+ .filter(p => p === null || p === void 0 ? void 0 : p.inject)
30
+ .map(p => p.inject)
31
+ .flat()
32
+ .map(mapInjectToTokens);
33
+ }
34
+ return result;
35
+ }
36
+ exports.getInjectionProviders = getInjectionProviders;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nestjs/common",
3
- "version": "9.0.3",
3
+ "version": "9.0.6",
4
4
  "description": "Nest - modern, fast, powerful node.js web framework (@common)",
5
5
  "author": "Kamil Mysliwiec",
6
6
  "homepage": "https://nestjs.com",
@@ -4,4 +4,9 @@ export interface ParseFileOptions {
4
4
  validators?: FileValidator[];
5
5
  errorHttpStatusCode?: ErrorHttpStatusCode;
6
6
  exceptionFactory?: (error: string) => any;
7
+ /**
8
+ * Defines if file parameter is required.
9
+ * @default true
10
+ */
11
+ fileIsRequired?: boolean;
7
12
  }
@@ -1,6 +1,6 @@
1
1
  import { PipeTransform } from '../../interfaces/features/pipe-transform.interface';
2
- import { ParseFileOptions } from './parse-file-options.interface';
3
2
  import { FileValidator } from './file-validator.interface';
3
+ import { ParseFileOptions } from './parse-file-options.interface';
4
4
  /**
5
5
  * Defines the built-in ParseFile Pipe. This pipe can be used to validate incoming files
6
6
  * with `@UploadedFile()` decorator. You can use either other specific built-in validators
@@ -14,6 +14,7 @@ import { FileValidator } from './file-validator.interface';
14
14
  export declare class ParseFilePipe implements PipeTransform<any> {
15
15
  protected exceptionFactory: (error: string) => any;
16
16
  private readonly validators;
17
+ private readonly fileIsRequired;
17
18
  constructor(options?: ParseFileOptions);
18
19
  transform(value: any): Promise<any>;
19
20
  protected validate(file: any): Promise<any>;
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ParseFilePipe = void 0;
4
4
  const tslib_1 = require("tslib");
5
+ const shared_utils_1 = require("../../utils/shared.utils");
5
6
  const core_1 = require("../../decorators/core");
6
7
  const enums_1 = require("../../enums");
7
8
  const http_error_by_code_util_1 = require("../../utils/http-error-by-code.util");
@@ -17,13 +18,20 @@ const http_error_by_code_util_1 = require("../../utils/http-error-by-code.util")
17
18
  */
18
19
  let ParseFilePipe = class ParseFilePipe {
19
20
  constructor(options = {}) {
20
- const { exceptionFactory, errorHttpStatusCode = enums_1.HttpStatus.BAD_REQUEST, validators = [], } = options;
21
+ const { exceptionFactory, errorHttpStatusCode = enums_1.HttpStatus.BAD_REQUEST, validators = [], fileIsRequired, } = options;
21
22
  this.exceptionFactory =
22
23
  exceptionFactory ||
23
24
  (error => new http_error_by_code_util_1.HttpErrorByCode[errorHttpStatusCode](error));
24
25
  this.validators = validators;
26
+ this.fileIsRequired = fileIsRequired !== null && fileIsRequired !== void 0 ? fileIsRequired : true;
25
27
  }
26
28
  async transform(value) {
29
+ if ((0, shared_utils_1.isUndefined)(value)) {
30
+ if (this.fileIsRequired) {
31
+ throw this.exceptionFactory('File is required');
32
+ }
33
+ return value;
34
+ }
27
35
  if (this.validators.length) {
28
36
  await this.validate(value);
29
37
  }
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.assignCustomParameterMetadata = void 0;
4
4
  const constants_1 = require("../constants");
5
5
  function assignCustomParameterMetadata(args, paramtype, index, factory, data, ...pipes) {
6
- return Object.assign(Object.assign({}, args), { [`${paramtype}${constants_1.CUSTOM_ROUTE_AGRS_METADATA}:${index}`]: {
6
+ return Object.assign(Object.assign({}, args), { [`${paramtype}${constants_1.CUSTOM_ROUTE_ARGS_METADATA}:${index}`]: {
7
7
  index,
8
8
  factory,
9
9
  data,