-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtsx-views.module.ts
93 lines (85 loc) · 2.26 KB
/
tsx-views.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import {
DynamicModule,
Inject,
MiddlewareConsumer,
Module,
NestModule,
Provider,
} from '@nestjs/common'
import { TSX_VIEWS_OPTIONS } from './tsx-views.constants'
import {
TsxViewsModuleOptions,
TsxViewsModuleOptionsAsyncOptions,
TsxViewsModuleOptionsFactory,
} from './tsx-views.interface'
import { TsxViewsMiddleware } from './tsx-views.middleware'
import { TsxViewsService } from './tsx-views.service'
@Module({})
export class TsxViewsModule implements NestModule {
constructor(
@Inject(TSX_VIEWS_OPTIONS) private readonly options: TsxViewsModuleOptions,
) {}
configure(consumer: MiddlewareConsumer): void {
consumer
.apply(TsxViewsMiddleware)
.exclude(...(this.options.exclude ?? []))
.forRoutes(...(this.options.forRoutes ?? '*'))
}
public static register(options: TsxViewsModuleOptions): DynamicModule {
return {
module: TsxViewsModule,
providers: [
{
provide: TSX_VIEWS_OPTIONS,
useValue: options,
},
],
}
}
public static registerAsync(
options: TsxViewsModuleOptionsAsyncOptions,
): DynamicModule {
const providers = this.createProviders(options)
return {
module: TsxViewsModule,
imports: options.imports,
providers: [
TsxViewsService,
...(options.extraProviders ?? []),
...providers,
],
exports: [TsxViewsService, ...providers],
}
}
static createProviders(
options: TsxViewsModuleOptionsAsyncOptions,
): Provider[] {
if (options.useExisting || options.useFactory) {
return [this.createOptionsProvider(options)]
}
return [
this.createOptionsProvider(options),
{
provide: options.useClass!,
useClass: options.useClass!,
},
]
}
private static createOptionsProvider(
options: TsxViewsModuleOptionsAsyncOptions,
): Provider {
if (options.useFactory) {
return {
provide: TSX_VIEWS_OPTIONS,
useFactory: options.useFactory,
inject: options.inject,
}
}
return {
provide: TSX_VIEWS_OPTIONS,
useFactory: async (optionsFactory: TsxViewsModuleOptionsFactory) =>
await optionsFactory.createTsxViewsOptions(),
inject: [options.useExisting || options.useClass!],
}
}
}