Update index.md
This commit is contained in:
parent
a57c4f53f9
commit
a11c4bbe48
@ -1,8 +1,8 @@
|
|||||||
# Resourcer
|
# Resourcer
|
||||||
|
|
||||||
## 概览
|
## Overview
|
||||||
|
|
||||||
Nocobase 中的接口遵循面向资源的设计模式。Resourcer 主要用于管理 API 资源与路由。
|
The interfaces in NocoBase follow a resource-oriented design pattern. Resourcer is mainly used to manage API resources and routes.
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
const Koa = require('koa');
|
const Koa = require('koa');
|
||||||
@ -10,7 +10,7 @@ const { Resourcer } = require('@nocobase/resourcer');
|
|||||||
|
|
||||||
const resourcer = new Resourcer();
|
const resourcer = new Resourcer();
|
||||||
|
|
||||||
// 定义一个资源接口
|
// Define a resourcer
|
||||||
resourcer.define({
|
resourcer.define({
|
||||||
name: 'users',
|
name: 'users',
|
||||||
actions: {
|
actions: {
|
||||||
@ -31,37 +31,38 @@ resourcer.define({
|
|||||||
|
|
||||||
const app = new Koa();
|
const app = new Koa();
|
||||||
|
|
||||||
// 可在 koa 实例中使用
|
// Use the resourcer in instance koa
|
||||||
app.use(
|
app.use(
|
||||||
resourcer.middleware({
|
resourcer.middleware({
|
||||||
prefix: '/api', // resourcer 路由前缀
|
prefix: '/api', // Route prefix of resourcer
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
|
||||||
app.listen(3000);
|
app.listen(3000);
|
||||||
```
|
```
|
||||||
|
|
||||||
启动服务后,使用`curl`发起请求:
|
After starting the service, make request using `curl`:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
>$ curl localhost:3000/api/users
|
>$ curl localhost:3000/api/users
|
||||||
[{"name":"u1","age":18},{"name":"u2","age":20}]
|
[{"name":"u1","age":18},{"name":"u2","age":20}]
|
||||||
```
|
```
|
||||||
更多 Resourcer 的使用说明可参考[资源与操作](/development/guide/resources-actions)。 Resourcer 内置于 [NocoBase Application](/api/server/application#resourcer) ,可以通过 `app.resourcer` 访问。
|
|
||||||
|
|
||||||
|
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`.
|
||||||
|
|
||||||
## 构造函数
|
## Constructor
|
||||||
|
|
||||||
用于创建 Resourcer 管理器实例。
|
To create resourcer manager instances.
|
||||||
|
|
||||||
**签名**
|
**Signature**
|
||||||
|
|
||||||
* `constructor(options: ResourcerOptions)`
|
* `constructor(options: ResourcerOptions)`
|
||||||
|
|
||||||
**参数**
|
**Parameter**
|
||||||
|
|
||||||
| 参数名 | 类型 | 默认值 | 描述 |
|
| Name | Type | Default | Description |
|
||||||
| --- | --- | --- | --- |
|
| --- | --- | --- | --- |
|
||||||
| `prefix` | `string` | - | 路由路径前缀 |
|
| `prefix` | `string` | - | Route prefix |
|
||||||
| `accessors` | `Object` | _以下成员值_ | 默认操作方法名称标识 |
|
| `accessors` | `Object` | _以下成员值_ | 默认操作方法名称标识 |
|
||||||
| `accessors.list` | `string` | `'list'` | 列举操作方法名称标识 |
|
| `accessors.list` | `string` | `'list'` | 列举操作方法名称标识 |
|
||||||
| `accessors.get` | `string` | `'get'` | 获取操作方法名称标识 |
|
| `accessors.get` | `string` | `'get'` | 获取操作方法名称标识 |
|
||||||
@ -72,7 +73,7 @@ app.listen(3000);
|
|||||||
| `accessors.remove` | `string` | `'remove'` | 移除关联操作方法名称标识 |
|
| `accessors.remove` | `string` | `'remove'` | 移除关联操作方法名称标识 |
|
||||||
| `accessors.set` | `string` | `'set'` | 全量设置关联操作方法名称标识 |
|
| `accessors.set` | `string` | `'set'` | 全量设置关联操作方法名称标识 |
|
||||||
|
|
||||||
**示例**
|
**Example**
|
||||||
|
|
||||||
在创建 app 时件时,可以通过 `resourcer` 选项传入:
|
在创建 app 时件时,可以通过 `resourcer` 选项传入:
|
||||||
|
|
||||||
@ -91,15 +92,15 @@ const app = new Application({
|
|||||||
|
|
||||||
定义并向资源管理器注册一个资源对象。通常代替 `Resource` 类的构造函数使用。
|
定义并向资源管理器注册一个资源对象。通常代替 `Resource` 类的构造函数使用。
|
||||||
|
|
||||||
**签名**
|
**Signature**
|
||||||
|
|
||||||
* `define(options: ResourceOptions): Resource`
|
* `define(options: ResourceOptions): Resource`
|
||||||
|
|
||||||
**参数**
|
**Parameter**
|
||||||
|
|
||||||
详见 [Resource 构造函数](/api/server/resourcer/resource#构造函数)。
|
详见 [Resource 构造函数](/api/server/resourcer/resource#构造函数)。
|
||||||
|
|
||||||
**示例**
|
**Example**
|
||||||
|
|
||||||
```ts
|
```ts
|
||||||
app.resourcer.define({
|
app.resourcer.define({
|
||||||
@ -117,17 +118,17 @@ app.resourcer.define({
|
|||||||
|
|
||||||
检查对应名称的资源是否已被注册。
|
检查对应名称的资源是否已被注册。
|
||||||
|
|
||||||
**签名**
|
**Signature**
|
||||||
|
|
||||||
* `isDefined(name: string): boolean`
|
* `isDefined(name: string): boolean`
|
||||||
|
|
||||||
**参数**
|
**Parameter**
|
||||||
|
|
||||||
| 参数名 | 类型 | 默认值 | 描述 |
|
| Name | Type | Default | Description |
|
||||||
| --- | --- | --- | --- |
|
| --- | --- | --- | --- |
|
||||||
| `name` | `string` | - | 资源名称 |
|
| `name` | `string` | - | 资源名称 |
|
||||||
|
|
||||||
**示例**
|
**Example**
|
||||||
|
|
||||||
```ts
|
```ts
|
||||||
app.resourcer.isDefined('books'); // true
|
app.resourcer.isDefined('books'); // true
|
||||||
@ -137,20 +138,20 @@ app.resourcer.isDefined('books'); // true
|
|||||||
|
|
||||||
向资源管理器注册一个操作,可以指定针对特定的资源,如不指定资源名称,则认为是针对全局所有资源都可访问的操作。
|
向资源管理器注册一个操作,可以指定针对特定的资源,如不指定资源名称,则认为是针对全局所有资源都可访问的操作。
|
||||||
|
|
||||||
**签名**
|
**Signature**
|
||||||
|
|
||||||
* `registerAction(name: string, handler: HandlerType): void`
|
* `registerAction(name: string, handler: HandlerType): void`
|
||||||
|
|
||||||
**参数**
|
**Parameter**
|
||||||
|
|
||||||
| 参数名 | 类型 | 默认值 | 描述 |
|
| Name | Type | Default | Description |
|
||||||
| --- | --- | --- | --- |
|
| --- | --- | --- | --- |
|
||||||
| `name` | `string` | - | 操作名称 |
|
| `name` | `string` | - | 操作名称 |
|
||||||
| `handler` | `HandlerType` | - | 操作处理函数 |
|
| `handler` | `HandlerType` | - | 操作处理函数 |
|
||||||
|
|
||||||
`name` 的值如果以 `<resourceName>:` 开头则代表仅针对 `<resourceName>` 资源可访问,否则认为是全局操作。
|
`name` 的值如果以 `<resourceName>:` 开头则代表仅针对 `<resourceName>` 资源可访问,否则认为是全局操作。
|
||||||
|
|
||||||
**示例**
|
**Example**
|
||||||
|
|
||||||
```ts
|
```ts
|
||||||
// 注册后任意资源都可以进行 upload 操作
|
// 注册后任意资源都可以进行 upload 操作
|
||||||
@ -168,17 +169,17 @@ app.resourcer.registerAction('attachments:upload', async (ctx, next) => {
|
|||||||
|
|
||||||
向资源管理器注册多个操作的集合方法。
|
向资源管理器注册多个操作的集合方法。
|
||||||
|
|
||||||
**签名**
|
**Signature**
|
||||||
|
|
||||||
* `registerActions(actions: { [name: string]: HandlerType }): void`
|
* `registerActions(actions: { [name: string]: HandlerType }): void`
|
||||||
|
|
||||||
**参数**
|
**Parameter**
|
||||||
|
|
||||||
| 参数名 | 类型 | 默认值 | 描述 |
|
| Name | Type | Default | Description |
|
||||||
| --- | --- | --- | --- |
|
| --- | --- | --- | --- |
|
||||||
| `actions` | `{ [name: string]: HandlerType }` | - | 操作集合 |
|
| `actions` | `{ [name: string]: HandlerType }` | - | 操作集合 |
|
||||||
|
|
||||||
**示例**
|
**Example**
|
||||||
|
|
||||||
```ts
|
```ts
|
||||||
app.resourcer.registerActions({
|
app.resourcer.registerActions({
|
||||||
@ -195,17 +196,17 @@ app.resourcer.registerActions({
|
|||||||
|
|
||||||
获取对应名称的资源对象。
|
获取对应名称的资源对象。
|
||||||
|
|
||||||
**签名**
|
**Signature**
|
||||||
|
|
||||||
* `getResource(name: string): Resource`
|
* `getResource(name: string): Resource`
|
||||||
|
|
||||||
**参数**
|
**Parameter**
|
||||||
|
|
||||||
| 参数名 | 类型 | 默认值 | 描述 |
|
| Name | Type | Default | Description |
|
||||||
| --- | --- | --- | --- |
|
| --- | --- | --- | --- |
|
||||||
| `name` | `string` | - | 资源名称 |
|
| `name` | `string` | - | 资源名称 |
|
||||||
|
|
||||||
**示例**
|
**Example**
|
||||||
|
|
||||||
```ts
|
```ts
|
||||||
app.resourcer.getResource('books');
|
app.resourcer.getResource('books');
|
||||||
@ -215,19 +216,19 @@ app.resourcer.getResource('books');
|
|||||||
|
|
||||||
获取对应名称的操作处理函数。
|
获取对应名称的操作处理函数。
|
||||||
|
|
||||||
**签名**
|
**Signature**
|
||||||
|
|
||||||
* `getAction(name: string): HandlerType`
|
* `getAction(name: string): HandlerType`
|
||||||
|
|
||||||
**参数**
|
**Parameter**
|
||||||
|
|
||||||
| 参数名 | 类型 | 默认值 | 描述 |
|
| Name | Type | Default | Description |
|
||||||
| --- | --- | --- | --- |
|
| --- | --- | --- | --- |
|
||||||
| `name` | `string` | - | 操作名称 |
|
| `name` | `string` | - | 操作名称 |
|
||||||
|
|
||||||
`name` 的值如果以 `<resourceName>:` 开头则代表仅针对 `<resourceName>` 资源的操作,否则认为是全局操作。
|
`name` 的值如果以 `<resourceName>:` 开头则代表仅针对 `<resourceName>` 资源的操作,否则认为是全局操作。
|
||||||
|
|
||||||
**示例**
|
**Example**
|
||||||
|
|
||||||
```ts
|
```ts
|
||||||
app.resourcer.getAction('upload');
|
app.resourcer.getAction('upload');
|
||||||
@ -238,17 +239,17 @@ app.resourcer.getAction('attachments:upload');
|
|||||||
|
|
||||||
以 Koa 的形式注册一个中间件,中间件形成一个队列,并排在所有资源的操作处理函数之前执行。
|
以 Koa 的形式注册一个中间件,中间件形成一个队列,并排在所有资源的操作处理函数之前执行。
|
||||||
|
|
||||||
**签名**
|
**Signature**
|
||||||
|
|
||||||
* `use(middleware: Middleware | Middleware[]): void`
|
* `use(middleware: Middleware | Middleware[]): void`
|
||||||
|
|
||||||
**参数**
|
**Parameter**
|
||||||
|
|
||||||
| 参数名 | 类型 | 默认值 | 描述 |
|
| Name | Type | Default | Description |
|
||||||
| --- | --- | --- | --- |
|
| --- | --- | --- | --- |
|
||||||
| `middleware` | `Middleware \| Middleware[]` | - | 中间件 |
|
| `middleware` | `Middleware \| Middleware[]` | - | 中间件 |
|
||||||
|
|
||||||
**示例**
|
**Example**
|
||||||
|
|
||||||
```ts
|
```ts
|
||||||
app.resourcer.use(async (ctx, next) => {
|
app.resourcer.use(async (ctx, next) => {
|
||||||
@ -261,18 +262,18 @@ app.resourcer.use(async (ctx, next) => {
|
|||||||
|
|
||||||
生成一个兼容 Koa 的中间件,用于将资源的路由处理注入到应用中。
|
生成一个兼容 Koa 的中间件,用于将资源的路由处理注入到应用中。
|
||||||
|
|
||||||
**签名**
|
**Signature**
|
||||||
|
|
||||||
* `middleware(options: KoaMiddlewareOptions): KoaMiddleware`
|
* `middleware(options: KoaMiddlewareOptions): KoaMiddleware`
|
||||||
|
|
||||||
**参数**
|
**Parameter**
|
||||||
|
|
||||||
| 参数名 | 类型 | 默认值 | 描述 |
|
| Name | Type | Default | Description |
|
||||||
| --- | --- | --- | --- |
|
| --- | --- | --- | --- |
|
||||||
| `options.prefix?` | `string` | `''` | 路径前缀。 |
|
| `options.prefix?` | `string` | `''` | 路径前缀。 |
|
||||||
| `options.accessors?` | `Object` | `{}` | 常用方法的名称映射,与构造函数的 `accessors` 参数结构相同。 |
|
| `options.accessors?` | `Object` | `{}` | 常用方法的名称映射,与构造函数的 `accessors` 参数结构相同。 |
|
||||||
|
|
||||||
**示例**
|
**Example**
|
||||||
|
|
||||||
```ts
|
```ts
|
||||||
const koa = new Koa();
|
const koa = new Koa();
|
||||||
|
Loading…
Reference in New Issue
Block a user