Skip to content

Commit

Permalink
doc(typescript.md): Clarify the middleware's usages (#3039)
Browse files Browse the repository at this point in the history
  • Loading branch information
Maledong authored and atian25 committed Sep 29, 2018
1 parent 6bf812f commit 3999026
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 19 deletions.
60 changes: 52 additions & 8 deletions docs/source/en/tutorials/typescript.md
Expand Up @@ -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<EggAppConfig>;

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<any>) => {
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
Expand Down
63 changes: 53 additions & 10 deletions docs/source/zh-cn/tutorials/typescript.md
Expand Up @@ -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<any>) => {
// 你可以获取 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<EggAppConfig>;

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<any>) => {
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)

Expand Down
1 change: 0 additions & 1 deletion index.d.ts
Expand Up @@ -1127,5 +1127,4 @@ declare module 'egg' {

load(): void;
}

}

0 comments on commit 3999026

Please sign in to comment.