tachybase_todo/docs/en-US/api/resourcer/resource.md

115 lines
2.6 KiB
Markdown
Raw Normal View History

# Resource
2023-02-09 00:06:11 +08:00
Resource is used to define resource instance. Resource instances managed by resourcer can be accessed through HTTP requests.
2023-02-07 23:29:08 +08:00
## Constructor
2023-02-09 00:06:11 +08:00
To create resource instance. Normally it is not used directly, but replaced by the call of the `define()` interface of resourcer.
2023-02-09 00:06:11 +08:00
**Signature**
* `constructor(options: ResourceOptions, resourcer: Resourcer)`
2023-02-09 00:06:11 +08:00
**Parameter**
2023-02-09 00:06:11 +08:00
| Name | Type | Default | Description |
| --- | --- | --- | --- |
2023-02-09 00:06:11 +08:00
| `options.name` | `string` | - | Name of the resource, corresponding to the resource address in the route of the URL |
| `options.type` | `string` | `'single'` | Type of the resource, options are `'single'`, `'hasOne'`, `'hasMany'`, `'belongsTo'`, `'belongsToMany'` |
| `options.actions` | `Object` | - | List of actions that can be taken on the resource, see the example for details |
| `options.middlewares` | `MiddlewareType \| MiddlewareType[]` | - | List of middlewares for any operational access to the resource that is definingsee the example for details |
| `options.only` | `ActionName[]` | `[]` | Whitelist for global actions, only actions contained in the array (if `length > 0`) can be accessed |
| `options.except` | `ActionName[]` | `[]` | Blacklist for global actions, all actions except those contained in the array (if `length > 0`) can be accessed |
| `resourcer` | `Resourcer` | - | The resourcer instance |
2023-02-09 00:06:11 +08:00
**Example**
```ts
app.resourcer.define({
name: 'books',
actions: {
2023-02-09 00:06:11 +08:00
// Extended action
publish(ctx, next) {
ctx.body = 'ok';
}
},
middleware: [
2023-02-09 00:06:11 +08:00
// Extended middleware
async (ctx, next) => {
await next();
}
]
});
```
2023-02-09 00:06:11 +08:00
## Instance Members
### `options`
2023-02-09 00:06:11 +08:00
Configuration items for the current resource.
### `resourcer`
2023-02-09 00:06:11 +08:00
The resourcer instance to which the resource belongs.
### `middlewares`
2023-02-09 00:06:11 +08:00
The registered middlewares.
### `actions`
2023-02-09 00:06:11 +08:00
The registered mapping table of actions.
### `except`
2023-02-09 00:06:11 +08:00
Actions that are excluded.
2023-02-09 00:06:11 +08:00
## Instance Methods
### `getName()`
2023-02-09 00:06:11 +08:00
Get the name of the current resource.
2023-02-09 00:06:11 +08:00
**Signature**
* `getName(): string`
2023-02-09 00:06:11 +08:00
**Example**
```ts
const resource = app.resourcer.define({
name: 'books'
});
resource.getName(); // 'books'
```
### `getAction()`
2023-02-09 00:06:11 +08:00
Get action with the corresponding name.
2023-02-09 00:06:11 +08:00
**Signature**
* `getAction(name: string): Action`
2023-02-09 00:06:11 +08:00
**Parameter**
2023-02-09 00:06:11 +08:00
| Name | Type | Default | Description |
| --- | --- | --- | --- |
2023-02-09 00:06:11 +08:00
| `name` | `string` | - | Name of the action |
2023-02-09 00:06:11 +08:00
**Example**
```ts
const resource = app.resourcer.define({
name: 'books',
actions: {
publish(ctx, next) {
ctx.body = 'ok';
}
}
});
resource.getAction('publish'); // [Function: publish]
```