Skip to content

Commit 253386b

Browse files
committed
feat(core): add module name to extension
1 parent d5745bb commit 253386b

File tree

12 files changed

+40
-26
lines changed

12 files changed

+40
-26
lines changed

packages/core/src/containers/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
import 'reflect-metadata';
22
export * from './types';
33
export * from './container';
4+
export * from './modules';

packages/core/src/containers/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export const TYPES = {
2727
ValidatorLoader: Symbol.for('ValidatorLoader'),
2828
// Extensions
2929
ExtensionConfig: Symbol.for('ExtensionConfig'),
30+
ExtensionName: Symbol.for('ExtensionName'),
3031
Extension_TemplateEngine: Symbol.for('Extension_TemplateEngine'),
3132
Extension_InputValidator: Symbol.for('Extension_InputValidator'),
3233
};

packages/core/src/lib/extension-loader/extensionLoader.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export class ExtensionLoader {
2525
this.config = config;
2626
}
2727

28+
/** Load external extensions (should be called by core package) */
2829
public async loadExternalExtensionModules() {
2930
if (this.bound)
3031
throw new Error(
@@ -81,7 +82,10 @@ export class ExtensionLoader {
8182
bind(TYPES.ExtensionConfig)
8283
// Note they we can't bind undefined to container or it throw error while unbinding.
8384
// https://github.com/inversify/InversifyJS/issues/1462#issuecomment-1202099036
84-
.toConstantValue(name.length > 0 ? this.config[name] : {})
85+
.toConstantValue(name.length > 0 ? this.config[name] || {} : {})
86+
.whenInjectedInto(extension);
87+
bind(TYPES.ExtensionName)
88+
.toConstantValue(name || '')
8589
.whenInjectedInto(extension);
8690
});
8791
}

packages/core/src/lib/template-engine/built-in-extensions/query-builder/reqTagRunner.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ export class ReqTagRunner extends TagRunner {
1515

1616
constructor(
1717
@inject(TYPES.ExtensionConfig) config: any,
18+
@inject(TYPES.ExtensionName) name: string,
1819
@inject(TYPES.Executor) executor: IExecutor
1920
) {
20-
super(config);
21+
super(config, name);
2122
this.executor = executor;
2223
}
2324

packages/core/src/lib/template-engine/built-in-extensions/validator/filterChecker.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@ export class FilterChecker extends CompileTimeExtension {
1616
constructor(
1717
@inject(TYPES.ExtensionConfig)
1818
config: any,
19+
@inject(TYPES.ExtensionName)
20+
name: string,
1921
@inject(TYPES.CompilerEnvironment)
2022
@named('compileTime')
2123
compileTimeEnv: nunjucks.Environment
2224
) {
23-
super(config);
25+
super(config, name);
2426
this.env = compileTimeEnv;
2527
}
2628

packages/core/src/models/extensions/base.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,16 @@ import { inject, injectable } from 'inversify';
33

44
@injectable()
55
export abstract class ExtensionBase<C = any> {
6+
public readonly moduleName: string;
67
public activate?(): Promise<void>;
78
private config?: C;
89

9-
constructor(@inject(TYPES.ExtensionConfig) config?: C) {
10+
constructor(
11+
@inject(TYPES.ExtensionConfig) config: C,
12+
@inject(TYPES.ExtensionName) name: string
13+
) {
1014
this.config = config;
15+
this.moduleName = name;
1116
}
1217

1318
protected getConfig(): C | undefined {

packages/core/test/validators/built-in-validators/dataTypeValidator.spec.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ describe('Test "date" type validator', () => {
1212
// Arrange
1313
const args = JSON.parse(inputArgs);
1414
// Act
15-
const validator = new DateTypeValidator();
15+
const validator = new DateTypeValidator({}, '');
1616
// Assert
1717
expect(() => validator.validateSchema(args)).not.toThrow();
1818
}
@@ -29,7 +29,7 @@ describe('Test "date" type validator', () => {
2929
// Arrange
3030
const args = JSON.parse(inputArgs);
3131
// Act
32-
const validator = new DateTypeValidator();
32+
const validator = new DateTypeValidator({}, '');
3333

3434
// Assert
3535
expect(() => validator.validateSchema(args)).toThrow();
@@ -50,7 +50,7 @@ describe('Test "date" type validator', () => {
5050
const args = JSON.parse(inputArgs);
5151

5252
// Act
53-
const validator = new DateTypeValidator();
53+
const validator = new DateTypeValidator({}, '');
5454

5555
// Assert
5656
expect(() => validator.validateData(data, args)).not.toThrow();
@@ -68,7 +68,7 @@ describe('Test "date" type validator', () => {
6868
const args = JSON.parse(inputArgs);
6969

7070
// Act
71-
const validator = new DateTypeValidator();
71+
const validator = new DateTypeValidator({}, '');
7272

7373
// Assert
7474
expect(() => validator.validateData(data, args)).toThrow();

packages/core/test/validators/built-in-validators/integerTypeValidator.spec.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ describe('Test "integer" type validator', () => {
2626
// Arrange
2727
const args = JSON.parse(inputArgs);
2828
// Act
29-
const validator = new IntegerTypeValidator();
29+
const validator = new IntegerTypeValidator({}, '');
3030

3131
// Assert
3232
expect(() => validator.validateSchema(args)).not.toThrow();
@@ -45,7 +45,7 @@ describe('Test "integer" type validator', () => {
4545
// Arrange
4646
const args = JSON.parse(inputArgs);
4747
// Act
48-
const validator = new IntegerTypeValidator();
48+
const validator = new IntegerTypeValidator({}, '');
4949

5050
// Assert
5151
expect(() => validator.validateSchema(args)).toThrow();
@@ -68,7 +68,7 @@ describe('Test "integer" type validator', () => {
6868
// Arrange
6969
const args = JSON.parse(inputArgs);
7070
// Act
71-
const validator = new IntegerTypeValidator();
71+
const validator = new IntegerTypeValidator({}, '');
7272

7373
// Assert
7474
expect(() => validator.validateData(data, args)).not.toThrow();
@@ -89,7 +89,7 @@ describe('Test "integer" type validator', () => {
8989
// Arrange
9090
const args = JSON.parse(inputArgs);
9191
// Act
92-
const validator = new IntegerTypeValidator();
92+
const validator = new IntegerTypeValidator({}, '');
9393

9494
// Assert
9595
expect(() => validator.validateData(data, args)).toThrow();

packages/core/test/validators/built-in-validators/requiredValidator.spec.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ describe('Test "required" type validator', () => {
1515
// Arrange
1616
const args = JSON.parse(inputArgs);
1717
// Act
18-
const validator = new RequiredValidator();
18+
const validator = new RequiredValidator({}, '');
1919

2020
// Assert
2121
expect(() => validator.validateSchema(args)).not.toThrow();
@@ -34,7 +34,7 @@ describe('Test "required" type validator', () => {
3434
// Arrange
3535
const args = JSON.parse(inputArgs);
3636
// Act
37-
const validator = new RequiredValidator();
37+
const validator = new RequiredValidator({}, '');
3838

3939
// Assert
4040
expect(() => validator.validateSchema(args)).toThrow();
@@ -55,7 +55,7 @@ describe('Test "required" type validator', () => {
5555
// Arrange
5656
const args = JSON.parse(inputArgs);
5757
// Act
58-
const validator = new RequiredValidator();
58+
const validator = new RequiredValidator({}, '');
5959

6060
// Assert
6161
expect(() => validator.validateData(data, args)).not.toThrow();
@@ -79,7 +79,7 @@ describe('Test "required" type validator', () => {
7979
// Arrange
8080
const args = JSON.parse(inputArgs);
8181
// Act
82-
const validator = new RequiredValidator();
82+
const validator = new RequiredValidator({}, '');
8383

8484
// Assert
8585
expect(() => validator.validateData(data, args)).toThrow();

packages/core/test/validators/built-in-validators/stringTypeValidator.spec.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ describe('Test "string" type validator', () => {
1818
// Arrange
1919
const args = JSON.parse(inputArgs);
2020
// Act
21-
const validator = new StringTypeValidator();
21+
const validator = new StringTypeValidator({}, '');
2222

2323
// Assert
2424
expect(() => validator.validateSchema(args)).not.toThrow();
@@ -37,7 +37,7 @@ describe('Test "string" type validator', () => {
3737
// Arrange
3838
const args = JSON.parse(inputArgs);
3939
// Act
40-
const validator = new StringTypeValidator();
40+
const validator = new StringTypeValidator({}, '');
4141

4242
// Assert
4343
expect(() => validator.validateSchema(args)).toThrow();
@@ -55,7 +55,7 @@ describe('Test "string" type validator', () => {
5555
// Arrange
5656
const args = JSON.parse(inputArgs);
5757
// Act
58-
const validator = new StringTypeValidator();
58+
const validator = new StringTypeValidator({}, '');
5959

6060
// Assert
6161
expect(() => validator.validateData(data, args)).not.toThrow();
@@ -72,7 +72,7 @@ describe('Test "string" type validator', () => {
7272
// Arrange
7373
const args = JSON.parse(inputArgs);
7474
// Act
75-
const validator = new StringTypeValidator();
75+
const validator = new StringTypeValidator({}, '');
7676

7777
// Assert
7878
expect(() => validator.validateData(data, args)).toThrow();

packages/core/test/validators/built-in-validators/uuidTypeValidator.spec.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ describe('Test "uuid" type validator ', () => {
1313
// Arrange
1414
const args = JSON.parse(inputArgs);
1515
// Act
16-
const validator = new UUIDTypeValidator();
16+
const validator = new UUIDTypeValidator({}, '');
1717

1818
// Assert
1919
expect(() => validator.validateSchema(args)).not.toThrow();
@@ -32,7 +32,7 @@ describe('Test "uuid" type validator ', () => {
3232
// Arrange
3333
const args = JSON.parse(inputArgs);
3434
// Act
35-
const validator = new UUIDTypeValidator();
35+
const validator = new UUIDTypeValidator({}, '');
3636

3737
// Assert
3838
expect(() => validator.validateSchema(args)).toThrow();
@@ -54,7 +54,7 @@ describe('Test "uuid" type validator ', () => {
5454
// Arrange
5555
const args = JSON.parse(inputArgs);
5656
// Act
57-
const validator = new UUIDTypeValidator();
57+
const validator = new UUIDTypeValidator({}, '');
5858

5959
// Assert
6060
expect(() => validator.validateData(data, args)).not.toThrow();
@@ -72,7 +72,7 @@ describe('Test "uuid" type validator ', () => {
7272
// Arrange
7373
const args = JSON.parse(inputArgs);
7474
// Act
75-
const validator = new UUIDTypeValidator();
75+
const validator = new UUIDTypeValidator({}, '');
7676

7777
// Assert
7878
expect(() => validator.validateData(data, args)).toThrow();

packages/core/test/validators/validatorLoader.spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ describe('Test validator loader for extension validators with one module', () =>
7979
.inSingletonScope();
8080
});
8181

82-
afterEach(() => {
83-
container.unbind(TYPES.ValidatorLoader);
82+
afterEach(async () => {
83+
await container.unbindAllAsync();
8484
});
8585
it.each([
8686
// custom validator

0 commit comments

Comments
 (0)