@nestjs/common 8.4.7 → 9.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.
Files changed (78) hide show
  1. package/Readme.md +2 -0
  2. package/cache/cache.constants.d.ts +0 -1
  3. package/cache/cache.constants.js +1 -2
  4. package/cache/cache.module-definition.d.ts +2 -0
  5. package/cache/cache.module-definition.js +10 -0
  6. package/cache/cache.module.d.ts +2 -3
  7. package/cache/cache.module.js +6 -43
  8. package/cache/cache.providers.js +2 -1
  9. package/cache/interfaces/cache-module.interface.d.ts +6 -2
  10. package/decorators/core/controller.decorator.js +1 -1
  11. package/exceptions/http.exception.d.ts +9 -0
  12. package/exceptions/http.exception.js +13 -0
  13. package/index.d.ts +2 -2
  14. package/index.js +1 -1
  15. package/interfaces/http/http-server.interface.d.ts +3 -1
  16. package/interfaces/http/index.d.ts +1 -0
  17. package/interfaces/http/index.js +1 -0
  18. package/{http/interfaces → interfaces/http}/raw-body-request.interface.d.ts +0 -0
  19. package/{http/interfaces → interfaces/http}/raw-body-request.interface.js +0 -0
  20. package/interfaces/modules/provider.interface.d.ts +27 -1
  21. package/interfaces/nest-application-context.interface.d.ts +1 -1
  22. package/interfaces/nest-application.interface.d.ts +0 -16
  23. package/interfaces/nest-microservice.interface.d.ts +0 -7
  24. package/interfaces/scope-options.interface.d.ts +7 -0
  25. package/interfaces/version-options.interface.d.ts +1 -1
  26. package/module-utils/configurable-module.builder.d.ts +93 -0
  27. package/module-utils/configurable-module.builder.js +195 -0
  28. package/module-utils/constants.d.ts +4 -0
  29. package/module-utils/constants.js +7 -0
  30. package/module-utils/index.d.ts +2 -0
  31. package/{http → module-utils}/index.js +1 -2
  32. package/module-utils/interfaces/configurable-module-async-options.interface.d.ts +36 -0
  33. package/{http/interfaces/http-module.interface.js → module-utils/interfaces/configurable-module-async-options.interface.js} +0 -0
  34. package/module-utils/interfaces/configurable-module-cls.interface.d.ts +13 -0
  35. package/module-utils/interfaces/configurable-module-cls.interface.js +2 -0
  36. package/module-utils/interfaces/configurable-module-host.interface.d.ts +62 -0
  37. package/module-utils/interfaces/configurable-module-host.interface.js +2 -0
  38. package/module-utils/interfaces/index.d.ts +3 -0
  39. package/module-utils/interfaces/index.js +6 -0
  40. package/module-utils/utils/generate-options-injection-token.util.d.ts +1 -0
  41. package/module-utils/utils/generate-options-injection-token.util.js +9 -0
  42. package/package.json +1 -2
  43. package/pipes/file/file-type.validator.d.ts +19 -0
  44. package/pipes/file/file-type.validator.js +30 -0
  45. package/pipes/file/file-validator.interface.d.ts +17 -0
  46. package/pipes/file/file-validator.interface.js +12 -0
  47. package/pipes/file/index.d.ts +6 -0
  48. package/pipes/file/index.js +9 -0
  49. package/pipes/file/max-file-size.validator.d.ts +15 -0
  50. package/pipes/file/max-file-size.validator.js +23 -0
  51. package/pipes/file/parse-file-options.interface.d.ts +7 -0
  52. package/pipes/file/parse-file-options.interface.js +2 -0
  53. package/pipes/file/parse-file-pipe.builder.d.ts +10 -0
  54. package/pipes/file/parse-file-pipe.builder.js +25 -0
  55. package/pipes/file/parse-file.pipe.d.ts +25 -0
  56. package/pipes/file/parse-file.pipe.js +57 -0
  57. package/pipes/index.d.ts +1 -0
  58. package/pipes/index.js +1 -0
  59. package/pipes/parse-uuid.pipe.d.ts +7 -0
  60. package/pipes/parse-uuid.pipe.js +19 -5
  61. package/services/console-logger.service.js +2 -2
  62. package/services/logger.service.js +1 -1
  63. package/utils/cli-colors.util.d.ts +1 -0
  64. package/utils/cli-colors.util.js +1 -0
  65. package/utils/shared.utils.d.ts +0 -5
  66. package/utils/shared.utils.js +1 -6
  67. package/http/http.constants.d.ts +0 -3
  68. package/http/http.constants.js +0 -6
  69. package/http/http.module.d.ts +0 -11
  70. package/http/http.module.js +0 -88
  71. package/http/http.service.d.ts +0 -19
  72. package/http/http.service.js +0 -74
  73. package/http/index.d.ts +0 -3
  74. package/http/interfaces/http-module.interface.d.ts +0 -13
  75. package/http/interfaces/index.d.ts +0 -2
  76. package/http/interfaces/index.js +0 -5
  77. package/utils/is-uuid.d.ts +0 -1
  78. package/utils/is-uuid.js +0 -19
@@ -0,0 +1,195 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ConfigurableModuleBuilder = void 0;
4
+ const logger_service_1 = require("../services/logger.service");
5
+ const random_string_generator_util_1 = require("../utils/random-string-generator.util");
6
+ const constants_1 = require("./constants");
7
+ const generate_options_injection_token_util_1 = require("./utils/generate-options-injection-token.util");
8
+ /**
9
+ * Factory that lets you create configurable modules and
10
+ * provides a way to reduce the majority of dynamic module boilerplate.
11
+ *
12
+ * @publicApi
13
+ */
14
+ class ConfigurableModuleBuilder {
15
+ constructor(options = {}, parentBuilder) {
16
+ this.options = options;
17
+ this.logger = new logger_service_1.Logger(ConfigurableModuleBuilder.name);
18
+ if (parentBuilder) {
19
+ this.staticMethodKey = parentBuilder.staticMethodKey;
20
+ this.factoryClassMethodKey =
21
+ parentBuilder.factoryClassMethodKey;
22
+ this.transformModuleDefinition = parentBuilder.transformModuleDefinition;
23
+ this.extras = parentBuilder.extras;
24
+ }
25
+ }
26
+ /**
27
+ * Registers the "extras" object (a set of extra options that can be used to modify the dynamic module definition).
28
+ * Values you specify within the "extras" object will be used as default values (that can be overriden by module consumers).
29
+ *
30
+ * This method also applies the so-called "module definition transform function" that takes the auto-generated
31
+ * dynamic module object ("DynamicModule") and the actual consumer "extras" object as input parameters.
32
+ * The "extras" object consists of values explicitly specified by module consumers and default values.
33
+ *
34
+ * @example
35
+ * ```typescript
36
+ * .setExtras<{ isGlobal?: boolean }>({ isGlobal: false }, (definition, extras) =>
37
+ * ({ ...definition, global: extras.isGlobal })
38
+ * )
39
+ * ```
40
+ */
41
+ setExtras(extras, transformDefinition) {
42
+ const builder = new ConfigurableModuleBuilder(this.options, this);
43
+ builder.extras = extras;
44
+ builder.transformModuleDefinition = transformDefinition;
45
+ return builder;
46
+ }
47
+ /**
48
+ * Dynamic modules must expose public static methods that let you pass in
49
+ * configuration parameters (control the module's behavior from the outside).
50
+ * Some frequently used names that you may have seen in other modules are:
51
+ * "forRoot", "forFeature", "register", "configure".
52
+ *
53
+ * This method "setClassMethodName" lets you specify the name of the
54
+ * method that will be auto-generated.
55
+ *
56
+ * @param key name of the method
57
+ */
58
+ setClassMethodName(key) {
59
+ const builder = new ConfigurableModuleBuilder(this.options, this);
60
+ builder.staticMethodKey = key;
61
+ return builder;
62
+ }
63
+ /**
64
+ * Asynchronously configured modules (that rely on other modules, i.e. "ConfigModule")
65
+ * let you pass the configuration factory class that will be registered and instantiated as a provider.
66
+ * This provider then will be used to retrieve the module's configuration. To provide the configuration,
67
+ * the corresponding factory method must be implemented.
68
+ *
69
+ * This method ("setFactoryMethodName") lets you control what method name will have to be
70
+ * implemented by the config factory (default is "create").
71
+ *
72
+ * @param key name of the method
73
+ */
74
+ setFactoryMethodName(key) {
75
+ const builder = new ConfigurableModuleBuilder(this.options, this);
76
+ builder.factoryClassMethodKey = key;
77
+ return builder;
78
+ }
79
+ /**
80
+ * Returns an object consisting of multiple properties that lets you
81
+ * easily construct dynamic configurable modules. See "ConfigurableModuleHost" interface for more details.
82
+ */
83
+ build() {
84
+ var _a, _b, _c, _d;
85
+ var _e;
86
+ (_a = this.staticMethodKey) !== null && _a !== void 0 ? _a : (this.staticMethodKey = constants_1.DEFAULT_METHOD_KEY);
87
+ (_b = this.factoryClassMethodKey) !== null && _b !== void 0 ? _b : (this.factoryClassMethodKey = constants_1.DEFAULT_FACTORY_CLASS_METHOD_KEY);
88
+ (_c = (_e = this.options).optionsInjectionToken) !== null && _c !== void 0 ? _c : (_e.optionsInjectionToken = this.options.moduleName
89
+ ? this.constructInjectionTokenString()
90
+ : (0, generate_options_injection_token_util_1.generateOptionsInjectionToken)());
91
+ (_d = this.transformModuleDefinition) !== null && _d !== void 0 ? _d : (this.transformModuleDefinition = definition => definition);
92
+ return {
93
+ ConfigurableModuleClass: this.createConfigurableModuleCls(),
94
+ MODULE_OPTIONS_TOKEN: this.options.optionsInjectionToken,
95
+ ASYNC_OPTIONS_TYPE: this.createTypeProxy('ASYNC_OPTIONS_TYPE'),
96
+ OPTIONS_TYPE: this.createTypeProxy('OPTIONS_TYPE'),
97
+ };
98
+ }
99
+ constructInjectionTokenString() {
100
+ const moduleNameInSnakeCase = this.options.moduleName
101
+ .trim()
102
+ .split(/(?=[A-Z])/)
103
+ .join('_')
104
+ .toUpperCase();
105
+ return `${moduleNameInSnakeCase}_MODULE_OPTIONS`;
106
+ }
107
+ createConfigurableModuleCls() {
108
+ // eslint-disable-next-line @typescript-eslint/no-this-alias
109
+ const self = this;
110
+ const asyncMethodKey = this.staticMethodKey + constants_1.ASYNC_METHOD_SUFFIX;
111
+ class InternalModuleClass {
112
+ static [self.staticMethodKey](options) {
113
+ const providers = [
114
+ {
115
+ provide: self.options.optionsInjectionToken,
116
+ useValue: this.omitExtras(options, self.extras),
117
+ },
118
+ ];
119
+ if (self.options.alwaysTransient) {
120
+ providers.push({
121
+ provide: constants_1.CONFIGURABLE_MODULE_ID,
122
+ useValue: (0, random_string_generator_util_1.randomStringGenerator)(),
123
+ });
124
+ }
125
+ return self.transformModuleDefinition({
126
+ module: this,
127
+ providers,
128
+ }, options);
129
+ }
130
+ static [asyncMethodKey](options) {
131
+ const providers = this.createAsyncProviders(options);
132
+ if (self.options.alwaysTransient) {
133
+ providers.push({
134
+ provide: constants_1.CONFIGURABLE_MODULE_ID,
135
+ useValue: (0, random_string_generator_util_1.randomStringGenerator)(),
136
+ });
137
+ }
138
+ return self.transformModuleDefinition({
139
+ module: this,
140
+ imports: options.imports || [],
141
+ providers,
142
+ }, options);
143
+ }
144
+ static omitExtras(input, extras) {
145
+ if (!extras) {
146
+ return input;
147
+ }
148
+ const moduleOptions = {};
149
+ const extrasKeys = Object.keys(extras);
150
+ Object.keys(input)
151
+ .filter(key => !extrasKeys.includes(key))
152
+ .forEach(key => {
153
+ moduleOptions[key] = input[key];
154
+ });
155
+ return moduleOptions;
156
+ }
157
+ static createAsyncProviders(options) {
158
+ if (options.useExisting || options.useFactory) {
159
+ return [this.createAsyncOptionsProvider(options)];
160
+ }
161
+ return [
162
+ this.createAsyncOptionsProvider(options),
163
+ {
164
+ provide: options.useClass,
165
+ useClass: options.useClass,
166
+ },
167
+ ];
168
+ }
169
+ static createAsyncOptionsProvider(options) {
170
+ if (options.useFactory) {
171
+ return {
172
+ provide: self.options.optionsInjectionToken,
173
+ useFactory: options.useFactory,
174
+ inject: options.inject || [],
175
+ };
176
+ }
177
+ return {
178
+ provide: self.options.optionsInjectionToken,
179
+ useFactory: async (optionsFactory) => await optionsFactory[self.factoryClassMethodKey](),
180
+ inject: [options.useExisting || options.useClass],
181
+ };
182
+ }
183
+ }
184
+ return InternalModuleClass;
185
+ }
186
+ createTypeProxy(typeName) {
187
+ const proxy = new Proxy({}, {
188
+ get: () => {
189
+ throw new Error(`"${typeName}" is not supposed to be used as a value.`);
190
+ },
191
+ });
192
+ return proxy;
193
+ }
194
+ }
195
+ exports.ConfigurableModuleBuilder = ConfigurableModuleBuilder;
@@ -0,0 +1,4 @@
1
+ export declare const DEFAULT_METHOD_KEY = "register";
2
+ export declare const DEFAULT_FACTORY_CLASS_METHOD_KEY = "create";
3
+ export declare const ASYNC_METHOD_SUFFIX = "Async";
4
+ export declare const CONFIGURABLE_MODULE_ID = "CONFIGURABLE_MODULE_ID";
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CONFIGURABLE_MODULE_ID = exports.ASYNC_METHOD_SUFFIX = exports.DEFAULT_FACTORY_CLASS_METHOD_KEY = exports.DEFAULT_METHOD_KEY = void 0;
4
+ exports.DEFAULT_METHOD_KEY = 'register';
5
+ exports.DEFAULT_FACTORY_CLASS_METHOD_KEY = 'create';
6
+ exports.ASYNC_METHOD_SUFFIX = 'Async';
7
+ exports.CONFIGURABLE_MODULE_ID = 'CONFIGURABLE_MODULE_ID';
@@ -0,0 +1,2 @@
1
+ export * from './configurable-module.builder';
2
+ export * from './interfaces';
@@ -1,6 +1,5 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const tslib_1 = require("tslib");
4
- tslib_1.__exportStar(require("./http.module"), exports);
5
- tslib_1.__exportStar(require("./http.service"), exports);
4
+ tslib_1.__exportStar(require("./configurable-module.builder"), exports);
6
5
  tslib_1.__exportStar(require("./interfaces"), exports);
@@ -0,0 +1,36 @@
1
+ import { FactoryProvider, ModuleMetadata, Type } from '../../interfaces';
2
+ import { DEFAULT_FACTORY_CLASS_METHOD_KEY } from '../constants';
3
+ /**
4
+ * Interface that must be implemented by the module options factory class.
5
+ * Method key varies depending on the "FactoryClassMethodKey" type argument.
6
+ *
7
+ * @publicApi
8
+ */
9
+ export declare type ConfigurableModuleOptionsFactory<ModuleOptions, FactoryClassMethodKey extends string> = Record<`${FactoryClassMethodKey}`, () => Promise<ModuleOptions> | ModuleOptions>;
10
+ /**
11
+ * Interface that represents the module async options object
12
+ * Factory method name varies depending on the "FactoryClassMethodKey" type argument.
13
+ *
14
+ * @publicApi
15
+ */
16
+ export interface ConfigurableModuleAsyncOptions<ModuleOptions, FactoryClassMethodKey extends string = typeof DEFAULT_FACTORY_CLASS_METHOD_KEY> extends Pick<ModuleMetadata, 'imports'> {
17
+ /**
18
+ * Injection token resolving to an existing provider. The provider must implement
19
+ * the corresponding interface.
20
+ */
21
+ useExisting?: Type<ConfigurableModuleOptionsFactory<ModuleOptions, FactoryClassMethodKey>>;
22
+ /**
23
+ * Injection token resolving to a class that will be instantiated as a provider.
24
+ * The class must implement the corresponding interface.
25
+ */
26
+ useClass?: Type<ConfigurableModuleOptionsFactory<ModuleOptions, FactoryClassMethodKey>>;
27
+ /**
28
+ * Function returning options (or a Promise resolving to options) to configure the
29
+ * cache module.
30
+ */
31
+ useFactory?: (...args: unknown[]) => Promise<ModuleOptions> | ModuleOptions;
32
+ /**
33
+ * Dependencies that a Factory may inject.
34
+ */
35
+ inject?: FactoryProvider['inject'];
36
+ }
@@ -0,0 +1,13 @@
1
+ import { DynamicModule } from '../../interfaces';
2
+ import { DEFAULT_FACTORY_CLASS_METHOD_KEY, DEFAULT_METHOD_KEY } from '../constants';
3
+ import { ConfigurableModuleAsyncOptions } from './configurable-module-async-options.interface';
4
+ /**
5
+ * Class that represents a blueprint/prototype for a configurable Nest module.
6
+ * This class provides static methods for constructing dynamic modules. Their names
7
+ * can be controlled through the "MethodKey" type argument.
8
+ *
9
+ * @publicApi
10
+ */
11
+ export declare type ConfigurableModuleCls<ModuleOptions, MethodKey extends string = typeof DEFAULT_METHOD_KEY, FactoryClassMethodKey extends string = typeof DEFAULT_FACTORY_CLASS_METHOD_KEY, ExtraModuleDefinitionOptions = {}> = {
12
+ new (): any;
13
+ } & Record<`${MethodKey}`, (options: ModuleOptions & ExtraModuleDefinitionOptions) => DynamicModule> & Record<`${MethodKey}Async`, (options: ConfigurableModuleAsyncOptions<ModuleOptions, FactoryClassMethodKey> & ExtraModuleDefinitionOptions) => DynamicModule>;
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,62 @@
1
+ import { ConfigurableModuleAsyncOptions } from './configurable-module-async-options.interface';
2
+ import { ConfigurableModuleCls } from './configurable-module-cls.interface';
3
+ /**
4
+ * Configurable module host. See properties for more details
5
+ *
6
+ * @publicApi
7
+ */
8
+ export interface ConfigurableModuleHost<ModuleOptions = Record<string, unknown>, MethodKey extends string = string, FactoryClassMethodKey extends string = string, ExtraModuleDefinitionOptions = {}> {
9
+ /**
10
+ * Class that represents a blueprint/prototype for a configurable Nest module.
11
+ * This class provides static methods for constructing dynamic modules. Their names
12
+ * can be controlled through the "MethodKey" type argument.
13
+ *
14
+ * Your module class should inherit from this class to make the static methods available.
15
+ *
16
+ * @example
17
+ * ```typescript
18
+ * @Module({})
19
+ * class IntegrationModule extends ConfigurableModuleCls {
20
+ * // ...
21
+ * }
22
+ * ```
23
+ */
24
+ ConfigurableModuleClass: ConfigurableModuleCls<ModuleOptions, MethodKey, FactoryClassMethodKey, ExtraModuleDefinitionOptions>;
25
+ /**
26
+ * Module options provider token. Can be used to inject the "options object" to
27
+ * providers registered within the host module.
28
+ */
29
+ MODULE_OPTIONS_TOKEN: string | symbol;
30
+ /**
31
+ * Can be used to auto-infer the compound "async module options" type.
32
+ * Note: this property is not supposed to be used as a value.
33
+ *
34
+ * @example
35
+ * ```typescript
36
+ * @Module({})
37
+ * class IntegrationModule extends ConfigurableModuleCls {
38
+ * static module = initializer(IntegrationModule);
39
+ *
40
+ * static registerAsync(options: typeof ASYNC_OPTIONS_TYPE): DynamicModule {
41
+ * return super.registerAsync(options);
42
+ * }
43
+ * ```
44
+ */
45
+ ASYNC_OPTIONS_TYPE: ConfigurableModuleAsyncOptions<ModuleOptions, FactoryClassMethodKey> & ExtraModuleDefinitionOptions;
46
+ /**
47
+ * Can be used to auto-infer the compound "module options" type (options interface + extra module definition options).
48
+ * Note: this property is not supposed to be used as a value.
49
+ *
50
+ * @example
51
+ * ```typescript
52
+ * @Module({})
53
+ * class IntegrationModule extends ConfigurableModuleCls {
54
+ * static module = initializer(IntegrationModule);
55
+ *
56
+ * static register(options: typeof OPTIONS_TYPE): DynamicModule {
57
+ * return super.register(options);
58
+ * }
59
+ * ```
60
+ */
61
+ OPTIONS_TYPE: ModuleOptions & ExtraModuleDefinitionOptions;
62
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,3 @@
1
+ export * from './configurable-module-async-options.interface';
2
+ export * from './configurable-module-cls.interface';
3
+ export * from './configurable-module-host.interface';
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const tslib_1 = require("tslib");
4
+ tslib_1.__exportStar(require("./configurable-module-async-options.interface"), exports);
5
+ tslib_1.__exportStar(require("./configurable-module-cls.interface"), exports);
6
+ tslib_1.__exportStar(require("./configurable-module-host.interface"), exports);
@@ -0,0 +1 @@
1
+ export declare function generateOptionsInjectionToken(): string;
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.generateOptionsInjectionToken = void 0;
4
+ const random_string_generator_util_1 = require("../../utils/random-string-generator.util");
5
+ function generateOptionsInjectionToken() {
6
+ const hash = (0, random_string_generator_util_1.randomStringGenerator)();
7
+ return `CONFIGURABLE_MODULE_OPTIONS[${hash}]`;
8
+ }
9
+ exports.generateOptionsInjectionToken = generateOptionsInjectionToken;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nestjs/common",
3
- "version": "8.4.7",
3
+ "version": "9.0.0",
4
4
  "description": "Nest - modern, fast, powerful node.js web framework (@common)",
5
5
  "author": "Kamil Mysliwiec",
6
6
  "homepage": "https://nestjs.com",
@@ -17,7 +17,6 @@
17
17
  },
18
18
  "license": "MIT",
19
19
  "dependencies": {
20
- "axios": "0.27.2",
21
20
  "iterare": "1.2.1",
22
21
  "tslib": "2.4.0",
23
22
  "uuid": "8.3.2"
@@ -0,0 +1,19 @@
1
+ import { FileValidator } from './file-validator.interface';
2
+ export declare type FileTypeValidatorOptions = {
3
+ fileType: string | RegExp;
4
+ };
5
+ /**
6
+ * Defines the built-in FileType File Validator. It validates incoming files mime-type
7
+ * matching a string or a regular expression. Note that this validator uses a naive strategy
8
+ * to check the mime-type and could be fooled if the client provided a file with renamed extension.
9
+ * (for instance, renaming a 'malicious.bat' to 'malicious.jpeg'). To handle such security issues
10
+ * with more reliability, consider checking against the file's [magic-numbers](https://en.wikipedia.org/wiki/Magic_number_%28programming%29)
11
+ *
12
+ * @see [File Validators](https://docs.nestjs.com/techniques/file-upload#validators)
13
+ *
14
+ * @publicApi
15
+ */
16
+ export declare class FileTypeValidator extends FileValidator<FileTypeValidatorOptions> {
17
+ buildErrorMessage(): string;
18
+ isValid(file: any): boolean;
19
+ }
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.FileTypeValidator = void 0;
4
+ const file_validator_interface_1 = require("./file-validator.interface");
5
+ /**
6
+ * Defines the built-in FileType File Validator. It validates incoming files mime-type
7
+ * matching a string or a regular expression. Note that this validator uses a naive strategy
8
+ * to check the mime-type and could be fooled if the client provided a file with renamed extension.
9
+ * (for instance, renaming a 'malicious.bat' to 'malicious.jpeg'). To handle such security issues
10
+ * with more reliability, consider checking against the file's [magic-numbers](https://en.wikipedia.org/wiki/Magic_number_%28programming%29)
11
+ *
12
+ * @see [File Validators](https://docs.nestjs.com/techniques/file-upload#validators)
13
+ *
14
+ * @publicApi
15
+ */
16
+ class FileTypeValidator extends file_validator_interface_1.FileValidator {
17
+ buildErrorMessage() {
18
+ return `Validation failed (expected type is ${this.validationOptions.fileType})`;
19
+ }
20
+ isValid(file) {
21
+ if (!this.validationOptions) {
22
+ return true;
23
+ }
24
+ if (!file.mimetype) {
25
+ return false;
26
+ }
27
+ return Boolean(file.mimetype.match(this.validationOptions.fileType));
28
+ }
29
+ }
30
+ exports.FileTypeValidator = FileTypeValidator;
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Interface describing FileValidators, which can be added to a {@link ParseFilePipe}.
3
+ */
4
+ export declare abstract class FileValidator<TValidationOptions = Record<string, any>> {
5
+ protected readonly validationOptions: TValidationOptions;
6
+ constructor(validationOptions: TValidationOptions);
7
+ /**
8
+ * Indicates if this file should be considered valid, according to the options passed in the constructor.
9
+ * @param file the file from the request object
10
+ */
11
+ abstract isValid(file?: any): boolean | Promise<boolean>;
12
+ /**
13
+ * Builds an error message in case the validation fails.
14
+ * @param file the file from the request object
15
+ */
16
+ abstract buildErrorMessage(file: any): string;
17
+ }
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.FileValidator = void 0;
4
+ /**
5
+ * Interface describing FileValidators, which can be added to a {@link ParseFilePipe}.
6
+ */
7
+ class FileValidator {
8
+ constructor(validationOptions) {
9
+ this.validationOptions = validationOptions;
10
+ }
11
+ }
12
+ exports.FileValidator = FileValidator;
@@ -0,0 +1,6 @@
1
+ export * from './file-type.validator';
2
+ export * from './file-validator.interface';
3
+ export * from './max-file-size.validator';
4
+ export * from './parse-file-options.interface';
5
+ export * from './parse-file.pipe';
6
+ export * from './parse-file-pipe.builder';
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const tslib_1 = require("tslib");
4
+ tslib_1.__exportStar(require("./file-type.validator"), exports);
5
+ tslib_1.__exportStar(require("./file-validator.interface"), exports);
6
+ tslib_1.__exportStar(require("./max-file-size.validator"), exports);
7
+ tslib_1.__exportStar(require("./parse-file-options.interface"), exports);
8
+ tslib_1.__exportStar(require("./parse-file.pipe"), exports);
9
+ tslib_1.__exportStar(require("./parse-file-pipe.builder"), exports);
@@ -0,0 +1,15 @@
1
+ import { FileValidator } from './file-validator.interface';
2
+ export declare type MaxFileSizeValidatorOptions = {
3
+ maxSize: number;
4
+ };
5
+ /**
6
+ * Defines the built-in MaxSize File Validator
7
+ *
8
+ * @see [File Validators](https://docs.nestjs.com/techniques/file-upload#validators)
9
+ *
10
+ * @publicApi
11
+ */
12
+ export declare class MaxFileSizeValidator extends FileValidator<MaxFileSizeValidatorOptions> {
13
+ buildErrorMessage(): string;
14
+ isValid(file: any): boolean;
15
+ }
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MaxFileSizeValidator = void 0;
4
+ const file_validator_interface_1 = require("./file-validator.interface");
5
+ /**
6
+ * Defines the built-in MaxSize File Validator
7
+ *
8
+ * @see [File Validators](https://docs.nestjs.com/techniques/file-upload#validators)
9
+ *
10
+ * @publicApi
11
+ */
12
+ class MaxFileSizeValidator extends file_validator_interface_1.FileValidator {
13
+ buildErrorMessage() {
14
+ return `Validation failed (expected size is less than ${this.validationOptions.maxSize})`;
15
+ }
16
+ isValid(file) {
17
+ if (!this.validationOptions) {
18
+ return true;
19
+ }
20
+ return file.size < this.validationOptions.maxSize;
21
+ }
22
+ }
23
+ exports.MaxFileSizeValidator = MaxFileSizeValidator;
@@ -0,0 +1,7 @@
1
+ import { ErrorHttpStatusCode } from '../../utils/http-error-by-code.util';
2
+ import { FileValidator } from './file-validator.interface';
3
+ export interface ParseFileOptions {
4
+ validators?: FileValidator[];
5
+ errorHttpStatusCode?: ErrorHttpStatusCode;
6
+ exceptionFactory?: (error: string) => any;
7
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,10 @@
1
+ import { FileTypeValidatorOptions } from './file-type.validator';
2
+ import { MaxFileSizeValidatorOptions } from './max-file-size.validator';
3
+ import { ParseFileOptions } from './parse-file-options.interface';
4
+ import { ParseFilePipe } from './parse-file.pipe';
5
+ export declare class ParseFilePipeBuilder {
6
+ private validators;
7
+ addMaxSizeValidator(options: MaxFileSizeValidatorOptions): this;
8
+ addFileTypeValidator(options: FileTypeValidatorOptions): this;
9
+ build(additionalOptions?: Omit<ParseFileOptions, 'validators'>): ParseFilePipe;
10
+ }
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ParseFilePipeBuilder = void 0;
4
+ const file_type_validator_1 = require("./file-type.validator");
5
+ const max_file_size_validator_1 = require("./max-file-size.validator");
6
+ const parse_file_pipe_1 = require("./parse-file.pipe");
7
+ class ParseFilePipeBuilder {
8
+ constructor() {
9
+ this.validators = [];
10
+ }
11
+ addMaxSizeValidator(options) {
12
+ this.validators.push(new max_file_size_validator_1.MaxFileSizeValidator(options));
13
+ return this;
14
+ }
15
+ addFileTypeValidator(options) {
16
+ this.validators.push(new file_type_validator_1.FileTypeValidator(options));
17
+ return this;
18
+ }
19
+ build(additionalOptions) {
20
+ const parseFilePipe = new parse_file_pipe_1.ParseFilePipe(Object.assign(Object.assign({}, additionalOptions), { validators: this.validators }));
21
+ this.validators = [];
22
+ return parseFilePipe;
23
+ }
24
+ }
25
+ exports.ParseFilePipeBuilder = ParseFilePipeBuilder;
@@ -0,0 +1,25 @@
1
+ import { PipeTransform } from '../../interfaces/features/pipe-transform.interface';
2
+ import { ParseFileOptions } from './parse-file-options.interface';
3
+ import { FileValidator } from './file-validator.interface';
4
+ /**
5
+ * Defines the built-in ParseFile Pipe. This pipe can be used to validate incoming files
6
+ * with `@UploadedFile()` decorator. You can use either other specific built-in validators
7
+ * or provide one of your own, simply implementing it through {@link FileValidator}
8
+ * interface and adding it to ParseFilePipe's constructor.
9
+ *
10
+ * @see [Built-in Pipes](https://docs.nestjs.com/pipes#built-in-pipes)
11
+ *
12
+ * @publicApi
13
+ */
14
+ export declare class ParseFilePipe implements PipeTransform<any> {
15
+ protected exceptionFactory: (error: string) => any;
16
+ private readonly validators;
17
+ constructor(options?: ParseFileOptions);
18
+ transform(value: any): Promise<any>;
19
+ protected validate(file: any): Promise<any>;
20
+ private validateOrThrow;
21
+ /**
22
+ * @returns list of validators used in this pipe.
23
+ */
24
+ getValidators(): FileValidator<Record<string, any>>[];
25
+ }