2022-10-31 03:52:17 +00:00
# Resourcer
2023-02-03 09:55:40 +00:00
## Overview
2022-11-13 15:00:59 +00:00
2023-02-08 15:51:52 +00:00
The interfaces in NocoBase follow a resource-oriented design pattern. Resourcer is mainly used to manage the resources and routes of API.
2022-11-13 15:00:59 +00:00
```javascript
const Koa = require('koa');
const { Resourcer } = require('@nocobase/resourcer');
const resourcer = new Resourcer();
2023-02-08 15:51:52 +00:00
// Define a resource interface
2022-11-13 15:00:59 +00:00
resourcer.define({
name: 'users',
actions: {
async list(ctx) {
ctx.body = [
{
name: "u1",
age: 18
},
{
name: "u2",
age: 20
}
]
}
},
});
const app = new Koa();
2023-02-08 15:51:52 +00:00
// Use the resourcer in koa instance
2022-11-13 15:00:59 +00:00
app.use(
resourcer.middleware({
2023-02-08 15:51:52 +00:00
prefix: '/api', // Route prefix of the resourcer
2022-11-13 15:00:59 +00:00
}),
);
app.listen(3000);
```
2022-10-31 03:52:17 +00:00
2023-02-08 15:51:52 +00:00
Once the service is started, make request using `curl` :
2023-02-03 09:55:40 +00:00
2022-11-13 15:00:59 +00:00
```bash
>$ curl localhost:3000/api/users
[{"name":"u1","age":18},{"name":"u2","age":20}]
2022-10-31 03:52:17 +00:00
```
2022-11-13 15:00:59 +00:00
2023-02-08 15:51:52 +00:00
More instructions of resourcer can be found in [Resources and Actions ](/development/guide/resources-actions ). Resourcer is built into [NocoBase Application ](/api/server/application#resourcer ), you can access it through `app.resourcer` .
2022-10-31 03:52:17 +00:00
2023-02-03 09:55:40 +00:00
## Constructor
2022-10-31 03:52:17 +00:00
2023-02-08 15:51:52 +00:00
To create resourcer instances.
2022-10-31 03:52:17 +00:00
2023-02-03 09:55:40 +00:00
**Signature**
2022-10-31 03:52:17 +00:00
* `constructor(options: ResourcerOptions)`
2023-02-03 09:55:40 +00:00
**Parameter**
2022-10-31 03:52:17 +00:00
2023-02-03 09:55:40 +00:00
| Name | Type | Default | Description |
2022-10-31 03:52:17 +00:00
| --- | --- | --- | --- |
2023-02-03 09:55:40 +00:00
| `prefix` | `string` | - | Route prefix |
2023-02-05 04:36:42 +00:00
| `accessors` | `Object` | _The following values of members_ | Name identifier of the default operation method |
| `accessors.list` | `string` | `'list'` | Name identifier of the list operation method |
| `accessors.get` | `string` | `'get'` | Name identifier of the get operation method |
| `accessors.create` | `string` | `'create'` | Name identifier of the create operation method |
| `accessors.update` | `string` | `'update'` | Name identifier of the update operation method |
| `accessors.delete` | `string` | `'destroy'` | Name identifier of the delete operation method |
| `accessors.add` | `string` | `'add'` | Name identifier of the add association operation method |
| `accessors.remove` | `string` | `'remove'` | Name identifier of the remove association operation method |
| `accessors.set` | `string` | `'set'` | Name identifier of the global set association operation method |
2022-10-31 03:52:17 +00:00
2023-02-03 09:55:40 +00:00
**Example**
2022-10-31 03:52:17 +00:00
2023-02-05 04:36:42 +00:00
Pass in through the `resourcer` option when creating app:
2022-10-31 03:52:17 +00:00
```ts
const app = new Application({
2023-02-05 04:36:42 +00:00
// Correspond to the configuration item of the default resourcer instance
2022-10-31 03:52:17 +00:00
resourcer: {
prefix: process.env.API_BASE_PATH
}
});
```
2023-02-05 04:36:42 +00:00
## Instance Methods
2022-10-31 03:52:17 +00:00
### `define()`
2023-02-08 15:51:52 +00:00
Define and register a resource object with the resourcer. Usually used instead of the constructor of the `Resource` class.
2022-10-31 03:52:17 +00:00
2023-02-03 09:55:40 +00:00
**Signature**
2022-10-31 03:52:17 +00:00
* `define(options: ResourceOptions): Resource`
2023-02-03 09:55:40 +00:00
**Parameter**
2022-10-31 03:52:17 +00:00
2023-02-07 15:24:08 +00:00
Refer to [Resource Constructor ](/api/server/resourcer/resource#constructor ) for details.
2022-10-31 03:52:17 +00:00
2023-02-03 09:55:40 +00:00
**Example**
2022-10-31 03:52:17 +00:00
```ts
app.resourcer.define({
name: 'books',
actions: {
2023-02-07 15:24:08 +00:00
// Extended action
2022-10-31 03:52:17 +00:00
publish(ctx, next) {
ctx.body = 'ok';
}
}
});
```
### `isDefined()`
2023-02-07 15:24:08 +00:00
Check whether the resource with the corresponding name has been registered.
2022-10-31 03:52:17 +00:00
2023-02-03 09:55:40 +00:00
**Signature**
2022-10-31 03:52:17 +00:00
* `isDefined(name: string): boolean`
2023-02-03 09:55:40 +00:00
**Parameter**
2022-10-31 03:52:17 +00:00
2023-02-03 09:55:40 +00:00
| Name | Type | Default | Description |
2022-10-31 03:52:17 +00:00
| --- | --- | --- | --- |
2023-02-07 15:24:08 +00:00
| `name` | `string` | - | Name of the resource |
2022-10-31 03:52:17 +00:00
2023-02-03 09:55:40 +00:00
**Example**
2022-10-31 03:52:17 +00:00
```ts
app.resourcer.isDefined('books'); // true
```
### `registerAction()`
2023-02-08 15:51:52 +00:00
Register an action with the resourcer. The action is accessible to a specified resource, or all resources if no resource name is specified.
2022-10-31 03:52:17 +00:00
2023-02-03 09:55:40 +00:00
**Signature**
2022-10-31 03:52:17 +00:00
* `registerAction(name: string, handler: HandlerType): void`
2023-02-03 09:55:40 +00:00
**Parameter**
2022-10-31 03:52:17 +00:00
2023-02-03 09:55:40 +00:00
| Name | Type | Default | Description |
2022-10-31 03:52:17 +00:00
| --- | --- | --- | --- |
2023-02-07 15:24:08 +00:00
| `name` | `string` | - | Name of the action |
| `handler` | `HandlerType` | - | Handler of the action |
2022-10-31 03:52:17 +00:00
2023-02-07 15:24:08 +00:00
A value of `name` starting with `<resourceName>:` means that the action is only accessible to `<resourceName>` rescource, otherwise it is considered as a global action.
2022-10-31 03:52:17 +00:00
2023-02-03 09:55:40 +00:00
**Example**
2022-10-31 03:52:17 +00:00
```ts
2023-02-07 15:24:08 +00:00
// All resources can take the upload action after registration
2022-10-31 03:52:17 +00:00
app.resourcer.registerAction('upload', async (ctx, next) => {
ctx.body = 'ok';
});
2023-02-07 15:24:08 +00:00
// Register the upload action only for attachments resource
2022-10-31 03:52:17 +00:00
app.resourcer.registerAction('attachments:upload', async (ctx, next) => {
ctx.body = 'ok';
});
```
### `registerActions()`
2023-02-08 15:51:52 +00:00
Register a set of actions with the resourcer.
2022-10-31 03:52:17 +00:00
2023-02-03 09:55:40 +00:00
**Signature**
2022-10-31 03:52:17 +00:00
* `registerActions(actions: { [name: string]: HandlerType }): void`
2023-02-03 09:55:40 +00:00
**Parameter**
2022-10-31 03:52:17 +00:00
2023-02-03 09:55:40 +00:00
| Name | Type | Default | Description |
2022-10-31 03:52:17 +00:00
| --- | --- | --- | --- |
2023-02-07 15:24:08 +00:00
| `actions` | `{ [name: string]: HandlerType }` | - | Set of actions |
2022-10-31 03:52:17 +00:00
2023-02-03 09:55:40 +00:00
**Example**
2022-10-31 03:52:17 +00:00
```ts
app.resourcer.registerActions({
upload: async (ctx, next) => {
ctx.body = 'ok';
},
'attachments:upload': async (ctx, next) => {
ctx.body = 'ok';
}
});
```
### `getResource()`
2023-02-07 15:24:08 +00:00
Get the resource object with the corresponding name.
2022-10-31 03:52:17 +00:00
2023-02-03 09:55:40 +00:00
**Signature**
2022-10-31 03:52:17 +00:00
* `getResource(name: string): Resource`
2023-02-03 09:55:40 +00:00
**Parameter**
2022-10-31 03:52:17 +00:00
2023-02-03 09:55:40 +00:00
| Name | Type | Default | Description |
2022-10-31 03:52:17 +00:00
| --- | --- | --- | --- |
2023-02-07 15:24:08 +00:00
| `name` | `string` | - | Name of the resource |
2022-10-31 03:52:17 +00:00
2023-02-03 09:55:40 +00:00
**Example**
2022-10-31 03:52:17 +00:00
```ts
app.resourcer.getResource('books');
```
### `getAction()`
2023-02-07 15:24:08 +00:00
Get the action handler function with the corresponding name.
2022-10-31 03:52:17 +00:00
2023-02-03 09:55:40 +00:00
**Signature**
2022-10-31 03:52:17 +00:00
* `getAction(name: string): HandlerType`
2023-02-03 09:55:40 +00:00
**Parameter**
2022-10-31 03:52:17 +00:00
2023-02-03 09:55:40 +00:00
| Name | Type | Default | Description |
2022-10-31 03:52:17 +00:00
| --- | --- | --- | --- |
2023-02-07 15:24:08 +00:00
| `name` | `string` | - | Name of the action |
2022-10-31 03:52:17 +00:00
2023-02-07 15:24:08 +00:00
A value of `name` starting with `<resourceName>:` means that the action is only accessible to `<resourceName>` rescource, otherwise it is considered as a global action.
2022-10-31 03:52:17 +00:00
2023-02-03 09:55:40 +00:00
**Example**
2022-10-31 03:52:17 +00:00
```ts
app.resourcer.getAction('upload');
app.resourcer.getAction('attachments:upload');
```
### `use()`
2023-02-07 15:24:08 +00:00
Register a middleware in the form of Koa; the middleware forms a queue which is executed before the action handlers of all resources.
2022-10-31 03:52:17 +00:00
2023-02-03 09:55:40 +00:00
**Signature**
2022-10-31 03:52:17 +00:00
* `use(middleware: Middleware | Middleware[]): void`
2023-02-03 09:55:40 +00:00
**Parameter**
2022-10-31 03:52:17 +00:00
2023-02-03 09:55:40 +00:00
| Name | Type | Default | Description |
2022-10-31 03:52:17 +00:00
| --- | --- | --- | --- |
2023-02-07 15:24:08 +00:00
| `middleware` | `Middleware \| Middleware[]` | - | Middleware |
2022-10-31 03:52:17 +00:00
2023-02-03 09:55:40 +00:00
**Example**
2022-10-31 03:52:17 +00:00
```ts
app.resourcer.use(async (ctx, next) => {
console.log(ctx.req.url);
await next();
});
```
### `middleware()`
2023-02-07 15:24:08 +00:00
Generate a Koa-compatible middleware for injecting routing processing of resources into the application.
2022-10-31 03:52:17 +00:00
2023-02-03 09:55:40 +00:00
**Signature**
2022-10-31 03:52:17 +00:00
* `middleware(options: KoaMiddlewareOptions): KoaMiddleware`
2023-02-03 09:55:40 +00:00
**Parameter**
2022-10-31 03:52:17 +00:00
2023-02-03 09:55:40 +00:00
| Name | Type | Default | Description |
2022-10-31 03:52:17 +00:00
| --- | --- | --- | --- |
2023-02-07 15:24:08 +00:00
| `options.prefix?` | `string` | `''` | Route prefix |
| `options.accessors?` | `Object` | `{}` | Name mapping for common methods, with the same parameter structure as `accessors` of the constructor |
2022-10-31 03:52:17 +00:00
2023-02-03 09:55:40 +00:00
**Example**
2022-10-31 03:52:17 +00:00
```ts
const koa = new Koa();
const resourcer = new Resourcer();
2023-02-08 15:51:52 +00:00
// Generate Koa-compatible middleware
2022-10-31 03:52:17 +00:00
koa.use(resourcer.middleware());
```