Skip to content

Commit

Permalink
chore: fix middleware typo (#6007)
Browse files Browse the repository at this point in the history
Co-authored-by: 名字 <邮箱>
  • Loading branch information
sorrycc committed Jan 16, 2021
1 parent db022e8 commit 90fc5d0
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 28 deletions.
8 changes: 4 additions & 4 deletions docs/plugins/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,14 +213,14 @@ api.skipPlugins(['@umijs/plugin-dva']);

通过 `api.registerMethod()` 扩展的方法。

### addBeforeMiddewares
### addBeforeMiddlewares

添加在 webpack compiler 中间件之前的中间件,返回值格式为 express 中间件。

e.g.

```ts
api.addBeforeMiddewares(() => {
api.addBeforeMiddlewares(() => {
return (req, res, next) => {
if (false) {
res.end('end');
Expand Down Expand Up @@ -379,14 +379,14 @@ api.addHTMLHeadScripts(() => {
});
```

### addMiddewares
### addMiddlewares

添加在 webpack compiler 中间件之后的中间件,返回值格式为 express 中间件。

e.g.

```ts
api.addMiddewares(async (ctx: Context, next: any) => {
api.addMiddlewares(async (ctx: Context, next: any) => {
// do something before request
await next();
// do something after request
Expand Down
8 changes: 4 additions & 4 deletions docs/plugins/api.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,14 +214,14 @@ api.skipPlugins(['@umijs/plugin-dva']);

通过 `api.registerMethod()` 扩展的方法。

### addBeforeMiddewares
### addBeforeMiddlewares

添加在 webpack compiler 中间件之前的中间件,返回值格式为 express 中间件。

e.g.

```ts
api.addBeforeMiddewares(() => {
api.addBeforeMiddlewares(() => {
return (req, res, next) => {
if (false) {
res.end('end');
Expand Down Expand Up @@ -380,14 +380,14 @@ api.addHTMLHeadScripts(() => {
});
```

### addMiddewares
### addMiddlewares

添加在 webpack compiler 中间件之后的中间件,返回值格式为 express 中间件。

e.g.

```ts
api.addMiddewares(async (ctx: Context, next: any) => {
api.addMiddlewares(async (ctx: Context, next: any) => {
// do something before request
await next();
// do something after request
Expand Down
40 changes: 28 additions & 12 deletions packages/preset-built-in/src/plugins/commands/dev/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,18 +154,34 @@ export default (api: IApi) => {
bundleImplementor,
});

const beforeMiddlewares = await api.applyPlugins({
key: 'addBeforeMiddewares',
type: api.ApplyPluginsType.add,
initialValue: [],
args: {},
});
const middlewares = await api.applyPlugins({
key: 'addMiddewares',
type: api.ApplyPluginsType.add,
initialValue: [],
args: {},
});
const beforeMiddlewares = [
...(await api.applyPlugins({
key: 'addBeforeMiddewares',
type: api.ApplyPluginsType.add,
initialValue: [],
args: {},
})),
...(await api.applyPlugins({
key: 'addBeforeMiddlewares',
type: api.ApplyPluginsType.add,
initialValue: [],
args: {},
})),
];
const middlewares = [
...(await api.applyPlugins({
key: 'addMiddewares',
type: api.ApplyPluginsType.add,
initialValue: [],
args: {},
})),
...(await api.applyPlugins({
key: 'addMiddlewares',
type: api.ApplyPluginsType.add,
initialValue: [],
args: {},
})),
];

server = new Server({
...opts,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export default function (api: IApi) {
...(userConfig?.mock?.exclude || []),
];

api.addBeforeMiddewares(async () => {
api.addBeforeMiddlewares(async () => {
const checkConflictPaths = async (
mockRes: IGetMockDataResult,
): Promise<void> => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default (api: IApi) => {
},
});

api.addBeforeMiddewares(() => {
api.addBeforeMiddlewares(() => {
return (req, res, next) => {
if (req.path.includes('@@/devScripts.js')) {
api
Expand Down
2 changes: 2 additions & 0 deletions packages/preset-built-in/src/plugins/registerMethods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ export default function (api: IApi) {
'onPatchRoutes',
'onPatchRoutesBefore',
'onDevCompileDone',
'addBeforeMiddlewares',
'addBeforeMiddewares',
'addDepInfo',
'addDevScripts',
'addMiddlewares',
'addMiddewares',
'addRuntimePlugin',
'addRuntimePluginKey',
Expand Down
2 changes: 2 additions & 0 deletions packages/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,9 @@ export interface IApi extends PluginAPI {
addEntryCode: IAdd<null, string>;
addEntryCodeAhead: IAdd<null, string>;
addTmpGenerateWatcherPaths: IAdd<null, string>;
addBeforeMiddlewares: IAdd<{ service: Service }, RequestHandler<any>>;
addBeforeMiddewares: IAdd<{ service: Service }, RequestHandler<any>>;
addMiddlewares: IAdd<{ service: Service }, RequestHandler<any>>;
addMiddewares: IAdd<{ service: Service }, RequestHandler<any>>;
}

Expand Down
6 changes: 0 additions & 6 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3524,12 +3524,6 @@
"@types/connect" "*"
"@types/node" "*"

"@types/cheerio@0.22.21":
version "0.22.21"
resolved "https://registry.yarnpkg.com/@types/cheerio/-/cheerio-0.22.21.tgz#5e37887de309ba11b2e19a6e14cad7874b31a8a3"
dependencies:
"@types/node" "*"

"@types/cheerio@0.22.23":
version "0.22.23"
resolved "https://registry.yarnpkg.com/@types/cheerio/-/cheerio-0.22.23.tgz#74bcfee9c5ee53f619711dca953a89fe5cfa4eb4"
Expand Down

0 comments on commit 90fc5d0

Please sign in to comment.