Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: inject moduleConfig read from tegg-config app.moduleConfigs config #169

Merged
merged 2 commits into from
Dec 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion plugin/tegg/lib/ModuleConfigLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
import { ModuleConfigUtil } from '@eggjs/tegg/helper';
import { COMPATIBLE_PROTO_IMPLE_TYPE } from './EggCompatibleProtoImpl';
import { Application } from 'egg';
import extend from 'extend2';

export class ModuleConfigLoader {
constructor(readonly app: Application) {
Expand Down Expand Up @@ -49,7 +50,9 @@ export class ModuleConfigLoader {
const moduleConfigMap: Record<string, ModuleConfigHolder> = {};
for (const reference of this.app.moduleReferences) {
const moduleName = ModuleConfigUtil.readModuleNameSync(reference.path);
const config = ModuleConfigUtil.loadModuleConfigSync(reference.path, undefined, this.app.config.env) || {};
const defaultConfig = ModuleConfigUtil.loadModuleConfigSync(reference.path, undefined, this.app.config.env);
// @eggjs/tegg-config moduleConfigs[module].config overwrite
const config = extend(true, {}, defaultConfig, this.app.moduleConfigs[moduleName]?.config);
moduleConfigMap[moduleName] = {
name: moduleName,
reference: {
Expand Down
1 change: 1 addition & 0 deletions plugin/tegg/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"@eggjs/tegg-loader": "^3.27.0",
"@eggjs/tegg-metadata": "^3.27.0",
"@eggjs/tegg-runtime": "^3.27.0",
"extend2": "^1.0.0",
"sdk-base": "^4.2.0"
},
"devDependencies": {
Expand Down
14 changes: 14 additions & 0 deletions plugin/tegg/test/ModuleConfig.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,18 @@ describe('test/ModuleConfig.test.ts', () => {
});
});
});

it('should work with overwrite', async () => {
mm(app.moduleConfigs.simple.config, 'features', { dynamic: { foo: 'bar', bar: 'overwrite foo' } });
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nightink 这个 mock 干扰比较大,是不是删除掉?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

哦哦 这个忘记删了 这个 case 里没用


await app.httpRequest()
.get('/overwrite_config')
.expect(200)
.expect(res => {
assert.deepStrictEqual(res.body, {
moduleConfigs: { features: { dynamic: { foo: 'bar', bar: 'overwrite foo' } } },
moduleConfig: { features: { dynamic: { foo: 'bar', bar: 'overwrite foo' } } },
});
});
});
});
15 changes: 15 additions & 0 deletions plugin/tegg/test/fixtures/apps/inject-module-config/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Application } from 'egg';

export default class AppBoot {
app: Application;

constructor(app: Application) {
this.app = app;
}

configWillLoad() {
if (this.app.moduleConfigs?.overwrite?.config) {
(this.app.moduleConfigs.overwrite.config as Record<string, any>).features.dynamic.bar = 'overwrite foo';
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,9 @@ export default class App extends Controller {
const configs = await this.ctx.module.simple.foo.getConfig();
this.ctx.body = configs;
}

async overwriteConfig() {
const configs = await this.ctx.module.overwrite.bar.getConfig();
this.ctx.body = configs;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ import { Application } from 'egg';

module.exports = (app: Application) => {
app.router.get('/config', app.controller.app.baseDir);
app.router.get('/overwrite_config', app.controller.app.overwriteConfig);
};
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import 'egg';
import { Foo } from '../../modules/module-with-config/foo';
import { Bar } from '../../modules/module-with-overwrite-config/bar';

declare module 'egg' {
export interface EggModule {
simple: {
foo: Foo;
}
},
overwrite: {
bar: Bar;
},
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { AccessLevel, ContextProto, Inject, ModuleConfigs } from '@eggjs/tegg';

@ContextProto({
accessLevel: AccessLevel.PUBLIC,
})
export class Bar {
@Inject()
moduleConfigs: ModuleConfigs;

@Inject()
moduleConfig: Record<string, any>;

async getConfig(): Promise<object> {
return {
moduleConfigs: this.moduleConfigs.get('overwrite'),
moduleConfig: this.moduleConfig,
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
features:
dynamic:
bar: 'foo'
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
features:
dynamic:
foo: 'bar'
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "overwrite",
"eggModule": {
"name": "overwrite"
}
}
Loading