import { ParameterLocation } from 'openapi3-ts'; import { Options as JsonpOptions } from 'fetch-jsonp'; import { Method, AxiosError, AxiosResponse, AxiosRequestConfig as AxiosOptions, } from 'axios'; export type StrOrNum = string | number; export type AnyFunction = (...args: any[]) => any; export type AnyPromiseFunction = (...args: any[]) => Promise; export type Mock = AnyFunction | any; export type ParamsConfig = string[] | ParamsObject | ((args?: Record) => Record); export type Middleware = (ctx: T, next: () => Promise) => Promise; export type RuntimeOptions = WxRuntimeOptions | WebRuntimeOptions; export type PathType = ( | string | ((params?: Record) => string) ); export type ReqType = ( | 'wx' | 'WX' | 'axios' | 'AXIOS' | 'jsonp' | 'JSONP' | 'custom' | 'CUSTOM' ); export type ParamsObject = { [k: string]: ( | { in?: ParameterLocation, default?: StrOrNum, required?: true, isRequired?: true, } | StrOrNum ) }; export interface CtxReqFnParams extends Omit { url: string, customFetch?: (p: { url: string, data: any, body?: any, method: Method, header: Record, }) => Promise, } export interface CtxReq extends RuntimeOptionsOnly { baseUrl: string, method: Method, args: any, path: PathType, apiName: string, params: Record reqType: ReqType, reqFnParams: CtxReqFnParams, reqParams: Record, mock?: Mock, prefix?: string, axiosOptions?: AxiosOptions, jsonpOptions?: JsonpOptions, [k: string]: any } export interface CtxRes extends AxiosResponse { rawData: T, error?: Error | AxiosError, [k: string]: any } export interface Ctx { req: CtxReq, res: CtxRes, endTime: number, reqTime: number, startTime: number, [k: string]: any } export type CommonApiConfig = { /** * @see [baseUrl 接口基础地址](https://tua-api.pages.woa.com/config/common.html#baseurl-%E6%8E%A5%E5%8F%A3%E5%9F%BA%E7%A1%80%E5%9C%B0%E5%9D%80) */ baseUrl?: string, /** * @see [method 请求类型](https://tua-api.pages.woa.com/config/common.html#method-%E8%AF%B7%E6%B1%82%E7%B1%BB%E5%9E%8B) */ method?: Method, /** * @see [mock 模拟接口数据](https://tua-api.pages.woa.com/config/common.html#mock-%E6%A8%A1%E6%8B%9F%E6%8E%A5%E5%8F%A3%E6%95%B0%E6%8D%AE) */ mock?: Mock, /** * @see [prefix 接口中间地址](https://tua-api.pages.woa.com/config/common.html#prefix-%E6%8E%A5%E5%8F%A3%E4%B8%AD%E9%97%B4%E5%9C%B0%E5%9D%80) */ prefix?: string, /** * @see [reqType 请求使用库类型](https://tua-api.pages.woa.com/config/common.html#reqtype-%E8%AF%B7%E6%B1%82%E4%BD%BF%E7%94%A8%E5%BA%93%E7%B1%BB%E5%9E%8B) */ reqType?: ReqType, /** * @see [afterFn 收到响应后的钩子函数](https://tua-api.pages.woa.com/config/common.html#afterfn-%E6%94%B6%E5%88%B0%E5%93%8D%E5%BA%94%E5%90%8E%E7%9A%84%E9%92%A9%E5%AD%90%E5%87%BD%E6%95%B0) */ afterFn?: (args: [T?, Ctx?]) => any, /** * @see [beforeFn 发起请求前钩子函数](https://tua-api.pages.woa.com/config/common.html#beforefn-%E5%8F%91%E8%B5%B7%E8%AF%B7%E6%B1%82%E5%89%8D%E9%92%A9%E5%AD%90%E5%87%BD%E6%95%B0) */ beforeFn?: () => Promise, /** * @see [middleware 中间件函数数组](https://tua-api.pages.woa.com/config/default.html#middleware-%E4%B8%AD%E9%97%B4%E4%BB%B6%E5%87%BD%E6%95%B0%E6%95%B0%E7%BB%84) */ middleware?: Middleware[], /** * @see [header 请求头](https://tua-api.pages.woa.com/config/common.html#header-%E8%AF%B7%E6%B1%82%E5%A4%B4) */ header?: Record, /** * @see [customFetch 自定义请求函数](https://tua-api.pages.woa.com/config/default.html#customfetch-%E8%87%AA%E5%AE%9A%E4%B9%89%E8%AF%B7%E6%B1%82%E5%87%BD%E6%95%B0) */ customFetch?: AnyPromiseFunction, /** * @see [commonParams 公共参数](https://tua-api.pages.woa.com/config/common.html#commonparams-%E5%85%AC%E5%85%B1%E5%8F%82%E6%95%B0) */ commonParams?: Record | ((args?: Record) => Record), /** * @see [axiosOptions 透传参数配置](https://tua-api.pages.woa.com/config/common.html#axiosoptions-%E9%80%8F%E4%BC%A0%E5%8F%82%E6%95%B0%E9%85%8D%E7%BD%AE) */ axiosOptions?: AxiosOptions, /** * @see [jsonpOptions 透传参数配置](https://tua-api.pages.woa.com/config/common.html#jsonpoptions-%E9%80%8F%E4%BC%A0%E5%8F%82%E6%95%B0%E9%85%8D%E7%BD%AE) */ jsonpOptions?: JsonpOptions, /** * @see [useGlobalMiddleware 使用全局中间件](https://tua-api.pages.woa.com/config/common.html#useglobalmiddleware-%E4%BD%BF%E7%94%A8%E5%85%A8%E5%B1%80%E4%B8%AD%E9%97%B4%E4%BB%B6) */ useGlobalMiddleware?: boolean }; export interface SelfApiConfigOnly { /** * @see [path 接口地址](https://tua-api.pages.woa.com/config/self.html#path-%E6%8E%A5%E5%8F%A3%E5%9C%B0%E5%9D%80) */ path: PathType, /** * @see [name 接口名称(可省略)](https://tua-api.pages.woa.com/config/self.html#name-%E6%8E%A5%E5%8F%A3%E5%90%8D%E7%A7%B0-%E5%8F%AF%E7%9C%81%E7%95%A5) */ name?: string, /** * @see [params 接口参数](https://tua-api.pages.woa.com/config/self.html#params-%E6%8E%A5%E5%8F%A3%E5%8F%82%E6%95%B0) */ params?: ParamsConfig, body?: ( | { required: true } | { isRequired: true } | any ) } // for wechat miniprogram export type WxApiConfigOnly = { /** * @see [isShowLoading (小程序 only)](https://tua-api.pages.woa.com/config/common.html#isshowloading-%E5%B0%8F%E7%A8%8B%E5%BA%8F-only) */ name?: string, /** * @see [showLoadingFn (小程序 only)](https://tua-api.pages.woa.com/config/common.html#showloadingfn-%E5%B0%8F%E7%A8%8B%E5%BA%8F-only) */ params?: ParamsConfig, body?: ( | { required: true } | { isRequired: true } | any ) }; export interface SelfApiConfig extends CommonApiConfig, SelfApiConfigOnly {} // for web export type WebApiConfig = CommonApiConfig & { pathList: SelfApiConfig[] }; // for wechat miniprogram export interface WxApiOnly { isShowLoading?: boolean showLoadingFn?: AnyFunction hideLoadingFn?: AnyFunction } export interface WxApiConfig extends CommonApiConfig, WxApiOnly { pathList: (SelfApiConfig & WxApiOnly)[] } export interface RuntimeOptionsOnly { body?: any params?: Record, header?: Record, callback?: string, callbackName?: string, } export interface WxRuntimeOptions extends Partial>, WxApiOnly, RuntimeOptionsOnly { } export interface WebRuntimeOptions extends Partial>, RuntimeOptionsOnly { } export interface Api { key: string, mock: Mock, params: ParamsConfig, , U = Record | void> ( params?: U, runtimeOptions?: RuntimeOptions ): Promise, } export interface Apis { [k: string]: SyncFnMap } export interface SyncFnMap { [k: string]: Api } export interface DefaultApiConfig { baseUrl?: string, reqType?: ReqType, middleware?: Middleware[], customFetch?: AnyPromiseFunction, axiosOptions?: AxiosOptions, jsonpOptions?: JsonpOptions, defaultErrorData?: any, }