logologo
开始
手册
开发
插件
API
English
简体中文
开始
手册
开发
插件
API
English
简体中文
logologo
API Overview

@nocobase/auth

AuthManager
Auth
BaseAuth

@nocobase/cache

CacheManager
Cache

@nocobase/cli

NocoBase CLI
全局环境变量

@nocobase/client

Application
Plugin

@nocobase/database

Collection
Field

interfaces

BaseInterface
Filter Operators

RelationRepository

BelongsToManyRepository
belongs-to-repository
HasManyRepository
HasOneRepository
Repository

shared

create-options
destroy-options
find-one
find-options
transaction
update-options

@nocobase/data-source-manager

DataSourceManager
DataSource (abstract)
ICollectionManager
ICollection
IField
IModel
IRepository

@nocobase/flow-engine

DataSourceManager
FlowContext
FlowEngine
FlowModel
FlowResource

@nocobase/logger

Logger

@nocobase/server

AppCommand
Application
AuditManager
Context
Migration
Plugin

@nocobase/sdk

Auth
Storage
Previous PagePlugin
Next PageAuth

#APIClient

#概览

APIClient 基于 axios 封装,用于在客户端通过 HTTP, 请求 NocoBase 的资源操作。

#基本使用

class PluginSampleAPIClient extends Plugin {
  async load() {
    const res = await this.app.apiClient.request({
      // ...
    });
  }
}

#实例属性

#axios

axios 实例,可以访问 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;
};

#详细信息

#AxiosRequestConfig

通用的 axios 请求参数。参考 Request Config.

const res = await apiClient.request({ url: '' });
#ResourceActionOptions

NocoBase 资源操作请求参数。

const res = await apiClient.request({
  resource: 'users',
  action: 'list',
  params: {
    pageSize: 10,
  },
});
属性类型描述
resourcestring1. 资源名称,比如 a
2. 资源的关联对象名称,比如 a.b
resourceOfany当 resource 为资源的关联对象名称时,资源的主键值。比如 a.b 时,代表 a 的主键值
actionstring操作名称
paramsany请求参数对象,主要是 URL 参数,请求体放到 params.values 中
params.valuesany请求体对象

#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): IResource

#类型

export interface ActionParams {
  filterByTk?: any;
  [key: string]: any;
}

type ResourceAction = (params?: ActionParams) => Promise<any>;

export type IResource = {
  [key: string]: ResourceAction;
};

#详细信息

参数名类型描述
namestring1. 资源名称,比如 a
2. 资源的关联对象名称,比如 a.b
ofany当 resource 为资源的关联对象名称时,资源的主键值。比如 a.b 时,代表 a 的主键值
headersAxiosRequestHeaders后续要发起资源操作请求时,携带的 HTTP 请求头