@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,263 @@
|
|
|
1
|
+
import { SystemExceptionFilter } from '@nest-yalc-2/errors/filters/index.js';
|
|
2
|
+
import {
|
|
3
|
+
BadRequestException,
|
|
4
|
+
ExceptionFilter,
|
|
5
|
+
NestApplicationOptions,
|
|
6
|
+
ValidationPipe,
|
|
7
|
+
ValidationPipeOptions,
|
|
8
|
+
} from '@nestjs/common';
|
|
9
|
+
import { NestFactory } from '@nestjs/core';
|
|
10
|
+
// import { GqlExceptionFilter } from '@nestjs/graphql';
|
|
11
|
+
import {
|
|
12
|
+
FastifyAdapter,
|
|
13
|
+
NestFastifyApplication,
|
|
14
|
+
} from '@nestjs/platform-fastify';
|
|
15
|
+
import fastifyCookie from '@fastify/cookie';
|
|
16
|
+
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';
|
|
17
|
+
import { fastify, FastifyInstance } from 'fastify';
|
|
18
|
+
import { envIsTrue } from '@nest-yalc-2/utils/env.helper.js';
|
|
19
|
+
import { useContainer } from 'class-validator';
|
|
20
|
+
import clc from 'cli-color';
|
|
21
|
+
import {
|
|
22
|
+
BaseAppBootstrap,
|
|
23
|
+
IGlobalOptions,
|
|
24
|
+
} from './app-bootstrap-base.helper.js';
|
|
25
|
+
import { getEnvLoggerLevels } from '@nest-yalc-2/logger/logger.helper.js';
|
|
26
|
+
import { UnwrapResultInterceptor } from './unwrap-result.interceptor.js';
|
|
27
|
+
|
|
28
|
+
export interface ICreateOptions {
|
|
29
|
+
enableSwagger?: boolean;
|
|
30
|
+
/**
|
|
31
|
+
* Path segment (without leading slash) where Swagger UI/JSON is exposed.
|
|
32
|
+
* Defaults to "api".
|
|
33
|
+
*/
|
|
34
|
+
swaggerPath?: string;
|
|
35
|
+
filters?: ExceptionFilter[];
|
|
36
|
+
validationPipeOptions?: ValidationPipeOptions;
|
|
37
|
+
/**
|
|
38
|
+
* On some cases we do want to manually override the apiPrefix of the service conf
|
|
39
|
+
*/
|
|
40
|
+
apiPrefix?: string;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export interface INestCreateOptions
|
|
44
|
+
extends ICreateOptions, NestApplicationOptions {}
|
|
45
|
+
|
|
46
|
+
export class AppBootstrap<
|
|
47
|
+
TGlobalOptions extends IGlobalOptions = IGlobalOptions,
|
|
48
|
+
> extends BaseAppBootstrap<NestFastifyApplication> {
|
|
49
|
+
private fastifyInstance?: FastifyInstance;
|
|
50
|
+
protected isSwaggerEnabled: boolean = false;
|
|
51
|
+
protected swaggerPath = 'api';
|
|
52
|
+
|
|
53
|
+
constructor(appAlias: string, module: any, options?: TGlobalOptions) {
|
|
54
|
+
super(appAlias, module, { globalsOptions: options });
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
async startServer(options?: {
|
|
58
|
+
createOptions?: INestCreateOptions;
|
|
59
|
+
fastifyInstance?: FastifyInstance;
|
|
60
|
+
}) {
|
|
61
|
+
await this.initApp(options);
|
|
62
|
+
|
|
63
|
+
if (envIsTrue(process.env.APP_DRY_RUN) === true) {
|
|
64
|
+
await this.closeApp();
|
|
65
|
+
process.exit(0);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// no need to wait here
|
|
69
|
+
// eslint-disable-next-line @typescript-eslint/no-floating-promises
|
|
70
|
+
this.listen();
|
|
71
|
+
|
|
72
|
+
return this;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
async initApp(options?: {
|
|
76
|
+
createOptions?: INestCreateOptions;
|
|
77
|
+
fastifyInstance?: FastifyInstance;
|
|
78
|
+
}) {
|
|
79
|
+
await this.createApp({
|
|
80
|
+
fastifyInstance: this.fastifyInstance,
|
|
81
|
+
createOptions: options?.createOptions,
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
return this.initSetup({
|
|
85
|
+
fastifyInstance: this.fastifyInstance,
|
|
86
|
+
createOptions: options?.createOptions,
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
async initSetup(options?: {
|
|
91
|
+
createOptions?: ICreateOptions;
|
|
92
|
+
fastifyInstance?: FastifyInstance;
|
|
93
|
+
}) {
|
|
94
|
+
try {
|
|
95
|
+
await this.applyBootstrapGlobals(options?.createOptions);
|
|
96
|
+
|
|
97
|
+
await this.getApp().init();
|
|
98
|
+
} catch (err) {
|
|
99
|
+
console.error('App init failed:', err);
|
|
100
|
+
this.closeCleanup();
|
|
101
|
+
throw new Error('Process aborted');
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
if (envIsTrue(process.env.APP_DRY_RUN) === true) {
|
|
105
|
+
this.loggerService?.log('Dry run, exiting...');
|
|
106
|
+
await this.closeApp();
|
|
107
|
+
process.exit(0);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
return this;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
async createApp(options?: {
|
|
114
|
+
createOptions?: INestCreateOptions;
|
|
115
|
+
fastifyInstance?: FastifyInstance;
|
|
116
|
+
}) {
|
|
117
|
+
this.fastifyInstance = options?.fastifyInstance ?? fastify();
|
|
118
|
+
|
|
119
|
+
let app;
|
|
120
|
+
try {
|
|
121
|
+
app = await NestFactory.create<NestFastifyApplication>(
|
|
122
|
+
this.module,
|
|
123
|
+
new FastifyAdapter(this.fastifyInstance as any),
|
|
124
|
+
{
|
|
125
|
+
bufferLogs: false,
|
|
126
|
+
abortOnError: options?.createOptions?.abortOnError ?? false,
|
|
127
|
+
logger: getEnvLoggerLevels(),
|
|
128
|
+
...(options?.createOptions ?? {}),
|
|
129
|
+
},
|
|
130
|
+
);
|
|
131
|
+
} catch (err) {
|
|
132
|
+
this.closeCleanup();
|
|
133
|
+
// eslint-disable-next-line no-console
|
|
134
|
+
console.error(clc.red('Failed to create app'), clc.red(err));
|
|
135
|
+
throw new Error('Process aborted');
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
return this.setApp(app);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
getFastifyInstance() {
|
|
142
|
+
return this.fastifyInstance;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
setSwaggerEnabled(enabled: boolean) {
|
|
146
|
+
this.isSwaggerEnabled = enabled;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
async applyBootstrapGlobals(options?: ICreateOptions) {
|
|
150
|
+
await super.applyBootstrapGlobals(options);
|
|
151
|
+
|
|
152
|
+
this.getApp().useGlobalPipes(
|
|
153
|
+
new ValidationPipe({
|
|
154
|
+
transform: true,
|
|
155
|
+
transformOptions: { enableImplicitConversion: false },
|
|
156
|
+
validateCustomDecorators: true,
|
|
157
|
+
exceptionFactory: (errors) => {
|
|
158
|
+
const errorMessages: { [key: string]: any } = {};
|
|
159
|
+
errors.forEach((error) => {
|
|
160
|
+
errorMessages[error.property] = error;
|
|
161
|
+
});
|
|
162
|
+
return new BadRequestException(errorMessages);
|
|
163
|
+
},
|
|
164
|
+
...(options?.validationPipeOptions ?? {}),
|
|
165
|
+
}),
|
|
166
|
+
);
|
|
167
|
+
|
|
168
|
+
this.getApp().setGlobalPrefix(
|
|
169
|
+
options?.apiPrefix ?? (this.getConf()?.apiPrefix || ''),
|
|
170
|
+
);
|
|
171
|
+
|
|
172
|
+
await this.getApp().register(fastifyCookie as any, {});
|
|
173
|
+
|
|
174
|
+
this.getApp().useGlobalInterceptors(new UnwrapResultInterceptor());
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* @todo refactor using a factory function to share with all services
|
|
178
|
+
*/
|
|
179
|
+
const filters = [
|
|
180
|
+
new SystemExceptionFilter(this.loggerService),
|
|
181
|
+
...(options?.filters ?? []),
|
|
182
|
+
];
|
|
183
|
+
this.getApp().useGlobalFilters(...filters);
|
|
184
|
+
|
|
185
|
+
if (options?.enableSwagger) {
|
|
186
|
+
const swaggerPath = sanitizeSwaggerPath(options.swaggerPath);
|
|
187
|
+
this.swaggerPath = swaggerPath;
|
|
188
|
+
this.setSwaggerEnabled(true);
|
|
189
|
+
const document = SwaggerModule.createDocument(
|
|
190
|
+
this.getApp(),
|
|
191
|
+
this.buildSwaggerConfig().build(),
|
|
192
|
+
);
|
|
193
|
+
SwaggerModule.setup(swaggerPath, this.getApp(), document, {
|
|
194
|
+
jsonDocumentUrl: `/${swaggerPath}/json`,
|
|
195
|
+
useGlobalPrefix: true,
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
useContainer(this.getApp().select(this.getModule()), {
|
|
200
|
+
fallbackOnErrors: true,
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
return this;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
buildSwaggerConfig() {
|
|
207
|
+
return new DocumentBuilder()
|
|
208
|
+
.setTitle(this.appAlias)
|
|
209
|
+
.setDescription(`${this.appAlias} rest api`);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
async listen(callback?: {
|
|
213
|
+
(port: number, host: string, domain: string): void;
|
|
214
|
+
}) {
|
|
215
|
+
const port = this.getConf()?.port || 0;
|
|
216
|
+
const host = this.getConf()?.host || '0.0.0.0';
|
|
217
|
+
let apiPrefix = this.getConf()?.apiPrefix;
|
|
218
|
+
apiPrefix = apiPrefix ? `/${apiPrefix}` : '';
|
|
219
|
+
const domain = this.getConf()?.domain || 'localhost';
|
|
220
|
+
await this.getApp().listen(port, host, async (_err, address) => {
|
|
221
|
+
// eslint-disable-next-line no-console
|
|
222
|
+
console.debug(`Server ${this.appAlias} listening on
|
|
223
|
+
http://localhost:${port}${apiPrefix}/
|
|
224
|
+
http://127.0.0.1:${port}${apiPrefix}/
|
|
225
|
+
http://${domain}:${port}${apiPrefix}/
|
|
226
|
+
${address}`);
|
|
227
|
+
|
|
228
|
+
// // eslint-disable-next-line no-console
|
|
229
|
+
// console.debug(`GraphQL ${this.appAlias} listening on
|
|
230
|
+
// http://localhost:${port}${apiPrefix}/graphql
|
|
231
|
+
// http://127.0.0.1:${port}${apiPrefix}/graphql
|
|
232
|
+
// http://${domain}:${port}${apiPrefix}/graphql`);
|
|
233
|
+
|
|
234
|
+
if (this.isSwaggerEnabled) {
|
|
235
|
+
// eslint-disable-next-line no-console
|
|
236
|
+
console.debug(`Swagger ${this.appAlias} listening on
|
|
237
|
+
http://localhost:${port}${apiPrefix}/${this.swaggerPath}
|
|
238
|
+
http://127.0.0.1:${port}${apiPrefix}/${this.swaggerPath}
|
|
239
|
+
http://${domain}:${port}${apiPrefix}/${this.swaggerPath}
|
|
240
|
+
${address}/${this.swaggerPath}
|
|
241
|
+
JSON: http://localhost:${port}${apiPrefix}/${this.swaggerPath}/json`);
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
callback?.(port, host, domain);
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* Hot reload @see https://docs.nestjs.com/recipes/hot-reload#hot-module-replacement-1
|
|
249
|
+
*/
|
|
250
|
+
const hmr = typeof module !== 'undefined' ? (module as any).hot : undefined;
|
|
251
|
+
if (hmr) {
|
|
252
|
+
// eslint-disable-next-line no-console
|
|
253
|
+
console.debug('Hot reload enabled. Reloading...');
|
|
254
|
+
hmr.accept();
|
|
255
|
+
hmr.dispose(() => this.closeApp());
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
function sanitizeSwaggerPath(path?: string): string {
|
|
261
|
+
const normalized = (path ?? 'api').replace(/^\/+/, '').replace(/\/+$/, '');
|
|
262
|
+
return normalized || 'api';
|
|
263
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Provider } from '@nestjs/common';
|
|
2
|
+
import { ConfigService } from '@nestjs/config';
|
|
3
|
+
export declare class AppConfigService<T = any> {
|
|
4
|
+
readonly service: ConfigService;
|
|
5
|
+
protected readonly appAlias: string;
|
|
6
|
+
protected _config: T;
|
|
7
|
+
constructor(service: ConfigService, appAlias: string);
|
|
8
|
+
get values(): T;
|
|
9
|
+
get(): T;
|
|
10
|
+
}
|
|
11
|
+
export declare function createAppConfigProvider(appAlias: string): Provider;
|
|
12
|
+
export declare function getAppConfigToken(appAlias: string): string;
|
|
13
|
+
export declare function getAppEventToken(appAlias: string): string;
|
|
14
|
+
export declare function getAppLoggerToken(appAlias: string): string;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AppConfigService = void 0;
|
|
4
|
+
exports.createAppConfigProvider = createAppConfigProvider;
|
|
5
|
+
exports.getAppConfigToken = getAppConfigToken;
|
|
6
|
+
exports.getAppEventToken = getAppEventToken;
|
|
7
|
+
exports.getAppLoggerToken = getAppLoggerToken;
|
|
8
|
+
const config_1 = require("@nestjs/config");
|
|
9
|
+
class AppConfigService {
|
|
10
|
+
constructor(service, appAlias) {
|
|
11
|
+
this.service = service;
|
|
12
|
+
this.appAlias = appAlias;
|
|
13
|
+
const config = this.service.get(this.appAlias);
|
|
14
|
+
if (!config) {
|
|
15
|
+
throw new Error(`AppConfigService: No config found for app alias '${this.appAlias}'`);
|
|
16
|
+
}
|
|
17
|
+
this._config = config;
|
|
18
|
+
}
|
|
19
|
+
get values() {
|
|
20
|
+
return this._config;
|
|
21
|
+
}
|
|
22
|
+
get() {
|
|
23
|
+
return this.values;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
exports.AppConfigService = AppConfigService;
|
|
27
|
+
function createAppConfigProvider(appAlias) {
|
|
28
|
+
return {
|
|
29
|
+
provide: getAppConfigToken(appAlias),
|
|
30
|
+
useFactory: (config) => {
|
|
31
|
+
return new AppConfigService(config, appAlias);
|
|
32
|
+
},
|
|
33
|
+
inject: [config_1.ConfigService],
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
function getAppConfigToken(appAlias) {
|
|
37
|
+
return `${appAlias}Config`;
|
|
38
|
+
}
|
|
39
|
+
function getAppEventToken(appAlias) {
|
|
40
|
+
return `${appAlias}Event`;
|
|
41
|
+
}
|
|
42
|
+
function getAppLoggerToken(appAlias) {
|
|
43
|
+
return `${appAlias}Logger`;
|
|
44
|
+
}
|
|
45
|
+
//# sourceMappingURL=app-config.service.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"app-config.service.js","sourceRoot":"","sources":["app-config.service.ts"],"names":[],"mappings":";;;AAwCA,0DAQC;AAED,8CAEC;AAED,4CAEC;AAED,8CAEC;AA3DD,2CAA+C;AAO/C,MAAa,gBAAgB;IAG3B,YACkB,OAAsB,EACnB,QAAgB;QADnB,YAAO,GAAP,OAAO,CAAe;QACnB,aAAQ,GAAR,QAAQ,CAAQ;QAEnC,MAAM,MAAM,GAAG,IAAI,CAAC,OAAO,CAAC,GAAG,CAAI,IAAI,CAAC,QAAQ,CAAC,CAAC;QAClD,IAAI,CAAC,MAAM,EAAE,CAAC;YACZ,MAAM,IAAI,KAAK,CACb,oDAAoD,IAAI,CAAC,QAAQ,GAAG,CACrE,CAAC;QACJ,CAAC;QAED,IAAI,CAAC,OAAO,GAAG,MAAM,CAAC;IACxB,CAAC;IAKD,IAAI,MAAM;QACR,OAAO,IAAI,CAAC,OAAO,CAAC;IACtB,CAAC;IAKD,GAAG;QACD,OAAO,IAAI,CAAC,MAAM,CAAC;IACrB,CAAC;CACF;AA9BD,4CA8BC;AAED,SAAgB,uBAAuB,CAAC,QAAgB;IACtD,OAAO;QACL,OAAO,EAAE,iBAAiB,CAAC,QAAQ,CAAC;QACpC,UAAU,EAAE,CAAC,MAAqB,EAAoB,EAAE;YACtD,OAAO,IAAI,gBAAgB,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC;QAChD,CAAC;QACD,MAAM,EAAE,CAAC,sBAAa,CAAC;KACxB,CAAC;AACJ,CAAC;AAED,SAAgB,iBAAiB,CAAC,QAAgB;IAChD,OAAO,GAAG,QAAQ,QAAQ,CAAC;AAC7B,CAAC;AAED,SAAgB,gBAAgB,CAAC,QAAgB;IAC/C,OAAO,GAAG,QAAQ,OAAO,CAAC;AAC5B,CAAC;AAED,SAAgB,iBAAiB,CAAC,QAAgB;IAChD,OAAO,GAAG,QAAQ,QAAQ,CAAC;AAC7B,CAAC"}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { Provider } from '@nestjs/common';
|
|
2
|
+
import { ConfigService } from '@nestjs/config';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* This service is used to access the app config registered with a specific app alias.
|
|
6
|
+
* This is useful when you want to access the config of the current app with the dependency injection and
|
|
7
|
+
* without specifying the app alias.
|
|
8
|
+
*/
|
|
9
|
+
export class AppConfigService<T = any> {
|
|
10
|
+
protected _config: T;
|
|
11
|
+
|
|
12
|
+
constructor(
|
|
13
|
+
public readonly service: ConfigService,
|
|
14
|
+
protected readonly appAlias: string,
|
|
15
|
+
) {
|
|
16
|
+
const config = this.service.get<T>(this.appAlias);
|
|
17
|
+
if (!config) {
|
|
18
|
+
throw new Error(
|
|
19
|
+
`AppConfigService: No config found for app alias '${this.appAlias}'`,
|
|
20
|
+
);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
this._config = config;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Type-safe access to the app config values of the current app.
|
|
28
|
+
*/
|
|
29
|
+
get values(): T {
|
|
30
|
+
return this._config;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* @deprecated - use .values instead
|
|
35
|
+
*/
|
|
36
|
+
get(): T {
|
|
37
|
+
return this.values;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function createAppConfigProvider(appAlias: string): Provider {
|
|
42
|
+
return {
|
|
43
|
+
provide: getAppConfigToken(appAlias),
|
|
44
|
+
useFactory: (config: ConfigService): AppConfigService => {
|
|
45
|
+
return new AppConfigService(config, appAlias);
|
|
46
|
+
},
|
|
47
|
+
inject: [ConfigService],
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function getAppConfigToken(appAlias: string): string {
|
|
52
|
+
return `${appAlias}Config`;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function getAppEventToken(appAlias: string): string {
|
|
56
|
+
return `${appAlias}Event`;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export function getAppLoggerToken(appAlias: string): string {
|
|
60
|
+
return `${appAlias}Logger`;
|
|
61
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
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.AppContextModule = void 0;
|
|
10
|
+
const common_1 = require("@nestjs/common");
|
|
11
|
+
const app_context_service_js_1 = require("./app-context.service.js");
|
|
12
|
+
let AppContextModule = class AppContextModule {
|
|
13
|
+
};
|
|
14
|
+
exports.AppContextModule = AppContextModule;
|
|
15
|
+
exports.AppContextModule = AppContextModule = __decorate([
|
|
16
|
+
(0, common_1.Global)(),
|
|
17
|
+
(0, common_1.Module)({ providers: [app_context_service_js_1.AppContextService], exports: [app_context_service_js_1.AppContextService] })
|
|
18
|
+
], AppContextModule);
|
|
19
|
+
//# sourceMappingURL=app-context.module.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"app-context.module.js","sourceRoot":"","sources":["app-context.module.ts"],"names":[],"mappings":";;;;;;;;;AAAA,2CAAgD;AAChD,qEAA6D;AAItD,IAAM,gBAAgB,GAAtB,MAAM,gBAAgB;CAAG,CAAA;AAAnB,4CAAgB;2BAAhB,gBAAgB;IAF5B,IAAA,eAAM,GAAE;IACR,IAAA,eAAM,EAAC,EAAE,SAAS,EAAE,CAAC,0CAAiB,CAAC,EAAE,OAAO,EAAE,CAAC,0CAAiB,CAAC,EAAE,CAAC;GAC5D,gBAAgB,CAAG"}
|
|
@@ -0,0 +1,26 @@
|
|
|
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.AppContextService = void 0;
|
|
10
|
+
const common_1 = require("@nestjs/common");
|
|
11
|
+
let AppContextService = class AppContextService {
|
|
12
|
+
constructor() {
|
|
13
|
+
this.initializedApps = new Set();
|
|
14
|
+
}
|
|
15
|
+
setSchema(schema) {
|
|
16
|
+
this.graphQLSchema = schema;
|
|
17
|
+
}
|
|
18
|
+
get schema() {
|
|
19
|
+
return this.graphQLSchema;
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
exports.AppContextService = AppContextService;
|
|
23
|
+
exports.AppContextService = AppContextService = __decorate([
|
|
24
|
+
(0, common_1.Injectable)()
|
|
25
|
+
], AppContextService);
|
|
26
|
+
//# sourceMappingURL=app-context.service.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"app-context.service.js","sourceRoot":"","sources":["app-context.service.ts"],"names":[],"mappings":";;;;;;;;;AAAA,2CAA4C;AAIrC,IAAM,iBAAiB,GAAvB,MAAM,iBAAiB;IAAvB;QACE,oBAAe,GAAG,IAAI,GAAG,EAAU,CAAC;IAU7C,CAAC;IAPQ,SAAS,CAAC,MAAqB;QACpC,IAAI,CAAC,aAAa,GAAG,MAAM,CAAC;IAC9B,CAAC;IAED,IAAW,MAAM;QACf,OAAO,IAAI,CAAC,aAAa,CAAC;IAC5B,CAAC;CACF,CAAA;AAXY,8CAAiB;4BAAjB,iBAAiB;IAD7B,IAAA,mBAAU,GAAE;GACA,iBAAiB,CAW7B"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { Injectable } from '@nestjs/common';
|
|
2
|
+
import { GraphQLSchema } from 'graphql';
|
|
3
|
+
|
|
4
|
+
@Injectable()
|
|
5
|
+
export class AppContextService {
|
|
6
|
+
public initializedApps = new Set<string>();
|
|
7
|
+
private graphQLSchema!: GraphQLSchema;
|
|
8
|
+
|
|
9
|
+
public setSchema(schema: GraphQLSchema) {
|
|
10
|
+
this.graphQLSchema = schema;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
public get schema() {
|
|
14
|
+
return this.graphQLSchema;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { ModuleMetadata } from '@nestjs/common';
|
|
2
|
+
import { BuildSchemaOptions } from '@nestjs/graphql';
|
|
3
|
+
import { ClassType } from '@nest-yalc-2/types/globals.d.js';
|
|
4
|
+
import { ApolloFederationDriverConfig } from '@nestjs/apollo';
|
|
5
|
+
type ApolloServerPlugin = NonNullable<ApolloFederationDriverConfig['plugins']>[number];
|
|
6
|
+
export interface IAppImportsFactory {
|
|
7
|
+
envPath?: string | string[];
|
|
8
|
+
setupEvents?: boolean;
|
|
9
|
+
setupJwt?: boolean;
|
|
10
|
+
keepDBConnectionAlive?: boolean;
|
|
11
|
+
operationPrefix?: string;
|
|
12
|
+
buildSchemaOptions?: BuildSchemaOptions;
|
|
13
|
+
gqlPlugin?: ClassType<ApolloServerPlugin>[];
|
|
14
|
+
disablePlayground?: boolean;
|
|
15
|
+
}
|
|
16
|
+
type PickModuleMetadata = 'imports' | 'providers' | 'exports';
|
|
17
|
+
export type AppModuleMetadata = Required<Pick<ModuleMetadata, PickModuleMetadata>>;
|
|
18
|
+
export declare function AppDependencyFactory(_context: string, confs: any[], dbConnNames: string[], gqlModules: any[], options?: IAppImportsFactory): AppModuleMetadata;
|
|
19
|
+
export {};
|