Skip to content

Commit

Permalink
fix: add app.keys (#1395)
Browse files Browse the repository at this point in the history
  • Loading branch information
czy88840616 committed Dec 3, 2021
1 parent 77af5c0 commit c44afc6
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 2 deletions.
1 change: 1 addition & 0 deletions packages/core/src/error/code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,5 @@ export enum FrameworkErrorEnum {
DEFINITION_NOT_FOUND = 10003,
FEATURE_NO_LONGER_SUPPORTED = 10004,
VALIDATE_FAIL = 10005,
MISSING_CONFIG = 10006,
}
9 changes: 9 additions & 0 deletions packages/core/src/error/framework.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,12 @@ export class MidwayValidationError extends MidwayError {
});
}
}

export class MidwayConfigMissingError extends MidwayError {
constructor(configKey: string) {
super(
`Can't found config key "${configKey}" in your config, please set it first`,
FrameworkErrorEnum.MISSING_CONFIG
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ import * as primary from '../../../../src'
@Configuration({
imports: [primary],
conflictCheck: true,
importConfigs: [
{
default: {
koa: {
keys: ['123']
}
}
}
]
})
export class ContainerLifeCycle {
@App()
Expand Down
11 changes: 10 additions & 1 deletion packages/prometheus/test/fixtures/base-app/src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,16 @@ import * as metrics from '../../../../src';


@Configuration({
imports: [metrics]
imports: [metrics],
importConfigs: [
{
default: {
koa: {
keys: ['123']
}
}
}
]
})
export class AutoConfiguration {
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ export const sequelize = {
},
sync: true
}

export const koa = {
keys: ['123']
}
2 changes: 1 addition & 1 deletion packages/sequelize/test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createApp, close } from '@midwayjs/mock';
import { Framework } from '@midwayjs/koa'
import { Framework } from '@midwayjs/koa';
import { join } from 'path';
import { UserService } from './fixtures/sequelize-demo/src/service/user'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,7 @@ export const view = {
},
keys: '123',
};

export const koa = {
keys: ['123']
}
10 changes: 10 additions & 0 deletions packages/web-koa/src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ import { extractKoaLikeValue, MidwayDecoratorService } from '@midwayjs/core';

@Configuration({
namespace: 'koa',
importConfigs: [
{
default: {
koa: {
keys: [],
onerror: {},
},
},
},
],
})
export class KoaConfiguration {
@Inject()
Expand Down
9 changes: 9 additions & 0 deletions packages/web-koa/src/framework.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
PathFileUtil,
RouterInfo,
WebControllerGenerator,
MidwayConfigMissingError,
} from '@midwayjs/core';

import { Framework } from '@midwayjs/decorator';
Expand Down Expand Up @@ -58,6 +59,14 @@ export class MidwayKoaFramework extends BaseFramework<
DefaultState,
IMidwayKoaContext
>() as IMidwayKoaApplication;
const appKeys =
this.configurationOptions['keys'] ||
this.configService.getConfiguration('keys');
if (appKeys) {
this.app.keys = appKeys;
} else {
throw new MidwayConfigMissingError('koa.keys');
}
onerror(this.app, this.configurationOptions.onerror);
this.app.use(async (ctx, next) => {
this.app.createAnonymousContext(ctx);
Expand Down

0 comments on commit c44afc6

Please sign in to comment.