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; method?: HttpMethod; header?: Record; params?: Record; } type RequestInterceptor = (config: RequestOptions) => RequestOptions; type ResponseInterceptor = (response: any) => any; interface Interceptors { request: (config: RequestOptions) => RequestOptions; response: (response: any) => any; } class uniFetch { baseUrl: string; defaultHeader: Record; interceptors: { request: RequestInterceptor | null; response: ResponseInterceptor | null }; constructor({ baseUrl = '', defaultHeader = {}, interceptors = { request: null, response: null } }: { baseUrl?: string; defaultHeader?: Record; 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): void { this.defaultHeader = header; } request(options: RequestOptions): Promise { 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 { 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 { options = options || {}; options.method = "POST"; return this.request(this.buildRequestOptions(options)); } put(options: RequestOptions): Promise { options = options || {}; options.method = "PUT"; return this.request(this.buildRequestOptions(options)); } delete(options: RequestOptions): Promise { options = options || {}; options.method = "DELETE"; return this.request(this.buildRequestOptions(options)); } } export { uniFetch}