2022-10-31 11:52:17 +08:00
# 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.
2022-10-31 11:52:17 +08:00
2023-02-07 23:29:08 +08:00
## Constructor
2022-10-31 11:52:17 +08:00
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.
2022-10-31 11:52:17 +08:00
2023-02-09 00:06:11 +08:00
**Signature**
2022-10-31 11:52:17 +08:00
* `constructor(options: ResourceOptions, resourcer: Resourcer)`
2023-02-09 00:06:11 +08:00
**Parameter**
2022-10-31 11:52:17 +08:00
2023-02-09 00:06:11 +08:00
| Name | Type | Default | Description |
2022-10-31 11:52:17 +08:00
| --- | --- | --- | --- |
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 defining, see 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 |
2022-10-31 11:52:17 +08:00
2023-02-09 00:06:11 +08:00
**Example**
2022-10-31 11:52:17 +08:00
```ts
app.resourcer.define({
name: 'books',
actions: {
2023-02-09 00:06:11 +08:00
// Extended action
2022-10-31 11:52:17 +08:00
publish(ctx, next) {
ctx.body = 'ok';
}
},
middleware: [
2023-02-09 00:06:11 +08:00
// Extended middleware
2022-10-31 11:52:17 +08:00
async (ctx, next) => {
await next();
}
]
});
```
2023-02-09 00:06:11 +08:00
## Instance Members
2022-10-31 11:52:17 +08:00
### `options`
2023-02-09 00:06:11 +08:00
Configuration items for the current resource.
2022-10-31 11:52:17 +08:00
### `resourcer`
2023-02-09 00:06:11 +08:00
The resourcer instance to which the resource belongs.
2022-10-31 11:52:17 +08:00
### `middlewares`
2023-02-09 00:06:11 +08:00
The registered middlewares.
2022-10-31 11:52:17 +08:00
### `actions`
2023-02-09 00:06:11 +08:00
The registered mapping table of actions.
2022-10-31 11:52:17 +08:00
### `except`
2023-02-09 00:06:11 +08:00
Actions that are excluded.
2022-10-31 11:52:17 +08:00
2023-02-09 00:06:11 +08:00
## Instance Methods
2022-10-31 11:52:17 +08:00
### `getName()`
2023-02-09 00:06:11 +08:00
Get the name of the current resource.
2022-10-31 11:52:17 +08:00
2023-02-09 00:06:11 +08:00
**Signature**
2022-10-31 11:52:17 +08:00
* `getName(): string`
2023-02-09 00:06:11 +08:00
**Example**
2022-10-31 11:52:17 +08:00
```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.
2022-10-31 11:52:17 +08:00
2023-02-09 00:06:11 +08:00
**Signature**
2022-10-31 11:52:17 +08:00
* `getAction(name: string): Action`
2023-02-09 00:06:11 +08:00
**Parameter**
2022-10-31 11:52:17 +08:00
2023-02-09 00:06:11 +08:00
| Name | Type | Default | Description |
2022-10-31 11:52:17 +08:00
| --- | --- | --- | --- |
2023-02-09 00:06:11 +08:00
| `name` | `string` | - | Name of the action |
2022-10-31 11:52:17 +08:00
2023-02-09 00:06:11 +08:00
**Example**
2022-10-31 11:52:17 +08:00
```ts
const resource = app.resourcer.define({
name: 'books',
actions: {
publish(ctx, next) {
ctx.body = 'ok';
}
}
});
resource.getAction('publish'); // [Function: publish]
```