2022-10-31 11:52:17 +08:00
|
|
|
|
# 概述
|
2022-05-19 00:40:55 +08:00
|
|
|
|
|
2022-10-31 11:52:17 +08:00
|
|
|
|
NocoBase 的 HTTP API 基于 Resource & Action 设计,是 REST API 的超集,操作不局限于增删改查,在 NocoBase 里,Resource Action 可以任意的扩展。
|
2022-05-19 00:40:55 +08:00
|
|
|
|
|
2022-10-31 11:52:17 +08:00
|
|
|
|
## 资源 Resource
|
2022-05-19 00:40:55 +08:00
|
|
|
|
|
2022-10-31 11:52:17 +08:00
|
|
|
|
在 NocoBase 里,资源(resource)有两种表达方式:
|
2022-05-19 00:40:55 +08:00
|
|
|
|
|
|
|
|
|
- `<collection>`
|
|
|
|
|
- `<collection>.<association>`
|
|
|
|
|
|
|
|
|
|
<Alert>
|
|
|
|
|
|
2022-10-31 11:52:17 +08:00
|
|
|
|
- collection 是所有抽象数据的集合
|
|
|
|
|
- association 为 collection 的关联数据
|
|
|
|
|
- resource 包括 collection 和 collection.association 两类
|
2022-05-19 00:40:55 +08:00
|
|
|
|
|
|
|
|
|
</Alert>
|
|
|
|
|
|
2022-10-31 11:52:17 +08:00
|
|
|
|
### 示例
|
2022-05-19 00:40:55 +08:00
|
|
|
|
|
2022-10-31 11:52:17 +08:00
|
|
|
|
- `posts` 文章
|
|
|
|
|
- `posts.user` 文章用户
|
|
|
|
|
- `posts.tags` 文章标签
|
2022-05-19 00:40:55 +08:00
|
|
|
|
|
2022-10-31 11:52:17 +08:00
|
|
|
|
## 操作 Action
|
2022-05-19 00:40:55 +08:00
|
|
|
|
|
2022-10-31 11:52:17 +08:00
|
|
|
|
以 `:<action>` 的方式表示资源操作
|
2022-05-19 00:40:55 +08:00
|
|
|
|
|
|
|
|
|
- `<collection>:<action>`
|
|
|
|
|
- `<collection>.<association>:<action>`
|
|
|
|
|
|
2022-10-31 11:52:17 +08:00
|
|
|
|
内置的全局操作,可用于 collection 或 association
|
2022-05-19 00:40:55 +08:00
|
|
|
|
|
|
|
|
|
- `create`
|
|
|
|
|
- `get`
|
|
|
|
|
- `list`
|
|
|
|
|
- `update`
|
|
|
|
|
- `destroy`
|
|
|
|
|
- `move`
|
|
|
|
|
|
2022-10-31 11:52:17 +08:00
|
|
|
|
内置的关联操作,仅用于 association
|
2022-05-19 00:40:55 +08:00
|
|
|
|
|
|
|
|
|
- `set`
|
|
|
|
|
- `add`
|
|
|
|
|
- `remove`
|
|
|
|
|
- `toggle`
|
|
|
|
|
|
2022-10-31 11:52:17 +08:00
|
|
|
|
### 示例
|
2022-05-19 00:40:55 +08:00
|
|
|
|
|
2022-10-31 11:52:17 +08:00
|
|
|
|
- `posts:create` 创建文章
|
|
|
|
|
- `posts.user:get` 查看文章用户
|
|
|
|
|
- `posts.tags:add` 附加文章标签(将现有的标签与文章关联)
|
2022-05-19 00:40:55 +08:00
|
|
|
|
|
2022-10-31 11:52:17 +08:00
|
|
|
|
## 请求 URL
|
2022-05-19 00:40:55 +08:00
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
<GET|POST> /api/<collection>:<action>
|
|
|
|
|
<GET|POST> /api/<collection>:<action>/<collectionIndex>
|
|
|
|
|
<GET|POST> /api/<collection>/<collectionIndex>/<association>:<action>
|
|
|
|
|
<GET|POST> /api/<collection>/<collectionIndex>/<association>:<action>/<associationIndex>
|
|
|
|
|
```
|
|
|
|
|
|
2022-10-31 11:52:17 +08:00
|
|
|
|
### 示例
|
2022-05-19 00:40:55 +08:00
|
|
|
|
|
2022-10-31 11:52:17 +08:00
|
|
|
|
posts 资源
|
2022-05-19 00:40:55 +08:00
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
POST /api/posts:create
|
|
|
|
|
GET /api/posts:list
|
|
|
|
|
GET /api/posts:get/1
|
|
|
|
|
POST /api/posts:update/1
|
|
|
|
|
POST /api/posts:destroy/1
|
|
|
|
|
```
|
|
|
|
|
|
2022-10-31 11:52:17 +08:00
|
|
|
|
posts.comments 资源
|
2022-05-19 00:40:55 +08:00
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
POST /api/posts/1/comments:create
|
|
|
|
|
GET /api/posts/1/comments:list
|
|
|
|
|
GET /api/posts/1/comments:get/1
|
|
|
|
|
POST /api/posts/1/comments:update/1
|
|
|
|
|
POST /api/posts/1/comments:destroy/1
|
|
|
|
|
```
|
|
|
|
|
|
2022-10-31 11:52:17 +08:00
|
|
|
|
posts.tags 资源
|
2022-05-19 00:40:55 +08:00
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
POST /api/posts/1/tags:create
|
|
|
|
|
GET /api/posts/1/tags:get
|
|
|
|
|
GET /api/posts/1/tags:list
|
|
|
|
|
POST /api/posts/1/tags:update
|
|
|
|
|
POST /api/posts/1/tags:destroy
|
|
|
|
|
POST /api/posts/1/tags:add
|
|
|
|
|
GET /api/posts/1/tags:remove
|
|
|
|
|
```
|
|
|
|
|
|
2022-10-31 11:52:17 +08:00
|
|
|
|
## 资源定位
|
2022-05-19 00:40:55 +08:00
|
|
|
|
|
2022-10-31 11:52:17 +08:00
|
|
|
|
- collection 资源,通过 `collectionIndex` 定位到待处理的数据,`collectionIndex` 必须唯一
|
|
|
|
|
- association 资源,通过 `collectionIndex` 和 `associationIndex` 联合定位待处理的数据,`associationIndex` 可能不是唯一的,但是 `collectionIndex` 和 `associationIndex` 的联合索引必须唯一
|
2022-05-19 00:40:55 +08:00
|
|
|
|
|
2022-10-31 11:52:17 +08:00
|
|
|
|
查看 association 资源详情时,请求的 URL 需要同时提供 `<collectionIndex>` 和 `<associationIndex>`,`<collectionIndex>` 并不多余,因为 `<associationIndex>` 可能不是唯一的。
|
2022-05-19 00:40:55 +08:00
|
|
|
|
|
2022-10-31 11:52:17 +08:00
|
|
|
|
例如 `tables.fields` 表示数据表的字段
|
2022-05-19 00:40:55 +08:00
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
GET /api/tables/table1/fields/title
|
|
|
|
|
GET /api/tables/table2/fields/title
|
|
|
|
|
```
|
|
|
|
|
|
2022-10-31 11:52:17 +08:00
|
|
|
|
table1 和 table2 都有 title 字段,title 在 table1 里是唯一的,但是其他表也可能有 title 字段
|
2022-05-19 00:40:55 +08:00
|
|
|
|
|
2022-10-31 11:52:17 +08:00
|
|
|
|
## 请求参数
|
2022-05-19 00:40:55 +08:00
|
|
|
|
|
2022-10-31 11:52:17 +08:00
|
|
|
|
请求的参数可以放在 Request 的 headers、parameters(query string)、body(GET 请求没有 body) 里。
|
2022-05-19 00:40:55 +08:00
|
|
|
|
|
2022-10-31 11:52:17 +08:00
|
|
|
|
几个特殊的 Parameters 请求参数
|
2022-05-19 00:40:55 +08:00
|
|
|
|
|
2022-10-31 11:52:17 +08:00
|
|
|
|
- `filter` 数据过滤,用于查询相关操作里;
|
|
|
|
|
- `filterByTk` 根据 tk 字段字过滤,用于指定详情数据的操作里;
|
|
|
|
|
- `sort` 排序,用于查询相关操作里。
|
|
|
|
|
- `fields` 输出哪些数据,用于查询相关操作里;
|
|
|
|
|
- `appends` 附加关系字段,用于查询相关操作里;
|
|
|
|
|
- `except` 排除哪些字段(不输出),用于查询相关操作里;
|
|
|
|
|
- `whitelist` 字段白名单,用于数据的创建和更新相关操作里;
|
|
|
|
|
- `blacklist` 字段黑名单,用于数据的创建和更新相关操作里;
|
2022-05-19 00:40:55 +08:00
|
|
|
|
|
|
|
|
|
### filter
|
|
|
|
|
|
2022-10-31 11:52:17 +08:00
|
|
|
|
数据过滤
|
2022-05-19 00:40:55 +08:00
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
# simple
|
|
|
|
|
GET /api/posts?filter[status]=publish
|
2022-10-31 11:52:17 +08:00
|
|
|
|
# 推荐使用 json string 的格式,需要 encodeURIComponent 编码
|
2022-05-19 00:40:55 +08:00
|
|
|
|
GET /api/posts?filter={"status":"published"}
|
|
|
|
|
|
|
|
|
|
# filter operators
|
|
|
|
|
GET /api/posts?filter[status.$eq]=publish
|
|
|
|
|
GET /api/posts?filter={"status.$eq":"published"}
|
|
|
|
|
|
|
|
|
|
# $and
|
|
|
|
|
GET /api/posts?filter={"$and": [{"status.$eq":"published"}, {"title.$includes":"a"}]}
|
|
|
|
|
# $or
|
|
|
|
|
GET /api/posts?filter={"$or": [{"status.$eq":"pending"}, {"status.$eq":"draft"}]}
|
|
|
|
|
|
|
|
|
|
# association field
|
|
|
|
|
GET /api/posts?filter[user.email.$includes]=gmail
|
|
|
|
|
GET /api/posts?filter={"user.email.$includes":"gmail"}
|
|
|
|
|
```
|
|
|
|
|
|
2022-10-31 11:52:17 +08:00
|
|
|
|
[点此查看更多关于 filter operators 的内容](http-api/filter-operators)
|
2022-05-19 00:40:55 +08:00
|
|
|
|
|
|
|
|
|
### filterByTk
|
|
|
|
|
|
2022-10-31 11:52:17 +08:00
|
|
|
|
根据 tk 字段过滤,默认情况:
|
2022-05-19 00:40:55 +08:00
|
|
|
|
|
2022-10-31 11:52:17 +08:00
|
|
|
|
- collection 资源,tk 为数据表的主键;
|
|
|
|
|
- association 资源,tk 为 association 的 targetKey 字段。
|
2022-05-19 00:40:55 +08:00
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
GET /api/posts:get?filterByTk=1&fields=name,title&appends=tags
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### sort
|
|
|
|
|
|
2022-10-31 11:52:17 +08:00
|
|
|
|
排序。降序时,字段前面加上减号 `-`。
|
2022-05-19 00:40:55 +08:00
|
|
|
|
|
|
|
|
|
```bash
|
2022-10-31 11:52:17 +08:00
|
|
|
|
# createAt 字段升序
|
|
|
|
|
GET /api/posts:get?sort=createdAt
|
|
|
|
|
# createAt 字段降序
|
|
|
|
|
GET /api/posts:get?sort=-createdAt
|
|
|
|
|
# 多个字段联合排序,createAt 字段降序、title A-Z 升序
|
|
|
|
|
GET /api/posts:get?sort=-createdAt,title
|
2022-05-19 00:40:55 +08:00
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### fields
|
|
|
|
|
|
2022-10-31 11:52:17 +08:00
|
|
|
|
输出哪些数据
|
2022-05-19 00:40:55 +08:00
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
GET /api/posts:list?fields=name,title
|
|
|
|
|
|
|
|
|
|
Response 200 (application/json)
|
|
|
|
|
{
|
|
|
|
|
"data": [
|
|
|
|
|
{
|
|
|
|
|
"name": "",
|
|
|
|
|
"title": ""
|
|
|
|
|
}
|
|
|
|
|
],
|
|
|
|
|
"meta": {}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### appends
|
|
|
|
|
|
2022-10-31 11:52:17 +08:00
|
|
|
|
附加关系字段
|
2022-05-19 00:40:55 +08:00
|
|
|
|
|
|
|
|
|
### except
|
|
|
|
|
|
2022-10-31 11:52:17 +08:00
|
|
|
|
排除哪些字段(不输出),用于查询相关操作里;
|
2022-05-19 00:40:55 +08:00
|
|
|
|
|
|
|
|
|
### whitelist
|
|
|
|
|
|
2022-10-31 11:52:17 +08:00
|
|
|
|
白名单
|
2022-05-19 00:40:55 +08:00
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
POST /api/posts:create?whitelist=title
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
"title": "My first post",
|
2022-10-31 11:52:17 +08:00
|
|
|
|
"date": "2022-05-19" # date 字段会被过滤掉,不会写入数据库
|
2022-05-19 00:40:55 +08:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### blacklist
|
|
|
|
|
|
2022-10-31 11:52:17 +08:00
|
|
|
|
黑名单
|
2022-05-19 00:40:55 +08:00
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
POST /api/posts:create?blacklist=date
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
"title": "My first post",
|
2022-10-31 11:52:17 +08:00
|
|
|
|
"date": "2022-05-19" # date 字段会被过滤掉,不会写入数据库
|
2022-05-19 00:40:55 +08:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2022-10-31 11:52:17 +08:00
|
|
|
|
## 请求响应
|
2022-05-19 00:40:55 +08:00
|
|
|
|
|
2022-10-31 11:52:17 +08:00
|
|
|
|
响应的格式
|
2022-05-19 00:40:55 +08:00
|
|
|
|
|
|
|
|
|
```ts
|
|
|
|
|
type ResponseResult = {
|
2022-10-31 11:52:17 +08:00
|
|
|
|
data?: any; // 主体数据
|
|
|
|
|
meta?: any; // 附加数据
|
|
|
|
|
errors?: ResponseError[]; // 报错
|
2022-05-19 00:40:55 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
type ResponseError = {
|
|
|
|
|
code?: string;
|
|
|
|
|
message: string;
|
|
|
|
|
};
|
|
|
|
|
```
|
|
|
|
|
|
2022-10-31 11:52:17 +08:00
|
|
|
|
### 示例
|
2022-05-19 00:40:55 +08:00
|
|
|
|
|
2022-10-31 11:52:17 +08:00
|
|
|
|
查看列表
|
2022-05-19 00:40:55 +08:00
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
GET /api/posts:list
|
|
|
|
|
|
|
|
|
|
Response 200 (application/json)
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
data: [
|
|
|
|
|
{
|
|
|
|
|
id: 1
|
|
|
|
|
}
|
|
|
|
|
],
|
|
|
|
|
meta: {
|
|
|
|
|
count: 1
|
|
|
|
|
page: 1,
|
|
|
|
|
pageSize: 1,
|
|
|
|
|
totalPage: 1
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2022-10-31 11:52:17 +08:00
|
|
|
|
查看详情
|
2022-05-19 00:40:55 +08:00
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
GET /api/posts:get/1
|
|
|
|
|
|
|
|
|
|
Response 200 (application/json)
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
data: {
|
|
|
|
|
id: 1
|
2022-10-31 11:52:17 +08:00
|
|
|
|
},
|
2022-05-19 00:40:55 +08:00
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2022-10-31 11:52:17 +08:00
|
|
|
|
报错
|
2022-05-19 00:40:55 +08:00
|
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
POST /api/posts:create
|
|
|
|
|
|
|
|
|
|
Response 400 (application/json)
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
errors: [
|
|
|
|
|
{
|
|
|
|
|
message: 'name must be required',
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
}
|
2022-10-31 11:52:17 +08:00
|
|
|
|
```
|