Skip to content

Commit

Permalink
fix(types): add custom loader typing (#3533)
Browse files Browse the repository at this point in the history
  • Loading branch information
whxaxes authored Mar 21, 2019
1 parent a73cfd0 commit f31cd38
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 4 deletions.
21 changes: 21 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { EggLogger, EggLoggers, LoggerLevel as EggLoggerLevel, EggContextLogger
import { HttpClient2, RequestOptions } from 'urllib';
import {
EggCoreBase,
FileLoaderOption,
EggLoader as CoreLoader,
EggCoreOptions as CoreOptions,
EggLoaderOptions as CoreLoaderOptions,
Expand Down Expand Up @@ -183,6 +184,18 @@ declare module 'egg' {
type IgnoreItem = string | RegExp | ((ctx: Context) => boolean);
type IgnoreOrMatch = IgnoreItem | IgnoreItem[];

/** Custom Loader Configuration */
export interface CustomLoaderConfig extends RemoveSpecProp<FileLoaderOption, 'inject' | 'target'> {
/**
* an object you wanner load to, value can only be 'ctx' or 'app'. default to app
*/
inject?: 'ctx' | 'app';
/**
* whether need to load files in plugins or framework, default to false
*/
loadunit?: boolean;
}

export interface EggAppConfig {
workerStartTimeout: number;
baseDir: string;
Expand Down Expand Up @@ -295,6 +308,14 @@ declare module 'egg' {
*/
reloadPattern: string[] | string;
};

/**
* customLoader config
*/
customLoader: {
[key: string]: CustomLoaderConfig;
};

/**
* It will ignore special keys when dumpConfig
*/
Expand Down
18 changes: 18 additions & 0 deletions test/fixtures/apps/app-ts-type-check/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import {
Agent,
Controller,
Service,
EggAppConfig,
PowerPartial,
Singleton,
} from 'egg';

new BaseContextClass({} as Context).ctx;
Expand Down Expand Up @@ -46,3 +49,18 @@ const yadan = new YadanApplication({ baseDir: __dirname, plugins: {}, type: 'app
new yadan.ContextHttpClient();
new yadan.HttpClient();
new YadanAgent(undefined, 1123);

// config
const config = {} as EggAppConfig;
config.customLoader = {
model: {
}
}

// partial config
const config2 = {} as PowerPartial<EggAppConfig>;
console.info(config2.security.csrf);

// singleton
const redis = {} as Singleton<{ test(): void; }>;
redis.get('123').checkSingleTon();
34 changes: 34 additions & 0 deletions test/fixtures/apps/app-ts-type-check/normal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import {
Controller,
Service,
Subscription,
EggAppConfig,
PowerPartial,
Singleton,
} from 'egg';

// base context class
Expand Down Expand Up @@ -86,6 +89,37 @@ class MySubscription extends Subscription {
}
new MySubscription({} as Context);

// config
const config = {} as EggAppConfig;
config.keys = '123123';
config.customLoader = {
model: {
directory: 'app/model',
inject: 'app',
}
}

// partial config
const config2 = {} as PowerPartial<EggAppConfig>;
config2.keys = '123123';
config2.customLoader = {
model: {
directory: 'app/model',
}
}
config2.customLoader = {
model: {
inject: 'app',
}
}
config2.security = {
csrf: false,
}

// singleton
const redis = {} as Singleton<{ test(): void; }>;
redis.get('123').test();

// extends egg
app.config.mySpecConfig.substring(0);
declare module 'egg' {
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/apps/app-ts/app/model/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default class Test {}
20 changes: 17 additions & 3 deletions test/fixtures/apps/app-ts/config/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
export default {
keys: 'foo',
serverTimeout: 2 * 60 * 1000,
import { EggAppConfig } from 'egg';

export default () => {
const config = {} as EggAppConfig;

config.keys = 'foo';

config.serverTimeout = 2 * 60 * 1000;

config.customLoader = {
model: {
directory: 'app/model',
inject: 'ctx',
},
};

return config;
}
5 changes: 4 additions & 1 deletion test/ts/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,18 @@ describe('test/ts/index.test.js', () => {
require.resolve('typescript/bin/tsc'),
[ '-p', path.resolve(__dirname, '../fixtures/apps/app-ts-type-check/tsconfig-error.json') ]
)
.debug()
// .debug()
.expect('stdout', /Property 'ctx' is protected/)
.expect('stdout', /Property 'localsCheckAny' does not exist on type 'string'/)
.expect('stdout', /Property 'configKeysCheckAny' does not exist on type 'string'/)
.expect('stdout', /Property 'appCheckAny' does not exist on type 'Application'/)
.expect('stdout', /Property 'serviceLocalCheckAny' does not exist on type 'string'/)
.expect('stdout', /Property 'serviceConfigCheckAny' does not exist on type 'string'/)
.expect('stdout', /Property 'serviceAppCheckAny' does not exist on type 'Application'/)
.expect('stdout', /Property 'checkSingleTon' does not exist/)
.expect('stdout', /Property 'directory' is missing in type '{}' but required in type 'CustomLoaderConfig'/)
.notExpect('stdout', /Cannot find module 'yadan'/)
.expect('stdout', /Object is possibly 'undefined'\./)
.expect('stdout', /Expected 1 arguments, but got 0\./)
.expect('stdout', /Expected 0-1 arguments, but got 2\./)
.expect('code', 2)
Expand Down

0 comments on commit f31cd38

Please sign in to comment.