diff --git a/src/http/main.ts b/src/http/main.ts index 00f24ce9..d2941769 100644 --- a/src/http/main.ts +++ b/src/http/main.ts @@ -23,10 +23,6 @@ interface RequestOptions { } 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; @@ -34,7 +30,7 @@ class uniFetch { constructor({ baseUrl = '', defaultHeader = {}, interceptors = { request: null, response: null } }: { baseUrl?: string; defaultHeader?: Record; - interceptors?: Interceptors; + interceptors?: { request: RequestInterceptor | null; response: ResponseInterceptor | null}; }) { this.baseUrl = baseUrl; this.defaultHeader = { @@ -51,18 +47,18 @@ class uniFetch { 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.method = (options.method?.toUpperCase() || "GET") as HttpMethod; options.header = options.header || this.defaultHeader; if (this.interceptors.request) { options = this.interceptors.request(options); } return new Promise((resolve, reject) => { + // @ts-ignore uni.request({ ...options, success: (res) => { @@ -109,9 +105,9 @@ class uniFetch { } private buildRequestOptions(options: RequestOptions): RequestOptions { - if (options.params) { + if (options.params && Object.keys(options.params).length > 0) { const queryString = Object.keys(options.params) - .map((key) => `${encodeURIComponent(key)}=${encodeURIComponent(options.params[key])}`) + .map((key) => `${encodeURIComponent(key)}=${encodeURIComponent(options.params![key])}`) .join('&'); options.url += `?${queryString}`; }