nestjs-form-data
Advanced tools
Comparing version 1.3.1 to 1.4.0
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.GLOBAL_CONFIG_INJECT_TOKEN = void 0; | ||
exports.GLOBAL_CONFIG_INJECT_TOKEN = Symbol(); | ||
exports.GLOBAL_CONFIG_INJECT_TOKEN = Symbol(`Inject token for NestJsFormData global configuration object`); | ||
//# sourceMappingURL=global-config-inject-token.config.js.map |
@@ -12,5 +12,5 @@ "use strict"; | ||
config.fileSystemStoragePath = defaults.fileSystemStoragePath; | ||
return Object.assign(config, defaults); | ||
return Object.assign({}, defaults, config); | ||
} | ||
exports.checkConfig = checkConfig; | ||
//# sourceMappingURL=check-config.js.map |
@@ -24,2 +24,3 @@ "use strict"; | ||
const check_config_1 = require("../helpers/check-config"); | ||
const type_is_1 = require("type-is"); | ||
let FormDataInterceptor = class FormDataInterceptor { | ||
@@ -33,2 +34,6 @@ constructor(globalConfig, reflector) { | ||
const res = context.switchToHttp().getResponse(); | ||
/** if the request is not multipart, skip **/ | ||
if (!type_is_1.is(req, ['multipart'])) | ||
return next.handle(); | ||
/** merge global config with method level config **/ | ||
const config = check_config_1.checkConfig(this.reflector.get(form_data_1.FORM_DATA_REQUEST_METADATA_KEY, context.getHandler()) || {}, this.globalConfig); | ||
@@ -35,0 +40,0 @@ const formReader = new FormReader_1.FormReader(req, config); |
import { DynamicModule } from '@nestjs/common'; | ||
import { FormDataInterceptorConfig } from './interfaces/FormDataInterceptorConfig'; | ||
import { FormDataInterceptorConfig, NestjsFormDataAsyncOptions } from './interfaces'; | ||
export declare class NestjsFormDataModule { | ||
static config(config: FormDataInterceptorConfig): DynamicModule; | ||
static configAsync(options: NestjsFormDataAsyncOptions): DynamicModule; | ||
private static createAsyncProviders; | ||
private static createAsyncOptionsProvider; | ||
} | ||
//# sourceMappingURL=nestjs-form-data.module.d.ts.map |
@@ -28,2 +28,40 @@ "use strict"; | ||
} | ||
static configAsync(options) { | ||
return { | ||
module: NestjsFormDataModule_1, | ||
imports: options.imports || [], | ||
providers: this.createAsyncProviders(options), | ||
}; | ||
} | ||
static createAsyncProviders(options) { | ||
if (options.useExisting || options.useFactory) { | ||
return [this.createAsyncOptionsProvider(options)]; | ||
} | ||
return [ | ||
this.createAsyncOptionsProvider(options), | ||
{ | ||
provide: options.useClass, | ||
useClass: options.useClass, | ||
}, | ||
]; | ||
} | ||
static createAsyncOptionsProvider(options) { | ||
if (options.useFactory) { | ||
return { | ||
provide: global_config_inject_token_config_1.GLOBAL_CONFIG_INJECT_TOKEN, | ||
useFactory: async (...args) => { | ||
return check_config_1.checkConfig(await options.useFactory(...args)); | ||
}, | ||
inject: options.inject || [], | ||
}; | ||
} | ||
return { | ||
provide: global_config_inject_token_config_1.GLOBAL_CONFIG_INJECT_TOKEN, | ||
useFactory: async (optionsFactory) => { | ||
const config = await optionsFactory.configAsync(); | ||
return check_config_1.checkConfig(config); | ||
}, | ||
inject: [options.useExisting || options.useClass], | ||
}; | ||
} | ||
}; | ||
@@ -30,0 +68,0 @@ NestjsFormDataModule = NestjsFormDataModule_1 = __decorate([ |
{ | ||
"name": "nestjs-form-data", | ||
"version": "1.3.1", | ||
"version": "1.4.0", | ||
"description": "", | ||
@@ -16,3 +16,3 @@ "main": "dist/index", | ||
"author": "dmitriy-nz", | ||
"license": "ISC", | ||
"license": "MIT", | ||
"repository": { | ||
@@ -26,2 +26,3 @@ "type": "git", | ||
"@types/node": "^14.14.41", | ||
"@types/type-is": "^1.6.3", | ||
"rimraf": "^3.0.2", | ||
@@ -40,3 +41,4 @@ "typescript": "^4.2.4" | ||
"reflect-metadata": "^0.1.13", | ||
"rxjs": "^6.6.7" | ||
"rxjs": "^6.6.7", | ||
"type-is": "^1.6.18" | ||
}, | ||
@@ -43,0 +45,0 @@ "keywords": [ |
@@ -52,4 +52,4 @@ # Description | ||
``` | ||
## Configuration | ||
### Static configuration | ||
You can set the global configuration when connecting the module using the `NestjsFormDataModule.config` method: | ||
@@ -67,2 +67,55 @@ ```ts | ||
``` | ||
### Async configuration | ||
Quite often you might want to asynchronously pass your module options instead of passing them beforehand. | ||
In such case, use `configAsync()` method, that provides a couple of various ways to deal with async data. | ||
##### 1. Use factory | ||
```ts | ||
NestjsFormDataModule.configAsync({ | ||
useFactory: () => ({ | ||
storage: MemoryStoredFile | ||
}) | ||
}); | ||
``` | ||
Our factory behaves like every other one (might be async and is able to inject dependencies through inject). | ||
```ts | ||
NestjsFormDataModule.configAsync({ | ||
imports: [ConfigModule], | ||
useFactory: async (configService: ConfigService) => ({ | ||
storage: MemoryStoredFile, | ||
limits: { | ||
files: configService.get<number>('files'), | ||
} | ||
}), | ||
inject: [ConfigService], | ||
}); | ||
``` | ||
##### 2. Use class | ||
```ts | ||
NestjsFormDataModule.configAsync({ | ||
useClass: MyNestJsFormDataConfigService | ||
}); | ||
``` | ||
Above construction will instantiate `MyNestJsFormDataConfigService` inside `NestjsFormDataModule` and will leverage it | ||
to create options object. | ||
```ts | ||
export class MyNestJsFormDataConfigService implements NestjsFormDataConfigFactory { | ||
configAsync(): Promise<FormDataInterceptorConfig> | FormDataInterceptorConfig { | ||
return { | ||
storage: FileSystemStoredFile, | ||
fileSystemStoragePath: '/tmp/nestjs-fd', | ||
}; | ||
} | ||
} | ||
``` | ||
##### 3. Use existing | ||
```ts | ||
NestjsFormDataModule.configAsync({ | ||
imports: [MyNestJsFormDataConfigModule], | ||
useExisting: MyNestJsFormDataConfigService | ||
}); | ||
``` | ||
It works the same as useClass with one critical difference - `NestjsFormDataModule` will lookup imported modules to | ||
reuse already created `MyNestJsFormDataConfigService`, instead of instantiating it on its own. | ||
### Method level configuration | ||
Or pass the config object while using the decorator on the method | ||
@@ -69,0 +122,0 @@ ```ts |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Mixed license
License(Experimental) Package contains multiple licenses.
Found 1 instance in 1 package
65109
95
0
702
202
11
6
+ Addedtype-is@^1.6.18
+ Addedmedia-typer@0.3.0(transitive)
+ Addedmime-db@1.52.0(transitive)
+ Addedmime-types@2.1.35(transitive)
+ Addedtype-is@1.6.18(transitive)