-
-
Notifications
You must be signed in to change notification settings - Fork 7.8k
/
Copy pathmultipart.module.ts
74 lines (70 loc) · 2.08 KB
/
multipart.module.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import { DynamicModule, Module, Provider } from '@nestjs/common';
import { randomStringGenerator } from '@nestjs/common/utils/random-string-generator.util';
import { MULTIPART_MODULE_OPTIONS } from './files.constants';
import {
MultipartModuleAsyncOptions,
MultipartModuleOptions,
MultipartOptionsFactory,
} from './interfaces/files-upload-module.interface';
import { MULTIPART_MODULE_ID } from './multipart.constants';
@Module({})
export class MultipartModule {
static register(options: MultipartModuleOptions = {}): DynamicModule {
return {
module: MultipartModule,
providers: [
{ provide: MULTIPART_MODULE_OPTIONS, useValue: options },
{
provide: MULTIPART_MODULE_ID,
useValue: randomStringGenerator(),
},
],
exports: [MULTIPART_MODULE_OPTIONS],
};
}
static registerAsync(options: MultipartModuleAsyncOptions): DynamicModule {
return {
module: MultipartModule,
imports: options.imports,
providers: [
...this.createAsyncProviders(options),
{
provide: MULTIPART_MODULE_ID,
useValue: randomStringGenerator(),
},
],
exports: [MULTIPART_MODULE_OPTIONS],
};
}
private static createAsyncProviders(
options: MultipartModuleAsyncOptions,
): Provider[] {
if (options.useExisting || options.useFactory) {
return [this.createAsyncOptionsProvider(options)];
}
return [
this.createAsyncOptionsProvider(options),
{
provide: options.useClass,
useClass: options.useClass,
},
];
}
private static createAsyncOptionsProvider(
options: MultipartModuleAsyncOptions,
): Provider {
if (options.useFactory) {
return {
provide: MULTIPART_MODULE_OPTIONS,
useFactory: options.useFactory,
inject: options.inject || [],
};
}
return {
provide: MULTIPART_MODULE_OPTIONS,
useFactory: async (optionsFactory: MultipartOptionsFactory) =>
optionsFactory.createMultipartOptions(),
inject: [options.useExisting || options.useClass],
};
}
}