@nest-forge/core 0.0.4 → 0.0.5

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.
@@ -0,0 +1,7 @@
1
+ export declare abstract class ForgeBaseComponent {
2
+ protected onModuleInit(): any;
3
+ protected onApplicationBootstrap(): any;
4
+ protected onModuleDestroy(signal: string): any;
5
+ protected beforeApplicationShutdown(signal: string): any;
6
+ protected onApplicationShutdown(signal: string): any;
7
+ }
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ var _a;
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.ForgeBaseComponent = void 0;
5
+ const tslib_1 = require("tslib");
6
+ const common_1 = require("@nestjs/common");
7
+ const constants_1 = require("../constants");
8
+ const core_1 = require("@nestjs/core");
9
+ class ForgeBaseComponent {
10
+ /**
11
+ * @internal
12
+ */
13
+ [_a = constants_1.FORGE_FIELD_MODULE_REF];
14
+ onModuleInit() { }
15
+ onApplicationBootstrap() { }
16
+ onModuleDestroy(signal) { }
17
+ beforeApplicationShutdown(signal) { }
18
+ onApplicationShutdown(signal) { }
19
+ }
20
+ exports.ForgeBaseComponent = ForgeBaseComponent;
21
+ tslib_1.__decorate([
22
+ (0, common_1.Inject)(core_1.ModuleRef),
23
+ tslib_1.__metadata("design:type", core_1.ModuleRef)
24
+ ], ForgeBaseComponent.prototype, _a, void 0);
@@ -0,0 +1,3 @@
1
+ import { ForgeBaseComponent } from './component';
2
+ export declare abstract class ForgeController extends ForgeBaseComponent {
3
+ }
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ForgeController = void 0;
4
+ const component_1 = require("./component");
5
+ class ForgeController extends component_1.ForgeBaseComponent {
6
+ }
7
+ exports.ForgeController = ForgeController;
@@ -0,0 +1,4 @@
1
+ export * from './component';
2
+ export * from './module';
3
+ export * from './controller';
4
+ export * from './service';
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const tslib_1 = require("tslib");
4
+ tslib_1.__exportStar(require("./component"), exports);
5
+ tslib_1.__exportStar(require("./module"), exports);
6
+ tslib_1.__exportStar(require("./controller"), exports);
7
+ tslib_1.__exportStar(require("./service"), exports);
@@ -0,0 +1,5 @@
1
+ import { MiddlewareConsumer } from '@nestjs/common';
2
+ import { ForgeBaseComponent } from './component';
3
+ export declare abstract class ForgeModule extends ForgeBaseComponent {
4
+ configure(consumer: MiddlewareConsumer): any;
5
+ }
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ForgeModule = void 0;
4
+ const component_1 = require("./component");
5
+ class ForgeModule extends component_1.ForgeBaseComponent {
6
+ configure(consumer) { }
7
+ }
8
+ exports.ForgeModule = ForgeModule;
@@ -0,0 +1,3 @@
1
+ import { ForgeBaseComponent } from './component';
2
+ export declare abstract class ForgeService extends ForgeBaseComponent {
3
+ }
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ForgeService = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const common_1 = require("@nestjs/common");
6
+ const component_1 = require("./component");
7
+ let ForgeService = class ForgeService extends component_1.ForgeBaseComponent {
8
+ };
9
+ exports.ForgeService = ForgeService;
10
+ exports.ForgeService = ForgeService = tslib_1.__decorate([
11
+ (0, common_1.Injectable)()
12
+ ], ForgeService);
@@ -0,0 +1,2 @@
1
+ export declare const FORGE_ROOT_MODULE: unique symbol;
2
+ export declare const FORGE_APP_OPTIONS: unique symbol;
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.FORGE_APP_OPTIONS = exports.FORGE_ROOT_MODULE = void 0;
4
+ exports.FORGE_ROOT_MODULE = Symbol('FORGE_ROOT_MODULE');
5
+ exports.FORGE_APP_OPTIONS = Symbol('FORGE_APP_OPTIONS');
@@ -0,0 +1,5 @@
1
+ export declare const FORGE_FIELD_MODULE_REF: unique symbol;
2
+ export declare const FORGE_TOKEN_ROOT_MODULE: unique symbol;
3
+ export declare const FORGE_PATCHED: unique symbol;
4
+ export declare const FORGE_PATCH_BOOT_CALLBACK: unique symbol;
5
+ export declare const FORGE_PATCH_ENABLE_INIT: unique symbol;
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.FORGE_PATCH_ENABLE_INIT = exports.FORGE_PATCH_BOOT_CALLBACK = exports.FORGE_PATCHED = exports.FORGE_TOKEN_ROOT_MODULE = exports.FORGE_FIELD_MODULE_REF = void 0;
4
+ exports.FORGE_FIELD_MODULE_REF = Symbol('FORGE_FIELD_MODULE_REF');
5
+ exports.FORGE_TOKEN_ROOT_MODULE = Symbol('FORGE_TOKEN_ROOT_MODULE');
6
+ exports.FORGE_PATCHED = Symbol('FORGE_PATCHED');
7
+ exports.FORGE_PATCH_BOOT_CALLBACK = Symbol('FORGE_PATCH_BOOT_CALLBACK');
8
+ exports.FORGE_PATCH_ENABLE_INIT = Symbol('FORGE_PATCH_ENABLE_INIT');
@@ -0,0 +1,92 @@
1
+ import { INestApplicationContext, INestMicroservice, MiddlewareConsumer, ModuleMetadata, NestApplicationOptions } from '@nestjs/common';
2
+ import { ForgeBaseComponent, ForgeController, ForgeModule, ForgeService } from '../architecture';
3
+ import { NestApplicationContextOptions } from '@nestjs/common/interfaces/nest-application-context-options.interface';
4
+ import { NestMicroserviceOptions } from '@nestjs/common/interfaces/microservices/nest-microservice-options.interface';
5
+ import { AbstractHttpAdapter, NestApplication } from '@nestjs/core';
6
+ export declare abstract class ForgeExtension {
7
+ /**
8
+ * Configures the HTTP adapter to use for the Nest application instance.
9
+ *
10
+ * @param current The current adapter instance, or `undefined` if not set (default will be used).
11
+ */
12
+ configureHttpAdapter(current?: AbstractHttpAdapter): ForgeHttpAdapterLike;
13
+ /**
14
+ * Configures a Nest application instance.
15
+ */
16
+ configureHttpApplication(application: NestApplication): any;
17
+ /**
18
+ * Configures the options object for a Nest application instance.
19
+ */
20
+ configureHttpApplicationOptions(options: NestApplicationOptions): NestApplicationOptions | Promise<NestApplicationOptions>;
21
+ /**
22
+ * Configures a Nest application context. This is for a standalone application that has no web server.
23
+ */
24
+ configureStandaloneApplication(context: INestApplicationContext): any;
25
+ /**
26
+ * Configures the options object for a standalone Nest application instance.
27
+ */
28
+ configureStandaloneApplicationOptions(options: NestApplicationContextOptions): NestApplicationContextOptions | Promise<NestApplicationContextOptions>;
29
+ /**
30
+ * Configures a Nest microservice context.
31
+ */
32
+ configureMicroserviceApplication(context: INestMicroservice): any;
33
+ /**
34
+ * Configures the options object for a Nest microservice instance.
35
+ */
36
+ configureMicroserviceApplicationOptions(options: NestMicroserviceOptions): NestMicroserviceOptions | Promise<NestMicroserviceOptions>;
37
+ /**
38
+ * Configures the root module of the application.
39
+ */
40
+ configureRootModule(consumer: MiddlewareConsumer): any;
41
+ /**
42
+ * Runs after all services in the application have booted.
43
+ */
44
+ afterBoot(app: INestApplicationContext): any;
45
+ /**
46
+ * Instruments the application context. If a value is returned, the instance is replaced with that value, and no further extensions are
47
+ * queried.
48
+ */
49
+ instrument(instance: unknown): any;
50
+ /**
51
+ * Augments a `ForgeBaseComponent` instance.
52
+ */
53
+ augmentComponent(instance: ForgeBaseComponent, moduleRef: any): any;
54
+ /**
55
+ * Augments a `ForgeModule` instance.
56
+ */
57
+ augmentModule(instance: ForgeModule, moduleRef: any): any;
58
+ /**
59
+ * Augments a `ForgeController` instance.
60
+ */
61
+ augmentController(instance: ForgeController, moduleRef: any): any;
62
+ /**
63
+ * Augments a `ForgeService` instance.
64
+ */
65
+ augmentService(instance: ForgeService, moduleRef: any): any;
66
+ /**
67
+ * Returns an array of imports to add onto the root module.
68
+ */
69
+ getRootImports(): ModuleImport[];
70
+ /**
71
+ * Returns an array of providers to add onto the root module.
72
+ */
73
+ getRootProviders(): ModuleProvider[];
74
+ /**
75
+ * Returns an array of controllers to add onto the root module.
76
+ */
77
+ getRootControllers(): ModuleController[];
78
+ /**
79
+ * Returns an array of exports to add onto the root module.
80
+ */
81
+ getRootExports(): ModuleExport[];
82
+ /**
83
+ * Returns an array of nested extensions that will be imported along with this one.
84
+ */
85
+ getNestedExtensions(): ForgeExtensionResolvable[];
86
+ }
87
+ export type ModuleImport = NonNullable<ModuleMetadata['imports']>[number];
88
+ export type ModuleProvider = NonNullable<ModuleMetadata['providers']>[number];
89
+ export type ModuleController = NonNullable<ModuleMetadata['controllers']>[number];
90
+ export type ModuleExport = NonNullable<ModuleMetadata['exports']>[number];
91
+ export type ForgeExtensionResolvable = ForgeExtension | (new () => ForgeExtension) | null | undefined | false;
92
+ export type ForgeHttpAdapterLike = AbstractHttpAdapter | null | undefined | Promise<AbstractHttpAdapter | null | undefined>;
@@ -0,0 +1,103 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ForgeExtension = void 0;
4
+ class ForgeExtension {
5
+ /**
6
+ * Configures the HTTP adapter to use for the Nest application instance.
7
+ *
8
+ * @param current The current adapter instance, or `undefined` if not set (default will be used).
9
+ */
10
+ configureHttpAdapter(current) {
11
+ return;
12
+ }
13
+ /**
14
+ * Configures a Nest application instance.
15
+ */
16
+ configureHttpApplication(application) { }
17
+ /**
18
+ * Configures the options object for a Nest application instance.
19
+ */
20
+ configureHttpApplicationOptions(options) {
21
+ return options;
22
+ }
23
+ /**
24
+ * Configures a Nest application context. This is for a standalone application that has no web server.
25
+ */
26
+ configureStandaloneApplication(context) { }
27
+ /**
28
+ * Configures the options object for a standalone Nest application instance.
29
+ */
30
+ configureStandaloneApplicationOptions(options) {
31
+ return options;
32
+ }
33
+ /**
34
+ * Configures a Nest microservice context.
35
+ */
36
+ configureMicroserviceApplication(context) { }
37
+ /**
38
+ * Configures the options object for a Nest microservice instance.
39
+ */
40
+ configureMicroserviceApplicationOptions(options) {
41
+ return options;
42
+ }
43
+ /**
44
+ * Configures the root module of the application.
45
+ */
46
+ configureRootModule(consumer) { }
47
+ /**
48
+ * Runs after all services in the application have booted.
49
+ */
50
+ afterBoot(app) { }
51
+ /**
52
+ * Instruments the application context. If a value is returned, the instance is replaced with that value, and no further extensions are
53
+ * queried.
54
+ */
55
+ instrument(instance) { }
56
+ /**
57
+ * Augments a `ForgeBaseComponent` instance.
58
+ */
59
+ augmentComponent(instance, moduleRef) { }
60
+ /**
61
+ * Augments a `ForgeModule` instance.
62
+ */
63
+ augmentModule(instance, moduleRef) { }
64
+ /**
65
+ * Augments a `ForgeController` instance.
66
+ */
67
+ augmentController(instance, moduleRef) { }
68
+ /**
69
+ * Augments a `ForgeService` instance.
70
+ */
71
+ augmentService(instance, moduleRef) { }
72
+ /**
73
+ * Returns an array of imports to add onto the root module.
74
+ */
75
+ getRootImports() {
76
+ return [];
77
+ }
78
+ /**
79
+ * Returns an array of providers to add onto the root module.
80
+ */
81
+ getRootProviders() {
82
+ return [];
83
+ }
84
+ /**
85
+ * Returns an array of controllers to add onto the root module.
86
+ */
87
+ getRootControllers() {
88
+ return [];
89
+ }
90
+ /**
91
+ * Returns an array of exports to add onto the root module.
92
+ */
93
+ getRootExports() {
94
+ return [];
95
+ }
96
+ /**
97
+ * Returns an array of nested extensions that will be imported along with this one.
98
+ */
99
+ getNestedExtensions() {
100
+ return [];
101
+ }
102
+ }
103
+ exports.ForgeExtension = ForgeExtension;
@@ -0,0 +1 @@
1
+ export * from './extension';
@@ -0,0 +1,4 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const tslib_1 = require("tslib");
4
+ tslib_1.__exportStar(require("./extension"), exports);
@@ -0,0 +1,13 @@
1
+ import { NestApplicationOptions } from '@nestjs/common';
2
+ import { ForgeExtensionResolvable } from './extensions';
3
+ import { NestApplicationContextOptions } from '@nestjs/common/interfaces/nest-application-context-options.interface';
4
+ import { NestMicroserviceOptions } from '@nestjs/common/interfaces/microservices/nest-microservice-options.interface';
5
+ export interface ForgeApplicationOptions extends NestApplicationOptions {
6
+ extensions?: ForgeExtensionResolvable | ForgeExtensionResolvable[];
7
+ }
8
+ export interface ForgeApplicationContextOptions extends NestApplicationContextOptions {
9
+ extensions?: ForgeExtensionResolvable | ForgeExtensionResolvable[];
10
+ }
11
+ export interface ForgeMicroserviceOptions extends NestMicroserviceOptions {
12
+ extensions?: ForgeExtensionResolvable | ForgeExtensionResolvable[];
13
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,49 @@
1
+ import { DynamicModule, ForwardReference, INestApplication, INestApplicationContext, MiddlewareConsumer, ModuleMetadata, Type } from '@nestjs/common';
2
+ import { ForgeApplicationContextOptions, ForgeApplicationOptions, ForgeMicroserviceOptions } from './forge-options.interface';
3
+ import { ForgeExtension, ForgeExtensionResolvable } from './extensions';
4
+ import { AbstractHttpAdapter, ModuleRef, NestApplication } from '@nestjs/core';
5
+ import { ForgeBaseComponent, ForgeController, ForgeModule, ForgeService } from './architecture';
6
+ import { FORGE_ROOT_MODULE } from './constants-public';
7
+ declare class Forge {
8
+ private _augmented;
9
+ create<T extends INestApplication = NestApplication>(appModule: IEntryNestModule, options?: ForgeApplicationOptions): Promise<T>;
10
+ create<T extends INestApplication = NestApplication>(appModule: IEntryNestModule, httpAdapter: AbstractHttpAdapter, options?: ForgeApplicationOptions): Promise<T>;
11
+ createApplicationContext(appModule: IEntryNestModule, options: ForgeApplicationContextOptions): Promise<INestApplicationContext>;
12
+ createMicroservice<T extends object>(appModule: IEntryNestModule, options: ForgeMicroserviceOptions & T): Promise<import("@nestjs/common").INestMicroservice>;
13
+ protected discoverExtensions(resolvables: ForgeExtensionResolvable | ForgeExtensionResolvable[]): ForgeExtension[];
14
+ protected resolveExtension(resolvable: ForgeExtensionResolvable): ForgeExtension | null;
15
+ protected createRootModule(appModule: IEntryNestModule, extensions: ForgeExtension[], options: any): Promise<{
16
+ new (moduleRef: ModuleRef): {
17
+ readonly moduleRef: ModuleRef;
18
+ configure(consumer: MiddlewareConsumer): Promise<void>;
19
+ readonly [FORGE_ROOT_MODULE]: true;
20
+ };
21
+ }>;
22
+ protected _getExtensionMetadata(extension: ForgeExtension): Promise<ExtensionMetadata>;
23
+ protected createInstrument(extensions: ForgeExtension[], originalInstrument?: Instrument): InstrumentResponse;
24
+ protected augmentComponents(instances: ForgeBaseComponent[], extensions: ForgeExtension[]): Promise<void>;
25
+ protected augmentComponent(instance: ForgeBaseComponent, extensions: ForgeExtension[]): Promise<void>;
26
+ protected augmentModule(instance: ForgeModule, extensions: ForgeExtension[]): Promise<void>;
27
+ protected augmentController(instance: ForgeController, extensions: ForgeExtension[]): Promise<void>;
28
+ protected augmentService(instance: ForgeService, extensions: ForgeExtension[]): Promise<void>;
29
+ protected _isRootModule(instance: unknown): instance is IForgeRootModule;
30
+ protected _isHttpAdapter(instance: unknown): instance is AbstractHttpAdapter;
31
+ protected _augmentBootHooks(createOptions: object, extensions: ForgeExtension[]): void;
32
+ protected _augmentNestApplication(): void;
33
+ }
34
+ type IEntryNestModule = Type<any> | DynamicModule | ForwardReference | Promise<IEntryNestModule>;
35
+ type Instrument = {
36
+ instanceDecorator: (instance: unknown) => unknown;
37
+ };
38
+ type NonNullableFields<T> = {
39
+ [P in keyof T]-?: NonNullable<T[P]>;
40
+ };
41
+ interface InstrumentResponse {
42
+ instances: ForgeBaseComponent[];
43
+ instanceDecorator: (instance: unknown) => any;
44
+ }
45
+ interface ExtensionMetadata extends NonNullableFields<ModuleMetadata> {
46
+ extensions: ForgeExtensionResolvable[];
47
+ }
48
+ declare const forge: Forge;
49
+ export { forge as Forge };
@@ -0,0 +1,296 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Forge = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const common_1 = require("@nestjs/common");
6
+ const core_1 = require("@nestjs/core");
7
+ const constants_1 = require("./constants");
8
+ const architecture_1 = require("./architecture");
9
+ const constants_public_1 = require("./constants-public");
10
+ class Forge {
11
+ _augmented = new Set();
12
+ async create(appModule, optionsOrHttpAdapter, optionsFallback) {
13
+ let adapter = this._isHttpAdapter(optionsOrHttpAdapter) ? optionsOrHttpAdapter : undefined;
14
+ const options = (typeof optionsFallback === 'object' ? optionsFallback : adapter ? {} : optionsOrHttpAdapter) ??
15
+ {};
16
+ const extensions = this.discoverExtensions(options?.extensions ?? []);
17
+ const root = await this.createRootModule(appModule, extensions, options);
18
+ const instrument = this.createInstrument(extensions, options.instrument);
19
+ let createOptions = {
20
+ ...options,
21
+ instrument: {
22
+ instanceDecorator: instrument.instanceDecorator,
23
+ },
24
+ };
25
+ for (const extension of extensions) {
26
+ const newOptions = await extension.configureHttpApplicationOptions(createOptions);
27
+ const newAdapter = await extension.configureHttpAdapter(adapter);
28
+ if (typeof newOptions === 'object' && newOptions !== null) {
29
+ createOptions = newOptions;
30
+ }
31
+ if (typeof newAdapter === 'object' && newAdapter !== null) {
32
+ adapter = newAdapter;
33
+ }
34
+ }
35
+ const createArgs = [root, createOptions];
36
+ if (adapter) {
37
+ createArgs.splice(1, 0, adapter);
38
+ }
39
+ this._augmentBootHooks(createOptions, extensions);
40
+ this._augmentNestApplication();
41
+ const app = await core_1.NestFactory.create.apply(core_1.NestFactory, createArgs);
42
+ await this.augmentComponents(instrument.instances, extensions);
43
+ for (const extension of extensions) {
44
+ await extension.configureHttpApplication(app);
45
+ }
46
+ return app;
47
+ }
48
+ async createApplicationContext(appModule, options) {
49
+ const extensions = this.discoverExtensions(options?.extensions ?? []);
50
+ const root = this.createRootModule(appModule, extensions, options);
51
+ const instrument = this.createInstrument(extensions, options.instrument);
52
+ let createOptions = {
53
+ ...options,
54
+ instrument: {
55
+ instanceDecorator: instrument.instanceDecorator,
56
+ },
57
+ };
58
+ for (const extension of extensions) {
59
+ const newOptions = await extension.configureStandaloneApplicationOptions(createOptions);
60
+ if (typeof newOptions === 'object' && newOptions !== null) {
61
+ createOptions = newOptions;
62
+ }
63
+ }
64
+ createOptions[constants_1.FORGE_PATCH_ENABLE_INIT] = false;
65
+ this._augmentBootHooks(createOptions, extensions);
66
+ this._augmentNestApplication();
67
+ const app = await core_1.NestFactory.createApplicationContext(root, createOptions);
68
+ await this.augmentComponents(instrument.instances, extensions);
69
+ for (const extension of extensions) {
70
+ await extension.configureStandaloneApplication(app);
71
+ }
72
+ createOptions[constants_1.FORGE_PATCH_ENABLE_INIT] = true;
73
+ await app.init();
74
+ return app;
75
+ }
76
+ async createMicroservice(appModule, options) {
77
+ const extensions = this.discoverExtensions(options?.extensions ?? []);
78
+ const root = this.createRootModule(appModule, extensions, options);
79
+ const instrument = this.createInstrument(extensions, options.instrument);
80
+ let createOptions = {
81
+ ...options,
82
+ instrument: {
83
+ instanceDecorator: instrument.instanceDecorator,
84
+ },
85
+ };
86
+ for (const extension of extensions) {
87
+ const newOptions = await extension.configureMicroserviceApplicationOptions(createOptions);
88
+ if (typeof newOptions === 'object' && newOptions !== null) {
89
+ createOptions = newOptions;
90
+ }
91
+ }
92
+ this._augmentBootHooks(createOptions, extensions);
93
+ this._augmentNestApplication();
94
+ const app = await core_1.NestFactory.createMicroservice(root, createOptions);
95
+ await this.augmentComponents(instrument.instances, extensions);
96
+ for (const extension of extensions) {
97
+ await extension.configureMicroserviceApplication(app);
98
+ }
99
+ return app;
100
+ }
101
+ discoverExtensions(resolvables) {
102
+ const extensions = new Map();
103
+ if (!Array.isArray(resolvables)) {
104
+ resolvables = [resolvables];
105
+ }
106
+ for (const resolvable of resolvables) {
107
+ const extension = this.resolveExtension(resolvable);
108
+ if (extension !== null) {
109
+ for (const nestedExtension of this.discoverExtensions(extension.getNestedExtensions())) {
110
+ extensions.delete(nestedExtension.constructor);
111
+ extensions.set(nestedExtension.constructor, nestedExtension);
112
+ }
113
+ extensions.delete(extension.constructor);
114
+ extensions.set(extension.constructor, extension);
115
+ }
116
+ }
117
+ return Array.from(extensions.values());
118
+ }
119
+ resolveExtension(resolvable) {
120
+ if (resolvable === false || resolvable === null || resolvable === undefined) {
121
+ return null;
122
+ }
123
+ if (typeof resolvable === 'object') {
124
+ return resolvable;
125
+ }
126
+ if (typeof resolvable === 'function') {
127
+ return new resolvable();
128
+ }
129
+ throw new Error(`Unsupported extension resolvable "${String(resolvable)}"`);
130
+ }
131
+ async createRootModule(appModule, extensions, options) {
132
+ const meta = {
133
+ imports: [],
134
+ controllers: [],
135
+ providers: [],
136
+ exports: [],
137
+ };
138
+ for (const extension of extensions) {
139
+ const extensionMeta = await this._getExtensionMetadata(extension);
140
+ meta.imports.push(...extensionMeta.imports);
141
+ meta.controllers.push(...extensionMeta.controllers);
142
+ meta.providers.push(...extensionMeta.providers);
143
+ meta.exports.push(...extensionMeta.exports);
144
+ }
145
+ meta.imports.push(appModule);
146
+ let ForgeRootModule = class ForgeRootModule {
147
+ moduleRef;
148
+ [constants_public_1.FORGE_ROOT_MODULE] = true;
149
+ constructor(moduleRef) {
150
+ this.moduleRef = moduleRef;
151
+ }
152
+ async configure(consumer) {
153
+ for (const extension of extensions) {
154
+ await extension.configureRootModule(consumer);
155
+ }
156
+ }
157
+ };
158
+ ForgeRootModule = tslib_1.__decorate([
159
+ (0, common_1.Module)(meta),
160
+ (0, common_1.Global)(),
161
+ tslib_1.__metadata("design:paramtypes", [core_1.ModuleRef])
162
+ ], ForgeRootModule);
163
+ let ForgeRootProviderModule = class ForgeRootProviderModule {
164
+ };
165
+ ForgeRootProviderModule = tslib_1.__decorate([
166
+ (0, common_1.Global)(),
167
+ (0, common_1.Module)({
168
+ providers: [
169
+ {
170
+ provide: constants_1.FORGE_TOKEN_ROOT_MODULE,
171
+ useClass: ForgeRootModule,
172
+ },
173
+ {
174
+ provide: constants_public_1.FORGE_APP_OPTIONS,
175
+ useValue: options,
176
+ },
177
+ ],
178
+ exports: [constants_1.FORGE_TOKEN_ROOT_MODULE, constants_public_1.FORGE_APP_OPTIONS],
179
+ })
180
+ ], ForgeRootProviderModule);
181
+ meta.imports.unshift(ForgeRootProviderModule);
182
+ return ForgeRootModule;
183
+ }
184
+ async _getExtensionMetadata(extension) {
185
+ const results = await Promise.all([
186
+ extension.getRootImports(),
187
+ extension.getRootProviders(),
188
+ extension.getRootExports(),
189
+ extension.getRootControllers(),
190
+ extension.getNestedExtensions(),
191
+ ]);
192
+ return {
193
+ imports: results[0],
194
+ providers: results[1],
195
+ exports: results[2],
196
+ controllers: results[3],
197
+ extensions: results[4],
198
+ };
199
+ }
200
+ createInstrument(extensions, originalInstrument) {
201
+ const hasOriginalInstrument = originalInstrument && originalInstrument.instanceDecorator;
202
+ const instances = new Array();
203
+ return {
204
+ instances,
205
+ instanceDecorator: (instance) => {
206
+ if (this._isRootModule(instance)) {
207
+ // TODO
208
+ }
209
+ if (instance instanceof architecture_1.ForgeBaseComponent) {
210
+ instances.push(instance);
211
+ }
212
+ for (const extension of extensions) {
213
+ const response = extension.instrument(instance);
214
+ if (response !== undefined) {
215
+ return response;
216
+ }
217
+ }
218
+ if (hasOriginalInstrument) {
219
+ return originalInstrument.instanceDecorator(instance);
220
+ }
221
+ return instance;
222
+ },
223
+ };
224
+ }
225
+ async augmentComponents(instances, extensions) {
226
+ for (const instance of instances) {
227
+ if (!this._augmented.has(instance)) {
228
+ await this.augmentComponent(instance, extensions);
229
+ if (instance instanceof architecture_1.ForgeModule) {
230
+ await this.augmentModule(instance, extensions);
231
+ }
232
+ else if (instance instanceof architecture_1.ForgeController) {
233
+ await this.augmentController(instance, extensions);
234
+ }
235
+ else if (instance instanceof architecture_1.ForgeService) {
236
+ await this.augmentService(instance, extensions);
237
+ }
238
+ this._augmented.add(instance);
239
+ }
240
+ }
241
+ }
242
+ async augmentComponent(instance, extensions) {
243
+ for (const extension of extensions) {
244
+ await extension.augmentComponent(instance, instance[constants_1.FORGE_FIELD_MODULE_REF]);
245
+ }
246
+ }
247
+ async augmentModule(instance, extensions) {
248
+ for (const extension of extensions) {
249
+ await extension.augmentModule(instance, instance[constants_1.FORGE_FIELD_MODULE_REF]);
250
+ }
251
+ }
252
+ async augmentController(instance, extensions) {
253
+ for (const extension of extensions) {
254
+ await extension.augmentController(instance, instance[constants_1.FORGE_FIELD_MODULE_REF]);
255
+ }
256
+ }
257
+ async augmentService(instance, extensions) {
258
+ for (const extension of extensions) {
259
+ await extension.augmentService(instance, instance[constants_1.FORGE_FIELD_MODULE_REF]);
260
+ }
261
+ }
262
+ _isRootModule(instance) {
263
+ return typeof instance === 'object' && instance !== null && instance[constants_public_1.FORGE_ROOT_MODULE] === true;
264
+ }
265
+ _isHttpAdapter(instance) {
266
+ return typeof instance === 'object' && instance !== null && typeof instance['use'] === 'function';
267
+ }
268
+ _augmentBootHooks(createOptions, extensions) {
269
+ createOptions[constants_1.FORGE_PATCH_BOOT_CALLBACK] = async (app) => {
270
+ await Promise.all(extensions.map((e) => e.afterBoot(app)));
271
+ };
272
+ }
273
+ _augmentNestApplication() {
274
+ const application = core_1.NestApplicationContext;
275
+ const originalBootstrapHook = application.prototype.callBootstrapHook;
276
+ const originalInit = application.prototype.init;
277
+ if (application.prototype[constants_1.FORGE_PATCHED]) {
278
+ return;
279
+ }
280
+ application.prototype[constants_1.FORGE_PATCHED] = true;
281
+ application.prototype.callBootstrapHook = async function () {
282
+ await originalBootstrapHook.call(this);
283
+ if (this.appOptions[constants_1.FORGE_PATCH_BOOT_CALLBACK]) {
284
+ await this.appOptions[constants_1.FORGE_PATCH_BOOT_CALLBACK](this);
285
+ }
286
+ };
287
+ application.prototype.init = async function () {
288
+ if (this.appOptions[constants_1.FORGE_PATCH_ENABLE_INIT] === false) {
289
+ return this;
290
+ }
291
+ return originalInit.call(this);
292
+ };
293
+ }
294
+ }
295
+ const forge = new Forge();
296
+ exports.Forge = forge;
@@ -0,0 +1,5 @@
1
+ export * from './forge.factory';
2
+ export * from './forge-options.interface';
3
+ export * from './extensions';
4
+ export * from './architecture';
5
+ export * from './constants-public';
package/dist/index.js ADDED
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const tslib_1 = require("tslib");
4
+ tslib_1.__exportStar(require("./forge.factory"), exports);
5
+ tslib_1.__exportStar(require("./forge-options.interface"), exports);
6
+ tslib_1.__exportStar(require("./extensions"), exports);
7
+ tslib_1.__exportStar(require("./architecture"), exports);
8
+ tslib_1.__exportStar(require("./constants-public"), exports);
package/package.json CHANGED
@@ -1,22 +1,26 @@
1
1
  {
2
2
  "name": "@nest-forge/core",
3
3
  "description": "",
4
- "version": "0.0.4",
4
+ "version": "0.0.5",
5
5
  "scripts": {
6
6
  "build": "rimraf dist && tsc -p tsconfig.json",
7
7
  "watch": "rimraf dist && tsc -w -p tsconfig.json",
8
8
  "format": "prettier --write \"{src,apps,libs,test}/**/*.ts\""
9
9
  },
10
+ "main": "./dist/index.js",
11
+ "types": "./dist/index.d.ts",
10
12
  "directories": {
11
13
  "lib": "src"
12
14
  },
13
- "main": "./dist/index.js",
14
- "types": "./dist/index.d.ts",
15
+ "files": [
16
+ "dist",
17
+ "tsconfig.json"
18
+ ],
15
19
  "keywords": [],
16
20
  "license": "UNLICENSED",
17
21
  "repository": {
18
22
  "type": "git",
19
- "url": "https://github.com/baileyherbert/nest-forge"
23
+ "url": "git+https://github.com/baileyherbert/nest-forge.git"
20
24
  },
21
25
  "bugs": {
22
26
  "url": "https://github.com/baileyherbert/nest-forge/issues"