@nest-yalc-2/app 1.3.3
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.
- package/LICENSE +661 -0
- package/README.md +49 -0
- package/package.json +33 -0
- package/src/__tests__/app-bootstrap-base.helper.spec.ts +38 -0
- package/src/__tests__/app-bootstrap-standalone.helper.spec.ts +35 -0
- package/src/__tests__/app-bootstrap.helper.spec.ts +98 -0
- package/src/__tests__/app-config.service.spec.ts +55 -0
- package/src/__tests__/app-context.service.spec.ts +28 -0
- package/src/__tests__/app.events.spec.ts +12 -0
- package/src/__tests__/app.helper.spec.ts +204 -0
- package/src/__tests__/base-app-module.helper.spec.ts +231 -0
- package/src/__tests__/base-app.controller.spec.ts +72 -0
- package/src/__tests__/base-app.service.spec.ts +39 -0
- package/src/__tests__/cls.module.spec.ts +33 -0
- package/src/__tests__/life-cycle-handler.service.spec.ts +48 -0
- package/src/__tests__/tsconfig.json +3 -0
- package/src/__tests__/unwrap-result.interceptor.spec.ts +52 -0
- package/src/app-bootstrap-base.helper.d.ts +41 -0
- package/src/app-bootstrap-base.helper.js +108 -0
- package/src/app-bootstrap-base.helper.js.map +1 -0
- package/src/app-bootstrap-base.helper.ts +199 -0
- package/src/app-bootstrap-standalone.helper.d.ts +15 -0
- package/src/app-bootstrap-standalone.helper.js +45 -0
- package/src/app-bootstrap-standalone.helper.js.map +1 -0
- package/src/app-bootstrap-standalone.helper.ts +60 -0
- package/src/app-bootstrap.helper.d.ts +43 -0
- package/src/app-bootstrap.helper.js +164 -0
- package/src/app-bootstrap.helper.js.map +1 -0
- package/src/app-bootstrap.helper.ts +263 -0
- package/src/app-config.service.d.ts +14 -0
- package/src/app-config.service.js +45 -0
- package/src/app-config.service.js.map +1 -0
- package/src/app-config.service.ts +61 -0
- package/src/app-context.module.d.ts +2 -0
- package/src/app-context.module.js +19 -0
- package/src/app-context.module.js.map +1 -0
- package/src/app-context.module.ts +6 -0
- package/src/app-context.service.d.ts +7 -0
- package/src/app-context.service.js +26 -0
- package/src/app-context.service.js.map +1 -0
- package/src/app-context.service.ts +16 -0
- package/src/app-imports.factory.d.ts +19 -0
- package/src/app-imports.factory.js +223 -0
- package/src/app-imports.factory.js.map +1 -0
- package/src/app-imports.factory.ts +320 -0
- package/src/app.events.d.ts +5 -0
- package/src/app.events.js +10 -0
- package/src/app.events.js.map +1 -0
- package/src/app.events.ts +8 -0
- package/src/app.helper.d.ts +17 -0
- package/src/app.helper.js +26 -0
- package/src/app.helper.js.map +1 -0
- package/src/app.helper.ts +73 -0
- package/src/base-app-module.helper.d.ts +24 -0
- package/src/base-app-module.helper.js +326 -0
- package/src/base-app-module.helper.js.map +1 -0
- package/src/base-app-module.helper.ts +447 -0
- package/src/base-app.controller.d.ts +9 -0
- package/src/base-app.controller.js +56 -0
- package/src/base-app.controller.js.map +1 -0
- package/src/base-app.controller.ts +43 -0
- package/src/base-app.interface.d.ts +33 -0
- package/src/base-app.interface.js +3 -0
- package/src/base-app.interface.js.map +1 -0
- package/src/base-app.interface.ts +55 -0
- package/src/base-app.service.d.ts +7 -0
- package/src/base-app.service.js +78 -0
- package/src/base-app.service.js.map +1 -0
- package/src/base-app.service.ts +29 -0
- package/src/cls.module.d.ts +15 -0
- package/src/cls.module.js +51 -0
- package/src/cls.module.js.map +1 -0
- package/src/cls.module.ts +68 -0
- package/src/conf.type.d.ts +21 -0
- package/src/conf.type.js +3 -0
- package/src/conf.type.js.map +1 -0
- package/src/conf.type.ts +22 -0
- package/src/db-conf.helper.ts-off +93 -0
- package/src/def.const.d.ts +10 -0
- package/src/def.const.js +14 -0
- package/src/def.const.js.map +1 -0
- package/src/def.const.ts +33 -0
- package/src/global.enum.d.ts +6 -0
- package/src/global.enum.js +11 -0
- package/src/global.enum.js.map +1 -0
- package/src/global.enum.ts +6 -0
- package/src/index.d.ts +0 -0
- package/src/index.js +2 -0
- package/src/index.js.map +1 -0
- package/src/index.ts +0 -0
- package/src/life-cycle-handler.service.d.ts +13 -0
- package/src/life-cycle-handler.service.js +47 -0
- package/src/life-cycle-handler.service.js.map +1 -0
- package/src/life-cycle-handler.service.ts +58 -0
- package/src/unwrap-result.interceptor.d.ts +5 -0
- package/src/unwrap-result.interceptor.js +36 -0
- package/src/unwrap-result.interceptor.js.map +1 -0
- package/src/unwrap-result.interceptor.ts +32 -0
- package/src/yalc-controller.interface.d.ts +3 -0
- package/src/yalc-controller.interface.js +3 -0
- package/src/yalc-controller.interface.js.map +1 -0
- package/src/yalc-controller.interface.ts +6 -0
|
@@ -0,0 +1,447 @@
|
|
|
1
|
+
import {
|
|
2
|
+
IYalcBaseAppOptions,
|
|
3
|
+
IYalcBaseDynamicModule,
|
|
4
|
+
IYalcBaseStaticModule,
|
|
5
|
+
} from './base-app.interface.js';
|
|
6
|
+
import {
|
|
7
|
+
APP_ALIAS_TOKEN,
|
|
8
|
+
APP_EVENT_SERVICE,
|
|
9
|
+
APP_LOGGER_SERVICE,
|
|
10
|
+
APP_OPTION_TOKEN,
|
|
11
|
+
MAIN_APP_CONFIG_SERVICE,
|
|
12
|
+
MODULE_ALIAS_TOKEN,
|
|
13
|
+
MODULE_OPTION_TOKEN,
|
|
14
|
+
SYSTEM_EVENT_SERVICE,
|
|
15
|
+
SYSTEM_LOGGER_SERVICE,
|
|
16
|
+
} from './def.const.js';
|
|
17
|
+
import { LifeCycleHandler } from './life-cycle-handler.service.js';
|
|
18
|
+
import { DynamicModule, Global, Logger, Module } from '@nestjs/common';
|
|
19
|
+
import { LoggerServiceFactory } from '@nest-yalc-2/logger/logger.service.js';
|
|
20
|
+
import {
|
|
21
|
+
AppConfigService,
|
|
22
|
+
createAppConfigProvider,
|
|
23
|
+
getAppConfigToken,
|
|
24
|
+
getAppEventToken,
|
|
25
|
+
getAppLoggerToken,
|
|
26
|
+
} from './app-config.service.js';
|
|
27
|
+
import { AppContextModule } from './app-context.module.js';
|
|
28
|
+
import { NODE_ENV } from './global.enum.js';
|
|
29
|
+
import { ConfigModule, ConfigService, registerAs } from '@nestjs/config';
|
|
30
|
+
import Joi from 'joi';
|
|
31
|
+
import { EventModule } from '@nest-yalc-2/event-manager/index.js';
|
|
32
|
+
import { EventEmitter2, EventEmitterModule } from '@nestjs/event-emitter';
|
|
33
|
+
import { YalcClsModule } from './cls.module.js';
|
|
34
|
+
import { IYalcControllerStaticInterface } from './yalc-controller.interface.js';
|
|
35
|
+
import * as _ from 'lodash-es';
|
|
36
|
+
import { IGlobalOptions } from './app-bootstrap-base.helper.js';
|
|
37
|
+
import { getEnvLoggerLevels } from '@nest-yalc-2/logger/logger.helper.js';
|
|
38
|
+
|
|
39
|
+
const singletonDynamicModules = new Map<any, any>();
|
|
40
|
+
|
|
41
|
+
export function registerSingletonDynamicModule(
|
|
42
|
+
isSingleton: boolean,
|
|
43
|
+
moduleToken: any,
|
|
44
|
+
module: any,
|
|
45
|
+
): any | true {
|
|
46
|
+
if (!isSingleton) {
|
|
47
|
+
return false;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const cached = singletonDynamicModules.get(moduleToken);
|
|
51
|
+
if (cached) {
|
|
52
|
+
return cached;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
singletonDynamicModules.set(moduleToken, module);
|
|
56
|
+
return singletonDynamicModules.get(moduleToken);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export function getCachedModule(module: any, isSingleton: boolean) {
|
|
60
|
+
if (isSingleton) {
|
|
61
|
+
return singletonDynamicModules.get(module);
|
|
62
|
+
}
|
|
63
|
+
return null;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function envFilePathList(dirname: string = '.') {
|
|
67
|
+
const envFilePath: string[] = [];
|
|
68
|
+
|
|
69
|
+
envFilePath.push(`${dirname}/.env`); // user-defined env (git-ignored)
|
|
70
|
+
|
|
71
|
+
if (process.env.NODE_ENV) {
|
|
72
|
+
envFilePath.push(`${dirname}/.env.${process.env.NODE_ENV}`); // user-defined env (git-ignored)
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// .env.dist is always loaded except in production
|
|
76
|
+
if (process.env.NODE_ENV !== 'production')
|
|
77
|
+
envFilePath.push(`${dirname}/.env.dist`);
|
|
78
|
+
|
|
79
|
+
return envFilePath;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const _buildEnvFilePath = _.memoize(
|
|
83
|
+
(envDir?: string, envPath?: string | string[]) => {
|
|
84
|
+
const envFilePath: string[] = [];
|
|
85
|
+
|
|
86
|
+
if (!envPath) {
|
|
87
|
+
Logger.debug(
|
|
88
|
+
`Loading env from: ${envDir}, NODE_ENV: ${process.env.NODE_ENV}`,
|
|
89
|
+
);
|
|
90
|
+
|
|
91
|
+
envFilePath.push(...envFilePathList(envDir));
|
|
92
|
+
} else {
|
|
93
|
+
envFilePath.push(...(Array.isArray(envPath) ? envPath : [envPath]));
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
Logger.debug(`Using env file paths: ${envFilePath}`);
|
|
97
|
+
|
|
98
|
+
return envFilePath;
|
|
99
|
+
},
|
|
100
|
+
// Since memoize use the first argument as the key, we need to generate a unique key based on both arguments
|
|
101
|
+
(envDir = '', envPath = '') => {
|
|
102
|
+
return `${envDir}-${Array.isArray(envPath) ? envPath.join(',') : envPath}`;
|
|
103
|
+
},
|
|
104
|
+
);
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* For some strange reason, the memoize doesn't work well with exporting the const at the same time
|
|
108
|
+
* and re-use it in the same file
|
|
109
|
+
*/
|
|
110
|
+
export const buildEnvFilePath = _buildEnvFilePath;
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Used for applications with controller/resolver support
|
|
114
|
+
* you should override it
|
|
115
|
+
*/
|
|
116
|
+
export function yalcBaseAppModuleMetadataFactory(
|
|
117
|
+
module: any,
|
|
118
|
+
appAlias: string,
|
|
119
|
+
options?: Omit<IYalcBaseAppOptions, 'module'>,
|
|
120
|
+
): IYalcBaseStaticModule {
|
|
121
|
+
const _options = {
|
|
122
|
+
// default values
|
|
123
|
+
isSingleton: false,
|
|
124
|
+
global: true,
|
|
125
|
+
// user defined values
|
|
126
|
+
...(options ?? {}),
|
|
127
|
+
};
|
|
128
|
+
const { controllers, exports, imports, providers } = _options;
|
|
129
|
+
|
|
130
|
+
const cached = getCachedModule(module, _options.isSingleton);
|
|
131
|
+
if (cached) {
|
|
132
|
+
// Logger.debug(`Using cached metadata for ${module.name}`);
|
|
133
|
+
return cached;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
const _providers: Array<any> = [
|
|
137
|
+
{
|
|
138
|
+
provide: MODULE_ALIAS_TOKEN,
|
|
139
|
+
useValue: appAlias,
|
|
140
|
+
},
|
|
141
|
+
{
|
|
142
|
+
provide: MODULE_OPTION_TOKEN,
|
|
143
|
+
useValue: options,
|
|
144
|
+
},
|
|
145
|
+
{
|
|
146
|
+
provide: APP_EVENT_SERVICE,
|
|
147
|
+
useExisting: 'INTERNAL_APP_EVENT_SERVICE',
|
|
148
|
+
},
|
|
149
|
+
{
|
|
150
|
+
provide: getAppEventToken(appAlias),
|
|
151
|
+
useExisting: 'INTERNAL_APP_EVENT_SERVICE',
|
|
152
|
+
},
|
|
153
|
+
];
|
|
154
|
+
|
|
155
|
+
const logger = options?.logger;
|
|
156
|
+
if (logger) {
|
|
157
|
+
_providers.push(
|
|
158
|
+
(logger === true ? LoggerServiceFactory : logger)(
|
|
159
|
+
appAlias,
|
|
160
|
+
APP_LOGGER_SERVICE,
|
|
161
|
+
appAlias,
|
|
162
|
+
),
|
|
163
|
+
);
|
|
164
|
+
|
|
165
|
+
_providers.push({
|
|
166
|
+
provide: getAppLoggerToken(appAlias),
|
|
167
|
+
useExisting: APP_LOGGER_SERVICE,
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
const hasConfig = _options.extraConfigs || _options.configFactory;
|
|
172
|
+
|
|
173
|
+
if (hasConfig) {
|
|
174
|
+
_providers.push(
|
|
175
|
+
createAppConfigProvider(appAlias),
|
|
176
|
+
/**
|
|
177
|
+
* Alias
|
|
178
|
+
*/
|
|
179
|
+
{
|
|
180
|
+
provide: AppConfigService,
|
|
181
|
+
useExisting: getAppConfigToken(appAlias),
|
|
182
|
+
},
|
|
183
|
+
);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
if (!_options.skipDuplicateAppCheck) {
|
|
187
|
+
_providers.push(LifeCycleHandler);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
191
|
+
|
|
192
|
+
if (providers) {
|
|
193
|
+
_providers.push(...providers);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
const _imports: DynamicModule['imports'] = [];
|
|
197
|
+
|
|
198
|
+
if (hasConfig) {
|
|
199
|
+
const envFilePath: string[] = _buildEnvFilePath(
|
|
200
|
+
options?.envDir,
|
|
201
|
+
options?.envPath,
|
|
202
|
+
);
|
|
203
|
+
|
|
204
|
+
_imports.push(
|
|
205
|
+
YalcGlobalStaticModule,
|
|
206
|
+
(options?.eventModuleClass ?? EventModule).forRootAsync({
|
|
207
|
+
imports: [module],
|
|
208
|
+
loggerProvider: {
|
|
209
|
+
provide: 'INTERNAL_APP_LOGGER_SERVICE',
|
|
210
|
+
useExisting: getAppLoggerToken(appAlias),
|
|
211
|
+
},
|
|
212
|
+
eventServiceToken: 'INTERNAL_APP_EVENT_SERVICE',
|
|
213
|
+
eventEmitter: {
|
|
214
|
+
provide: 'INTERNAL_APP_EVENT_EMITTER',
|
|
215
|
+
useExisting: EventEmitter2,
|
|
216
|
+
},
|
|
217
|
+
}),
|
|
218
|
+
ConfigModule.forRoot({
|
|
219
|
+
envFilePath,
|
|
220
|
+
load: [
|
|
221
|
+
registerAs(appAlias, async () => {
|
|
222
|
+
/**
|
|
223
|
+
* @see https://docs.nestjs.com/techniques/configuration#environment-variables-loaded-hook
|
|
224
|
+
*/
|
|
225
|
+
await ConfigModule.envVariablesLoaded;
|
|
226
|
+
|
|
227
|
+
return await (_options.configFactory?.() ?? {});
|
|
228
|
+
}),
|
|
229
|
+
...(_options.extraConfigs ?? []),
|
|
230
|
+
],
|
|
231
|
+
validationSchema: Joi.object({
|
|
232
|
+
NODE_ENV: Joi.string()
|
|
233
|
+
.valid(
|
|
234
|
+
NODE_ENV.DEVELOPMENT,
|
|
235
|
+
NODE_ENV.PRODUCTION,
|
|
236
|
+
NODE_ENV.TEST,
|
|
237
|
+
NODE_ENV.PIPELINE,
|
|
238
|
+
)
|
|
239
|
+
.default(NODE_ENV.DEVELOPMENT),
|
|
240
|
+
}),
|
|
241
|
+
validationOptions: {
|
|
242
|
+
allowUnknown: true,
|
|
243
|
+
abortEarly: true,
|
|
244
|
+
},
|
|
245
|
+
/**
|
|
246
|
+
* It can be global because the ConfigService registers configurations by using an alias, hence there won't be any conflict
|
|
247
|
+
* It allows us to use the ConfigService in any module without having to import the ConfigModule
|
|
248
|
+
*/
|
|
249
|
+
isGlobal: true,
|
|
250
|
+
}),
|
|
251
|
+
YalcClsModule,
|
|
252
|
+
);
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
if (imports) {
|
|
256
|
+
_imports.push(...imports);
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
const _exports: DynamicModule['exports'] = [getAppEventToken(appAlias)];
|
|
260
|
+
|
|
261
|
+
if (options?.logger) {
|
|
262
|
+
_exports.push(getAppLoggerToken(appAlias));
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
if (hasConfig) {
|
|
266
|
+
_exports.push(getAppConfigToken(appAlias));
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
if (exports) {
|
|
270
|
+
_exports.push(...exports);
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
const _controllers: DynamicModule['controllers'] = [];
|
|
274
|
+
|
|
275
|
+
if (controllers) {
|
|
276
|
+
_controllers.push(
|
|
277
|
+
...controllers.map((c) => {
|
|
278
|
+
(c as unknown as IYalcControllerStaticInterface)._appAlias = appAlias;
|
|
279
|
+
return c;
|
|
280
|
+
}),
|
|
281
|
+
);
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
const config = {
|
|
285
|
+
imports: _imports,
|
|
286
|
+
exports: _exports,
|
|
287
|
+
controllers: _controllers,
|
|
288
|
+
providers: _providers,
|
|
289
|
+
};
|
|
290
|
+
|
|
291
|
+
registerSingletonDynamicModule(_options.isSingleton, module, config);
|
|
292
|
+
|
|
293
|
+
return config;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* Util class that can be extended to create a NestJS application
|
|
298
|
+
*/
|
|
299
|
+
export class YalcBaseAppModule {
|
|
300
|
+
/**
|
|
301
|
+
* Used by the CLI and other non-network processes
|
|
302
|
+
* you should override it
|
|
303
|
+
*
|
|
304
|
+
* NOTE: if you need singleton dynamic modules, you should use the `isSingleton` property
|
|
305
|
+
* but in most of the cases is better to use the static module instead
|
|
306
|
+
*/
|
|
307
|
+
protected static _forRootStandalone(
|
|
308
|
+
appAlias: string,
|
|
309
|
+
options?: IYalcBaseAppOptions,
|
|
310
|
+
): IYalcBaseDynamicModule {
|
|
311
|
+
return this.assignDynamicProperties(
|
|
312
|
+
yalcBaseAppModuleMetadataFactory(this, appAlias, {
|
|
313
|
+
isStandalone: true,
|
|
314
|
+
...this.assignDynamicProperties({}),
|
|
315
|
+
...options,
|
|
316
|
+
}),
|
|
317
|
+
options,
|
|
318
|
+
);
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
/**
|
|
322
|
+
* Used for applications with controller/resolver support
|
|
323
|
+
* you should override it
|
|
324
|
+
*
|
|
325
|
+
* NOTE: if you need singleton dynamic modules, you should use the `isSingleton` property
|
|
326
|
+
* but in most of the cases is better to use the static module instead
|
|
327
|
+
*/
|
|
328
|
+
protected static _forRoot(
|
|
329
|
+
appAlias: string,
|
|
330
|
+
options?: IYalcBaseAppOptions,
|
|
331
|
+
): IYalcBaseDynamicModule {
|
|
332
|
+
return this.assignDynamicProperties(
|
|
333
|
+
yalcBaseAppModuleMetadataFactory(this, appAlias, {
|
|
334
|
+
isStandalone: false,
|
|
335
|
+
...this.assignDynamicProperties({}),
|
|
336
|
+
...options,
|
|
337
|
+
}),
|
|
338
|
+
options,
|
|
339
|
+
);
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
public static assignDynamicProperties(
|
|
343
|
+
config: any,
|
|
344
|
+
options?: IYalcBaseAppOptions,
|
|
345
|
+
) {
|
|
346
|
+
config.module = this;
|
|
347
|
+
config.global = options?.global ?? true;
|
|
348
|
+
config.isSingleton = options?.isSingleton ?? false;
|
|
349
|
+
|
|
350
|
+
return config;
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
function yalcGlobalStaticModuleFactory(): Partial<IYalcBaseStaticModule> {
|
|
355
|
+
return {
|
|
356
|
+
imports: [
|
|
357
|
+
EventEmitterModule.forRoot({
|
|
358
|
+
global: true,
|
|
359
|
+
maxListeners: 1000,
|
|
360
|
+
wildcard: true,
|
|
361
|
+
}),
|
|
362
|
+
],
|
|
363
|
+
};
|
|
364
|
+
}
|
|
365
|
+
@Global()
|
|
366
|
+
@Module(yalcGlobalStaticModuleFactory())
|
|
367
|
+
export class YalcGlobalStaticModule {}
|
|
368
|
+
|
|
369
|
+
/**
|
|
370
|
+
* This class is used to create true singleton providers
|
|
371
|
+
*/
|
|
372
|
+
export class YalcDefaultAppModule {
|
|
373
|
+
static forRoot(
|
|
374
|
+
appAlias: string,
|
|
375
|
+
imports: NonNullable<DynamicModule['imports']>,
|
|
376
|
+
options?: IGlobalOptions,
|
|
377
|
+
) {
|
|
378
|
+
const _imports: NonNullable<DynamicModule['imports']> = [
|
|
379
|
+
YalcGlobalStaticModule,
|
|
380
|
+
(options?.eventModuleClass ?? EventModule).forRootAsync({
|
|
381
|
+
loggerProvider: {
|
|
382
|
+
provide: 'INTERNAL_SYSTEM_LOGGER_SERVICE',
|
|
383
|
+
useExisting: SYSTEM_LOGGER_SERVICE,
|
|
384
|
+
},
|
|
385
|
+
eventServiceToken: 'INTERNAL_SYSTEM_EVENT_SERVICE',
|
|
386
|
+
}),
|
|
387
|
+
AppContextModule,
|
|
388
|
+
YalcClsModule,
|
|
389
|
+
...imports,
|
|
390
|
+
];
|
|
391
|
+
|
|
392
|
+
const providers: NonNullable<DynamicModule['providers']> = [
|
|
393
|
+
{
|
|
394
|
+
provide: APP_ALIAS_TOKEN,
|
|
395
|
+
useValue: appAlias,
|
|
396
|
+
},
|
|
397
|
+
{
|
|
398
|
+
provide: APP_OPTION_TOKEN,
|
|
399
|
+
useValue: options,
|
|
400
|
+
},
|
|
401
|
+
];
|
|
402
|
+
|
|
403
|
+
providers.push(
|
|
404
|
+
{
|
|
405
|
+
provide: SYSTEM_LOGGER_SERVICE,
|
|
406
|
+
useFactory: (configService, eventEmitter) => {
|
|
407
|
+
const loggerFactory = options?.logger ?? LoggerServiceFactory;
|
|
408
|
+
|
|
409
|
+
return loggerFactory(appAlias, SYSTEM_LOGGER_SERVICE, appAlias, {
|
|
410
|
+
event: {
|
|
411
|
+
eventEmitter: eventEmitter,
|
|
412
|
+
},
|
|
413
|
+
overrideLoggerLevels: getEnvLoggerLevels(),
|
|
414
|
+
}).useFactory(configService, eventEmitter);
|
|
415
|
+
},
|
|
416
|
+
inject: [MAIN_APP_CONFIG_SERVICE, EventEmitter2],
|
|
417
|
+
},
|
|
418
|
+
{
|
|
419
|
+
provide: MAIN_APP_CONFIG_SERVICE,
|
|
420
|
+
useFactory: (config: ConfigService) => {
|
|
421
|
+
return new AppConfigService(config, appAlias);
|
|
422
|
+
},
|
|
423
|
+
inject: [ConfigService],
|
|
424
|
+
},
|
|
425
|
+
{
|
|
426
|
+
provide: SYSTEM_EVENT_SERVICE,
|
|
427
|
+
useExisting: 'INTERNAL_SYSTEM_EVENT_SERVICE',
|
|
428
|
+
},
|
|
429
|
+
);
|
|
430
|
+
|
|
431
|
+
const exports: NonNullable<DynamicModule['exports']> = [
|
|
432
|
+
APP_OPTION_TOKEN,
|
|
433
|
+
APP_ALIAS_TOKEN,
|
|
434
|
+
SYSTEM_LOGGER_SERVICE,
|
|
435
|
+
SYSTEM_EVENT_SERVICE,
|
|
436
|
+
MAIN_APP_CONFIG_SERVICE,
|
|
437
|
+
];
|
|
438
|
+
|
|
439
|
+
return {
|
|
440
|
+
exports,
|
|
441
|
+
providers,
|
|
442
|
+
imports: _imports,
|
|
443
|
+
module: YalcDefaultAppModule,
|
|
444
|
+
global: true,
|
|
445
|
+
};
|
|
446
|
+
}
|
|
447
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { ConfigService } from '@nestjs/config';
|
|
2
|
+
import { BaseAppService } from './base-app.service.js';
|
|
3
|
+
export declare abstract class BaseAppController {
|
|
4
|
+
protected readonly appService: BaseAppService;
|
|
5
|
+
protected readonly configService: ConfigService;
|
|
6
|
+
constructor(appService: BaseAppService, configService: ConfigService);
|
|
7
|
+
getHello(): string;
|
|
8
|
+
shutdown(): void;
|
|
9
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.BaseAppController = void 0;
|
|
13
|
+
const def_const_js_1 = require("@nest-yalc-2/app/def.const.js");
|
|
14
|
+
const common_1 = require("@nestjs/common");
|
|
15
|
+
const config_1 = require("@nestjs/config");
|
|
16
|
+
const base_app_service_js_1 = require("./base-app.service.js");
|
|
17
|
+
const swagger_1 = require("@nestjs/swagger");
|
|
18
|
+
let BaseAppController = class BaseAppController {
|
|
19
|
+
constructor(appService, configService) {
|
|
20
|
+
this.appService = appService;
|
|
21
|
+
this.configService = configService;
|
|
22
|
+
}
|
|
23
|
+
getHello() {
|
|
24
|
+
const conf = this.configService.get(def_const_js_1.CURAPP_CONF_ALIAS);
|
|
25
|
+
return this.appService.getHello(conf?.appName || 'no-name');
|
|
26
|
+
}
|
|
27
|
+
shutdown() {
|
|
28
|
+
const conf = this.configService.get(def_const_js_1.CURAPP_CONF_ALIAS);
|
|
29
|
+
if (conf && (conf.isDev || conf.isTest)) {
|
|
30
|
+
console.log(`Bye bye!`);
|
|
31
|
+
process.exit(0);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
exports.BaseAppController = BaseAppController;
|
|
36
|
+
__decorate([
|
|
37
|
+
(0, common_1.Get)(),
|
|
38
|
+
__metadata("design:type", Function),
|
|
39
|
+
__metadata("design:paramtypes", []),
|
|
40
|
+
__metadata("design:returntype", String)
|
|
41
|
+
], BaseAppController.prototype, "getHello", null);
|
|
42
|
+
__decorate([
|
|
43
|
+
(0, swagger_1.ApiOperation)({
|
|
44
|
+
description: 'Shutdown the application, only available on development and test environment',
|
|
45
|
+
}),
|
|
46
|
+
(0, common_1.Get)('shutdown'),
|
|
47
|
+
__metadata("design:type", Function),
|
|
48
|
+
__metadata("design:paramtypes", []),
|
|
49
|
+
__metadata("design:returntype", void 0)
|
|
50
|
+
], BaseAppController.prototype, "shutdown", null);
|
|
51
|
+
exports.BaseAppController = BaseAppController = __decorate([
|
|
52
|
+
(0, common_1.Controller)(),
|
|
53
|
+
__metadata("design:paramtypes", [base_app_service_js_1.BaseAppService,
|
|
54
|
+
config_1.ConfigService])
|
|
55
|
+
], BaseAppController);
|
|
56
|
+
//# sourceMappingURL=base-app.controller.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"base-app.controller.js","sourceRoot":"","sources":["base-app.controller.ts"],"names":[],"mappings":";;;;;;;;;;;;AACA,gEAAkE;AAClE,2CAAiD;AACjD,2CAA+C;AAC/C,+DAAuD;AACvD,6CAA+C;AAMxC,IAAe,iBAAiB,GAAhC,MAAe,iBAAiB;IACrC,YACqB,UAA0B,EAC1B,aAA4B;QAD5B,eAAU,GAAV,UAAU,CAAgB;QAC1B,kBAAa,GAAb,aAAa,CAAe;IAC9C,CAAC;IAMJ,QAAQ;QACN,MAAM,IAAI,GAAG,IAAI,CAAC,aAAa,CAAC,GAAG,CAAe,gCAAiB,CAAC,CAAC;QACrE,OAAO,IAAI,CAAC,UAAU,CAAC,QAAQ,CAAC,IAAI,EAAE,OAAO,IAAI,SAAS,CAAC,CAAC;IAC9D,CAAC;IAUD,QAAQ;QACN,MAAM,IAAI,GAAG,IAAI,CAAC,aAAa,CAAC,GAAG,CAAe,gCAAiB,CAAC,CAAC;QACrE,IAAI,IAAI,IAAI,CAAC,IAAI,CAAC,KAAK,IAAI,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC;YAExC,OAAO,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC;YACxB,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;QAClB,CAAC;IACH,CAAC;CACF,CAAA;AA/BqB,8CAAiB;AAUrC;IADC,IAAA,YAAG,GAAE;;;;iDAIL;AAUD;IALC,IAAA,sBAAY,EAAC;QACZ,WAAW,EACT,8EAA8E;KACjF,CAAC;IACD,IAAA,YAAG,EAAC,UAAU,CAAC;;;;iDAQf;4BA9BmB,iBAAiB;IADtC,IAAA,mBAAU,GAAE;qCAGsB,oCAAc;QACX,sBAAa;GAH7B,iBAAiB,CA+BtC"}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { IServiceConf } from '@nest-yalc-2/app/conf.type.js';
|
|
2
|
+
import { CURAPP_CONF_ALIAS } from '@nest-yalc-2/app/def.const.js';
|
|
3
|
+
import { Controller, Get } from '@nestjs/common';
|
|
4
|
+
import { ConfigService } from '@nestjs/config';
|
|
5
|
+
import { BaseAppService } from './base-app.service.js';
|
|
6
|
+
import { ApiOperation } from '@nestjs/swagger';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Application controller
|
|
10
|
+
*/
|
|
11
|
+
@Controller()
|
|
12
|
+
export abstract class BaseAppController {
|
|
13
|
+
constructor(
|
|
14
|
+
protected readonly appService: BaseAppService,
|
|
15
|
+
protected readonly configService: ConfigService,
|
|
16
|
+
) {}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Expose the getHello method
|
|
20
|
+
*/
|
|
21
|
+
@Get()
|
|
22
|
+
getHello(): string {
|
|
23
|
+
const conf = this.configService.get<IServiceConf>(CURAPP_CONF_ALIAS);
|
|
24
|
+
return this.appService.getHello(conf?.appName || 'no-name');
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Only for dev purpose
|
|
29
|
+
*/
|
|
30
|
+
@ApiOperation({
|
|
31
|
+
description:
|
|
32
|
+
'Shutdown the application, only available on development and test environment',
|
|
33
|
+
})
|
|
34
|
+
@Get('shutdown')
|
|
35
|
+
shutdown() {
|
|
36
|
+
const conf = this.configService.get<IServiceConf>(CURAPP_CONF_ALIAS);
|
|
37
|
+
if (conf && (conf.isDev || conf.isTest)) {
|
|
38
|
+
// eslint-disable-next-line no-console
|
|
39
|
+
console.log(`Bye bye!`);
|
|
40
|
+
process.exit(0);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { EventModule } from '@nest-yalc-2/event-manager/event.module.js';
|
|
2
|
+
import { LoggerServiceFactory } from '@nest-yalc-2/logger/index.js';
|
|
3
|
+
import { ClassType } from '@nest-yalc-2/types/globals.d.js';
|
|
4
|
+
import { DynamicModule, ModuleMetadata } from '@nestjs/common';
|
|
5
|
+
import { ConfigFactory, ConfigObject, ConfigFactoryKeyHost } from '@nestjs/config';
|
|
6
|
+
export interface ISingletonOption {
|
|
7
|
+
isSingleton?: boolean;
|
|
8
|
+
}
|
|
9
|
+
export interface ISingletonDynamicModule extends ISingletonOption, DynamicModule {
|
|
10
|
+
}
|
|
11
|
+
export interface IYalcBaseAppOptions extends Partial<ISingletonDynamicModule> {
|
|
12
|
+
configFactory?: ConfigFactory<ConfigObject>;
|
|
13
|
+
extraConfigs?: (ConfigFactory<ConfigObject> & ConfigFactoryKeyHost<ReturnType<ConfigFactory<ConfigObject>>>)[];
|
|
14
|
+
envPath?: string | string[];
|
|
15
|
+
envDir?: string;
|
|
16
|
+
migrations?: ClassType[];
|
|
17
|
+
skipDuplicateAppCheck?: boolean;
|
|
18
|
+
logger?: boolean | typeof LoggerServiceFactory;
|
|
19
|
+
eventModuleClass?: typeof EventModule;
|
|
20
|
+
}
|
|
21
|
+
export type BaseAppStaticOptions = Omit<IYalcBaseAppOptions, 'module'>;
|
|
22
|
+
export interface IYalcBaseDynamicModule extends ISingletonDynamicModule {
|
|
23
|
+
imports: NonNullable<DynamicModule['imports']>;
|
|
24
|
+
exports: NonNullable<DynamicModule['exports']>;
|
|
25
|
+
controllers: NonNullable<DynamicModule['controllers']>;
|
|
26
|
+
providers: NonNullable<DynamicModule['providers']>;
|
|
27
|
+
}
|
|
28
|
+
export interface IYalcBaseStaticModule extends ModuleMetadata {
|
|
29
|
+
imports: NonNullable<DynamicModule['imports']>;
|
|
30
|
+
exports: NonNullable<DynamicModule['exports']>;
|
|
31
|
+
controllers: NonNullable<DynamicModule['controllers']>;
|
|
32
|
+
providers: NonNullable<DynamicModule['providers']>;
|
|
33
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"base-app.interface.js","sourceRoot":"","sources":["base-app.interface.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { EventModule } from '@nest-yalc-2/event-manager/event.module.js';
|
|
2
|
+
import { LoggerServiceFactory } from '@nest-yalc-2/logger/index.js';
|
|
3
|
+
import { ClassType } from '@nest-yalc-2/types/globals.d.js';
|
|
4
|
+
import { DynamicModule, ModuleMetadata } from '@nestjs/common';
|
|
5
|
+
import {
|
|
6
|
+
ConfigFactory,
|
|
7
|
+
ConfigObject,
|
|
8
|
+
ConfigFactoryKeyHost,
|
|
9
|
+
} from '@nestjs/config';
|
|
10
|
+
|
|
11
|
+
export interface ISingletonOption {
|
|
12
|
+
/**
|
|
13
|
+
* This is used to make sure that the module is not instantiated multiple times
|
|
14
|
+
* This is useful when you have a dynamic module that is imported in multiple other modules
|
|
15
|
+
* If you use static modules, this is not needed. Please use it with caution because it might
|
|
16
|
+
* cause unexpected behavior
|
|
17
|
+
*
|
|
18
|
+
* NOTE: should be used in conjunction with the `global` property
|
|
19
|
+
*/
|
|
20
|
+
isSingleton?: boolean;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface ISingletonDynamicModule
|
|
24
|
+
extends ISingletonOption, DynamicModule {}
|
|
25
|
+
|
|
26
|
+
export interface IYalcBaseAppOptions extends Partial<ISingletonDynamicModule> {
|
|
27
|
+
configFactory?: ConfigFactory<ConfigObject>;
|
|
28
|
+
extraConfigs?: (ConfigFactory<ConfigObject> &
|
|
29
|
+
ConfigFactoryKeyHost<ReturnType<ConfigFactory<ConfigObject>>>)[];
|
|
30
|
+
envPath?: string | string[];
|
|
31
|
+
/**
|
|
32
|
+
* When envPath is not defined, this will be used to find the env file
|
|
33
|
+
*/
|
|
34
|
+
envDir?: string;
|
|
35
|
+
migrations?: ClassType[];
|
|
36
|
+
skipDuplicateAppCheck?: boolean;
|
|
37
|
+
logger?: boolean | typeof LoggerServiceFactory;
|
|
38
|
+
eventModuleClass?: typeof EventModule;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export type BaseAppStaticOptions = Omit<IYalcBaseAppOptions, 'module'>;
|
|
42
|
+
|
|
43
|
+
export interface IYalcBaseDynamicModule extends ISingletonDynamicModule {
|
|
44
|
+
imports: NonNullable<DynamicModule['imports']>;
|
|
45
|
+
exports: NonNullable<DynamicModule['exports']>;
|
|
46
|
+
controllers: NonNullable<DynamicModule['controllers']>;
|
|
47
|
+
providers: NonNullable<DynamicModule['providers']>;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export interface IYalcBaseStaticModule extends ModuleMetadata {
|
|
51
|
+
imports: NonNullable<DynamicModule['imports']>;
|
|
52
|
+
exports: NonNullable<DynamicModule['exports']>;
|
|
53
|
+
controllers: NonNullable<DynamicModule['controllers']>;
|
|
54
|
+
providers: NonNullable<DynamicModule['providers']>;
|
|
55
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import * as common from '@nestjs/common';
|
|
2
|
+
export declare class BaseAppService {
|
|
3
|
+
protected logger: common.LoggerService;
|
|
4
|
+
constructor(logger: common.LoggerService);
|
|
5
|
+
getHello(appName: string): string;
|
|
6
|
+
handleBeforeAllRoutes(context: common.ExecutionContext): void;
|
|
7
|
+
}
|