@nest-forge/core 0.0.1 → 0.0.2

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,3 @@
1
+ export declare const FORGE_ROOT_MODULE: unique symbol;
2
+ export declare const FORGE_FIELD_MODULE_REF: unique symbol;
3
+ export declare const FORGE_TOKEN_ROOT_MODULE: unique symbol;
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.FORGE_TOKEN_ROOT_MODULE = exports.FORGE_FIELD_MODULE_REF = exports.FORGE_ROOT_MODULE = void 0;
4
+ exports.FORGE_ROOT_MODULE = Symbol('FORGE_ROOT_MODULE');
5
+ exports.FORGE_FIELD_MODULE_REF = Symbol('FORGE_FIELD_MODULE_REF');
6
+ exports.FORGE_TOKEN_ROOT_MODULE = Symbol('FORGE_TOKEN_ROOT_MODULE');
@@ -1,4 +1,5 @@
1
- import { INestApplication, INestApplicationContext, MiddlewareConsumer, ModuleMetadata } from '@nestjs/common';
1
+ import { INestApplication, INestApplicationContext, INestMicroservice, MiddlewareConsumer, ModuleMetadata } from '@nestjs/common';
2
+ import { ForgeController, ForgeModule, ForgeService } from '../architecture';
2
3
  export declare abstract class ForgeExtension {
3
4
  /**
4
5
  * The metdata for this extension.
@@ -13,9 +14,13 @@ export declare abstract class ForgeExtension {
13
14
  */
14
15
  configureHttpApplication(application: INestApplication): any;
15
16
  /**
16
- * Configures a Nest application context. This is
17
+ * Configures a Nest application context. This is for a standalone application that has no web server.
17
18
  */
18
19
  configureStandaloneApplication(context: INestApplicationContext): any;
20
+ /**
21
+ * Configures a Nest microservice context.
22
+ */
23
+ configureMicroserviceApplication(context: INestMicroservice): any;
19
24
  /**
20
25
  * Configures the root module of the application.
21
26
  */
@@ -25,6 +30,18 @@ export declare abstract class ForgeExtension {
25
30
  * queried.
26
31
  */
27
32
  instrument(instance: unknown): any;
33
+ /**
34
+ * Augments a `ForgeModule` instance.
35
+ */
36
+ augmentModule(instance: ForgeModule, moduleRef: any): any;
37
+ /**
38
+ * Augments a `ForgeController` instance.
39
+ */
40
+ augmentController(instance: ForgeController, moduleRef: any): any;
41
+ /**
42
+ * Augments a `ForgeService` instance.
43
+ */
44
+ augmentService(instance: ForgeService, moduleRef: any): any;
28
45
  /**
29
46
  * Returns the metadata for this extension.
30
47
  */
@@ -17,9 +17,13 @@ class ForgeExtension {
17
17
  */
18
18
  configureHttpApplication(application) { }
19
19
  /**
20
- * Configures a Nest application context. This is
20
+ * Configures a Nest application context. This is for a standalone application that has no web server.
21
21
  */
22
22
  configureStandaloneApplication(context) { }
23
+ /**
24
+ * Configures a Nest microservice context.
25
+ */
26
+ configureMicroserviceApplication(context) { }
23
27
  /**
24
28
  * Configures the root module of the application.
25
29
  */
@@ -29,6 +33,18 @@ class ForgeExtension {
29
33
  * queried.
30
34
  */
31
35
  instrument(instance) { }
36
+ /**
37
+ * Augments a `ForgeModule` instance.
38
+ */
39
+ augmentModule(instance, moduleRef) { }
40
+ /**
41
+ * Augments a `ForgeController` instance.
42
+ */
43
+ augmentController(instance, moduleRef) { }
44
+ /**
45
+ * Augments a `ForgeService` instance.
46
+ */
47
+ augmentService(instance, moduleRef) { }
32
48
  /**
33
49
  * Returns the metadata for this extension.
34
50
  */
@@ -1,9 +1,13 @@
1
1
  import { NestApplicationOptions } from '@nestjs/common';
2
2
  import { ForgeExtensionResolvable } from './extensions';
3
3
  import { NestApplicationContextOptions } from '@nestjs/common/interfaces/nest-application-context-options.interface';
4
+ import { NestMicroserviceOptions } from '@nestjs/common/interfaces/microservices/nest-microservice-options.interface';
4
5
  export interface ForgeApplicationOptions extends NestApplicationOptions {
5
6
  extensions?: ForgeExtensionResolvable | ForgeExtensionResolvable[];
6
7
  }
7
8
  export interface ForgeApplicationContextOptions extends NestApplicationContextOptions {
8
9
  extensions?: ForgeExtensionResolvable | ForgeExtensionResolvable[];
9
10
  }
11
+ export interface ForgeMicroserviceOptions extends NestMicroserviceOptions {
12
+ extensions?: ForgeExtensionResolvable | ForgeExtensionResolvable[];
13
+ }
@@ -1,21 +1,37 @@
1
1
  import { DynamicModule, ForwardReference, MiddlewareConsumer, Type } from '@nestjs/common';
2
- import { ForgeApplicationContextOptions, ForgeApplicationOptions } from './forge-options.interface';
2
+ import { ForgeApplicationContextOptions, ForgeApplicationOptions, ForgeMicroserviceOptions } from './forge-options.interface';
3
3
  import { ForgeExtension, ForgeExtensionResolvable } from './extensions';
4
+ import { ModuleRef } from '@nestjs/core';
5
+ import { FORGE_ROOT_MODULE } from './constants';
6
+ import { ForgeBaseComponent, ForgeController, ForgeModule, ForgeService } from './architecture';
4
7
  declare class Forge {
8
+ private _augmented;
5
9
  create(appModule: IEntryNestModule, options?: ForgeApplicationOptions): Promise<import("@nestjs/common").INestApplication<any>>;
6
10
  createApplicationContext(appModule: IEntryNestModule, options: ForgeApplicationContextOptions): Promise<import("@nestjs/common").INestApplicationContext>;
11
+ createMicroservice<T extends object>(appModule: IEntryNestModule, options: ForgeMicroserviceOptions & T): Promise<import("@nestjs/common").INestMicroservice>;
7
12
  protected discoverExtensions(resolvables: ForgeExtensionResolvable | ForgeExtensionResolvable[]): ForgeExtension[];
8
13
  protected resolveExtension(resolvable: ForgeExtensionResolvable): ForgeExtension;
9
14
  protected createRootModule(appModule: IEntryNestModule, extensions: ForgeExtension[]): {
10
- new (): {
15
+ new (moduleRef: ModuleRef): {
16
+ readonly moduleRef: ModuleRef;
11
17
  configure(consumer: MiddlewareConsumer): void;
18
+ readonly [FORGE_ROOT_MODULE]: true;
12
19
  };
13
20
  };
14
- protected createInstrument(extensions: ForgeExtension[], originalInstrument?: Instrument): Instrument;
21
+ protected createInstrument(extensions: ForgeExtension[], originalInstrument?: Instrument): InstrumentResponse;
22
+ protected augmentComponents(instances: ForgeBaseComponent[], extensions: ForgeExtension[]): void;
23
+ protected augmentModule(instance: ForgeModule, extensions: ForgeExtension[]): void;
24
+ protected augmentController(instance: ForgeController, extensions: ForgeExtension[]): void;
25
+ protected augmentService(instance: ForgeService, extensions: ForgeExtension[]): void;
26
+ protected _isRootModule(instance: unknown): instance is IForgeRootModule;
15
27
  }
16
28
  type IEntryNestModule = Type<any> | DynamicModule | ForwardReference | Promise<IEntryNestModule>;
17
29
  type Instrument = {
18
30
  instanceDecorator: (instance: unknown) => unknown;
19
31
  };
32
+ interface InstrumentResponse {
33
+ instances: ForgeBaseComponent[];
34
+ instanceDecorator: (instance: unknown) => any;
35
+ }
20
36
  declare const forge: Forge;
21
37
  export { forge as Forge };
@@ -4,14 +4,21 @@ exports.Forge = void 0;
4
4
  const tslib_1 = require("tslib");
5
5
  const common_1 = require("@nestjs/common");
6
6
  const core_1 = require("@nestjs/core");
7
+ const constants_1 = require("./constants");
8
+ const architecture_1 = require("./architecture");
7
9
  class Forge {
10
+ _augmented = new Set();
8
11
  async create(appModule, options) {
9
12
  const extensions = this.discoverExtensions(options?.extensions ?? []);
10
13
  const root = this.createRootModule(appModule, extensions);
14
+ const instrument = this.createInstrument(extensions, options.instrument);
11
15
  const app = await core_1.NestFactory.create(root, {
12
16
  ...options,
13
- instrument: this.createInstrument(extensions, options.instrument),
17
+ instrument: {
18
+ instanceDecorator: instrument.instanceDecorator,
19
+ },
14
20
  });
21
+ this.augmentComponents(instrument.instances, extensions);
15
22
  for (const extension of extensions) {
16
23
  extension.configureHttpApplication(app);
17
24
  }
@@ -20,15 +27,35 @@ class Forge {
20
27
  async createApplicationContext(appModule, options) {
21
28
  const extensions = this.discoverExtensions(options?.extensions ?? []);
22
29
  const root = this.createRootModule(appModule, extensions);
30
+ const instrument = this.createInstrument(extensions, options.instrument);
23
31
  const app = await core_1.NestFactory.createApplicationContext(root, {
24
32
  ...options,
25
- instrument: this.createInstrument(extensions, options.instrument),
33
+ instrument: {
34
+ instanceDecorator: instrument.instanceDecorator,
35
+ },
26
36
  });
37
+ this.augmentComponents(instrument.instances, extensions);
27
38
  for (const extension of extensions) {
28
39
  extension.configureStandaloneApplication(app);
29
40
  }
30
41
  return app;
31
42
  }
43
+ async createMicroservice(appModule, options) {
44
+ const extensions = this.discoverExtensions(options?.extensions ?? []);
45
+ const root = this.createRootModule(appModule, extensions);
46
+ const instrument = this.createInstrument(extensions, options.instrument);
47
+ const app = await core_1.NestFactory.createMicroservice(root, {
48
+ ...options,
49
+ instrument: {
50
+ instanceDecorator: instrument.instanceDecorator,
51
+ },
52
+ });
53
+ this.augmentComponents(instrument.instances, extensions);
54
+ for (const extension of extensions) {
55
+ extension.configureMicroserviceApplication(app);
56
+ }
57
+ return app;
58
+ }
32
59
  discoverExtensions(resolvables) {
33
60
  const extensions = new Map();
34
61
  if (!Array.isArray(resolvables)) {
@@ -62,7 +89,7 @@ class Forge {
62
89
  }
63
90
  createRootModule(appModule, extensions) {
64
91
  const meta = {
65
- imports: [appModule],
92
+ imports: [],
66
93
  controllers: [],
67
94
  providers: [],
68
95
  exports: [],
@@ -74,7 +101,13 @@ class Forge {
74
101
  meta.providers.push(...(extensionMeta.providers ?? []));
75
102
  meta.exports.push(...(extensionMeta.exports ?? []));
76
103
  }
104
+ meta.imports.push(appModule);
77
105
  let ForgeRootModule = class ForgeRootModule {
106
+ moduleRef;
107
+ [constants_1.FORGE_ROOT_MODULE] = true;
108
+ constructor(moduleRef) {
109
+ this.moduleRef = moduleRef;
110
+ }
78
111
  configure(consumer) {
79
112
  for (const extension of extensions) {
80
113
  extension.configureRootModule(consumer);
@@ -82,14 +115,39 @@ class Forge {
82
115
  }
83
116
  };
84
117
  ForgeRootModule = tslib_1.__decorate([
85
- (0, common_1.Module)(meta)
118
+ (0, common_1.Module)(meta),
119
+ (0, common_1.Global)(),
120
+ tslib_1.__metadata("design:paramtypes", [core_1.ModuleRef])
86
121
  ], ForgeRootModule);
122
+ let ForgeRootProviderModule = class ForgeRootProviderModule {
123
+ };
124
+ ForgeRootProviderModule = tslib_1.__decorate([
125
+ (0, common_1.Global)(),
126
+ (0, common_1.Module)({
127
+ providers: [
128
+ {
129
+ provide: constants_1.FORGE_TOKEN_ROOT_MODULE,
130
+ useClass: ForgeRootModule,
131
+ },
132
+ ],
133
+ exports: [constants_1.FORGE_TOKEN_ROOT_MODULE],
134
+ })
135
+ ], ForgeRootProviderModule);
136
+ meta.imports.unshift(ForgeRootProviderModule);
87
137
  return ForgeRootModule;
88
138
  }
89
139
  createInstrument(extensions, originalInstrument) {
90
140
  const hasOriginalInstrument = originalInstrument && originalInstrument.instanceDecorator;
141
+ const instances = new Array();
91
142
  return {
92
- instanceDecorator(instance) {
143
+ instances,
144
+ instanceDecorator: (instance) => {
145
+ if (this._isRootModule(instance)) {
146
+ // TODO
147
+ }
148
+ if (instance instanceof architecture_1.ForgeBaseComponent) {
149
+ instances.push(instance);
150
+ }
93
151
  for (const extension of extensions) {
94
152
  const response = extension.instrument(instance);
95
153
  if (response !== undefined) {
@@ -103,6 +161,40 @@ class Forge {
103
161
  },
104
162
  };
105
163
  }
164
+ augmentComponents(instances, extensions) {
165
+ for (const instance of instances) {
166
+ if (!this._augmented.has(instance)) {
167
+ if (instance instanceof architecture_1.ForgeModule) {
168
+ this.augmentModule(instance, extensions);
169
+ }
170
+ else if (instance instanceof architecture_1.ForgeController) {
171
+ this.augmentController(instance, extensions);
172
+ }
173
+ else if (instance instanceof architecture_1.ForgeService) {
174
+ this.augmentService(instance, extensions);
175
+ }
176
+ this._augmented.add(instance);
177
+ }
178
+ }
179
+ }
180
+ augmentModule(instance, extensions) {
181
+ for (const extension of extensions) {
182
+ extension.augmentModule(instance, instance[constants_1.FORGE_FIELD_MODULE_REF]);
183
+ }
184
+ }
185
+ augmentController(instance, extensions) {
186
+ for (const extension of extensions) {
187
+ extension.augmentController(instance, instance[constants_1.FORGE_FIELD_MODULE_REF]);
188
+ }
189
+ }
190
+ augmentService(instance, extensions) {
191
+ for (const extension of extensions) {
192
+ extension.augmentService(instance, instance[constants_1.FORGE_FIELD_MODULE_REF]);
193
+ }
194
+ }
195
+ _isRootModule(instance) {
196
+ return typeof instance === 'object' && instance !== null && instance[constants_1.FORGE_ROOT_MODULE] === true;
197
+ }
106
198
  }
107
199
  const forge = new Forge();
108
200
  exports.Forge = forge;
package/dist/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
1
  export * from './forge.factory';
2
2
  export * from './forge-options.interface';
3
3
  export * from './extensions';
4
+ export * from './architecture';
package/dist/index.js CHANGED
@@ -4,3 +4,4 @@ const tslib_1 = require("tslib");
4
4
  tslib_1.__exportStar(require("./forge.factory"), exports);
5
5
  tslib_1.__exportStar(require("./forge-options.interface"), exports);
6
6
  tslib_1.__exportStar(require("./extensions"), exports);
7
+ tslib_1.__exportStar(require("./architecture"), exports);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@nest-forge/core",
3
3
  "description": "",
4
- "version": "0.0.1",
4
+ "version": "0.0.2",
5
5
  "scripts": {
6
6
  "build": "rimraf dist && tsc -p tsconfig.json",
7
7
  "watch": "rimraf dist && tsc -w -p tsconfig.json",
package/tsconfig.json CHANGED
@@ -15,7 +15,6 @@
15
15
  "sourceMap": false,
16
16
  "allowJs": false,
17
17
  "outDir": "./dist",
18
- "baseUrl": ".",
19
18
  "forceConsistentCasingInFileNames": true,
20
19
  "strictPropertyInitialization": false,
21
20
  "stripInternal": true