|
|
@ -1,18 +1,19 @@
|
|
|
|
type HttpMethod =
|
|
|
|
type HttpMethod =
|
|
|
|
| "GET"
|
|
|
|
| 'GET'
|
|
|
|
| 'get'
|
|
|
|
| 'get'
|
|
|
|
| "POST"
|
|
|
|
| 'POST'
|
|
|
|
| 'post'
|
|
|
|
| 'post'
|
|
|
|
| "PUT"
|
|
|
|
| 'PUT'
|
|
|
|
| 'put'
|
|
|
|
| 'put'
|
|
|
|
| "DELETE"
|
|
|
|
| 'DELETE'
|
|
|
|
| 'delete'
|
|
|
|
| 'delete'
|
|
|
|
| "CONNECT"
|
|
|
|
| 'CONNECT'
|
|
|
|
| 'connect'
|
|
|
|
| 'connect'
|
|
|
|
| "OPTIONS"
|
|
|
|
| 'OPTIONS'
|
|
|
|
| 'options'
|
|
|
|
| 'options'
|
|
|
|
| "TRACE"
|
|
|
|
| 'TRACE'
|
|
|
|
| 'trace';
|
|
|
|
| 'trace';
|
|
|
|
|
|
|
|
|
|
|
|
interface RequestOptions {
|
|
|
|
interface RequestOptions {
|
|
|
|
baseUrl?: string;
|
|
|
|
baseUrl?: string;
|
|
|
|
url: string;
|
|
|
|
url: string;
|
|
|
@ -20,24 +21,44 @@ interface RequestOptions {
|
|
|
|
method?: HttpMethod;
|
|
|
|
method?: HttpMethod;
|
|
|
|
header?: Record<string, string>;
|
|
|
|
header?: Record<string, string>;
|
|
|
|
params?: Record<string, any>;
|
|
|
|
params?: Record<string, any>;
|
|
|
|
|
|
|
|
timeout?: number,
|
|
|
|
|
|
|
|
dataType?: string,
|
|
|
|
|
|
|
|
responseType?: string,
|
|
|
|
|
|
|
|
sslVerify?: boolean,
|
|
|
|
|
|
|
|
withCredentials?: boolean
|
|
|
|
|
|
|
|
firstIpv4?: boolean,
|
|
|
|
|
|
|
|
enableHttp2?: boolean,
|
|
|
|
|
|
|
|
enableQuic?: boolean,
|
|
|
|
|
|
|
|
enableCache?: boolean,
|
|
|
|
|
|
|
|
enableHttpDNS?: boolean,
|
|
|
|
|
|
|
|
httpDNSServiceId?: string,
|
|
|
|
|
|
|
|
enableChunked?: boolean,
|
|
|
|
|
|
|
|
forceCellularNetwork?: boolean,
|
|
|
|
|
|
|
|
enableCookie?: boolean,
|
|
|
|
|
|
|
|
cloudCache?: object | boolean,
|
|
|
|
|
|
|
|
defer?: boolean,
|
|
|
|
|
|
|
|
requestInterceptor?: RequestInterceptor,
|
|
|
|
|
|
|
|
responseInterceptor?: ResponseInterceptor
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
type instantiationParameters = Omit<Omit<RequestOptions, 'baseUrl'>, 'url'> & {
|
|
|
|
|
|
|
|
baseUrl: string;
|
|
|
|
|
|
|
|
};
|
|
|
|
type RequestInterceptor = (config: RequestOptions) => RequestOptions;
|
|
|
|
type RequestInterceptor = (config: RequestOptions) => RequestOptions;
|
|
|
|
type ResponseInterceptor = (response: any) => any;
|
|
|
|
type ResponseInterceptor = (response: any) => any;
|
|
|
|
|
|
|
|
|
|
|
|
class uniFetch {
|
|
|
|
class uniFetch {
|
|
|
|
baseUrl: string;
|
|
|
|
baseUrl: string;
|
|
|
|
defaultHeader: Record<string, string>;
|
|
|
|
defaultHeader: Record<string, string>;
|
|
|
|
interceptors: { request: RequestInterceptor | null; response: ResponseInterceptor | null };
|
|
|
|
interceptors: { request: RequestInterceptor | undefined; response: ResponseInterceptor | undefined };
|
|
|
|
constructor({ baseUrl = '', defaultHeader = {}, interceptors = { request: null, response: null } }: {
|
|
|
|
|
|
|
|
baseUrl?: string;
|
|
|
|
constructor(request: instantiationParameters) {
|
|
|
|
defaultHeader?: Record<string, string>;
|
|
|
|
this.baseUrl = request.baseUrl;
|
|
|
|
interceptors?: { request: RequestInterceptor | null; response: ResponseInterceptor | null};
|
|
|
|
|
|
|
|
}) {
|
|
|
|
|
|
|
|
this.baseUrl = baseUrl;
|
|
|
|
|
|
|
|
this.defaultHeader = {
|
|
|
|
this.defaultHeader = {
|
|
|
|
"Content-Type": "application/json;charset=UTF-8",
|
|
|
|
"Content-Type": "application/json;charset=UTF-8",
|
|
|
|
...defaultHeader,
|
|
|
|
...request.header,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
this.interceptors = interceptors;
|
|
|
|
this.interceptors = {request: request.requestInterceptor, response: request.responseInterceptor};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
setBaseUrl(baseUrl: string): void {
|
|
|
|
setBaseUrl(baseUrl: string): void {
|
|
|
@ -47,14 +68,18 @@ class uniFetch {
|
|
|
|
setDefaultHeader(header: Record<string, string>): void {
|
|
|
|
setDefaultHeader(header: Record<string, string>): void {
|
|
|
|
this.defaultHeader = header;
|
|
|
|
this.defaultHeader = header;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
request(options: RequestOptions): Promise<any> {
|
|
|
|
request(options: RequestOptions): Promise<any> {
|
|
|
|
|
|
|
|
options = this.buildRequestOptions(options)
|
|
|
|
options = options || {};
|
|
|
|
options = options || {};
|
|
|
|
options.baseUrl = options.baseUrl || this.baseUrl;
|
|
|
|
options.baseUrl = options.baseUrl || this.baseUrl;
|
|
|
|
options.url = options.baseUrl + options.url;
|
|
|
|
options.url = options.baseUrl + options.url;
|
|
|
|
options.data = options.data || {};
|
|
|
|
options.data = options.data || {};
|
|
|
|
options.method = (options.method?.toUpperCase() || "GET") as HttpMethod;
|
|
|
|
options.method = (options.method?.toUpperCase() || "GET") as HttpMethod;
|
|
|
|
options.header = options.header || this.defaultHeader;
|
|
|
|
options.header = options.header || this.defaultHeader;
|
|
|
|
if (this.interceptors.request) {
|
|
|
|
if (typeof options.requestInterceptor === 'function') {
|
|
|
|
|
|
|
|
options = options.requestInterceptor(options);
|
|
|
|
|
|
|
|
} else if (typeof this.interceptors.request === 'function') {
|
|
|
|
options = this.interceptors.request(options);
|
|
|
|
options = this.interceptors.request(options);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
@ -77,7 +102,6 @@ class uniFetch {
|
|
|
|
if (this.interceptors.response) {
|
|
|
|
if (this.interceptors.response) {
|
|
|
|
response = this.interceptors.response(response);
|
|
|
|
response = this.interceptors.response(response);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const statusCode = response.statusCode;
|
|
|
|
const statusCode = response.statusCode;
|
|
|
|
if (statusCode === 200) {
|
|
|
|
if (statusCode === 200) {
|
|
|
|
return response.data;
|
|
|
|
return response.data;
|
|
|
@ -114,20 +138,24 @@ class uniFetch {
|
|
|
|
delete options.params;
|
|
|
|
delete options.params;
|
|
|
|
return options;
|
|
|
|
return options;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
post(options: RequestOptions): Promise<any> {
|
|
|
|
post(options: RequestOptions): Promise<any> {
|
|
|
|
options = options || {};
|
|
|
|
options = options || {};
|
|
|
|
options.method = "POST";
|
|
|
|
options.method = "POST";
|
|
|
|
return this.request(this.buildRequestOptions(options));
|
|
|
|
return this.request(this.buildRequestOptions(options));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
put(options: RequestOptions): Promise<any> {
|
|
|
|
put(options: RequestOptions): Promise<any> {
|
|
|
|
options = options || {};
|
|
|
|
options = options || {};
|
|
|
|
options.method = "PUT";
|
|
|
|
options.method = "PUT";
|
|
|
|
return this.request(this.buildRequestOptions(options));
|
|
|
|
return this.request(this.buildRequestOptions(options));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
delete(options: RequestOptions): Promise<any> {
|
|
|
|
delete(options: RequestOptions): Promise<any> {
|
|
|
|
options = options || {};
|
|
|
|
options = options || {};
|
|
|
|
options.method = "DELETE";
|
|
|
|
options.method = "DELETE";
|
|
|
|
return this.request(this.buildRequestOptions(options));
|
|
|
|
return this.request(this.buildRequestOptions(options));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
export { uniFetch}
|
|
|
|
|
|
|
|
|
|
|
|
export {uniFetch}
|
|
|
|