@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,78 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
19
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
20
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
21
|
+
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;
|
|
22
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
23
|
+
};
|
|
24
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
25
|
+
var ownKeys = function(o) {
|
|
26
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
27
|
+
var ar = [];
|
|
28
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
29
|
+
return ar;
|
|
30
|
+
};
|
|
31
|
+
return ownKeys(o);
|
|
32
|
+
};
|
|
33
|
+
return function (mod) {
|
|
34
|
+
if (mod && mod.__esModule) return mod;
|
|
35
|
+
var result = {};
|
|
36
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
37
|
+
__setModuleDefault(result, mod);
|
|
38
|
+
return result;
|
|
39
|
+
};
|
|
40
|
+
})();
|
|
41
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
42
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
43
|
+
};
|
|
44
|
+
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
45
|
+
return function (target, key) { decorator(target, key, paramIndex); }
|
|
46
|
+
};
|
|
47
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
48
|
+
exports.BaseAppService = void 0;
|
|
49
|
+
const common = __importStar(require("@nestjs/common"));
|
|
50
|
+
const event_emitter_1 = require("@nestjs/event-emitter");
|
|
51
|
+
const def_const_js_1 = require("@nest-yalc-2/app/def.const.js");
|
|
52
|
+
const app_events_js_1 = require("./app.events.js");
|
|
53
|
+
let BaseAppService = class BaseAppService {
|
|
54
|
+
constructor(logger) {
|
|
55
|
+
this.logger = logger;
|
|
56
|
+
}
|
|
57
|
+
getHello(appName) {
|
|
58
|
+
return `Hello World from ${appName}!`;
|
|
59
|
+
}
|
|
60
|
+
handleBeforeAllRoutes(context) {
|
|
61
|
+
const handlerName = context.getHandler().name;
|
|
62
|
+
if (!handlerName.startsWith('_') && handlerName.includes('_'))
|
|
63
|
+
this.logger.debug?.(`Running Handler: ${handlerName}`);
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
exports.BaseAppService = BaseAppService;
|
|
67
|
+
__decorate([
|
|
68
|
+
(0, event_emitter_1.OnEvent)(app_events_js_1.AppEvents.BEFORE_ALL_ROUTES),
|
|
69
|
+
__metadata("design:type", Function),
|
|
70
|
+
__metadata("design:paramtypes", [Object]),
|
|
71
|
+
__metadata("design:returntype", void 0)
|
|
72
|
+
], BaseAppService.prototype, "handleBeforeAllRoutes", null);
|
|
73
|
+
exports.BaseAppService = BaseAppService = __decorate([
|
|
74
|
+
common.Injectable(),
|
|
75
|
+
__param(0, common.Inject(def_const_js_1.APP_LOGGER_SERVICE)),
|
|
76
|
+
__metadata("design:paramtypes", [Object])
|
|
77
|
+
], BaseAppService);
|
|
78
|
+
//# sourceMappingURL=base-app.service.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"base-app.service.js","sourceRoot":"","sources":["base-app.service.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,uDAAyC;AACzC,yDAAgD;AAChD,gEAAmE;AACnE,mDAA4C;AAMrC,IAAM,cAAc,GAApB,MAAM,cAAc;IACzB,YAC+C,MAA4B;QAA5B,WAAM,GAAN,MAAM,CAAsB;IACxE,CAAC;IAKJ,QAAQ,CAAC,OAAe;QACtB,OAAO,oBAAoB,OAAO,GAAG,CAAC;IACxC,CAAC;IAGD,qBAAqB,CAAC,OAAgC;QACpD,MAAM,WAAW,GAAG,OAAO,CAAC,UAAU,EAAE,CAAC,IAAI,CAAC;QAE9C,IAAI,CAAC,WAAW,CAAC,UAAU,CAAC,GAAG,CAAC,IAAI,WAAW,CAAC,QAAQ,CAAC,GAAG,CAAC;YAC3D,IAAI,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC,oBAAoB,WAAW,EAAE,CAAC,CAAC;IAC3D,CAAC;CACF,CAAA;AAnBY,wCAAc;AAazB;IADC,IAAA,uBAAO,EAAC,yBAAS,CAAC,iBAAiB,CAAC;;;;2DAMpC;yBAlBU,cAAc;IAD1B,MAAM,CAAC,UAAU,EAAE;IAGf,WAAA,MAAM,CAAC,MAAM,CAAC,iCAAkB,CAAC,CAAA;;GAFzB,cAAc,CAmB1B"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import * as common from '@nestjs/common';
|
|
2
|
+
import { OnEvent } from '@nestjs/event-emitter';
|
|
3
|
+
import { APP_LOGGER_SERVICE } from '@nest-yalc-2/app/def.const.js';
|
|
4
|
+
import { AppEvents } from './app.events.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Application service
|
|
8
|
+
*/
|
|
9
|
+
@common.Injectable()
|
|
10
|
+
export class BaseAppService {
|
|
11
|
+
constructor(
|
|
12
|
+
@common.Inject(APP_LOGGER_SERVICE) protected logger: common.LoggerService,
|
|
13
|
+
) {}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Hello world function
|
|
17
|
+
*/
|
|
18
|
+
getHello(appName: string): string {
|
|
19
|
+
return `Hello World from ${appName}!`;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
@OnEvent(AppEvents.BEFORE_ALL_ROUTES)
|
|
23
|
+
handleBeforeAllRoutes(context: common.ExecutionContext) {
|
|
24
|
+
const handlerName = context.getHandler().name;
|
|
25
|
+
// exclude special handlers such as _service
|
|
26
|
+
if (!handlerName.startsWith('_') && handlerName.includes('_'))
|
|
27
|
+
this.logger.debug?.(`Running Handler: ${handlerName}`);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { ClsService } from 'nestjs-cls';
|
|
2
|
+
import { ClsStore } from 'nestjs-cls/dist/src/lib/cls.options.js';
|
|
3
|
+
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
4
|
+
export interface IYalcCls extends ClsStore {
|
|
5
|
+
headers: Record<string, string>;
|
|
6
|
+
}
|
|
7
|
+
export declare class YalcGlobalClsService<TCls extends IYalcCls = IYalcCls> extends ClsService<TCls> {
|
|
8
|
+
}
|
|
9
|
+
export interface IYalcAsyncLocalStorageAls {
|
|
10
|
+
placeholder: string;
|
|
11
|
+
}
|
|
12
|
+
export declare class YalcAlsService<TStorage extends IYalcAsyncLocalStorageAls = IYalcAsyncLocalStorageAls> extends AsyncLocalStorage<TStorage> {
|
|
13
|
+
}
|
|
14
|
+
export declare class YalcClsModule {
|
|
15
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
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
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.YalcClsModule = exports.YalcAlsService = exports.YalcGlobalClsService = void 0;
|
|
10
|
+
const common_1 = require("@nestjs/common");
|
|
11
|
+
const nestjs_cls_1 = require("nestjs-cls");
|
|
12
|
+
const node_async_hooks_1 = require("node:async_hooks");
|
|
13
|
+
const node_crypto_1 = require("node:crypto");
|
|
14
|
+
class YalcGlobalClsService extends nestjs_cls_1.ClsService {
|
|
15
|
+
}
|
|
16
|
+
exports.YalcGlobalClsService = YalcGlobalClsService;
|
|
17
|
+
class YalcAlsService extends node_async_hooks_1.AsyncLocalStorage {
|
|
18
|
+
}
|
|
19
|
+
exports.YalcAlsService = YalcAlsService;
|
|
20
|
+
let YalcClsModule = class YalcClsModule {
|
|
21
|
+
};
|
|
22
|
+
exports.YalcClsModule = YalcClsModule;
|
|
23
|
+
exports.YalcClsModule = YalcClsModule = __decorate([
|
|
24
|
+
(0, common_1.Module)({
|
|
25
|
+
imports: [
|
|
26
|
+
nestjs_cls_1.ClsModule.forRoot({
|
|
27
|
+
global: true,
|
|
28
|
+
middleware: {
|
|
29
|
+
mount: true,
|
|
30
|
+
setup: (cls, req) => {
|
|
31
|
+
cls.set('headers', req.headers);
|
|
32
|
+
},
|
|
33
|
+
generateId: true,
|
|
34
|
+
idGenerator: async (req) => req.headers['X-Request-Id']?.toString() ?? (0, node_crypto_1.randomUUID)(),
|
|
35
|
+
},
|
|
36
|
+
}),
|
|
37
|
+
],
|
|
38
|
+
providers: [
|
|
39
|
+
{
|
|
40
|
+
provide: YalcGlobalClsService,
|
|
41
|
+
useExisting: nestjs_cls_1.ClsService,
|
|
42
|
+
},
|
|
43
|
+
{
|
|
44
|
+
provide: YalcAlsService,
|
|
45
|
+
useValue: new YalcAlsService(),
|
|
46
|
+
},
|
|
47
|
+
],
|
|
48
|
+
exports: [YalcGlobalClsService],
|
|
49
|
+
})
|
|
50
|
+
], YalcClsModule);
|
|
51
|
+
//# sourceMappingURL=cls.module.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cls.module.js","sourceRoot":"","sources":["cls.module.ts"],"names":[],"mappings":";;;;;;;;;AAAA,2CAAwC;AAExC,2CAAmD;AAEnD,uDAAqD;AACrD,6CAAyC;AASzC,MAAa,oBAEX,SAAQ,uBAAgB;CAAG;AAF7B,oDAE6B;AAS7B,MAAa,cAEX,SAAQ,oCAA2B;CAAG;AAFxC,wCAEwC;AAwCjC,IAAM,aAAa,GAAnB,MAAM,aAAa;CAAG,CAAA;AAAhB,sCAAa;wBAAb,aAAa;IAtCzB,IAAA,eAAM,EAAC;QACN,OAAO,EAAE;YACP,sBAAS,CAAC,OAAO,CAAC;gBAChB,MAAM,EAAE,IAAI;gBACZ,UAAU,EAAE;oBAGV,KAAK,EAAE,IAAI;oBAGX,KAAK,EAAE,CAAC,GAAG,EAAE,GAAG,EAAE,EAAE;wBAClB,GAAG,CAAC,GAAG,CAAC,SAAS,EAAE,GAAG,CAAC,OAAO,CAAC,CAAC;oBAClC,CAAC;oBAKD,UAAU,EAAE,IAAI;oBAChB,WAAW,EAAE,KAAK,EAAE,GAAmB,EAAE,EAAE,CACzC,GAAG,CAAC,OAAO,CAAC,cAAc,CAAC,EAAE,QAAQ,EAAE,IAAI,IAAA,wBAAU,GAAE;iBAC1D;aACF,CAAC;SACH;QACD,SAAS,EAAE;YAIT;gBACE,OAAO,EAAE,oBAAoB;gBAC7B,WAAW,EAAE,uBAAU;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"}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { Module } from '@nestjs/common';
|
|
2
|
+
import { FastifyRequest } from 'fastify/types/request.js';
|
|
3
|
+
import { ClsModule, ClsService } from 'nestjs-cls';
|
|
4
|
+
import { ClsStore } from 'nestjs-cls/dist/src/lib/cls.options.js';
|
|
5
|
+
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
6
|
+
import { randomUUID } from 'node:crypto';
|
|
7
|
+
|
|
8
|
+
export interface IYalcCls extends ClsStore {
|
|
9
|
+
headers: Record<string, string>;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Global Cls. This is useful if you want to have a single Cls instance for your entire application.
|
|
14
|
+
*/
|
|
15
|
+
export class YalcGlobalClsService<
|
|
16
|
+
TCls extends IYalcCls = IYalcCls,
|
|
17
|
+
> extends ClsService<TCls> {}
|
|
18
|
+
|
|
19
|
+
export interface IYalcAsyncLocalStorageAls {
|
|
20
|
+
placeholder: string;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Module specific AsyncLocalStorage. This is useful if you want to have multiple AsyncLocalStorage instances for different modules.
|
|
25
|
+
*/
|
|
26
|
+
export class YalcAlsService<
|
|
27
|
+
TStorage extends IYalcAsyncLocalStorageAls = IYalcAsyncLocalStorageAls,
|
|
28
|
+
> extends AsyncLocalStorage<TStorage> {}
|
|
29
|
+
|
|
30
|
+
@Module({
|
|
31
|
+
imports: [
|
|
32
|
+
ClsModule.forRoot({
|
|
33
|
+
global: true,
|
|
34
|
+
middleware: {
|
|
35
|
+
// automatically mount the
|
|
36
|
+
// ClsMiddleware for all routes
|
|
37
|
+
mount: true,
|
|
38
|
+
// and use the setup method to
|
|
39
|
+
// provide default store values.
|
|
40
|
+
setup: (cls, req) => {
|
|
41
|
+
cls.set('headers', req.headers);
|
|
42
|
+
},
|
|
43
|
+
/**
|
|
44
|
+
* The generation of the id uses setIfUndefined internally
|
|
45
|
+
* So it won't be overwritten if it's already set (e.g. http local strategy case)
|
|
46
|
+
*/
|
|
47
|
+
generateId: true,
|
|
48
|
+
idGenerator: async (req: FastifyRequest) =>
|
|
49
|
+
req.headers['X-Request-Id']?.toString() ?? randomUUID(),
|
|
50
|
+
},
|
|
51
|
+
}),
|
|
52
|
+
],
|
|
53
|
+
providers: [
|
|
54
|
+
/**
|
|
55
|
+
* @see https://papooch.github.io/nestjs-cls/features-and-use-cases/type-safety-and-type-inference#using-a-custom-provider
|
|
56
|
+
*/
|
|
57
|
+
{
|
|
58
|
+
provide: YalcGlobalClsService,
|
|
59
|
+
useExisting: ClsService,
|
|
60
|
+
},
|
|
61
|
+
{
|
|
62
|
+
provide: YalcAlsService,
|
|
63
|
+
useValue: new YalcAlsService(),
|
|
64
|
+
},
|
|
65
|
+
],
|
|
66
|
+
exports: [YalcGlobalClsService],
|
|
67
|
+
})
|
|
68
|
+
export class YalcClsModule {}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { LogLevel } from '@nestjs/common';
|
|
2
|
+
import type { LoggerTypeEnum } from '@nest-yalc-2/logger/logger.enum.js';
|
|
3
|
+
export interface IServiceConf {
|
|
4
|
+
appName: string;
|
|
5
|
+
loggerType: LoggerTypeEnum | string;
|
|
6
|
+
logLevels: LogLevel[];
|
|
7
|
+
logContextLevels?: {
|
|
8
|
+
[key: string]: LogLevel[];
|
|
9
|
+
};
|
|
10
|
+
domain?: string;
|
|
11
|
+
host: string;
|
|
12
|
+
port: number;
|
|
13
|
+
isDev: boolean;
|
|
14
|
+
isTest: boolean;
|
|
15
|
+
isPipeline: boolean;
|
|
16
|
+
isProduction: boolean;
|
|
17
|
+
env: typeof process.env.NODE_ENV;
|
|
18
|
+
apiPrefix?: string;
|
|
19
|
+
basePath?: string;
|
|
20
|
+
operationPrefix?: string;
|
|
21
|
+
}
|
package/src/conf.type.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"conf.type.js","sourceRoot":"","sources":["conf.type.ts"],"names":[],"mappings":""}
|
package/src/conf.type.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { LogLevel } from '@nestjs/common';
|
|
2
|
+
import type { LoggerTypeEnum } from '@nest-yalc-2/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
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { IDbConfObject } from '@nestjs-yalc/database/conf.interface.js';
|
|
2
|
+
import { getConnectionName } from '@nestjs-yalc/database/conn.helper.js';
|
|
3
|
+
import { ClassType } from '@nestjs-yalc/types/globals.d.js';
|
|
4
|
+
import { envIsTrue } from '@nestjs-yalc/utils/env.helper.js';
|
|
5
|
+
import { EntitySchema } from 'typeorm';
|
|
6
|
+
import { Seeder } from 'typeorm-seeding';
|
|
7
|
+
import { getPrefixedEnv, ENV_PREFIX } from './conf.helper.js';
|
|
8
|
+
|
|
9
|
+
type DbConfigObjectParams = {
|
|
10
|
+
realmAlias?: string;
|
|
11
|
+
entities: Array<ClassType | string | EntitySchema<any>>;
|
|
12
|
+
seeds?: { new (): Seeder }[];
|
|
13
|
+
sourceDir?: string;
|
|
14
|
+
migrationsDir?: string;
|
|
15
|
+
connectionName?: string;
|
|
16
|
+
type: DbType;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export function getRealmDbName(realmName: string, dbName: string) {
|
|
20
|
+
return `${realmName}_${dbName}`;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function getRealmDbConnectionName(realmName: string, dbName: string) {
|
|
24
|
+
return getConnectionName(getRealmDbName(realmName, dbName));
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function realmDbConfFactory({
|
|
28
|
+
realmAlias,
|
|
29
|
+
entities,
|
|
30
|
+
seeds,
|
|
31
|
+
sourceDir,
|
|
32
|
+
migrationsDir,
|
|
33
|
+
connectionName = 'default',
|
|
34
|
+
type,
|
|
35
|
+
}: DbConfigObjectParams) {
|
|
36
|
+
const connName = getRealmDbConnectionName(
|
|
37
|
+
realmAlias ?? 'default',
|
|
38
|
+
connectionName,
|
|
39
|
+
);
|
|
40
|
+
|
|
41
|
+
const dbConf: IDbConfObject = () => {
|
|
42
|
+
const _realmAlias = realmAlias || process.env.AC_API_REALM || undefined;
|
|
43
|
+
const prefix = `${ENV_PREFIX}${type}_`;
|
|
44
|
+
|
|
45
|
+
const dbName = getPrefixedEnv(prefix, 'MYSQL_DB_NAME', _realmAlias);
|
|
46
|
+
|
|
47
|
+
dbConf.dbName = dbName;
|
|
48
|
+
|
|
49
|
+
const noSelDb = envIsTrue(process.env.TYPEORM_NO_SEL_DB || 'false');
|
|
50
|
+
const sync = getPrefixedEnv(prefix, 'TYPEORM_SYNCHRONIZE', _realmAlias);
|
|
51
|
+
const logging = getPrefixedEnv(prefix, 'TYPEORM_LOGGING', _realmAlias);
|
|
52
|
+
const dbPort = getPrefixedEnv(prefix, 'MYSQL_PORT', _realmAlias);
|
|
53
|
+
|
|
54
|
+
const host =
|
|
55
|
+
global.__JEST_DISABLE_DB !== true
|
|
56
|
+
? getPrefixedEnv(prefix, 'MYSQL_HOST', _realmAlias)
|
|
57
|
+
: 'jest-db-disabled';
|
|
58
|
+
|
|
59
|
+
const port = dbPort ? parseInt(dbPort, 10) : 3306;
|
|
60
|
+
const username =
|
|
61
|
+
getPrefixedEnv(prefix, 'MYSQL_USER', _realmAlias) || 'root';
|
|
62
|
+
const password =
|
|
63
|
+
getPrefixedEnv(prefix, 'MYSQL_PASSWORD', _realmAlias) ??
|
|
64
|
+
getPrefixedEnv(prefix, 'MYSQL_ROOT_PASSWORD', _realmAlias);
|
|
65
|
+
|
|
66
|
+
return {
|
|
67
|
+
host,
|
|
68
|
+
port,
|
|
69
|
+
username,
|
|
70
|
+
password,
|
|
71
|
+
type: 'mysql',
|
|
72
|
+
extra: {
|
|
73
|
+
decimalNumbers: true,
|
|
74
|
+
},
|
|
75
|
+
synchronize: envIsTrue(sync || 'false'),
|
|
76
|
+
logging: envIsTrue(logging || 'false'),
|
|
77
|
+
name: connectionName,
|
|
78
|
+
database: noSelDb ? undefined : dbName,
|
|
79
|
+
entities: noSelDb ? undefined : entities,
|
|
80
|
+
seeds,
|
|
81
|
+
factories: sourceDir ? [`${sourceDir}/**/*.factory.{ts,js}`] : undefined,
|
|
82
|
+
migrations: migrationsDir ? [`${migrationsDir}/**/*.{ts,js}`] : undefined,
|
|
83
|
+
cli: {
|
|
84
|
+
migrationsDir,
|
|
85
|
+
},
|
|
86
|
+
};
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
dbConf.connName = connName;
|
|
90
|
+
dbConf.dbName = connName;
|
|
91
|
+
|
|
92
|
+
return dbConf;
|
|
93
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export declare const APP_LOGGER_SERVICE = "APP_LOGGER_SERVICE";
|
|
2
|
+
export declare const APP_EVENT_SERVICE = "APP_EVENT_SERVICE";
|
|
3
|
+
export declare const SYSTEM_EVENT_SERVICE = "SYSTEM_EVENT_SERVICE";
|
|
4
|
+
export declare const SYSTEM_LOGGER_SERVICE = "SYSTEM_LOGGER_SERVICE";
|
|
5
|
+
export declare const CURAPP_CONF_ALIAS = "conf";
|
|
6
|
+
export declare const APP_ALIAS_TOKEN = "APP_ALIAS_TOKEN";
|
|
7
|
+
export declare const APP_OPTION_TOKEN = "APP_OPTION_TOKEN";
|
|
8
|
+
export declare const MODULE_OPTION_TOKEN = "MODULE_OPTION_TOKEN";
|
|
9
|
+
export declare const MODULE_ALIAS_TOKEN = "MODULE_ALIAS_TOKEN";
|
|
10
|
+
export declare const MAIN_APP_CONFIG_SERVICE = "MAIN_APP_CONFIG_SERVICE";
|
package/src/def.const.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MAIN_APP_CONFIG_SERVICE = exports.MODULE_ALIAS_TOKEN = exports.MODULE_OPTION_TOKEN = exports.APP_OPTION_TOKEN = exports.APP_ALIAS_TOKEN = exports.CURAPP_CONF_ALIAS = exports.SYSTEM_LOGGER_SERVICE = exports.SYSTEM_EVENT_SERVICE = exports.APP_EVENT_SERVICE = exports.APP_LOGGER_SERVICE = void 0;
|
|
4
|
+
exports.APP_LOGGER_SERVICE = 'APP_LOGGER_SERVICE';
|
|
5
|
+
exports.APP_EVENT_SERVICE = 'APP_EVENT_SERVICE';
|
|
6
|
+
exports.SYSTEM_EVENT_SERVICE = 'SYSTEM_EVENT_SERVICE';
|
|
7
|
+
exports.SYSTEM_LOGGER_SERVICE = 'SYSTEM_LOGGER_SERVICE';
|
|
8
|
+
exports.CURAPP_CONF_ALIAS = 'conf';
|
|
9
|
+
exports.APP_ALIAS_TOKEN = 'APP_ALIAS_TOKEN';
|
|
10
|
+
exports.APP_OPTION_TOKEN = 'APP_OPTION_TOKEN';
|
|
11
|
+
exports.MODULE_OPTION_TOKEN = 'MODULE_OPTION_TOKEN';
|
|
12
|
+
exports.MODULE_ALIAS_TOKEN = 'MODULE_ALIAS_TOKEN';
|
|
13
|
+
exports.MAIN_APP_CONFIG_SERVICE = 'MAIN_APP_CONFIG_SERVICE';
|
|
14
|
+
//# sourceMappingURL=def.const.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"def.const.js","sourceRoot":"","sources":["def.const.ts"],"names":[],"mappings":";;;AAIa,QAAA,kBAAkB,GAAG,oBAAoB,CAAC;AAC1C,QAAA,iBAAiB,GAAG,mBAAmB,CAAC;AAKxC,QAAA,oBAAoB,GAAG,sBAAsB,CAAC;AAC9C,QAAA,qBAAqB,GAAG,uBAAuB,CAAC;AAEhD,QAAA,iBAAiB,GAAG,MAAM,CAAC;AAK3B,QAAA,eAAe,GAAG,iBAAiB,CAAC;AAIpC,QAAA,gBAAgB,GAAG,kBAAkB,CAAC;AAItC,QAAA,mBAAmB,GAAG,qBAAqB,CAAC;AAI5C,QAAA,kBAAkB,GAAG,oBAAoB,CAAC;AAE1C,QAAA,uBAAuB,GAAG,yBAAyB,CAAC"}
|
package/src/def.const.ts
ADDED
|
@@ -0,0 +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';
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.NODE_ENV = void 0;
|
|
4
|
+
var NODE_ENV;
|
|
5
|
+
(function (NODE_ENV) {
|
|
6
|
+
NODE_ENV["DEVELOPMENT"] = "development";
|
|
7
|
+
NODE_ENV["PRODUCTION"] = "production";
|
|
8
|
+
NODE_ENV["TEST"] = "test";
|
|
9
|
+
NODE_ENV["PIPELINE"] = "pipeline";
|
|
10
|
+
})(NODE_ENV || (exports.NODE_ENV = NODE_ENV = {}));
|
|
11
|
+
//# sourceMappingURL=global.enum.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"global.enum.js","sourceRoot":"","sources":["global.enum.ts"],"names":[],"mappings":";;;AAAA,IAAY,QAKX;AALD,WAAY,QAAQ;IAClB,uCAA2B,CAAA;IAC3B,qCAAyB,CAAA;IACzB,yBAAa,CAAA;IACb,iCAAqB,CAAA;AACvB,CAAC,EALW,QAAQ,wBAAR,QAAQ,QAKnB"}
|
package/src/index.d.ts
ADDED
|
File without changes
|
package/src/index.js
ADDED
package/src/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":""}
|
package/src/index.ts
ADDED
|
File without changes
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { type ImprovedLoggerService } from '@nest-yalc-2/logger/logger-abstract.service.js';
|
|
2
|
+
import { OnModuleInit, OnModuleDestroy } from '@nestjs/common';
|
|
3
|
+
import { type IYalcBaseAppOptions } from './base-app.interface.js';
|
|
4
|
+
import { AppContextService } from './app-context.service.js';
|
|
5
|
+
export declare class LifeCycleHandler implements OnModuleDestroy, OnModuleInit {
|
|
6
|
+
private readonly logger;
|
|
7
|
+
private readonly moduleAlias;
|
|
8
|
+
private readonly appContextService;
|
|
9
|
+
private readonly options?;
|
|
10
|
+
constructor(logger: ImprovedLoggerService, moduleAlias: string, appContextService: AppContextService, options?: IYalcBaseAppOptions | undefined);
|
|
11
|
+
onModuleInit(): void;
|
|
12
|
+
onModuleDestroy(): void;
|
|
13
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
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
|
+
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
12
|
+
return function (target, key) { decorator(target, key, paramIndex); }
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.LifeCycleHandler = void 0;
|
|
16
|
+
const common_1 = require("@nestjs/common");
|
|
17
|
+
const def_const_js_1 = require("./def.const.js");
|
|
18
|
+
const app_context_service_js_1 = require("./app-context.service.js");
|
|
19
|
+
let LifeCycleHandler = class LifeCycleHandler {
|
|
20
|
+
constructor(logger, moduleAlias, appContextService, options) {
|
|
21
|
+
this.logger = logger;
|
|
22
|
+
this.moduleAlias = moduleAlias;
|
|
23
|
+
this.appContextService = appContextService;
|
|
24
|
+
this.options = options;
|
|
25
|
+
this.logger.debug?.(`====================== Init ${this.moduleAlias} ======================`);
|
|
26
|
+
if (this.options?.skipDuplicateAppCheck !== true &&
|
|
27
|
+
this.appContextService.initializedApps.has(this.moduleAlias)) {
|
|
28
|
+
throw new Error(`Cannot initialize the same app (${this.moduleAlias}) twice`);
|
|
29
|
+
}
|
|
30
|
+
this.appContextService.initializedApps.add(this.moduleAlias);
|
|
31
|
+
}
|
|
32
|
+
onModuleInit() { }
|
|
33
|
+
onModuleDestroy() {
|
|
34
|
+
this.logger.debug?.(`====================== Close ${this.moduleAlias} ======================`);
|
|
35
|
+
this.appContextService.initializedApps.delete(this.moduleAlias);
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
exports.LifeCycleHandler = LifeCycleHandler;
|
|
39
|
+
exports.LifeCycleHandler = LifeCycleHandler = __decorate([
|
|
40
|
+
(0, common_1.Injectable)(),
|
|
41
|
+
__param(0, (0, common_1.Inject)(def_const_js_1.APP_LOGGER_SERVICE)),
|
|
42
|
+
__param(1, (0, common_1.Inject)(def_const_js_1.MODULE_ALIAS_TOKEN)),
|
|
43
|
+
__param(2, (0, common_1.Inject)(app_context_service_js_1.AppContextService)),
|
|
44
|
+
__param(3, (0, common_1.Inject)(def_const_js_1.MODULE_OPTION_TOKEN)),
|
|
45
|
+
__metadata("design:paramtypes", [Object, String, app_context_service_js_1.AppContextService, Object])
|
|
46
|
+
], LifeCycleHandler);
|
|
47
|
+
//# sourceMappingURL=life-cycle-handler.service.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"life-cycle-handler.service.js","sourceRoot":"","sources":["life-cycle-handler.service.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AACA,2CAKwB;AAExB,iDAIwB;AACxB,qEAA6D;AAOtD,IAAM,gBAAgB,GAAtB,MAAM,gBAAgB;IAO3B,YAC+C,MAA6B,EAC7B,WAAmB,EAE/C,iBAAoC,EAEpC,OAA6B;QALD,WAAM,GAAN,MAAM,CAAuB;QAC7B,gBAAW,GAAX,WAAW,CAAQ;QAE/C,sBAAiB,GAAjB,iBAAiB,CAAmB;QAEpC,YAAO,GAAP,OAAO,CAAsB;QAE9C,IAAI,CAAC,MAAM,CAAC,KAAK,EAAE,CACjB,+BAA+B,IAAI,CAAC,WAAW,yBAAyB,CACzE,CAAC;QACF,IACE,IAAI,CAAC,OAAO,EAAE,qBAAqB,KAAK,IAAI;YAC5C,IAAI,CAAC,iBAAiB,CAAC,eAAe,CAAC,GAAG,CAAC,IAAI,CAAC,WAAW,CAAC,EAC5D,CAAC;YACD,MAAM,IAAI,KAAK,CACb,mCAAmC,IAAI,CAAC,WAAW,SAAS,CAC7D,CAAC;QACJ,CAAC;QACD,IAAI,CAAC,iBAAiB,CAAC,eAAe,CAAC,GAAG,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC;IAC/D,CAAC;IAED,YAAY,KAAI,CAAC;IAEjB,eAAe;QACb,IAAI,CAAC,MAAM,CAAC,KAAK,EAAE,CACjB,gCAAgC,IAAI,CAAC,WAAW,yBAAyB,CAC1E,CAAC;QACF,IAAI,CAAC,iBAAiB,CAAC,eAAe,CAAC,MAAM,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC;IAClE,CAAC;CACF,CAAA;AArCY,4CAAgB;2BAAhB,gBAAgB;IAD5B,IAAA,mBAAU,GAAE;IASR,WAAA,IAAA,eAAM,EAAC,iCAAkB,CAAC,CAAA;IAC1B,WAAA,IAAA,eAAM,EAAC,iCAAkB,CAAC,CAAA;IAC1B,WAAA,IAAA,eAAM,EAAC,0CAAiB,CAAC,CAAA;IAEzB,WAAA,IAAA,eAAM,EAAC,kCAAmB,CAAC,CAAA;qDADQ,0CAAiB;GAX5C,gBAAgB,CAqC5B"}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { type ImprovedLoggerService } from '@nest-yalc-2/logger/logger-abstract.service.js';
|
|
2
|
+
import {
|
|
3
|
+
Injectable,
|
|
4
|
+
Inject,
|
|
5
|
+
OnModuleInit,
|
|
6
|
+
OnModuleDestroy,
|
|
7
|
+
} from '@nestjs/common';
|
|
8
|
+
import { type IYalcBaseAppOptions } from './base-app.interface.js';
|
|
9
|
+
import {
|
|
10
|
+
APP_LOGGER_SERVICE,
|
|
11
|
+
MODULE_ALIAS_TOKEN,
|
|
12
|
+
MODULE_OPTION_TOKEN,
|
|
13
|
+
} from './def.const.js';
|
|
14
|
+
import { AppContextService } from './app-context.service.js';
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* This class is used to handle the lifecycle of the app
|
|
18
|
+
* and apply some common logic and checks to all apps
|
|
19
|
+
*/
|
|
20
|
+
@Injectable()
|
|
21
|
+
export class LifeCycleHandler implements OnModuleDestroy, OnModuleInit {
|
|
22
|
+
/**
|
|
23
|
+
* We do this in the constructor since onModuleInit doesn't seem to be called
|
|
24
|
+
* when we use 'useFactory' to create the provider
|
|
25
|
+
* @todo: investigate why onModuleInit is not called
|
|
26
|
+
*
|
|
27
|
+
*/
|
|
28
|
+
constructor(
|
|
29
|
+
@Inject(APP_LOGGER_SERVICE) private readonly logger: ImprovedLoggerService,
|
|
30
|
+
@Inject(MODULE_ALIAS_TOKEN) private readonly moduleAlias: string,
|
|
31
|
+
@Inject(AppContextService)
|
|
32
|
+
private readonly appContextService: AppContextService,
|
|
33
|
+
@Inject(MODULE_OPTION_TOKEN)
|
|
34
|
+
private readonly options?: IYalcBaseAppOptions,
|
|
35
|
+
) {
|
|
36
|
+
this.logger.debug?.(
|
|
37
|
+
`====================== Init ${this.moduleAlias} ======================`,
|
|
38
|
+
);
|
|
39
|
+
if (
|
|
40
|
+
this.options?.skipDuplicateAppCheck !== true &&
|
|
41
|
+
this.appContextService.initializedApps.has(this.moduleAlias)
|
|
42
|
+
) {
|
|
43
|
+
throw new Error(
|
|
44
|
+
`Cannot initialize the same app (${this.moduleAlias}) twice`,
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
this.appContextService.initializedApps.add(this.moduleAlias);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
onModuleInit() {}
|
|
51
|
+
|
|
52
|
+
onModuleDestroy() {
|
|
53
|
+
this.logger.debug?.(
|
|
54
|
+
`====================== Close ${this.moduleAlias} ======================`,
|
|
55
|
+
);
|
|
56
|
+
this.appContextService.initializedApps.delete(this.moduleAlias);
|
|
57
|
+
}
|
|
58
|
+
}
|