2023-12-07 03:08:43 +00:00
|
|
|
type HttpMethod =
|
|
|
|
| "GET"
|
|
|
|
| 'get'
|
|
|
|
| "POST"
|
|
|
|
| 'post'
|
|
|
|
| "PUT"
|
|
|
|
| 'put'
|
|
|
|
| "DELETE"
|
|
|
|
| 'delete'
|
|
|
|
| "PATCH"
|
|
|
|
| 'patch'
|
|
|
|
| "OPTIONS"
|
|
|
|
| 'options'
|
|
|
|
| "HEAD"
|
|
|
|
| 'head'
|
|
|
|
| "TRACE"
|
|
|
|
| 'trace'
|
|
|
|
| "CONNECT"
|
|
|
|
| 'connect';
|
|
|
|
interface RequestOptions {
|
|
|
|
baseUrl?: string;
|
|
|
|
url: string;
|
|
|
|
data?: Record<string, any>;
|
|
|
|
method?: HttpMethod;
|
|
|
|
header?: Record<string, string>;
|
2023-12-07 03:18:10 +00:00
|
|
|
params?: Record<string, any>;
|
2023-12-07 03:08:43 +00:00
|
|
|
}
|
|
|
|
type RequestInterceptor = (config: RequestOptions) => RequestOptions;
|
|
|
|
type ResponseInterceptor = (response: any) => any;
|
2023-12-07 03:12:06 +00:00
|
|
|
interface Interceptors {
|
|
|
|
request: (config: RequestOptions) => RequestOptions;
|
|
|
|
response: (response: any) => any;
|
2023-12-07 03:08:43 +00:00
|
|
|
}
|
|
|
|
class uniFetch {
|
|
|
|
baseUrl: string;
|
|
|
|
defaultHeader: Record<string, string>;
|
|
|
|
interceptors: { request: RequestInterceptor | null; response: ResponseInterceptor | null };
|
|
|
|
constructor({ baseUrl = '', defaultHeader = {}, interceptors = { request: null, response: null } }: {
|
|
|
|
baseUrl?: string;
|
|
|
|
defaultHeader?: Record<string, string>;
|
|
|
|
interceptors?: Interceptors;
|
|
|
|
}) {
|
|
|
|
this.baseUrl = baseUrl;
|
|
|
|
this.defaultHeader = {
|
|
|
|
"Content-Type": "application/json;charset=UTF-8",
|
|
|
|
...defaultHeader,
|
|
|
|
};
|
|
|
|
this.interceptors = interceptors;
|
|
|
|
}
|
|
|
|
|
|
|
|
setBaseUrl(baseUrl: string): void {
|
|
|
|
this.baseUrl = baseUrl;
|
|
|
|
}
|
|
|
|
|
|
|
|
setDefaultHeader(header: Record<string, string>): void {
|
|
|
|
this.defaultHeader = header;
|
|
|
|
}
|
|
|
|
|
|
|
|
request(options: RequestOptions): Promise<any> {
|
|
|
|
options = options || {};
|
|
|
|
options.baseUrl = options.baseUrl || this.baseUrl;
|
|
|
|
options.url = options.baseUrl + options.url;
|
|
|
|
options.data = options.data || {};
|
|
|
|
options.method = options.method || "GET";
|
|
|
|
options.header = options.header || this.defaultHeader;
|
|
|
|
if (this.interceptors.request) {
|
|
|
|
options = this.interceptors.request(options);
|
|
|
|
}
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
uni.request({
|
|
|
|
...options,
|
|
|
|
success: (res) => {
|
|
|
|
const response = this.handleResponse(res);
|
|
|
|
resolve(response);
|
|
|
|
},
|
|
|
|
fail: (err) => {
|
|
|
|
const error = this.handleError(err);
|
|
|
|
reject(error);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private handleResponse(response: any): any {
|
|
|
|
if (this.interceptors.response) {
|
|
|
|
response = this.interceptors.response(response);
|
|
|
|
}
|
|
|
|
|
|
|
|
const statusCode = response.statusCode;
|
|
|
|
if (statusCode === 200) {
|
|
|
|
return response.data;
|
|
|
|
} else {
|
|
|
|
throw response;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private handleError(error: any): any {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
|
|
|
setRequestInterceptor(interceptor: RequestInterceptor): void {
|
|
|
|
this.interceptors.request = interceptor;
|
|
|
|
}
|
|
|
|
|
|
|
|
setResponseInterceptor(interceptor: ResponseInterceptor): void {
|
|
|
|
this.interceptors.response = interceptor;
|
|
|
|
}
|
|
|
|
|
|
|
|
get(options: RequestOptions): Promise<any> {
|
|
|
|
options = options || {};
|
|
|
|
options.method = "GET";
|
|
|
|
return this.request(this.buildRequestOptions(options));
|
|
|
|
}
|
|
|
|
|
|
|
|
private buildRequestOptions(options: RequestOptions): RequestOptions {
|
|
|
|
if (options.params) {
|
|
|
|
const queryString = Object.keys(options.params)
|
|
|
|
.map((key) => `${encodeURIComponent(key)}=${encodeURIComponent(options.params[key])}`)
|
|
|
|
.join('&');
|
|
|
|
options.url += `?${queryString}`;
|
|
|
|
}
|
|
|
|
delete options.params;
|
|
|
|
return options;
|
|
|
|
}
|
|
|
|
post(options: RequestOptions): Promise<any> {
|
|
|
|
options = options || {};
|
|
|
|
options.method = "POST";
|
|
|
|
return this.request(this.buildRequestOptions(options));
|
|
|
|
}
|
|
|
|
put(options: RequestOptions): Promise<any> {
|
|
|
|
options = options || {};
|
|
|
|
options.method = "PUT";
|
|
|
|
return this.request(this.buildRequestOptions(options));
|
|
|
|
}
|
|
|
|
delete(options: RequestOptions): Promise<any> {
|
|
|
|
options = options || {};
|
|
|
|
options.method = "DELETE";
|
|
|
|
return this.request(this.buildRequestOptions(options));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
export { uniFetch}
|