2022-06-10 18:03:07 +08:00

185 lines
5.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

type AnyObject = Record<string | number | symbol, any>;
export type HttpResponse<T = any> =
| HttpSuccess<T>
| HttpError<T>
| HttpDownloadResponse
| HttpUploadResponse<T>;
type HttpPromise<T> = Promise<HttpResponse<T>>;
type Tasks = UniApp.RequestTask | UniApp.UploadTask | UniApp.DownloadTask;
type Method =
| 'GET'
| 'POST'
| 'PUT'
| 'DELETE'
| 'CONNECT'
| 'HEAD'
| 'OPTIONS'
| 'TRACE'
| 'UPLOAD'
| 'DOWNLOAD';
export interface RequestTask {
abort: () => void;
offHeadersReceived: () => void;
onHeadersReceived: () => void;
}
export interface CustomConfig {
auth?: boolean;
loading?: boolean;
}
export interface HttpRequestConfig<T = Tasks> extends Record<string, any> {
/** 请求基地址 */
baseURL?: string;
/** 请求服务器接口地址 */
url?: string;
/** 请求查询参数,自动拼接为查询字符串 */
params?: AnyObject;
/** 请求体参数 */
data?: AnyObject;
/** 文件对应的 key */
name?: string;
/** HTTP 请求中其他额外的 form data */
formData?: AnyObject;
/** 要上传文件资源的路径。 */
filePath?: string;
/** 需要上传的文件列表。使用 files 时filePath 和 name 不生效App、H5 2.6.15+ */
files?: Array<{
name?: string;
file?: File;
uri: string;
}>;
/** 要上传的文件对象仅H52.6.15+)支持 */
file?: File;
/** 文要上传的件类型, 支付宝小程序,且必填 **/
fileType?: string;
/** 请求头信息 */
header?: AnyObject;
/** 请求方式 */
method?: Method;
/** 如果设为 json会尝试对返回的数据做一次 JSON.parse */
dataType?: string;
/** 设置响应的数据类型,支付宝小程序不支持 */
responseType?: 'text' | 'arraybuffer';
/** 自定义参数 */
custom?: CustomConfig;
/** 超时时间仅微信小程序2.10.0)、支付宝小程序支持 */
timeout?: number;
/** DNS解析时优先使用ipv4仅 App-Android 支持 (HBuilderX 2.8.0+) */
firstIpv4?: boolean;
/** 验证 ssl 证书 仅5+App安卓端支持HBuilderX 2.3.3+ */
sslVerify?: boolean;
/** 跨域请求时是否携带凭证cookies仅H5支持HBuilderX 2.6.15+ */
withCredentials?: boolean;
/** 返回当前请求的task, options。请勿在此处修改options。 */
getTask?: (task: T, options: HttpRequestConfig<T>) => void;
/** 全局自定义验证器 */
validateStatus?: (statusCode: number) => boolean | void;
}
export interface HttpSuccess<T = any> {
config: HttpRequestConfig;
statusCode: number;
cookies: Array<string>;
data: T;
errMsg: string;
header: AnyObject;
}
export interface HttpUploadResponse<T = any> {
config: HttpRequestConfig;
statusCode: number;
data: T;
errMsg: string;
}
export interface HttpDownloadResponse extends HttpSuccess {
tempFilePath: string;
}
export interface HttpError<T = any> {
config: HttpRequestConfig;
statusCode?: number;
cookies?: Array<string>;
data?: T;
errMsg: string;
header?: AnyObject;
}
export interface HttpInterceptorManager<V, E = V> {
use(
onFulfilled?: (config: V) => Promise<V> | V,
onRejected?: (config: E) => Promise<E> | E,
): void;
eject(id: number): void;
forEach(h: any): void;
}
export interface InterceptorsRequestConfig {
config: HttpRequestConfig;
}
export type InterceptorsRequest = HttpInterceptorManager<
InterceptorsRequestConfig,
InterceptorsRequestConfig
>;
export type InterceptorsResponse = HttpInterceptorManager<HttpSuccess, HttpError>;
export interface Interceptors {
request: InterceptorsRequest;
response: InterceptorsResponse;
}
export abstract class HttpRequestAbstract {
constructor(config?: Partial<HttpRequestConfig>);
config: HttpRequestConfig;
interceptors: Interceptors;
middleware<T = any>(config: HttpRequestConfig): HttpPromise<T>;
request<T = any>(config: HttpRequestConfig<UniApp.RequestTask>): HttpPromise<T>;
get<T = any>(url: string, config?: HttpRequestConfig<UniApp.RequestTask>): HttpPromise<T>;
upload<T = any>(url: string, config?: HttpRequestConfig<UniApp.UploadTask>): HttpPromise<T>;
delete<T = any>(
url: string,
data?: AnyObject,
config?: HttpRequestConfig<UniApp.RequestTask>,
): HttpPromise<T>;
head<T = any>(
url: string,
data?: AnyObject,
config?: HttpRequestConfig<UniApp.RequestTask>,
): HttpPromise<T>;
post<T = any>(
url: string,
data?: AnyObject,
config?: HttpRequestConfig<UniApp.RequestTask>,
): HttpPromise<T>;
put<T = any>(
url: string,
data?: AnyObject,
config?: HttpRequestConfig<UniApp.RequestTask>,
): HttpPromise<T>;
connect<T = any>(
url: string,
data?: AnyObject,
config?: HttpRequestConfig<UniApp.RequestTask>,
): HttpPromise<T>;
options<T = any>(
url: string,
data?: AnyObject,
config?: HttpRequestConfig<UniApp.RequestTask>,
): HttpPromise<T>;
trace<T = any>(
url: string,
data?: AnyObject,
config?: HttpRequestConfig<UniApp.RequestTask>,
): HttpPromise<T>;
download(
url: string,
config?: HttpRequestConfig<UniApp.DownloadTask>,
): Promise<HttpDownloadResponse>;
setConfig(onSend: (config: HttpRequestConfig) => HttpRequestConfig): void;
}
declare class HttpRequest extends HttpRequestAbstract {}
export default HttpRequest;