diff --git a/index.d.ts b/index.d.ts index 79a6f6609b..1997f2b5a6 100644 --- a/index.d.ts +++ b/index.d.ts @@ -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, @@ -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 { + /** + * 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; @@ -295,6 +308,14 @@ declare module 'egg' { */ reloadPattern: string[] | string; }; + + /** + * customLoader config + */ + customLoader: { + [key: string]: CustomLoaderConfig; + }; + /** * It will ignore special keys when dumpConfig */ diff --git a/test/fixtures/apps/app-ts-type-check/error.ts b/test/fixtures/apps/app-ts-type-check/error.ts index 840df1c626..ab1a99c80a 100644 --- a/test/fixtures/apps/app-ts-type-check/error.ts +++ b/test/fixtures/apps/app-ts-type-check/error.ts @@ -5,6 +5,9 @@ import { Agent, Controller, Service, + EggAppConfig, + PowerPartial, + Singleton, } from 'egg'; new BaseContextClass({} as Context).ctx; @@ -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; +console.info(config2.security.csrf); + +// singleton +const redis = {} as Singleton<{ test(): void; }>; +redis.get('123').checkSingleTon(); diff --git a/test/fixtures/apps/app-ts-type-check/normal.ts b/test/fixtures/apps/app-ts-type-check/normal.ts index 45212b475d..c64b064a9c 100644 --- a/test/fixtures/apps/app-ts-type-check/normal.ts +++ b/test/fixtures/apps/app-ts-type-check/normal.ts @@ -6,6 +6,9 @@ import { Controller, Service, Subscription, + EggAppConfig, + PowerPartial, + Singleton, } from 'egg'; // base context class @@ -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; +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' { diff --git a/test/fixtures/apps/app-ts/app/model/test.ts b/test/fixtures/apps/app-ts/app/model/test.ts new file mode 100644 index 0000000000..f94081b083 --- /dev/null +++ b/test/fixtures/apps/app-ts/app/model/test.ts @@ -0,0 +1 @@ +export default class Test {} diff --git a/test/fixtures/apps/app-ts/config/config.ts b/test/fixtures/apps/app-ts/config/config.ts index 0ec84155ab..183eeb2483 100644 --- a/test/fixtures/apps/app-ts/config/config.ts +++ b/test/fixtures/apps/app-ts/config/config.ts @@ -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; } diff --git a/test/ts/index.test.js b/test/ts/index.test.js index 5ef83b4fbc..9d2a590ad9 100644 --- a/test/ts/index.test.js +++ b/test/ts/index.test.js @@ -78,7 +78,7 @@ 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'/) @@ -86,7 +86,10 @@ describe('test/ts/index.test.js', () => { .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)