This repository has been archived by the owner on Jan 7, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
automapper.module.ts
79 lines (71 loc) · 2.65 KB
/
automapper.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
75
76
77
78
79
import { AutoMapper, AutoMapperGlobalSettings } from '@nartc/automapper';
import { DynamicModule, Global, Logger, Module } from '@nestjs/common';
import { OnModuleInit } from '@nestjs/common/interfaces';
import { AutomapperExplorer } from './automapper.explorer';
import { forRootProviders, withMapperProviders } from './automapper.provider';
import { AutomapperModuleRootOptions } from './interfaces';
import { getWithMapperArgs } from './utils/getWithMapperArgs';
import { MAPPER_MAP, MapperMap } from './utils/mapperMap';
import { PROFILE_MAP, ProfileMap } from './utils/profileMap';
@Global()
@Module({})
export class AutomapperModule implements OnModuleInit {
private static readonly logger: Logger = new Logger('AutomapperModule');
/**
* Initialize a Mapper with name and globalSettings
*
* @param {string} name - name of the Mapper instance. Default to 'default'
* @param {AutoMapperGlobalSettings} globalSettings - Global Settings for the current Mapper instance
*/
static withMapper(
name?: string,
globalSettings?: AutoMapperGlobalSettings
): DynamicModule;
static withMapper(globalSettings?: AutoMapperGlobalSettings): DynamicModule;
static withMapper(...args: any[]): DynamicModule {
const [name, globalSettings] = getWithMapperArgs(args);
const mapper = new AutoMapper();
if (globalSettings != null) {
mapper.withGlobalSettings(globalSettings);
}
const providers = withMapperProviders(mapper, name);
return {
module: AutomapperModule,
providers: [
...providers,
AutomapperExplorer,
{ provide: PROFILE_MAP, useValue: ProfileMap },
{ provide: MAPPER_MAP, useValue: MapperMap },
{ provide: Logger, useValue: this.logger },
],
exports: providers,
};
}
/**
* Initialize an AutoMapper instance with a name. Default to "default"
*
* Generally, `forRoot` only needs to be ran once to provide a singleton for the whole application
*
* @param {AutomapperModuleRootOptions} options
* @deprecated Please use withMapper instead
*/
static forRoot(options?: AutomapperModuleRootOptions): DynamicModule {
const mapper = new AutoMapper();
const providers = forRootProviders(mapper, options);
return {
module: AutomapperModule,
providers: [
...providers,
AutomapperExplorer,
{ provide: PROFILE_MAP, useValue: ProfileMap },
{ provide: MAPPER_MAP, useValue: MapperMap },
{ provide: Logger, useValue: this.logger },
],
exports: providers,
};
}
constructor(private readonly explorer: AutomapperExplorer) {}
onModuleInit(): void {
this.explorer.explore();
}
}