From fe94253fa6bb839935734ec0ba2c20462e8a3d5e Mon Sep 17 00:00:00 2001 From: whxaxes Date: Sun, 1 Apr 2018 15:53:44 +0800 Subject: [PATCH 01/17] chore: add EggAppInfo in d.ts --- index.d.ts | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/index.d.ts b/index.d.ts index d91653a16d..54f9eb159b 100644 --- a/index.d.ts +++ b/index.d.ts @@ -165,6 +165,15 @@ declare module 'egg' { export type LoggerLevel = 'DEBUG' | 'INFO' | 'WARN' | 'ERROR' | 'NONE'; + export interface EggAppInfo { + pkg: string; // package.json + name: string; // the application name from package.json + baseDir: string; // current directory of application + env: string; // equals to serverEnv + HOME: string; // home directory of the OS + root: string; // baseDir when local and unittest, HOME when other environment + } + export interface EggAppConfig { workerStartTimeout: number; baseDir: string; From 20264f25e8df30fc6dbb11f4f3b744db0762c898 Mon Sep 17 00:00:00 2001 From: whxaxes Date: Sun, 1 Apr 2018 16:33:09 +0800 Subject: [PATCH 02/17] docs: add js doc --- index.d.ts | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/index.d.ts b/index.d.ts index 54f9eb159b..a29d5d10b2 100644 --- a/index.d.ts +++ b/index.d.ts @@ -165,6 +165,18 @@ declare module 'egg' { export type LoggerLevel = 'DEBUG' | 'INFO' | 'WARN' | 'ERROR' | 'NONE'; + /** + * egg app info + * @example + * ```js + * // config/config.default.ts + * import { EggAppInfo } from 'egg'; + * + * export default (appInfo: EggAppInfo) => { + * return {}; + * } + * ``` + */ export interface EggAppInfo { pkg: string; // package.json name: string; // the application name from package.json From efb29bd8b5c6b6b5a4d0832599add0f69b485cdb Mon Sep 17 00:00:00 2001 From: whxaxes Date: Sun, 1 Apr 2018 16:34:45 +0800 Subject: [PATCH 03/17] docs: update jsdoc --- index.d.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/index.d.ts b/index.d.ts index a29d5d10b2..6a54d93956 100644 --- a/index.d.ts +++ b/index.d.ts @@ -173,7 +173,9 @@ declare module 'egg' { * import { EggAppInfo } from 'egg'; * * export default (appInfo: EggAppInfo) => { - * return {}; + * return { + * keys: appInfo.name + '123456', + * }; * } * ``` */ From 06a035716275919f3c3207658e0b4eb94aaf3ad5 Mon Sep 17 00:00:00 2001 From: whxaxes Date: Mon, 2 Apr 2018 14:12:26 +0800 Subject: [PATCH 04/17] fix: typo --- index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.d.ts b/index.d.ts index 6a54d93956..09f358f234 100644 --- a/index.d.ts +++ b/index.d.ts @@ -180,7 +180,7 @@ declare module 'egg' { * ``` */ export interface EggAppInfo { - pkg: string; // package.json + pkg: any; // package.json name: string; // the application name from package.json baseDir: string; // current directory of application env: string; // equals to serverEnv From 1abcef90b85ed69cac4c282c9503e0b58b95e335 Mon Sep 17 00:00:00 2001 From: whxaxes Date: Wed, 4 Apr 2018 11:27:18 +0800 Subject: [PATCH 05/17] feat: add PowerPartial type and plugin interfaces --- index.d.ts | 46 ++++++++++++++++++++++++++++++++++++++++++++++ package.json | 2 +- 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/index.d.ts b/index.d.ts index baa6274b7f..670dad95ae 100644 --- a/index.d.ts +++ b/index.d.ts @@ -873,6 +873,52 @@ declare module 'egg' { urlFor(name: string, params?: { [key: string]: any }): string; } + /** + * Powerful Partial, Support adding ? modifier to a mapped property in three level + * @example + * import { PowerPartial, EggAppConfig } from 'egg'; + * + * // { keys?: string, ... } + * type EggConfig = PowerPartial + */ + type PowerPartial = { + [U in keyof T]?: T[U] extends {} + ? { [V in keyof T[U]]?: T[U][V] extends {} ? Partial : T[U][V] } + : T[U] + }; + + // egg env type + type EggEnvType = 'local' | 'unittest' | 'prod'; + + /** + * plugin config item + */ + interface EggPluginItem { + env?: EggEnvType; + path?: string; + package?: string; + enabled?: boolean; + } + + /** + * build-in plugin list + */ + interface EggPluginList { + [key: string]: EggPluginItem; + onerror: EggPluginItem; + session: EggPluginItem; + i18n: EggPluginItem; + watcher: EggPluginItem; + multipart: EggPluginItem; + security: EggPluginItem; + development: EggPluginItem; + logrotator: EggPluginItem; + schedule: EggPluginItem; + static: EggPluginItem; + jsonp: EggPluginItem; + view: EggPluginItem; + } + /** * Singleton instance in Agent Worker, extend {@link EggApplication} */ diff --git a/package.json b/package.json index aa04124df1..e429bad682 100644 --- a/package.json +++ b/package.json @@ -77,7 +77,7 @@ "spy": "^1.0.0", "supertest": "^3.0.0", "ts-node": "^3.0.6", - "typescript": "^2.3.4", + "typescript": "^2.8.0", "webstorm-disable-index": "^1.1.2" }, "main": "index.js", From 0a357c287faca35cccb194698e9cff6988b4ca7c Mon Sep 17 00:00:00 2001 From: whxaxes Date: Wed, 4 Apr 2018 11:40:17 +0800 Subject: [PATCH 06/17] feat: change any type to object --- index.d.ts | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/index.d.ts b/index.d.ts index 670dad95ae..66ba528eca 100644 --- a/index.d.ts +++ b/index.d.ts @@ -40,10 +40,10 @@ declare module 'egg' { } export interface Logger { - info(info: string, ...args: any[]): void; - warn(info: string, ...args: any[]): void; - debug(info: string, ...args: any[]): void; - error(info: string | Error, ...args: any[]): void; + info(msg: any, ...args: any[]): void; + warn(msg: any, ...args: any[]): void; + debug(msg: any, ...args: any[]): void; + error(msg: any, ...args: any[]): void; } export type RequestArrayBody = any[]; @@ -371,11 +371,31 @@ declare module 'egg' { siteFile: any; - static: any; + static: { + prefix: string; + dir: string; + // support lazy load + dynamic: boolean; + preload: boolean; + buffer: boolean; + maxFiles: number; + }; - view: any; + view: { + root: string; + cache: boolean; + defaultExtension: string; + defaultViewEngine: string; + mapping: any; + }; - watcher: any; + watcher: { + type: string; + eventSources: { + default: string; + development: string; + } + }; } export interface Router extends KoaRouter { From c088b2c8582e196ee5b836bed1bf36c507fc9fe4 Mon Sep 17 00:00:00 2001 From: whxaxes Date: Wed, 4 Apr 2018 11:43:02 +0800 Subject: [PATCH 07/17] fix: typo --- index.d.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.d.ts b/index.d.ts index 66ba528eca..a9bda93fca 100644 --- a/index.d.ts +++ b/index.d.ts @@ -917,13 +917,13 @@ declare module 'egg' { env?: EggEnvType; path?: string; package?: string; - enabled?: boolean; + enable?: boolean; } /** * build-in plugin list */ - interface EggPluginList { + interface EggPlugin { [key: string]: EggPluginItem; onerror: EggPluginItem; session: EggPluginItem; From 76b08eba88c52cd3a237c89007a0e4516c5700da Mon Sep 17 00:00:00 2001 From: whxaxes Date: Wed, 4 Apr 2018 11:44:44 +0800 Subject: [PATCH 08/17] docs: update comment --- index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.d.ts b/index.d.ts index a9bda93fca..09771b3004 100644 --- a/index.d.ts +++ b/index.d.ts @@ -898,7 +898,7 @@ declare module 'egg' { * @example * import { PowerPartial, EggAppConfig } from 'egg'; * - * // { keys?: string, ... } + * // { view: { defaultEngines: string } } => { view?: { defaultEngines?: string } } * type EggConfig = PowerPartial */ type PowerPartial = { From 4585b869aef63749690d3c6cb239a787c3d19eaa Mon Sep 17 00:00:00 2001 From: whxaxes Date: Wed, 4 Apr 2018 12:56:01 +0800 Subject: [PATCH 09/17] feat: change mapping type to plainobject --- index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.d.ts b/index.d.ts index 09771b3004..60f9b08e52 100644 --- a/index.d.ts +++ b/index.d.ts @@ -386,7 +386,7 @@ declare module 'egg' { cache: boolean; defaultExtension: string; defaultViewEngine: string; - mapping: any; + mapping: { [key: string]: string }; }; watcher: { From 56ff3e252e6722d923ffa09380ca123570512808 Mon Sep 17 00:00:00 2001 From: whxaxes Date: Wed, 4 Apr 2018 13:06:10 +0800 Subject: [PATCH 10/17] feat: add plainobject type --- index.d.ts | 55 ++++++++++++++++++++++++++---------------------------- 1 file changed, 26 insertions(+), 29 deletions(-) diff --git a/index.d.ts b/index.d.ts index 60f9b08e52..993932aa27 100644 --- a/index.d.ts +++ b/index.d.ts @@ -5,6 +5,23 @@ import { RequestOptions } from 'urllib'; import { Readable } from 'stream'; declare module 'egg' { + // plain object + type PlainObject = { [key: string]: T }; + + /** + * Powerful Partial, Support adding ? modifier to a mapped property in three level + * @example + * import { PowerPartial, EggAppConfig } from 'egg'; + * + * // { view: { defaultEngines: string } } => { view?: { defaultEngines?: string } } + * type EggConfig = PowerPartial + */ + type PowerPartial = { + [U in keyof T]?: T[U] extends {} + ? { [V in keyof T[U]]?: T[U][V] extends {} ? Partial : T[U][V] } + : T[U] + }; + /** * BaseContextClass is a base class that can be extended, * it's instantiated in context level, @@ -47,7 +64,7 @@ declare module 'egg' { } export type RequestArrayBody = any[]; - export type RequestObjectBody = { [key: string]: any }; + export type RequestObjectBody = PlainObject; interface Request extends KoaApplication.Request { // tslint:disable-line /** * detect if response should be json @@ -103,7 +120,7 @@ declare module 'egg' { * } * ``` */ - queries: { [key: string]: string[] }; + queries: PlainObject; /** * get params pass by querystring, all value are String type. @@ -125,7 +142,7 @@ declare module 'egg' { * } * ``` */ - query: { [key: string]: string }; + query: PlainObject; body: any; } @@ -369,7 +386,7 @@ declare module 'egg' { csp: any; }; - siteFile: any; + siteFile: PlainObject; static: { prefix: string; @@ -386,16 +403,10 @@ declare module 'egg' { cache: boolean; defaultExtension: string; defaultViewEngine: string; - mapping: { [key: string]: string }; + mapping: PlainObject; }; - watcher: { - type: string; - eventSources: { - default: string; - development: string; - } - }; + watcher: PlainObject; } export interface Router extends KoaRouter { @@ -650,7 +661,7 @@ declare module 'egg' { /** * @see Request#accept */ - queries: { [key: string]: string[] }; + queries: PlainObject; /** * @see Request#accept @@ -875,7 +886,7 @@ declare module 'egg' { * ``` * @return {String} url path(without host) */ - pathFor(name: string, params?: { [key: string]: any }): string; + pathFor(name: string, params?: PlainObject): string; /** * Generate full URL(with host) for route. Takes the route name and a map of named params. @@ -890,23 +901,9 @@ declare module 'egg' { * ``` * @return {String} full url(with host) */ - urlFor(name: string, params?: { [key: string]: any }): string; + urlFor(name: string, params?: PlainObject): string; } - /** - * Powerful Partial, Support adding ? modifier to a mapped property in three level - * @example - * import { PowerPartial, EggAppConfig } from 'egg'; - * - * // { view: { defaultEngines: string } } => { view?: { defaultEngines?: string } } - * type EggConfig = PowerPartial - */ - type PowerPartial = { - [U in keyof T]?: T[U] extends {} - ? { [V in keyof T[U]]?: T[U][V] extends {} ? Partial : T[U][V] } - : T[U] - }; - // egg env type type EggEnvType = 'local' | 'unittest' | 'prod'; From 26a3a740ff1bf668f6ba1bb50a6bac878b62531b Mon Sep 17 00:00:00 2001 From: whxaxes Date: Wed, 4 Apr 2018 15:12:55 +0800 Subject: [PATCH 11/17] feat: change EggEnvType --- index.d.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/index.d.ts b/index.d.ts index 993932aa27..06c5087646 100644 --- a/index.d.ts +++ b/index.d.ts @@ -200,7 +200,7 @@ declare module 'egg' { pkg: any; // package.json name: string; // the application name from package.json baseDir: string; // current directory of application - env: string; // equals to serverEnv + env: EggEnvType; // equals to serverEnv HOME: string; // home directory of the OS root: string; // baseDir when local and unittest, HOME when other environment } @@ -253,7 +253,7 @@ declare module 'egg' { logger: { dir: string; encoding: string; - env: string; + env: EggEnvType; level: LoggerLevel; consoleLevel: LoggerLevel; outputJSON: boolean; @@ -298,7 +298,7 @@ declare module 'egg' { /** * The environment of egg */ - env: string; + env: EggEnvType; /** * The current HOME directory @@ -445,7 +445,7 @@ declare module 'egg' { /** * app.env delegate app.config.env */ - env: string; + env: EggEnvType; /** * core logger for framework and plugins, log file is $HOME/logs/{appname}/egg-web @@ -905,13 +905,13 @@ declare module 'egg' { } // egg env type - type EggEnvType = 'local' | 'unittest' | 'prod'; + type EggEnvType = 'local' | 'unittest' | 'prod' | string; /** * plugin config item */ interface EggPluginItem { - env?: EggEnvType; + env?: EggEnvType[]; path?: string; package?: string; enable?: boolean; From ff21f750a7a31cacc41edc6afe5461529c5f2f2a Mon Sep 17 00:00:00 2001 From: whxaxes Date: Wed, 4 Apr 2018 16:56:22 +0800 Subject: [PATCH 12/17] feat: add IMiddleware --- index.d.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/index.d.ts b/index.d.ts index 06c5087646..b0d3ec481f 100644 --- a/index.d.ts +++ b/index.d.ts @@ -594,6 +594,8 @@ declare module 'egg' { controller: IController; Controller: Controller; + + middlewares: IMiddleware; } interface FileStream extends Readable { // tslint:disable-line @@ -867,10 +869,12 @@ declare module 'egg' { * * Now I can get ctx.service.foo at controller and other service file. */ - export interface IService { }// tslint:disable-line + export interface IService { } // tslint:disable-line export interface IController { } // tslint:disable-line + export interface IMiddleware { } // tslint:disable-line + export interface IHelper { /** * Generate URL path(without host) for route. Takes the route name and a map of named params. From f1711b54f9afb14486e853b09e5321f0d36d5f01 Mon Sep 17 00:00:00 2001 From: whxaxes Date: Wed, 4 Apr 2018 17:10:26 +0800 Subject: [PATCH 13/17] chore: add any key to static config --- index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.d.ts b/index.d.ts index b0d3ec481f..7c2c33b606 100644 --- a/index.d.ts +++ b/index.d.ts @@ -396,7 +396,7 @@ declare module 'egg' { preload: boolean; buffer: boolean; maxFiles: number; - }; + } & PlainObject; view: { root: string; From bbca0c025793b256a2006d677352555e5db5f5bc Mon Sep 17 00:00:00 2001 From: whxaxes Date: Wed, 4 Apr 2018 17:12:18 +0800 Subject: [PATCH 14/17] chore: rename middlewares to middleware --- index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.d.ts b/index.d.ts index 7c2c33b606..377933f34f 100644 --- a/index.d.ts +++ b/index.d.ts @@ -595,7 +595,7 @@ declare module 'egg' { Controller: Controller; - middlewares: IMiddleware; + middleware: IMiddleware; } interface FileStream extends Readable { // tslint:disable-line From 37b9b77e6c2734470a91d894578092f775a93cd7 Mon Sep 17 00:00:00 2001 From: whxaxes Date: Wed, 4 Apr 2018 17:24:52 +0800 Subject: [PATCH 15/17] chore: change order of PowerPartial --- index.d.ts | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/index.d.ts b/index.d.ts index 377933f34f..a0ee80789b 100644 --- a/index.d.ts +++ b/index.d.ts @@ -8,20 +8,6 @@ declare module 'egg' { // plain object type PlainObject = { [key: string]: T }; - /** - * Powerful Partial, Support adding ? modifier to a mapped property in three level - * @example - * import { PowerPartial, EggAppConfig } from 'egg'; - * - * // { view: { defaultEngines: string } } => { view?: { defaultEngines?: string } } - * type EggConfig = PowerPartial - */ - type PowerPartial = { - [U in keyof T]?: T[U] extends {} - ? { [V in keyof T[U]]?: T[U][V] extends {} ? Partial : T[U][V] } - : T[U] - }; - /** * BaseContextClass is a base class that can be extended, * it's instantiated in context level, @@ -960,4 +946,18 @@ declare module 'egg' { } export function startCluster(options: ClusterOptions, callback: (...args: any[]) => any): void; + + /** + * Powerful Partial, Support adding ? modifier to a mapped property in three level + * @example + * import { PowerPartial, EggAppConfig } from 'egg'; + * + * // { view: { defaultEngines: string } } => { view?: { defaultEngines?: string } } + * type EggConfig = PowerPartial + */ + type PowerPartial = { + [U in keyof T]?: T[U] extends {} + ? { [V in keyof T[U]]?: T[U][V] extends {} ? Partial : T[U][V] } + : T[U] + }; } From 4205e9be9184ffc97d6fbd22bac2d8e50d82caf7 Mon Sep 17 00:00:00 2001 From: whxaxes Date: Wed, 4 Apr 2018 17:57:54 +0800 Subject: [PATCH 16/17] chore: import all plugin --- index.d.ts | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/index.d.ts b/index.d.ts index a0ee80789b..79a9922ea9 100644 --- a/index.d.ts +++ b/index.d.ts @@ -3,6 +3,18 @@ import * as KoaApplication from 'koa'; import * as KoaRouter from 'koa-router'; import { RequestOptions } from 'urllib'; import { Readable } from 'stream'; +import 'egg-onerror'; +import 'egg-session'; +import 'egg-i18n'; +import 'egg-watcher'; +import 'egg-multipart'; +import 'egg-security'; +import 'egg-development'; +import 'egg-logrotator'; +import 'egg-schedule'; +import 'egg-static'; +import 'egg-jsonp'; +import 'egg-view'; declare module 'egg' { // plain object @@ -948,7 +960,7 @@ declare module 'egg' { export function startCluster(options: ClusterOptions, callback: (...args: any[]) => any): void; /** - * Powerful Partial, Support adding ? modifier to a mapped property in three level + * Powerful Partial, Support adding ? modifier to a mapped property in deep level * @example * import { PowerPartial, EggAppConfig } from 'egg'; * @@ -957,7 +969,7 @@ declare module 'egg' { */ type PowerPartial = { [U in keyof T]?: T[U] extends {} - ? { [V in keyof T[U]]?: T[U][V] extends {} ? Partial : T[U][V] } + ? PowerPartial : T[U] }; } From 745cb273fe2fc45c13feffd7c771a30ecbffb3a6 Mon Sep 17 00:00:00 2001 From: whxaxes Date: Wed, 4 Apr 2018 18:34:44 +0800 Subject: [PATCH 17/17] chore: remove middleware --- index.d.ts | 4 ---- 1 file changed, 4 deletions(-) diff --git a/index.d.ts b/index.d.ts index 79a9922ea9..00bd5243d2 100644 --- a/index.d.ts +++ b/index.d.ts @@ -592,8 +592,6 @@ declare module 'egg' { controller: IController; Controller: Controller; - - middleware: IMiddleware; } interface FileStream extends Readable { // tslint:disable-line @@ -871,8 +869,6 @@ declare module 'egg' { export interface IController { } // tslint:disable-line - export interface IMiddleware { } // tslint:disable-line - export interface IHelper { /** * Generate URL path(without host) for route. Takes the route name and a map of named params.