diff --git a/docs/source/en/tutorials/typescript.md b/docs/source/en/tutorials/typescript.md index fff8b87819..dd17dba68f 100644 --- a/docs/source/en/tutorials/typescript.md +++ b/docs/source/en/tutorials/typescript.md @@ -127,30 +127,74 @@ export interface NewsItem { import { Context } from 'egg'; -export default function robotMiddleware() { +// Your own middleware here +export default function fooMiddleware() { return async (ctx: Context, next: any) => { + // Get configs like this: + // const config = ctx.app.config; + // config.xxx.... await next(); }; } ``` -Middlewares support input parameters, and the first one is the config of the same name. If you have other requirements, a full version is needed: +When some property's name in config matches your middleware files's, Egg will automatically read out all of its sub properties. + +Let's assume you've got a middleware named `uuid`, and its config.default.js is: + +```javascript +'use strict'; + +import { EggAppConfig, PowerPartial } from 'egg'; + +export default function(appInfo: EggAppConfig) { + const config = {} as PowerPartial; + + config.keys = appInfo.name + '123123'; + + config.middleware = ['uuid']; + + config.security = { + csrf: { + ignore: '123', + }, + }; + + const bizConfig = { + local: { + msg: 'local', + }, + + uuid: { + name: 'ebuuid', + maxAge: 1000 * 60 * 60 * 24 * 365 * 10, + }, + }; + + return { + ...config, + ...bizConfig, + }; +} +``` +In `uuid` middleware: ```typescript -// app/middleware/news.ts +// app/middleware/uuid.ts -import { Context, Application } from 'egg'; -import { BizConfig } from '../../config/config.default'; +import { Context, Application, EggAppConfig } from 'egg'; -// We must use ['news'] instead of '.news', because 'BizConfig' is a type instead of instance -export default function newsMiddleware(options: BizConfig['news'], app: Application) { +export default function uuid(options: EggAppConfig['uuid'], app: Application): any { return async (ctx: Context, next: () => Promise) => { - console.info(options.serverUrl); + // The 'name' is just the sub prop in uuid in the config.default.js + console.info(options.name); await next(); }; } ``` +**Notice: The return value of any middleware must be `any` now, otherwise there's a compiling error about the compatibility of context between Koa's context in route.get/all and Egg's Context.** + ### Extend ```typescript diff --git a/docs/source/zh-cn/tutorials/typescript.md b/docs/source/zh-cn/tutorials/typescript.md index 1e08359c8a..88657e5ab7 100644 --- a/docs/source/zh-cn/tutorials/typescript.md +++ b/docs/source/zh-cn/tutorials/typescript.md @@ -123,33 +123,76 @@ export interface NewsItem { ### 中间件(Middleware) ```typescript -// app/middleware/robot.ts import { Context } from 'egg'; -export default function robotMiddleware() { - return async (ctx: Context, next: any) => { +// 这里是你自定义的中间件 +export default function fooMiddleware(): any { + return async (ctx: Context, next: () => Promise) => { + // 你可以获取 config 的配置: + // const config = ctx.app.config; + // config.xxx.... await next(); }; } ``` -因为 Middleware 定义是支持入参的,第一个参数为同名的 Config,如有需求,可以用完整版: +当某个 Middleware 文件的名称与 config 中某个属性名一致时,Middleware 会自动把这个属性下的所有配置读取过来。 + +我们假定你有一个 Middleware,名称是 uuid,其 config.default.js 中配置如下: + +```javascript +'use strict'; + +import { EggAppConfig, PowerPartial } from 'egg'; + +export default function(appInfo: EggAppConfig) { + const config = {} as PowerPartial; + + config.keys = appInfo.name + '123123'; + + config.middleware = ['uuid']; + + config.security = { + csrf: { + ignore: '123', + }, + }; + + const bizConfig = { + local: { + msg: 'local', + }, + + uuid: { + name: 'ebuuid', + maxAge: 1000 * 60 * 60 * 24 * 365 * 10, + }, + }; + + return { + ...config, + ...bizConfig, + }; +} + +``` +在对应的 uuid 中间件中: ```typescript -// app/middleware/news.ts +// app/middleware/uuid.ts -import { Context, Application } from 'egg'; -import { BizConfig } from '../../config/config.default'; +import { Context, Application, EggAppConfig } from 'egg'; -// 注意,这里必须要用 ['news'] 而不能用 .news,因为 BizConfig 是 type,不是实例 -export default function newsMiddleware(options: BizConfig['news'], app: Application) { +export default function uuidMiddleWare(options: EggAppConfig['uuid'], app: Application): any { return async (ctx: Context, next: () => Promise) => { - console.info(options.serverUrl); + // name 就是 config.default.js 中 uuid 下的属性 + console.info(options.name); await next(); }; } ``` +**注意:Middleware 目前返回值必须都是 `any`,否则使用 route.get/all 等方法的时候因为 Koa 的 `IRouteContext` 和 Egg 自身的 `Context` 不兼容导致编译报错。** ### 扩展(Extend) diff --git a/index.d.ts b/index.d.ts index 94e0c7995d..0f317902bf 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1127,5 +1127,4 @@ declare module 'egg' { load(): void; } - }