docs/docs/en/api/sdk/index.md
APIClient is a wrapper based on <a href="https://axios-http.com/" target="_blank">axios</a>, used to request NocoBase resource actions on the client side via HTTP.
class PluginSampleAPIClient extends Plugin {
async load() {
const res = await this.app.apiClient.request({
// ...
});
}
}
axiosThe axios instance, which can be used to access the axios API, for example, apiClient.axios.interceptors.
authClient-side authentication class, see Auth.
storageClient-side storage class, see Storage.
constructor()Constructor, creates an APIClient instance.
constructor(instance?: APIClientOptions)interface ExtendedOptions {
authClass?: any;
storageClass?: any;
}
export type APIClientOptions =
| AxiosInstance
| (AxiosRequestConfig & ExtendedOptions);
request()Initiates an HTTP request.
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;
};
General axios request parameters. See <a href="https://axios-http.com/docs/req_config" target="_blank">Request Config</a>.
const res = await apiClient.request({ url: '' });
NocoBase resource action request parameters.
const res = await apiClient.request({
resource: 'users',
action: 'list',
params: {
pageSize: 10,
},
});
| Property | Type | Description |
|---|---|---|
resource | string | 1. Resource name, e.g., a |
a.b |
| resourceOf | any | When resource is the name of the resource's associated object, it is the primary key value of the resource. For example, for a.b, it represents the primary key value of a. |
| action | string | Action name |
| params | any | Request parameter object, mainly URL parameters. The request body is placed in params.values. |
| params.values | any | Request body object |resource()Gets the NocoBase resource action method object.
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;
};
| Parameter | Type | Description |
|---|---|---|
name | string | 1. Resource name, e.g., a |
a.b |
| of | any | When name is the name of the resource's associated object, it is the primary key value of the resource. For example, for a.b, it represents the primary key value of a. |
| headers | AxiosRequestHeaders | HTTP headers to include in subsequent resource action requests. |