@nest-yalc-2/app 2.2.1 → 2.2.4
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/package.json +10 -2
- package/src/__tests__/app-bootstrap-base.helper.spec.ts +86 -38
- package/src/__tests__/app-bootstrap-standalone.helper.spec.ts +35 -35
- package/src/__tests__/app-bootstrap.helper.spec.ts +162 -98
- package/src/__tests__/app-config.service.spec.ts +55 -55
- package/src/__tests__/app-context.service.spec.ts +28 -28
- package/src/__tests__/app.events.spec.ts +12 -12
- package/src/__tests__/app.helper.spec.ts +71 -204
- package/src/__tests__/base-app-module.helper.spec.ts +295 -231
- package/src/__tests__/base-app.controller.spec.ts +90 -90
- package/src/__tests__/base-app.service.spec.ts +39 -39
- package/src/__tests__/cls.module.spec.ts +33 -33
- package/src/__tests__/life-cycle-handler.service.spec.ts +57 -48
- package/src/__tests__/unwrap-result.interceptor.spec.ts +52 -52
- package/src/app-bootstrap-base.helper.ts +206 -206
- package/src/app-bootstrap-standalone.helper.ts +67 -67
- package/src/app-bootstrap.helper.js +9 -9
- package/src/app-bootstrap.helper.ts +279 -279
- package/src/app-config.service.ts +61 -61
- package/src/app-context.module.ts +6 -6
- package/src/app-context.service.ts +16 -16
- package/src/app-imports.factory.js +2 -2
- package/src/app-imports.factory.js.map +1 -1
- package/src/app-imports.factory.ts +321 -321
- package/src/app.events.ts +8 -8
- package/src/app.helper.ts +73 -73
- package/src/base-app-module.helper.d.ts +2 -2
- package/src/base-app-module.helper.js +8 -5
- package/src/base-app-module.helper.js.map +1 -1
- package/src/base-app-module.helper.ts +457 -453
- package/src/base-app.controller.ts +51 -51
- package/src/base-app.interface.ts +55 -55
- package/src/base-app.service.ts +33 -33
- package/src/cls.module.d.ts +2 -1
- package/src/cls.module.js.map +1 -1
- package/src/cls.module.ts +76 -75
- package/src/conf.type.ts +22 -22
- package/src/def.const.ts +33 -33
- package/src/global.enum.ts +6 -6
- package/src/life-cycle-handler.service.ts +66 -66
- package/src/unwrap-result.interceptor.ts +32 -32
- package/src/yalc-controller.interface.ts +6 -6
|
@@ -1,51 +1,51 @@
|
|
|
1
|
-
/* istanbul ignore file */
|
|
2
|
-
import { IServiceConf } from '@nest-yalc-2/app/conf.type.js';
|
|
3
|
-
import { CURAPP_CONF_ALIAS } from '@nest-yalc-2/app/def.const.js';
|
|
4
|
-
import { Controller, Get } from '@nestjs/common';
|
|
5
|
-
import { ConfigService } from '@nestjs/config';
|
|
6
|
-
import { BaseAppService } from './base-app.service.js';
|
|
7
|
-
import { ApiOperation } from '@nestjs/swagger';
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Application controller
|
|
11
|
-
*/
|
|
12
|
-
/* istanbul ignore next */
|
|
13
|
-
@Controller()
|
|
14
|
-
export abstract class BaseAppController {
|
|
15
|
-
/* istanbul ignore next */
|
|
16
|
-
constructor(
|
|
17
|
-
/* istanbul ignore next */
|
|
18
|
-
protected readonly appService: BaseAppService,
|
|
19
|
-
/* istanbul ignore next */
|
|
20
|
-
protected readonly configService: ConfigService,
|
|
21
|
-
) {}
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* Expose the getHello method
|
|
25
|
-
*/
|
|
26
|
-
/* istanbul ignore next */
|
|
27
|
-
@Get()
|
|
28
|
-
getHello(): string {
|
|
29
|
-
const conf = this.configService.get<IServiceConf>(CURAPP_CONF_ALIAS);
|
|
30
|
-
return this.appService.getHello(conf?.appName || 'no-name');
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Only for dev purpose
|
|
35
|
-
*/
|
|
36
|
-
/* istanbul ignore next */
|
|
37
|
-
@ApiOperation({
|
|
38
|
-
description:
|
|
39
|
-
'Shutdown the application, only available on development and test environment',
|
|
40
|
-
})
|
|
41
|
-
/* istanbul ignore next */
|
|
42
|
-
@Get('shutdown')
|
|
43
|
-
shutdown() {
|
|
44
|
-
const conf = this.configService.get<IServiceConf>(CURAPP_CONF_ALIAS);
|
|
45
|
-
if (conf && (conf.isDev || conf.isTest)) {
|
|
46
|
-
// eslint-disable-next-line no-console
|
|
47
|
-
console.log(`Bye bye!`);
|
|
48
|
-
process.exit(0);
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
}
|
|
1
|
+
/* istanbul ignore file */
|
|
2
|
+
import { IServiceConf } from '@nest-yalc-2/app/conf.type.js';
|
|
3
|
+
import { CURAPP_CONF_ALIAS } from '@nest-yalc-2/app/def.const.js';
|
|
4
|
+
import { Controller, Get } from '@nestjs/common';
|
|
5
|
+
import { ConfigService } from '@nestjs/config';
|
|
6
|
+
import { BaseAppService } from './base-app.service.js';
|
|
7
|
+
import { ApiOperation } from '@nestjs/swagger';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Application controller
|
|
11
|
+
*/
|
|
12
|
+
/* istanbul ignore next */
|
|
13
|
+
@Controller()
|
|
14
|
+
export abstract class BaseAppController {
|
|
15
|
+
/* istanbul ignore next */
|
|
16
|
+
constructor(
|
|
17
|
+
/* istanbul ignore next */
|
|
18
|
+
protected readonly appService: BaseAppService,
|
|
19
|
+
/* istanbul ignore next */
|
|
20
|
+
protected readonly configService: ConfigService,
|
|
21
|
+
) {}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Expose the getHello method
|
|
25
|
+
*/
|
|
26
|
+
/* istanbul ignore next */
|
|
27
|
+
@Get()
|
|
28
|
+
getHello(): string {
|
|
29
|
+
const conf = this.configService.get<IServiceConf>(CURAPP_CONF_ALIAS);
|
|
30
|
+
return this.appService.getHello(conf?.appName || 'no-name');
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Only for dev purpose
|
|
35
|
+
*/
|
|
36
|
+
/* istanbul ignore next */
|
|
37
|
+
@ApiOperation({
|
|
38
|
+
description:
|
|
39
|
+
'Shutdown the application, only available on development and test environment',
|
|
40
|
+
})
|
|
41
|
+
/* istanbul ignore next */
|
|
42
|
+
@Get('shutdown')
|
|
43
|
+
shutdown() {
|
|
44
|
+
const conf = this.configService.get<IServiceConf>(CURAPP_CONF_ALIAS);
|
|
45
|
+
if (conf && (conf.isDev || conf.isTest)) {
|
|
46
|
+
// eslint-disable-next-line no-console
|
|
47
|
+
console.log(`Bye bye!`);
|
|
48
|
+
process.exit(0);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
@@ -1,55 +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 '@node-yalc/types/globals.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
|
-
}
|
|
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 '@node-yalc/types/globals.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
|
+
}
|
package/src/base-app.service.ts
CHANGED
|
@@ -1,33 +1,33 @@
|
|
|
1
|
-
/* istanbul ignore file */
|
|
2
|
-
import * as common from '@nestjs/common';
|
|
3
|
-
import { OnEvent } from '@nestjs/event-emitter';
|
|
4
|
-
import { APP_LOGGER_SERVICE } from '@nest-yalc-2/app/def.const.js';
|
|
5
|
-
import { AppEvents } from './app.events.js';
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Application service
|
|
9
|
-
*/
|
|
10
|
-
/* istanbul ignore next */
|
|
11
|
-
@common.Injectable()
|
|
12
|
-
export class BaseAppService {
|
|
13
|
-
constructor(
|
|
14
|
-
/* istanbul ignore next */
|
|
15
|
-
@common.Inject(APP_LOGGER_SERVICE) protected logger: common.LoggerService,
|
|
16
|
-
) {}
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Hello world function
|
|
20
|
-
*/
|
|
21
|
-
getHello(appName: string): string {
|
|
22
|
-
return `Hello World from ${appName}!`;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
@OnEvent(AppEvents.BEFORE_ALL_ROUTES)
|
|
26
|
-
/* istanbul ignore next */
|
|
27
|
-
handleBeforeAllRoutes(context: common.ExecutionContext) {
|
|
28
|
-
const handlerName = context.getHandler().name;
|
|
29
|
-
// exclude special handlers such as _service
|
|
30
|
-
if (!handlerName.startsWith('_') && handlerName.includes('_'))
|
|
31
|
-
this.logger.debug?.(`Running Handler: ${handlerName}`);
|
|
32
|
-
}
|
|
33
|
-
}
|
|
1
|
+
/* istanbul ignore file */
|
|
2
|
+
import * as common from '@nestjs/common';
|
|
3
|
+
import { OnEvent } from '@nestjs/event-emitter';
|
|
4
|
+
import { APP_LOGGER_SERVICE } from '@nest-yalc-2/app/def.const.js';
|
|
5
|
+
import { AppEvents } from './app.events.js';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Application service
|
|
9
|
+
*/
|
|
10
|
+
/* istanbul ignore next */
|
|
11
|
+
@common.Injectable()
|
|
12
|
+
export class BaseAppService {
|
|
13
|
+
constructor(
|
|
14
|
+
/* istanbul ignore next */
|
|
15
|
+
@common.Inject(APP_LOGGER_SERVICE) protected logger: common.LoggerService,
|
|
16
|
+
) {}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Hello world function
|
|
20
|
+
*/
|
|
21
|
+
getHello(appName: string): string {
|
|
22
|
+
return `Hello World from ${appName}!`;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
@OnEvent(AppEvents.BEFORE_ALL_ROUTES)
|
|
26
|
+
/* istanbul ignore next */
|
|
27
|
+
handleBeforeAllRoutes(context: common.ExecutionContext) {
|
|
28
|
+
const handlerName = context.getHandler().name;
|
|
29
|
+
// exclude special handlers such as _service
|
|
30
|
+
if (!handlerName.startsWith('_') && handlerName.includes('_'))
|
|
31
|
+
this.logger.debug?.(`Running Handler: ${handlerName}`);
|
|
32
|
+
}
|
|
33
|
+
}
|
package/src/cls.module.d.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { ClsService } from 'nestjs-cls';
|
|
2
|
-
import { ClsStore } from 'nestjs-cls
|
|
2
|
+
import { ClsStore } from 'nestjs-cls';
|
|
3
3
|
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
4
4
|
export interface IYalcCls extends ClsStore {
|
|
5
5
|
headers: Record<string, string>;
|
|
6
|
+
[key: symbol]: any;
|
|
6
7
|
}
|
|
7
8
|
export declare class YalcGlobalClsService<TCls extends IYalcCls = IYalcCls> extends ClsService<TCls> {
|
|
8
9
|
}
|
package/src/cls.module.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cls.module.js","sourceRoot":"","sources":["cls.module.ts"],"names":[],"mappings":";AACA,OAAO,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAC;AAExC,OAAO,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAEnD,OAAO,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AACrD,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"cls.module.js","sourceRoot":"","sources":["cls.module.ts"],"names":[],"mappings":";AACA,OAAO,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAC;AAExC,OAAO,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAEnD,OAAO,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AACrD,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAUzC,MAAM,OAAO,oBAEX,SAAQ,UAAgB;CAAG;AAS7B,MAAM,OAAO,cAEX,SAAQ,iBAA2B;CAAG;AA8CjC,IAAM,aAAa,GAAnB,MAAM,aAAa;CAAG,CAAA;AAAhB,aAAa;IA3CzB,MAAM,CAAC;QACN,OAAO,EAAE;YACP,SAAS,CAAC,OAAO,CAAC;gBAChB,MAAM,EAAE,IAAI;gBACZ,UAAU,EAAE;oBAGV,KAAK,EAAE,IAAI;oBAIX,KAAK,EAAE,CAAC,GAAG,EAAE,GAAG,EAAE,EAAE;wBAElB,GAAG,CAAC,GAAG,CAAC,SAAS,EAAE,GAAG,CAAC,OAAO,CAAC,CAAC;oBAClC,CAAC;oBAKD,UAAU,EAAE,IAAI;oBAEhB,WAAW,EAAE,KAAK,EAAE,GAAmB,EAAE,EAAE;wBAEzC,OAAO,GAAG,CAAC,OAAO,CAAC,cAAc,CAAC,EAAE,QAAQ,EAAE,IAAI,UAAU,EAAE,CAAC;oBACjE,CAAC;iBACF;aACF,CAAC;SACH;QACD,SAAS,EAAE;YAIT;gBACE,OAAO,EAAE,oBAAoB;gBAC7B,WAAW,EAAE,UAAU;aACxB;YACD;gBACE,OAAO,EAAE,cAAc;gBACvB,QAAQ,EAAE,IAAI,cAAc,EAAE;aAC/B;SACF;QACD,OAAO,EAAE,CAAC,oBAAoB,CAAC;KAChC,CAAC;GACW,aAAa,CAAG"}
|
package/src/cls.module.ts
CHANGED
|
@@ -1,75 +1,76 @@
|
|
|
1
|
-
/* istanbul ignore file */
|
|
2
|
-
import { Module } from '@nestjs/common';
|
|
3
|
-
import { FastifyRequest } from 'fastify/types/request.js';
|
|
4
|
-
import { ClsModule, ClsService } from 'nestjs-cls';
|
|
5
|
-
import { ClsStore } from 'nestjs-cls
|
|
6
|
-
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
7
|
-
import { randomUUID } from 'node:crypto';
|
|
8
|
-
|
|
9
|
-
export interface IYalcCls extends ClsStore {
|
|
10
|
-
headers: Record<string, string>;
|
|
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
|
-
|
|
1
|
+
/* istanbul ignore file */
|
|
2
|
+
import { Module } from '@nestjs/common';
|
|
3
|
+
import { FastifyRequest } from 'fastify/types/request.js';
|
|
4
|
+
import { ClsModule, ClsService } from 'nestjs-cls';
|
|
5
|
+
import { ClsStore } from 'nestjs-cls';
|
|
6
|
+
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
7
|
+
import { randomUUID } from 'node:crypto';
|
|
8
|
+
|
|
9
|
+
export interface IYalcCls extends ClsStore {
|
|
10
|
+
headers: Record<string, string>;
|
|
11
|
+
[key: symbol]: any;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Global Cls. This is useful if you want to have a single Cls instance for your entire application.
|
|
16
|
+
*/
|
|
17
|
+
export class YalcGlobalClsService<
|
|
18
|
+
TCls extends IYalcCls = IYalcCls,
|
|
19
|
+
> extends ClsService<TCls> {}
|
|
20
|
+
|
|
21
|
+
export interface IYalcAsyncLocalStorageAls {
|
|
22
|
+
placeholder: string;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Module specific AsyncLocalStorage. This is useful if you want to have multiple AsyncLocalStorage instances for different modules.
|
|
27
|
+
*/
|
|
28
|
+
export class YalcAlsService<
|
|
29
|
+
TStorage extends IYalcAsyncLocalStorageAls = IYalcAsyncLocalStorageAls,
|
|
30
|
+
> extends AsyncLocalStorage<TStorage> {}
|
|
31
|
+
|
|
32
|
+
/* istanbul ignore next */
|
|
33
|
+
@Module({
|
|
34
|
+
imports: [
|
|
35
|
+
ClsModule.forRoot({
|
|
36
|
+
global: true,
|
|
37
|
+
middleware: {
|
|
38
|
+
// automatically mount the
|
|
39
|
+
// ClsMiddleware for all routes
|
|
40
|
+
mount: true,
|
|
41
|
+
// and use the setup method to
|
|
42
|
+
// provide default store values.
|
|
43
|
+
/* istanbul ignore next */
|
|
44
|
+
setup: (cls, req) => {
|
|
45
|
+
/* istanbul ignore next */
|
|
46
|
+
cls.set('headers', req.headers);
|
|
47
|
+
},
|
|
48
|
+
/**
|
|
49
|
+
* The generation of the id uses setIfUndefined internally
|
|
50
|
+
* So it won't be overwritten if it's already set (e.g. http local strategy case)
|
|
51
|
+
*/
|
|
52
|
+
generateId: true,
|
|
53
|
+
/* istanbul ignore next */
|
|
54
|
+
idGenerator: async (req: FastifyRequest) => {
|
|
55
|
+
/* istanbul ignore next */
|
|
56
|
+
return req.headers['X-Request-Id']?.toString() ?? randomUUID();
|
|
57
|
+
},
|
|
58
|
+
},
|
|
59
|
+
}),
|
|
60
|
+
],
|
|
61
|
+
providers: [
|
|
62
|
+
/**
|
|
63
|
+
* @see https://papooch.github.io/nestjs-cls/features-and-use-cases/type-safety-and-type-inference#using-a-custom-provider
|
|
64
|
+
*/
|
|
65
|
+
{
|
|
66
|
+
provide: YalcGlobalClsService,
|
|
67
|
+
useExisting: ClsService,
|
|
68
|
+
},
|
|
69
|
+
{
|
|
70
|
+
provide: YalcAlsService,
|
|
71
|
+
useValue: new YalcAlsService(),
|
|
72
|
+
},
|
|
73
|
+
],
|
|
74
|
+
exports: [YalcGlobalClsService],
|
|
75
|
+
})
|
|
76
|
+
export class YalcClsModule {}
|
package/src/conf.type.ts
CHANGED
|
@@ -1,22 +1,22 @@
|
|
|
1
|
-
import type { LogLevel } from '@nestjs/common';
|
|
2
|
-
import type { LoggerTypeEnum } from '@node-yalc/logger/logger.enum.js';
|
|
3
|
-
|
|
4
|
-
export interface IServiceConf {
|
|
5
|
-
appName: string;
|
|
6
|
-
loggerType: LoggerTypeEnum | string;
|
|
7
|
-
logLevels: LogLevel[];
|
|
8
|
-
/** You can specialize log levels per each logger context. By default logLevels is used instead */
|
|
9
|
-
logContextLevels?: { [key: string]: LogLevel[] };
|
|
10
|
-
domain?: string;
|
|
11
|
-
|
|
12
|
-
host: string;
|
|
13
|
-
port: number;
|
|
14
|
-
isDev: boolean;
|
|
15
|
-
isTest: boolean;
|
|
16
|
-
isPipeline: boolean;
|
|
17
|
-
isProduction: boolean;
|
|
18
|
-
env: typeof process.env.NODE_ENV;
|
|
19
|
-
apiPrefix?: string;
|
|
20
|
-
basePath?: string;
|
|
21
|
-
operationPrefix?: string;
|
|
22
|
-
}
|
|
1
|
+
import type { LogLevel } from '@nestjs/common';
|
|
2
|
+
import type { LoggerTypeEnum } from '@node-yalc/logger/logger.enum.js';
|
|
3
|
+
|
|
4
|
+
export interface IServiceConf {
|
|
5
|
+
appName: string;
|
|
6
|
+
loggerType: LoggerTypeEnum | string;
|
|
7
|
+
logLevels: LogLevel[];
|
|
8
|
+
/** You can specialize log levels per each logger context. By default logLevels is used instead */
|
|
9
|
+
logContextLevels?: { [key: string]: LogLevel[] };
|
|
10
|
+
domain?: string;
|
|
11
|
+
|
|
12
|
+
host: string;
|
|
13
|
+
port: number;
|
|
14
|
+
isDev: boolean;
|
|
15
|
+
isTest: boolean;
|
|
16
|
+
isPipeline: boolean;
|
|
17
|
+
isProduction: boolean;
|
|
18
|
+
env: typeof process.env.NODE_ENV;
|
|
19
|
+
apiPrefix?: string;
|
|
20
|
+
basePath?: string;
|
|
21
|
+
operationPrefix?: string;
|
|
22
|
+
}
|
package/src/def.const.ts
CHANGED
|
@@ -1,33 +1,33 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Exported by the app modules and used within the apps
|
|
3
|
-
* NOTE: a system can run multiple apps at the same time
|
|
4
|
-
*/
|
|
5
|
-
export const APP_LOGGER_SERVICE = 'APP_LOGGER_SERVICE';
|
|
6
|
-
export const APP_EVENT_SERVICE = 'APP_EVENT_SERVICE';
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Globally exported from the main yalc module
|
|
10
|
-
*/
|
|
11
|
-
export const SYSTEM_EVENT_SERVICE = 'SYSTEM_EVENT_SERVICE';
|
|
12
|
-
export const SYSTEM_LOGGER_SERVICE = 'SYSTEM_LOGGER_SERVICE';
|
|
13
|
-
|
|
14
|
-
export const CURAPP_CONF_ALIAS = 'conf';
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Provider token to access the alias value of the app
|
|
18
|
-
*/
|
|
19
|
-
export const APP_ALIAS_TOKEN = 'APP_ALIAS_TOKEN';
|
|
20
|
-
/**
|
|
21
|
-
* Provider token to access the option values of the app
|
|
22
|
-
*/
|
|
23
|
-
export const APP_OPTION_TOKEN = 'APP_OPTION_TOKEN';
|
|
24
|
-
/**
|
|
25
|
-
* Provider token to access the option values of the current module
|
|
26
|
-
*/
|
|
27
|
-
export const MODULE_OPTION_TOKEN = 'MODULE_OPTION_TOKEN';
|
|
28
|
-
/**
|
|
29
|
-
* Provider token to access the alias value of the current module
|
|
30
|
-
*/
|
|
31
|
-
export const MODULE_ALIAS_TOKEN = 'MODULE_ALIAS_TOKEN';
|
|
32
|
-
|
|
33
|
-
export const MAIN_APP_CONFIG_SERVICE = 'MAIN_APP_CONFIG_SERVICE';
|
|
1
|
+
/**
|
|
2
|
+
* Exported by the app modules and used within the apps
|
|
3
|
+
* NOTE: a system can run multiple apps at the same time
|
|
4
|
+
*/
|
|
5
|
+
export const APP_LOGGER_SERVICE = 'APP_LOGGER_SERVICE';
|
|
6
|
+
export const APP_EVENT_SERVICE = 'APP_EVENT_SERVICE';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Globally exported from the main yalc module
|
|
10
|
+
*/
|
|
11
|
+
export const SYSTEM_EVENT_SERVICE = 'SYSTEM_EVENT_SERVICE';
|
|
12
|
+
export const SYSTEM_LOGGER_SERVICE = 'SYSTEM_LOGGER_SERVICE';
|
|
13
|
+
|
|
14
|
+
export const CURAPP_CONF_ALIAS = 'conf';
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Provider token to access the alias value of the app
|
|
18
|
+
*/
|
|
19
|
+
export const APP_ALIAS_TOKEN = 'APP_ALIAS_TOKEN';
|
|
20
|
+
/**
|
|
21
|
+
* Provider token to access the option values of the app
|
|
22
|
+
*/
|
|
23
|
+
export const APP_OPTION_TOKEN = 'APP_OPTION_TOKEN';
|
|
24
|
+
/**
|
|
25
|
+
* Provider token to access the option values of the current module
|
|
26
|
+
*/
|
|
27
|
+
export const MODULE_OPTION_TOKEN = 'MODULE_OPTION_TOKEN';
|
|
28
|
+
/**
|
|
29
|
+
* Provider token to access the alias value of the current module
|
|
30
|
+
*/
|
|
31
|
+
export const MODULE_ALIAS_TOKEN = 'MODULE_ALIAS_TOKEN';
|
|
32
|
+
|
|
33
|
+
export const MAIN_APP_CONFIG_SERVICE = 'MAIN_APP_CONFIG_SERVICE';
|
package/src/global.enum.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
export enum NODE_ENV {
|
|
2
|
-
DEVELOPMENT = 'development',
|
|
3
|
-
PRODUCTION = 'production',
|
|
4
|
-
TEST = 'test',
|
|
5
|
-
PIPELINE = 'pipeline',
|
|
6
|
-
}
|
|
1
|
+
export enum NODE_ENV {
|
|
2
|
+
DEVELOPMENT = 'development',
|
|
3
|
+
PRODUCTION = 'production',
|
|
4
|
+
TEST = 'test',
|
|
5
|
+
PIPELINE = 'pipeline',
|
|
6
|
+
}
|