Skip to content

Commit

Permalink
fix: component get config and merge egg config (#649)
Browse files Browse the repository at this point in the history
  • Loading branch information
czy88840616 authored Sep 28, 2020
1 parent 0e996a9 commit aa95a3e
Show file tree
Hide file tree
Showing 17 changed files with 98 additions and 4 deletions.
4 changes: 4 additions & 0 deletions packages/core/src/service/configService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,8 @@ export class MidwayConfigService implements IConfigService {
}
return result;
}

clearAllConfig() {
this.configuration.clear();
}
}
3 changes: 3 additions & 0 deletions packages/web/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
const path = require('path');

module.exports = {
preset: 'ts-jest',
testPathIgnorePatterns: ['<rootDir>/test/fixtures'],
coveragePathIgnorePatterns: ['<rootDir>/test/'],
setupFilesAfterEnv: [path.join(__dirname, 'test/.setup.js')],
};
1 change: 1 addition & 0 deletions packages/web/src/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class AppBootHook {
this.bootstrap
.configure({
baseDir: this.app.appDir,
globalConfig: this.app.config,
})
.load(this.framework);
await this.bootstrap.init();
Expand Down
1 change: 1 addition & 0 deletions packages/web/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class AppBootHook {
this.framework = new Framework().configure({
processType: 'application',
app: this.app,
globalConfig: this.app.config,
});
Bootstrap.configure({
baseDir: this.app.appDir,
Expand Down
16 changes: 16 additions & 0 deletions packages/web/src/framework.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export class MidwayWebFramework extends MidwayKoaBaseFramework<
protected async afterDirectoryLoad(
options: Partial<IMidwayBootstrapOptions>
) {
const self = this;
if (this.isTsMode) {
process.env.EGG_TYPESCRIPT = 'true';
}
Expand All @@ -66,6 +67,21 @@ export class MidwayWebFramework extends MidwayKoaBaseFramework<
mode: 'single',
isTsMode: this.isTsMode,
});

this.configurationOptions.globalConfig = this.app.config;
}

if (this.configurationOptions.globalConfig) {
this.getApplicationContext()
.getConfigService()
.addObject(this.configurationOptions.globalConfig);

Object.defineProperty(this.app, 'config', {
get() {
return self.getConfiguration();
},
});
// this.app.config = this.getConfiguration();
}

this.defineApplicationProperties(this.app);
Expand Down
1 change: 1 addition & 0 deletions packages/web/src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export interface IMidwayWebConfigurationOptions extends IMidwayKoaConfigurationO
};
typescript?: boolean;
processType?: 'application' | 'agent';
globalConfig?: any;
}

export type MidwayWebMiddleware = Middleware<DefaultState, Context>;
Expand Down
1 change: 1 addition & 0 deletions packages/web/test/.setup.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const path = require('path');

process.env.MIDWAY_TS_MODE = 'true';
process.env.MIDWAY_EGG_PLUGIN_PATH = path.join(__dirname, '../../../');
jest.setTimeout(30000);
15 changes: 11 additions & 4 deletions packages/web/test/feature.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { closeApp, creatApp, createHttpRequest } from './utils';
import { IMidwayWebApplication } from "../src";
import { IMidwayWebApplication } from '../src';

describe('/test/feature.test.ts', () => {

describe('test new decorator', () => {
let app: IMidwayWebApplication;
beforeAll(async () => {
Expand All @@ -14,7 +13,9 @@ describe('/test/feature.test.ts', () => {
});

it('test get method with return value', async () => {
const result = await createHttpRequest(app).get('/').query({ name: 'harry' });
const result = await createHttpRequest(app)
.get('/')
.query({ name: 'harry' });
expect(result.status).toBe(201);
expect(result.text).toBe('hello world,harry');
});
Expand All @@ -27,9 +28,15 @@ describe('/test/feature.test.ts', () => {

it('should test global use midway middleware id in egg', async () => {
const app = await creatApp('feature/base-app-middleware');
const result = await createHttpRequest(app).get('/')
const result = await createHttpRequest(app).get('/');
expect(result.text).toEqual('1111222233334444');
await closeApp(app);
});

it('should got component config in app', async () => {
const app = await creatApp('feature/base-app-component-config');
const result = await createHttpRequest(app).get('/');
expect(result.text).toEqual('hello world1');
await closeApp(app);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "ali-demo"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "@component/sql",
"main": "src/index"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const mock = {
bbb: 1
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// src/configuration.ts
import { Configuration, Config } from '@midwayjs/decorator';

@Configuration({
importConfigs: [
'./config/'
],
})
export class ContainerLifeCycle {

@Config()
mock;

onReady() {
console.log(this.mock);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Config, Controller, Get, Provide } from '@midwayjs/decorator';

@Provide()
@Controller('/')
export class HomeController {
@Config()
mock;

@Get()
async home() {
return 'hello world' + this.mock.bbb;
}
}
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

export const keys = 'key';

export const hello = {
a: 1,
b: 2,
d: [1, 2, 3],
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

exports.hello = {
b: 4,
c: 3,
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { Configuration } from '@midwayjs/decorator';

@Configuration({
imports: ['./component/sql/src'],
})
export class ContainerConfiguration {}

0 comments on commit aa95a3e

Please sign in to comment.