uni-ticket-system/src/http/main.ts

199 lines
6.0 KiB
TypeScript
Raw Normal View History

2023-12-07 03:08:43 +00:00
type HttpMethod =
2023-12-08 11:03:40 +00:00
| 'GET'
2023-12-07 03:08:43 +00:00
| 'get'
2023-12-08 11:03:40 +00:00
| 'POST'
2023-12-07 03:08:43 +00:00
| 'post'
2023-12-08 11:03:40 +00:00
| 'PUT'
2023-12-07 03:08:43 +00:00
| 'put'
2023-12-08 11:03:40 +00:00
| 'DELETE'
2023-12-07 03:08:43 +00:00
| 'delete'
2023-12-08 11:03:40 +00:00
| 'CONNECT'
2023-12-08 07:55:35 +00:00
| 'connect'
2023-12-08 11:03:40 +00:00
| 'OPTIONS'
2023-12-07 03:08:43 +00:00
| 'options'
2023-12-08 11:03:40 +00:00
| 'TRACE'
2023-12-08 07:55:35 +00:00
| 'trace';
2023-12-07 03:08:43 +00:00
interface RequestOptions {
baseUrl?: string;
2023-12-11 10:46:53 +00:00
url?: string;
2023-12-07 03:08:43 +00:00
data?: Record<string, any>;
method?: HttpMethod;
header?: Record<string, string>;
2023-12-07 03:18:10 +00:00
params?: Record<string, any>;
2023-12-08 11:03:40 +00:00
timeout?: number,
dataType?: string,
responseType?: string,
sslVerify?: boolean,
2023-12-13 06:56:40 +00:00
withCredentials?: boolean,
2023-12-08 11:03:40 +00:00
firstIpv4?: boolean,
enableHttp2?: boolean,
enableQuic?: boolean,
enableCache?: boolean,
enableHttpDNS?: boolean,
httpDNSServiceId?: string,
enableChunked?: boolean,
forceCellularNetwork?: boolean,
enableCookie?: boolean,
cloudCache?: object | boolean,
defer?: boolean,
2023-12-12 06:22:55 +00:00
interceptor?: {
2023-12-11 10:46:53 +00:00
request?: RequestInterceptor,
response?: ResponseInterceptor
}
2023-12-07 03:08:43 +00:00
}
2023-12-12 06:22:55 +00:00
interface UploadOptions {
url: string,
baseUrl?: string,
files?: File[],
fileType?: 'image' | 'video' | 'audio',
file?: File,
filePath?: string,
name?: string,
header?: object,
timeout?: number,
formData?: object,
interceptor?: {
request?: (config: UploadOptions) => UploadOptions,
response?: (response: any) => any;
}
}
2023-12-07 03:08:43 +00:00
type RequestInterceptor = (config: RequestOptions) => RequestOptions;
type ResponseInterceptor = (response: any) => any;
2023-12-08 11:03:40 +00:00
2023-12-11 10:46:53 +00:00
class uniRequest {
baseUrl?: string;
2023-12-07 03:08:43 +00:00
defaultHeader: Record<string, string>;
2023-12-11 10:46:53 +00:00
interceptors: { request?: RequestInterceptor; response?: ResponseInterceptor };
2023-12-12 06:22:55 +00:00
2023-12-11 10:46:53 +00:00
constructor(request: RequestOptions) {
2023-12-08 11:03:40 +00:00
this.baseUrl = request.baseUrl;
2023-12-07 03:08:43 +00:00
this.defaultHeader = {
"Content-Type": "application/json;charset=UTF-8",
2023-12-08 11:03:40 +00:00
...request.header,
2023-12-07 03:08:43 +00:00
};
2023-12-11 10:46:53 +00:00
this.interceptors = {request: request.interceptor?.request, response: request.interceptor?.response};
2023-12-07 03:08:43 +00:00
}
2023-12-12 06:22:55 +00:00
2023-12-07 03:08:43 +00:00
setBaseUrl(baseUrl: string): void {
this.baseUrl = baseUrl;
}
setDefaultHeader(header: Record<string, string>): void {
this.defaultHeader = header;
}
2023-12-12 06:22:55 +00:00
static created(options: RequestOptions) {
2023-12-11 10:46:53 +00:00
return new uniRequest(options);
}
2023-12-07 03:08:43 +00:00
request(options: RequestOptions): Promise<any> {
2023-12-08 11:03:40 +00:00
options = this.buildRequestOptions(options)
2023-12-07 03:08:43 +00:00
options = options || {};
options.baseUrl = options.baseUrl || this.baseUrl;
2023-12-11 10:46:53 +00:00
options.url = `${options.baseUrl}${options.url}`;
2023-12-07 03:08:43 +00:00
options.data = options.data || {};
2023-12-08 08:59:43 +00:00
options.method = (options.method?.toUpperCase() || "GET") as HttpMethod;
2023-12-07 03:08:43 +00:00
options.header = options.header || this.defaultHeader;
2023-12-11 10:46:53 +00:00
if (typeof options.interceptor?.request === 'function') {
options = options.interceptor.request(options);
} else if (typeof this.interceptors?.request === 'function') {
2023-12-07 03:08:43 +00:00
options = this.interceptors.request(options);
}
return new Promise((resolve, reject) => {
2023-12-08 08:59:43 +00:00
// @ts-ignore
2023-12-07 03:08:43 +00:00
uni.request({
...options,
success: (res) => {
2023-12-12 06:22:55 +00:00
const response = this.handleResponse(res, options);
2023-12-07 03:08:43 +00:00
resolve(response);
},
fail: (err) => {
const error = this.handleError(err);
reject(error);
},
});
});
}
2023-12-12 06:22:55 +00:00
private handleResponse(response: any, options: RequestOptions): any {
if (options.interceptor?.response) {
2023-12-11 10:46:53 +00:00
response = options.interceptor?.response(response);
2023-12-12 06:22:55 +00:00
} else if (this.interceptors?.response) {
2023-12-11 10:46:53 +00:00
response = this.interceptors?.response(response);
2023-12-07 03:08:43 +00:00
}
2023-12-12 06:22:55 +00:00
return response
}
private handleUploadResponse(res: any, options: UploadOptions){
if (options.interceptor?.response){
res = options.interceptor?.response(res);
}
return res
2023-12-07 03:08:43 +00:00
}
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));
}
2023-12-12 06:22:55 +00:00
upload(options: UploadOptions) {
options.url = `${options.baseUrl ?? this.baseUrl}${options.url}`;
if (typeof options.interceptor?.request==='function'){
options = options.interceptor.request(options);
}
return new Promise((resolve, reject) => {
uni.uploadFile({
...options,
success: (res) => {
resolve(this.handleUploadResponse(res, options))
},
fail(res){
reject(res)
}
});
})
}
2023-12-07 03:08:43 +00:00
private buildRequestOptions(options: RequestOptions): RequestOptions {
2023-12-08 08:59:43 +00:00
if (options.params && Object.keys(options.params).length > 0) {
2023-12-07 03:08:43 +00:00
const queryString = Object.keys(options.params)
2023-12-08 08:59:43 +00:00
.map((key) => `${encodeURIComponent(key)}=${encodeURIComponent(options.params![key])}`)
2023-12-07 03:08:43 +00:00
.join('&');
options.url += `?${queryString}`;
}
delete options.params;
return options;
}
2023-12-08 11:03:40 +00:00
2023-12-07 03:08:43 +00:00
post(options: RequestOptions): Promise<any> {
options = options || {};
options.method = "POST";
return this.request(this.buildRequestOptions(options));
}
2023-12-08 11:03:40 +00:00
2023-12-07 03:08:43 +00:00
put(options: RequestOptions): Promise<any> {
options = options || {};
options.method = "PUT";
return this.request(this.buildRequestOptions(options));
}
2023-12-08 11:03:40 +00:00
2023-12-07 03:08:43 +00:00
delete(options: RequestOptions): Promise<any> {
options = options || {};
options.method = "DELETE";
return this.request(this.buildRequestOptions(options));
}
}
2023-12-08 11:03:40 +00:00
2023-12-11 10:46:53 +00:00
export {uniRequest}