Update resource.md
This commit is contained in:
parent
5fa583eb27
commit
cdc73506de
@ -1,40 +1,40 @@
|
|||||||
# Resource
|
# Resource
|
||||||
|
|
||||||
Resource is used to define resource instance. Resource instances managed by Resourcer can be accessed through HTTP requests.
|
Resource is used to define resource instance. Resource instances managed by resourcer can be accessed through HTTP requests.
|
||||||
|
|
||||||
## Constructor
|
## Constructor
|
||||||
|
|
||||||
To create Resource instance. Normally it is not used directly, but replaced by the call of the `define()` interface of Resourcer manager.
|
To create resource instance. Normally it is not used directly, but replaced by the call of the `define()` interface of resourcer.
|
||||||
|
|
||||||
**签名**
|
**Signature**
|
||||||
|
|
||||||
* `constructor(options: ResourceOptions, resourcer: Resourcer)`
|
* `constructor(options: ResourceOptions, resourcer: Resourcer)`
|
||||||
|
|
||||||
**参数**
|
**Parameter**
|
||||||
|
|
||||||
| 参数名 | 类型 | 默认值 | 描述 |
|
| Name | Type | Default | Description |
|
||||||
| --- | --- | --- | --- |
|
| --- | --- | --- | --- |
|
||||||
| `options.name` | `string` | - | 资源名称,对应 URL 路由中的资源地址部分。 |
|
| `options.name` | `string` | - | Name of the resource, corresponding to the resource address in the route of the URL |
|
||||||
| `options.type` | `string` | `'single'` | 资源类型,可选项为 `'single'`、`'hasOne'`、`'hasMany'`、`'belongsTo'`、`'belongsToMany'`。 |
|
| `options.type` | `string` | `'single'` | Type of the resource, options are `'single'`, `'hasOne'`, `'hasMany'`, `'belongsTo'`, `'belongsToMany'` |
|
||||||
| `options.actions` | `Object` | - | 对资源可进行的操作列表,详见示例部分。 |
|
| `options.actions` | `Object` | - | List of actions that can be taken on the resource, see the example for details |
|
||||||
| `options.middlewares` | `MiddlewareType \| MiddlewareType[]` | - | 对当前定义资源进行任意操作访问时的中间件列表,详见示例部分。 |
|
| `options.middlewares` | `MiddlewareType \| MiddlewareType[]` | - | List of middlewares for any operational access to the resource that is defining,see the example for details |
|
||||||
| `options.only` | `ActionName[]` | `[]` | 针对全局操作的白名单列表,当数组中有值时(`length > 0`),只有数组中的操作可被访问。 |
|
| `options.only` | `ActionName[]` | `[]` | Whitelist for global actions, only actions contained in the array (if `length > 0`) can be accessed |
|
||||||
| `options.except` | `ActionName[]` | `[]` | 针对全局操作的黑名单列表,当数组中有值时(`length > 0`),除数组中的操作外,其他操作可被访问。 |
|
| `options.except` | `ActionName[]` | `[]` | Blacklist for global actions, all actions except those contained in the array (if `length > 0`) can be accessed |
|
||||||
| `resourcer` | `Resourcer` | - | 所属资源管理器实例。 |
|
| `resourcer` | `Resourcer` | - | The resourcer instance |
|
||||||
|
|
||||||
**示例**
|
**Example**
|
||||||
|
|
||||||
```ts
|
```ts
|
||||||
app.resourcer.define({
|
app.resourcer.define({
|
||||||
name: 'books',
|
name: 'books',
|
||||||
actions: {
|
actions: {
|
||||||
// 扩展的 action
|
// Extended action
|
||||||
publish(ctx, next) {
|
publish(ctx, next) {
|
||||||
ctx.body = 'ok';
|
ctx.body = 'ok';
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
middleware: [
|
middleware: [
|
||||||
// 扩展的中间件
|
// Extended middleware
|
||||||
async (ctx, next) => {
|
async (ctx, next) => {
|
||||||
await next();
|
await next();
|
||||||
}
|
}
|
||||||
@ -42,39 +42,39 @@ app.resourcer.define({
|
|||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
## 实例成员
|
## Instance Members
|
||||||
|
|
||||||
### `options`
|
### `options`
|
||||||
|
|
||||||
当前资源的配置项。
|
Configuration items for the current resource.
|
||||||
|
|
||||||
### `resourcer`
|
### `resourcer`
|
||||||
|
|
||||||
所属的资源管理器实例。
|
The resourcer instance to which the resource belongs.
|
||||||
|
|
||||||
### `middlewares`
|
### `middlewares`
|
||||||
|
|
||||||
已注册的中间件列表。
|
The registered middlewares.
|
||||||
|
|
||||||
### `actions`
|
### `actions`
|
||||||
|
|
||||||
已注册的操作映射表。
|
The registered mapping table of actions.
|
||||||
|
|
||||||
### `except`
|
### `except`
|
||||||
|
|
||||||
操作排除的名单列表。
|
Actions that are excluded.
|
||||||
|
|
||||||
## 实例方法
|
## Instance Methods
|
||||||
|
|
||||||
### `getName()`
|
### `getName()`
|
||||||
|
|
||||||
获取当前资源的名称。
|
Get the name of the current resource.
|
||||||
|
|
||||||
**签名**
|
**Signature**
|
||||||
|
|
||||||
* `getName(): string`
|
* `getName(): string`
|
||||||
|
|
||||||
**示例**
|
**Example**
|
||||||
|
|
||||||
```ts
|
```ts
|
||||||
const resource = app.resourcer.define({
|
const resource = app.resourcer.define({
|
||||||
@ -86,19 +86,19 @@ resource.getName(); // 'books'
|
|||||||
|
|
||||||
### `getAction()`
|
### `getAction()`
|
||||||
|
|
||||||
根据名称获取当前资源的操作。
|
Get action with the corresponding name.
|
||||||
|
|
||||||
**签名**
|
**Signature**
|
||||||
|
|
||||||
* `getAction(name: string): Action`
|
* `getAction(name: string): Action`
|
||||||
|
|
||||||
**参数**
|
**Parameter**
|
||||||
|
|
||||||
| 参数名 | 类型 | 默认值 | 描述 |
|
| Name | Type | Default | Description |
|
||||||
| --- | --- | --- | --- |
|
| --- | --- | --- | --- |
|
||||||
| `name` | `string` | - | 操作名称。 |
|
| `name` | `string` | - | Name of the action |
|
||||||
|
|
||||||
**示例**
|
**Example**
|
||||||
|
|
||||||
```ts
|
```ts
|
||||||
const resource = app.resourcer.define({
|
const resource = app.resourcer.define({
|
||||||
|
Loading…
Reference in New Issue
Block a user