UNPKG

@shencom/request

Version:

封装 axios 请求,兼容了 uni-app 的 uni.request 和 uni.uploadFile 的适配器

32 lines (31 loc) 1.25 kB
import type { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios'; declare module 'axios' { interface AxiosRequestConfig { isFilterEmpty?: boolean; errcode?: string; } } type Dictionary<T = any> = Record<string, T>; export type Platforms = true | 'weixin' | 'ali' | 'h5' | 'app'; export interface ScHeaders extends Dictionary { scid: string; Authorization?: string | null; miniProgram: Platforms; } export interface ScResponse<T> { data: T; errcode: string; errmsg: string; } export interface Config<D = Dictionary> extends AxiosRequestConfig<D> { headers?: Partial<ScHeaders>; } export type ScRequest = <R = Dictionary, D = Dictionary, CustomRes = false>(url: string, data?: D, config?: Config<D>) => Promise<CustomRes extends true ? R : ScResponse<R>>; export interface CustomAxiosInstance extends Omit<AxiosInstance, 'post' | 'get'> { post: ScRequest; get: ScRequest; upload: typeof __upload; } declare const instance: CustomAxiosInstance; declare const __upload: <R = Dictionary<any>, D = Dictionary<any>, CustomRes = false>(url: string, data: D, config?: Config<D> | undefined) => Promise<CustomRes extends true ? AxiosResponse<R, D> : ScResponse<R>>; export { instance };