2022-10-31 11:52:17 +08:00
# Middleware
2023-02-10 16:57:56 +08:00
It is similar to the middleware of Koa, but with more enhanced features for easy extensions.
2022-10-31 11:52:17 +08:00
2023-02-10 16:57:56 +08:00
The defined middleware can be inserted for use in multiple places, such as the resourcer, and it is up to the developer for when to invoke it.
2022-10-31 11:52:17 +08:00
2023-02-10 16:57:56 +08:00
## Constructor
2022-10-31 11:52:17 +08:00
2023-02-10 16:57:56 +08:00
**Signature**
2022-10-31 11:52:17 +08:00
* `constructor(options: Function)`
* `constructor(options: MiddlewareOptions)`
2023-02-10 16:57:56 +08:00
**Parameter**
2022-10-31 11:52:17 +08:00
2023-02-10 16:57:56 +08:00
| Name | Type | Default | Description |
2022-10-31 11:52:17 +08:00
| --- | --- | --- | --- |
2023-02-10 16:57:56 +08:00
| `options` | `Function` | - | Handler function of middlware |
| `options` | `MiddlewareOptions ` | - | Configuration items of middlware |
| `options.only` | `string[]` | - | Only the specified actions are allowed |
| `options.except` | `string[]` | - | The specified actions are excluded |
| `options.handler` | `Function` | - | Handler function |
2022-10-31 11:52:17 +08:00
2023-02-10 16:57:56 +08:00
**Example**
2022-10-31 11:52:17 +08:00
2023-02-10 16:57:56 +08:00
Simple definition:
2022-10-31 11:52:17 +08:00
```ts
const middleware = new Middleware((ctx, next) => {
await next();
});
```
2023-02-10 16:57:56 +08:00
Definition with relevant parameters:
2022-10-31 11:52:17 +08:00
```ts
const middleware = new Middleware({
only: ['create', 'update'],
async handler(ctx, next) {
await next();
},
});
```
2023-02-10 16:57:56 +08:00
## Instance Methods
2022-10-31 11:52:17 +08:00
### `getHandler()`
2023-02-10 16:57:56 +08:00
Get the orchestrated handler functions.
2022-10-31 11:52:17 +08:00
2023-02-10 16:57:56 +08:00
**Example**
2022-10-31 11:52:17 +08:00
2023-02-10 16:57:56 +08:00
The following middleware will output `1` and then `2` when requested.
2022-10-31 11:52:17 +08:00
```ts
const middleware = new Middleware((ctx, next) => {
console.log(1);
await next();
});
middleware.use(async (ctx, next) => {
console.log(2);
await next();
});
app.resourcer.use(middleware.getHandler());
```
### `use()`
2023-02-10 16:57:56 +08:00
Add a middleware function to the current middleware. Used to provide extension points for the middleware. See `getHandler()` for the examples.
2022-10-31 11:52:17 +08:00
2023-02-10 16:57:56 +08:00
**Signature**
2022-10-31 11:52:17 +08:00
* `use(middleware: Function)`
2023-02-10 16:57:56 +08:00
**Parameter**
2022-10-31 11:52:17 +08:00
2023-02-10 16:57:56 +08:00
| Name | Type | Default | Description |
2022-10-31 11:52:17 +08:00
| --- | --- | --- | --- |
2023-02-10 16:57:56 +08:00
| `middleware` | `Function` | - | Handler function of the middleware |
2022-10-31 11:52:17 +08:00
### `disuse()`
2023-02-10 16:57:56 +08:00
Remove the middleware functions that have been added to the current middleware.
2022-10-31 11:52:17 +08:00
2023-02-10 16:57:56 +08:00
**Signature**
2022-10-31 11:52:17 +08:00
* `disuse(middleware: Function)`
2023-02-10 16:57:56 +08:00
**Parameter**
2022-10-31 11:52:17 +08:00
2023-02-10 16:57:56 +08:00
| Name | Type | Default | Description |
2022-10-31 11:52:17 +08:00
| --- | --- | --- | --- |
2023-02-10 16:57:56 +08:00
| `middleware` | `Function` | - | Handler function of the middleware |
2022-10-31 11:52:17 +08:00
2023-02-10 16:57:56 +08:00
**Example**
2022-10-31 11:52:17 +08:00
2023-02-10 16:57:56 +08:00
The following example will only output `1` when requested, the output of `2` in fn1 will not be executed.
2022-10-31 11:52:17 +08:00
```ts
const middleware = new Middleware((ctx, next) => {
console.log(1);
await next();
});
async function fn1(ctx, next) {
console.log(2);
await next();
}
middleware.use(fn1);
app.resourcer.use(middleware.getHandler());
middleware.disuse(fn1);
```
### `canAccess()`
2023-02-10 16:57:56 +08:00
Check whether the current middleware is to be invoked for a specific action, it is usually handled by the resourcer internally.
2022-10-31 11:52:17 +08:00
2023-02-10 16:57:56 +08:00
**Signature**
2022-10-31 11:52:17 +08:00
* `canAccess(name: string): boolean`
2023-02-10 16:57:56 +08:00
**Parameter**
2022-10-31 11:52:17 +08:00
2023-02-10 16:57:56 +08:00
| Name | Type | Default | Description |
2022-10-31 11:52:17 +08:00
| --- | --- | --- | --- |
2023-02-10 16:57:56 +08:00
| `name` | `string` | - | Name of the action |
2022-10-31 11:52:17 +08:00
2023-02-10 16:57:56 +08:00
## Other Exports
2022-10-31 11:52:17 +08:00
### `branch()`
2023-02-10 16:57:56 +08:00
Create a branch middleware for branching in the middleware.
2022-10-31 11:52:17 +08:00
2023-02-10 16:57:56 +08:00
**Signature**
2022-10-31 11:52:17 +08:00
* `branch(map: { [key: string]: Function }, reducer: Function, options): Function`
2023-02-10 16:57:56 +08:00
**Parameter**
2022-10-31 11:52:17 +08:00
2023-02-10 16:57:56 +08:00
| Name | Type | Default | Description |
2022-10-31 11:52:17 +08:00
| --- | --- | --- | --- |
2023-02-10 16:57:56 +08:00
| `map` | `{ [key: string]: Function }` | - | Mapping table of the branch handler function, key names are given by subsequent calculation functions when called |
| `reducer` | `(ctx) => string` | - | Calculation function, it is used to calculate the key name of the branch based on the context |
| `options?` | `Object` | - | Configuration items of the branch |
| `options.keyNotFound?` | `Function` | `ctx.throw(404)` | Handler function when key name is not found |
| `options.handlerNotSet?` | `Function` | `ctx.throw(404)` | The function when no handler function is defined |
2022-10-31 11:52:17 +08:00
2023-02-10 16:57:56 +08:00
**Example**
2022-10-31 11:52:17 +08:00
2023-02-10 16:57:56 +08:00
When authenticating user, determine what to do next according to the value of the `authenticator` parameter in the query section of the request URL.
2022-10-31 11:52:17 +08:00
```ts
app.resourcer.use(branch({
'password': async (ctx, next) => {
// ...
},
'sms': async (ctx, next) => {
// ...
},
}, (ctx) => {
return ctx.action.params.authenticator ?? 'password';
}));
```