📖 文档
指南
APIConfig 说明

APIConfig 说明

APIConfig 定义了 tsdk 的 API 基本配置规范,内容如下:

src/shared/tsdk-types.ts
import * as z from 'zod';
 
export const APITypes = {
  user: 'user',
  admin: 'admin',
  common: 'common',
} as const;
 
export const APITypesKey = Object.keys(APITypes).filter((item) => item !== APITypes.common);
 
export type APIType = keyof typeof APITypes;
 
export interface APIConfig {
  /** The API type. Like: user side or admin side. */
  type: APIType;
  /** The API path */
  path: string;
  method: 'get' | 'post' | 'delete' | 'put' | 'patch' | 'head' | 'options';
  /** Request data validate scheme */
  schema?: z.ZodTypeAny;
  /** The API need auth? Default is false */
  needAuth?: boolean;
  /** The API disabled? Default is false */
  disabled?: boolean;
  /** The API description */
  description: string;
  /** The API category */
  category?: string;
 
  /** custom headers for client */
  headers?: { [key: string]: any };
  /**
   * is params in url? for generate API sdk base documentation.
   * default undefined,
   * if `:`, will support `/api/:a/b/:c`,
   * if `{}`, will support `/api/{a}/b/{c}`,
   * and will replace with data with {a: 1, c: 2} to `/api/1/b/2`  */
  paramsInUrl?: ':' | '{}';
  /** Force the API is fetch data, for sometimes the backend API is all `post` method */
  isGet?: boolean;
}

字段说明

  • type - API 类型
  • path - API 路径
  • method - HTTP 请求方法
  • description - API 备注
  • category - 可选,API 模块分类
  • schema - 可选,运行时类型校验表达式,使用 zod
  • needAuth - 可选,是否需要登录才能访问
  • disabled - 可选,是否禁止 API
  • headers - 可选,自定义 headers
  • paramsInUrl - 可选,值为 {} | :, 用来替换 url 中的参数
  • isGet - 可选,值为 true 时,生成 SWR 或者 ReactQuery,即使 method 不是 get 方法,也会被当作 get 方法看待;为 false,则当作非 get 看待,生成更新类型的 hook