This commit is contained in:
Aiden 2023-12-08 17:11:06 +08:00
commit f62148c7f5

View File

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