z-util-page
Version:
194 lines (193 loc) • 5.18 kB
TypeScript
import type { ResponseMessage } from './message';
/**
* @category HTTTP请求操作辅助类
*/
export declare class Http {
/**
* 默认参数
*/
options: HttpOptions;
/**
* 构造函数
* @example
* ```ts
* const http = new Http({
* //超时等待时间(ms)
* timeout: 10000,
* //基地址
* baseUrl: 'http://localhost:3000',
* //请求体数据格式
* contentType: 'application/json',
* //响应数据格式
* responseType: 'json'
* });
* ```
* @param options 默认参数
*/
constructor(options?: CustomHttpOptions);
/**
* XMLHttpRequest异步请求
* @example
* ```ts
* const http = new Http();
* // 拦截器
* http.Interceptor.request((param) => {
* // 请求参数
* console.log(param);
* param.url = 'http://localhost:3000' + param.url;
* })
* http.Interceptor.response((res) => {
* // 请求结果
* console.log(res);
* res.data = res.data + '拦截器修改';
* return res;
* })
*
* // 请求
* const req = http.ajax({
* // 请求地址
* baseUrl: 'http://localhost:3000',
* url: '/api/user',
* // 请求方法
* method: 'GET' | 'HEAD' | 'POST' | 'PUT' | 'DELETE',
* // 响应数据格式
* type: "arraybuffer" | "blob" | "document" | "json" | "text",
* // 请求头
* headers: {
* 'Content-Type': 'application/json',
* 'Authorization': 'Bearer ' + token
* }
* // 请求体
* data: {
* name: 'jack'
* }
* // 请求参数
* params: {
* name: 'jack'
* }
* // 请求超时时间
* timeout: 10000
* // 请求体数据格式
* contentType: 'application/json',
* // 响应数据类型
* responseType: 'json',
* // 上传文件
* file: {
* file: new Blob([JSON.stringify({a: '身体和心灵,总有一个在路上。'}, null, 2)], {type : 'application/json'})
* }
* }).then((res) => {
* // 请求成功
* }).catch((err) => {
* // 请求失败
* }).finally(() => {
* // 请求完成
* }).progress(() => {
* // 请求进度
* });
*
* // 取消请求
* req.abort();
* ```
* @param param 请求参数
*/
ajax(param: Param): PromiseHandle;
/**
* XMLHttpRequest同步请求,绝大多数情况下只能在work进程内使用。
* @example
* ```ts
* const http = new Http();
* // 请求
* const req = http.ajax({
* // 请求地址
* baseUrl: 'http://localhost:3000',
* url: '/api/user',
* // 请求方法
* method: 'GET' | 'HEAD' | 'POST' | 'PUT' | 'DELETE',
* // 响应数据格式
* type: "arraybuffer" | "blob" | "document" | "json" | "text",
* // 请求头
* headers: {
* 'Content-Type': 'application/json',
* 'Authorization': 'Bearer ' + token
* }
* // 请求体
* data: {
* name: 'jack'
* }
* // 请求参数
* params: {
* name: 'jack'
* }
* // 请求超时时间
* timeout: 10000
* // 请求体数据格式
* contentType: 'application/json',
* // 响应数据类型
* responseType: 'json',
* // 上传文件
* file: {
* file: new Blob([JSON.stringify({a: '身体和心灵,总有一个在路上。'}, null, 2)], {type : 'application/json'})
* }
* })
* // 请求成功
* console.log(res);
* ```
* @param param 请求参数
*/
ajaxAsync(param: Param): any;
/**
* 拦截器
*/
Interceptor: Interceptor;
}
declare class PromiseHandle {
private xhr;
result: any;
constructor(xhr: XMLHttpRequest);
then(callback: Callback): this;
catch(callback: Callback): this;
finally(callback: Callback): this;
progress(callback: Callback): this;
downProgress(callback: Callback): this;
upProgress(callback: Callback): this;
abort(): this;
}
interface HttpOptions {
timeout: number;
baseUrl: string;
contentType: ContentType;
responseType: XMLHttpRequestResponseType;
}
interface CustomHttpOptions {
timeout?: number;
baseUrl?: string;
contentType?: ContentType;
responseType?: XMLHttpRequestResponseType;
}
interface Param {
url: string;
baseUrl?: string;
method?: 'GET' | 'HEAD' | 'POST' | 'PUT' | 'DELETE';
responseType?: XMLHttpRequestResponseType;
timeout?: number;
header?: Head;
param?: any;
data?: any;
file?: {
[propName: string]: File | Blob;
};
}
interface Head {
Accept?: string;
ContentType?: ContentType;
[propName: string]: any;
}
interface Interceptor {
requestArr: Array<Function>;
responseArr: Array<Function>;
request: (func: Function) => void;
response: (func: Function) => void;
}
type Callback = (res: ResponseMessage) => void;
type ContentType = "" | "application/x-www-form-urlencoded" | "text/plain" | "multipart/form-data" | "application/json";
export {};