import { objectToFormData } from 'object-to-formdata' declare type ResponseType = | 'text' | 'blob' | 'json' | 'document' | 'arraybuffer' | 'JSON' | 'base64' declare enum EngineName { /** 微信小程序 api */ WX = 'wx', /** 支付宝小程序 api */ MY = 'my', /** xhr api */ XHR = 'xhr', /** fetch api */ Fetch = 'fetch', } interface ProgressEv { total: number transmitted: number progress: number } interface HttpHeaders { [key: string]: string } declare type RequestData = | { [key in string | number]: any } | FormData interface RequestSharedConfig { method: string /** * 对应: responseType、dataType * Default: json * */ responseType: ResponseType /** * 微信 wx 和支付宝 my 中不支持,设置无效果 * Default: false * */ withCredentials: boolean /** * Default: 30000 * * fetch 不支持 * */ timeout: number /** * Default: { 'Content-Type': 'application/x-www-form-urlencoded' } * */ headers: HttpHeaders convertFormDataOptions?: Parameters[1] & { customConvertFn?(data: Exclude): FormData } /** * Default: status => status === undefined || (200 <= status < 300) * */ statusValidator?(status?: number): boolean [key: string]: any [key: number]: any } interface RequestEngineConfig extends RequestSharedConfig { url: string data?: RequestData | null /** do not work in engine: wx, my, fetch */ onUploadProgress?(ev: ProgressEv): void /** do not work in engine: wx, my, fetch */ onDownloadProgress?(ev: ProgressEv): void } interface RequestConfig extends RequestSharedConfig { baseURL: string } interface RequestResponse { /** api url */ url: string data: T statusCode: number headers: HttpHeaders [key: string]: any } interface DownloadEngineConfig { url: string headers: HttpHeaders /** * 微信小程序配置,支付宝小程序、H5 中设置无效果 * */ timeout: RequestSharedConfig['timeout'] /** * 微信小程序配置,H5 中设置表示需要生成临时 url,支付宝小程序中设置无效果 * */ filePath: string withCredentials?: boolean convertFormDataOptions?: RequestSharedConfig['convertFormDataOptions'] onDownloadProgress?(ev: ProgressEv): void [key: string]: any } interface DownloadResponse { /** api url */ url: string /** base64 url */ tempFilePath: string filePath: string statusCode: number /** filename that get from server */ filename: string blob?: Blob headers?: { [k: string]: string } [key: string]: any } interface UploadEngineConfig { url: string timeout: RequestSharedConfig['timeout'] headers: HttpHeaders /** * 对应微信、支付宝小程序中的 filePath * H5 中只能使用 blob | file 类型 * */ file: string | Blob | File /** * 文件对应的 key * 对应微信小程序中的 name * 对应支付宝小程序中的 fileName * */ fileKey: string /** * 文件类型 * 支付宝小程序中需要 * */ fileType?: 'image' | 'video' | 'audio' extraData: RequestData withCredentials?: boolean convertFormDataOptions?: RequestSharedConfig['convertFormDataOptions'] onUploadProgress?(ev: ProgressEv): void [key: string]: any } interface RequestEngine { name: EngineName config: Config response: Response | RequestResponse requestInstance: any requestTask: any aborted: boolean open(): Promise abort(): void } declare type ConfigInterceptor = (config: any) => any declare type ResponseInterceptor = (response: any) => any declare type RequestError = Error & { $request?: RequestEngine [key: string]: any [key: number]: any } declare type ErrorHandler = (e: RequestError) => any interface HttpInterceptors { request: { interceptors: ConfigInterceptor[] use(resolve: ConfigInterceptor): void } response: { interceptors: { resolves: ResponseInterceptor[] rejects: ErrorHandler[] } use(resolve?: ResponseInterceptor, reject?: ErrorHandler): void } } declare class BaseEngine { config: Config requestInstance: any requestTask: any response: Response | RequestResponse aborted: boolean constructor(config: Config) } declare class FetchBase extends BaseEngine { name: EngineName constructor(config: Config) abort(): void getConfig(): any } declare class Fetch extends FetchBase> implements RequestEngine> { open(): Promise> } declare class FetchDownload extends FetchBase implements RequestEngine { open(): Promise } declare class FetchUpload extends FetchBase> implements RequestEngine> { open(): Promise> getConfig(): any } declare class MYBase extends BaseEngine { name: EngineName constructor(config: Config) abort(): void } declare class MY extends MYBase> implements RequestEngine> { getConfig(): { url: string method: string data: | { [x: string]: any [x: number]: any } | FormData | null | undefined headers: HttpHeaders header: HttpHeaders timeout: number dataType: ResponseType } open(): Promise> } declare class MYDownload extends MYBase implements RequestEngine { getConfig(): { url: string header: HttpHeaders headers: HttpHeaders } open(): Promise } declare class MYUpload extends MYBase> implements RequestEngine> { getConfig(): { url: string filePath: string | Blob | File fileName: string fileType: 'image' | 'video' | 'audio' header: HttpHeaders headers: HttpHeaders formData: RequestData } open(): Promise> } declare class WXBase extends BaseEngine { name: EngineName constructor(config: Config) abort(): void } declare class WX extends WXBase> implements RequestEngine> { getConfig(): { url: string data: | { [x: string]: any [x: number]: any } | FormData | null | undefined method: string dataType: ResponseType timeout: number header: HttpHeaders headers: HttpHeaders } open(): Promise> } declare class WXDownload extends WXBase implements RequestEngine { getConfig(): { url: string timeout: number header: HttpHeaders headers: HttpHeaders filePath: string } open(): Promise } declare class WXUpload extends WXBase> implements RequestEngine> { getConfig(): { url: string timeout: number header: HttpHeaders headers: HttpHeaders } open(): Promise> } declare class XhrBase extends BaseEngine { name: EngineName constructor(config: Config) abort(): void } declare class Xhr extends XhrBase> implements RequestEngine> { open(): Promise> } declare class XhrDownload extends XhrBase implements RequestEngine { open(): Promise } declare class XhrUpload extends XhrBase> implements RequestEngine> { open(): Promise> } declare class RequestPromise { promise: Promise abort: () => void constructor( cb: ( resolve: (value?: T | PromiseLike) => void, reject: (reason?: any) => void, ) => void, ) then( onfulfilled?: | ((value: T) => TResult1 | PromiseLike) | undefined | null, onrejected?: | ((reason: any) => TResult2 | PromiseLike) | undefined | null, ): RequestPromise catch( onrejected?: | ((reason: any) => TResult | PromiseLike) | undefined | null, ): RequestPromise finally(cb: () => void): RequestPromise } declare class Http { engineName: EngineName config: RequestConfig interceptors: HttpInterceptors constructor(engineName?: EngineName, config?: Partial) calcConfig( config?: any, ): { config: Promise abort: () => void } getRequestInstance( config: RequestEngineConfig, ): WX | MY | Fetch | Xhr getDownloadInstance( config: DownloadEngineConfig, ): FetchDownload | MYDownload | WXDownload | XhrDownload getUploadInstance( config: UploadEngineConfig, ): WXUpload | MYUpload | FetchUpload | XhrUpload static createError( object: any, request?: RequestEngine, msg?: string, ): RequestError static dealResponse( result: T, request: any, ): T extends { [k in string | number]: any } ? T & { $request: any } : T checkStatus< T extends { statusCode: number } >(res: T): Promise request( url: string, data?: RequestData, options?: Partial, ): RequestPromise /** * 对应微信/支付宝小程序的 downloadFile * */ downloadFile( options: Partial & Pick, ): RequestPromise< DownloadResponse & { $request: any } > /** * 对应微信/支付宝小程序的 uploadFile * */ uploadFile( options: Partial & Pick, ): RequestPromise< RequestResponse & { $request: any } > get( url: string, data?: RequestData, options?: Partial< Pick> >, ): RequestPromise post( url: string, data?: RequestData, options?: Partial< Pick> >, ): RequestPromise put( url: string, data?: RequestData, options?: Partial< Pick> >, ): RequestPromise delete( url: string, data?: RequestData, options?: Partial< Pick> >, ): RequestPromise } export default Http export { ConfigInterceptor, DownloadEngineConfig, DownloadResponse, EngineName, ErrorHandler, Http, HttpHeaders, HttpInterceptors, ProgressEv, RequestConfig, RequestData, RequestEngine, RequestEngineConfig, RequestError, RequestPromise, RequestResponse, RequestSharedConfig, ResponseInterceptor, ResponseType, UploadEngineConfig, }