tachybase_todo/docs/en-US/api/http/index.md

302 lines
6.3 KiB
Markdown
Raw Normal View History

2022-10-31 23:20:27 +08:00
# Overview
2022-12-23 17:16:46 +08:00
HTTP API of NocoBase is designed based on Resource & Action, a superset of REST API. The operation includes but not limited to create, read, update and delete. Resource Action can be extended arbitrarily in NocoBase.
2022-10-31 23:20:27 +08:00
## Resource
2022-12-23 17:53:52 +08:00
In NocoBase, resource has two expressions:
2022-10-31 23:20:27 +08:00
- `<collection>`
- `<collection>.<association>`
<Alert>
2022-12-23 17:53:52 +08:00
- Collection is the set of all abstract data
- Association is the associated data of collection
- Resource includes both collection and collection.association
2022-10-31 23:20:27 +08:00
</Alert>
### Example
- `posts` Post
- `posts.user` Post user
- `posts.tags` Post tags
## Action
2022-12-23 17:53:52 +08:00
Action on resource is expressed by `:<action>`
2022-10-31 23:20:27 +08:00
- `<collection>:<action>`
- `<collection>.<association>:<action>`
2022-12-23 17:53:52 +08:00
Built-in global actions for collection or association:
2022-10-31 23:20:27 +08:00
- `create`
- `get`
- `list`
- `update`
- `destroy`
- `move`
2022-12-23 17:53:52 +08:00
Built-in association actions for association only:
2022-10-31 23:20:27 +08:00
- `set`
- `add`
- `remove`
- `toggle`
### Example
2022-12-23 17:16:46 +08:00
- `posts:create` Create post
2022-12-23 17:53:52 +08:00
- `posts.user:get` Get post user
- `posts.tags:add` Add tags to post (associate existing tags with post)
2022-10-31 23:20:27 +08:00
## Request URL
```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>
```
### Example
posts resource
```bash
POST /api/posts:create
GET /api/posts:list
GET /api/posts:get/1
POST /api/posts:update/1
POST /api/posts:destroy/1
```
posts.comments resource
```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
```
posts.tags resource
```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-12-23 17:16:46 +08:00
## Locate Resource
2022-10-31 23:20:27 +08:00
2022-12-23 17:53:52 +08:00
- Collection resource locates the data to be processed by `collectionIndex`, `collectionIndex` must be unique.
- Association resource locates the data to be processed by `collectionIndex` and `associationIndex` jointly, `associationIndex` may not be unique, but the joint index of `collectionIndex` and `associationIndex` must be unique.
2022-10-31 23:20:27 +08:00
2022-12-23 17:53:52 +08:00
When viewing details of association resource, the requested URL needs to provide both `<collectionIndex>` and `<associationIndex>`, `<collectionIndex>` is necessary as `<associationIndex>` may not be unique.
2022-10-31 23:20:27 +08:00
2022-12-23 17:16:46 +08:00
For example, `tables.fields` represents the fields of a data table:
2022-10-31 23:20:27 +08:00
```bash
GET /api/tables/table1/fields/title
GET /api/tables/table2/fields/title
```
2022-12-23 17:16:46 +08:00
Both table1 and table2 have the title field, title is unique in one table, but other tables may also have fields of that name.
2022-10-31 23:20:27 +08:00
2022-12-23 17:16:46 +08:00
## Request Parameters
2022-10-31 23:20:27 +08:00
2022-12-23 17:16:46 +08:00
Request parameters can be placed in the headers, parameters (query string), and body (GET requests do not have a body) of the request.
2022-10-31 23:20:27 +08:00
2022-12-23 17:16:46 +08:00
Some special request parameters:
2022-10-31 23:20:27 +08:00
2022-12-23 17:16:46 +08:00
- `filter` Data filtering, used in actions related to query.
- `filterByTk` Filter by tk field, used in actions to specify details of data.
- `sort` Sorting, used in actions related to query.
- `fields` Date to output, used in actions related to query
- `appends` Fields of additional relationship, used in actions related to query.
- `except` Exclude some fields (not to output), used in actions related to query.
- `whitelist` Fields whitelist, used in actions related to data creation and update.
- `blacklist` Fields blacklist, used in actions related to data creation and update.
2022-10-31 23:20:27 +08:00
### filter
2022-12-23 17:16:46 +08:00
Data filtering.
2022-10-31 23:20:27 +08:00
```bash
# simple
GET /api/posts?filter[status]=publish
2022-12-23 17:16:46 +08:00
# json string format is recommended, which requires encodeURIComponent encoding
2022-10-31 23:20:27 +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"}
```
[Click here for more information about filter operators](http-api/filter-operators)
### filterByTk
2022-12-23 17:16:46 +08:00
Filter by tk field. In the default settings:
2022-10-31 23:20:27 +08:00
2022-12-23 17:16:46 +08:00
- collection resource: tk is the primary key of the data table.
- association resource: tk is the targetKey field of the association.
2022-10-31 23:20:27 +08:00
```bash
GET /api/posts:get?filterByTk=1&fields=name,title&appends=tags
```
### sort
2022-12-23 17:16:46 +08:00
Sorting. To sort in the descending order, put `-` in front of the field.
2022-10-31 23:20:27 +08:00
```bash
2022-12-23 17:16:46 +08:00
# Sort createAt field in the ascending order
2022-10-31 23:20:27 +08:00
GET /api/posts:get?sort=createdAt
2022-12-23 17:16:46 +08:00
# Sort createAt field in the descending order
2022-10-31 23:20:27 +08:00
GET /api/posts:get?sort=-createdAt
2022-12-23 17:16:46 +08:00
# Sort multiple fields jointly, createAt field descending, title A-Z ascending
2022-10-31 23:20:27 +08:00
GET /api/posts:get?sort=-createdAt,title
```
### fields
2022-12-23 17:16:46 +08:00
Data to output.
2022-10-31 23:20:27 +08:00
```bash
GET /api/posts:list?fields=name,title
Response 200 (application/json)
{
"data": [
{
"name": "",
"title": ""
}
],
"meta": {}
}
```
### appends
2022-12-23 17:16:46 +08:00
Fields of additional relationship.
2022-10-31 23:20:27 +08:00
### except
2022-12-23 17:16:46 +08:00
Exclude some fields (not to output), used in actions related to query.
2022-10-31 23:20:27 +08:00
### whitelist
2022-12-23 17:16:46 +08:00
Whitelist.
2022-10-31 23:20:27 +08:00
```bash
POST /api/posts:create?whitelist=title
{
"title": "My first post",
2022-12-23 17:16:46 +08:00
"date": "2022-05-19" # The date field will be filtered out and not be written to the database
2022-10-31 23:20:27 +08:00
}
```
### blacklist
2022-12-23 17:16:46 +08:00
Blacklist.
2022-10-31 23:20:27 +08:00
```bash
POST /api/posts:create?blacklist=date
{
"title": "My first post",
2022-12-23 17:16:46 +08:00
"date": "2022-05-19" # The date field will be filtered out and not be written to the database
2022-10-31 23:20:27 +08:00
}
```
## Request Response
2022-12-23 17:53:52 +08:00
Format of the response:
2022-10-31 23:20:27 +08:00
```ts
type ResponseResult = {
2022-12-23 17:16:46 +08:00
data?: any; // Main data
2022-10-31 23:20:27 +08:00
meta?: any; // Additional Data
errors?: ResponseError[]; // Errors
};
type ResponseError = {
code?: string;
message: string;
};
```
### Example
2022-12-23 17:16:46 +08:00
View list:
2022-10-31 23:20:27 +08:00
```bash
GET /api/posts:list
Response 200 (application/json)
{
data: [
{
id: 1
}
],
meta: {
count: 1
page: 1,
pageSize: 1,
totalPage: 1
},
}
```
2022-12-23 17:16:46 +08:00
View details:
2022-10-31 23:20:27 +08:00
```bash
GET /api/posts:get/1
Response 200 (application/json)
{
data: {
id: 1
}
}
```
2022-12-23 17:16:46 +08:00
Error:
2022-10-31 23:20:27 +08:00
```bash
POST /api/posts:create
Response 400 (application/json)
{
errors: [
{
message: 'name must be required',
},
],
}
```