docs/docs/cn/api/sdk/index.md
APIClient 基于 <a href="https://axios-http.com/" target="_blank">axios</a> 封装,用于在客户端通过 HTTP, 请求 NocoBase 的资源操作。
class PluginSampleAPIClient extends Plugin {
async load() {
const res = await this.app.apiClient.request({
// ...
});
}
}
axiosaxios 实例,可以访问 axios API, 比如 apiClient.axios.interceptors.
auth客户端鉴权类,参考 Auth.
storage客户端存储类,参考 Storage.
constructor()构造函数,创建一个 APIClient 实例。
constructor(instance?: APIClientOptions)interface ExtendedOptions {
authClass?: any;
storageClass?: any;
}
export type APIClientOptions =
| AxiosInstance
| (AxiosRequestConfig & ExtendedOptions);
request()发起 HTTP 请求。
request<T = any, R = AxiosResponse<T>, D = any>(config: AxiosRequestConfig<D> | ResourceActionOptions): Promise<R>type ResourceActionOptions<P = any> = {
resource?: string;
resourceOf?: any;
action?: string;
params?: P;
};
通用的 axios 请求参数。参考 <a href="https://axios-http.com/docs/req_config" target="_blank">Request Config</a>.
const res = await apiClient.request({ url: '' });
NocoBase 资源操作请求参数。
const res = await apiClient.request({
resource: 'users',
action: 'list',
params: {
pageSize: 10,
},
});
| 属性 | 类型 | 描述 |
|---|---|---|
resource | string | 1. 资源名称,比如 a |
a.b |
| resourceOf | any | 当 resource 为资源的关联对象名称时,资源的主键值。比如 a.b 时,代表 a 的主键值 |
| action | string | 操作名称 |
| params | any | 请求参数对象,主要是 URL 参数,请求体放到 params.values 中 |
| params.values | any | 请求体对象 |resource()获取 NocoBase 资源操作方法对象。
const resource = apiClient.resource('users');
await resource.create({
values: {
username: 'admin',
},
});
const res = await resource.list({
page: 2,
pageSize: 20,
});
resource(name: string, of?: any, headers?: AxiosRequestHeaders): IResourceexport interface ActionParams {
filterByTk?: any;
[key: string]: any;
}
type ResourceAction = (params?: ActionParams) => Promise<any>;
export type IResource = {
[key: string]: ResourceAction;
};
| 参数名 | 类型 | 描述 |
|---|---|---|
name | string | 1. 资源名称,比如 a |
a.b |
| of | any | 当 resource 为资源的关联对象名称时,资源的主键值。比如 a.b 时,代表 a 的主键值 |
| headers | AxiosRequestHeaders | 后续要发起资源操作请求时,携带的 HTTP 请求头 |