nocobase/docs/en-US/api/resourcer/middleware.md

169 lines
4.0 KiB
Markdown
Raw Normal View History

# Middleware
2023-02-10 08:57:56 +00:00
It is similar to the middleware of Koa, but with more enhanced features for easy extensions.
2023-02-10 08:57:56 +00: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.
2023-02-10 08:57:56 +00:00
## Constructor
2023-02-10 08:57:56 +00:00
**Signature**
* `constructor(options: Function)`
* `constructor(options: MiddlewareOptions)`
2023-02-10 08:57:56 +00:00
**Parameter**
2023-02-10 08:57:56 +00:00
| Name | Type | Default | Description |
| --- | --- | --- | --- |
2023-02-10 08:57:56 +00: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 |
2023-02-10 08:57:56 +00:00
**Example**
2023-02-10 08:57:56 +00:00
Simple definition:
```ts
const middleware = new Middleware((ctx, next) => {
await next();
});
```
2023-02-10 08:57:56 +00:00
Definition with relevant parameters:
```ts
const middleware = new Middleware({
only: ['create', 'update'],
async handler(ctx, next) {
await next();
},
});
```
2023-02-10 08:57:56 +00:00
## Instance Methods
### `getHandler()`
2023-02-10 08:57:56 +00:00
Get the orchestrated handler functions.
2023-02-10 08:57:56 +00:00
**Example**
2023-02-10 08:57:56 +00:00
The following middleware will output `1` and then `2` when requested.
```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 08:57:56 +00:00
Add a middleware function to the current middleware. Used to provide extension points for the middleware. See `getHandler()` for the examples.
2023-02-10 08:57:56 +00:00
**Signature**
* `use(middleware: Function)`
2023-02-10 08:57:56 +00:00
**Parameter**
2023-02-10 08:57:56 +00:00
| Name | Type | Default | Description |
| --- | --- | --- | --- |
2023-02-10 08:57:56 +00:00
| `middleware` | `Function` | - | Handler function of the middleware |
### `disuse()`
2023-02-10 08:57:56 +00:00
Remove the middleware functions that have been added to the current middleware.
2023-02-10 08:57:56 +00:00
**Signature**
* `disuse(middleware: Function)`
2023-02-10 08:57:56 +00:00
**Parameter**
2023-02-10 08:57:56 +00:00
| Name | Type | Default | Description |
| --- | --- | --- | --- |
2023-02-10 08:57:56 +00:00
| `middleware` | `Function` | - | Handler function of the middleware |
2023-02-10 08:57:56 +00:00
**Example**
2023-02-10 08:57:56 +00:00
The following example will only output `1` when requested, the output of `2` in fn1 will not be executed.
```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 08:57:56 +00:00
Check whether the current middleware is to be invoked for a specific action, it is usually handled by the resourcer internally.
2023-02-10 08:57:56 +00:00
**Signature**
* `canAccess(name: string): boolean`
2023-02-10 08:57:56 +00:00
**Parameter**
2023-02-10 08:57:56 +00:00
| Name | Type | Default | Description |
| --- | --- | --- | --- |
2023-02-10 08:57:56 +00:00
| `name` | `string` | - | Name of the action |
2023-02-10 08:57:56 +00:00
## Other Exports
### `branch()`
2023-02-10 08:57:56 +00:00
Create a branch middleware for branching in the middleware.
2023-02-10 08:57:56 +00:00
**Signature**
* `branch(map: { [key: string]: Function }, reducer: Function, options): Function`
2023-02-10 08:57:56 +00:00
**Parameter**
2023-02-10 08:57:56 +00:00
| Name | Type | Default | Description |
| --- | --- | --- | --- |
2023-02-10 08:57:56 +00: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 |
2023-02-10 08:57:56 +00:00
**Example**
2023-02-10 08:57:56 +00: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.
```ts
app.resourcer.use(branch({
'password': async (ctx, next) => {
// ...
},
'sms': async (ctx, next) => {
// ...
},
}, (ctx) => {
return ctx.action.params.authenticator ?? 'password';
}));
```