Skip to content

Commit

Permalink
chore: shorter nameing
Browse files Browse the repository at this point in the history
  • Loading branch information
suhaotian committed Mar 24, 2024
1 parent 744aa20 commit 270d722
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 14 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## v0.3.0 2024/03/24

- fix(core): POST/DELETE/PUT/PATCH methods when have `data`, use formData to body(before put in url)
- fix(core): POST/DELETE/PUT/PATCH methods when `content-type=application/x-www-form-urlencoded`, use formData to in body(previous put in url)
- refactor(core): default request inteceptor should work before send fetch
- refactor(core): remove `_data` in request config
- refactor(core): remove `encode` in options, use `paramsSerializer` option instead
Expand Down
4 changes: 2 additions & 2 deletions src/interceptors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export default async function defaultRequestInterceptor(req: XiorInterceptorRequ
let _data = data;
const headers = req?.headers || {};
let newParams = req.params || {};
const isGet = likeGET(method);
if (data && !(data instanceof FormData)) {
let contentType = '';
if (req?.headers) {
Expand All @@ -34,7 +35,6 @@ export default async function defaultRequestInterceptor(req: XiorInterceptorRequ
}
}
}
const isGet = likeGET(method);
if (!contentType) {
contentType = isGet ? formUrl : jsonType;
headers['Content-Type'] = contentType;
Expand Down Expand Up @@ -63,6 +63,6 @@ export default async function defaultRequestInterceptor(req: XiorInterceptorRequ
_url,
method,
headers,
paramsSerializer,
isGet,
};
}
25 changes: 14 additions & 11 deletions src/xior.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import defaultRequestInterceptor, { likeGET } from './interceptors';
import defaultRequestInterceptor from './interceptors';
import type {
XiorInterceptorRequestConfig,
XiorPlugin,
Expand Down Expand Up @@ -191,6 +191,7 @@ export class xior {
data,
_data,
_url,
isGet,
...rest
} = await defaultRequestInterceptor(requestConfig as XiorInterceptorRequestConfig);
requestConfig._url = _url;
Expand Down Expand Up @@ -222,7 +223,7 @@ export class xior {
}

const response = await fetch(finalURL, {
body: likeGET(method) ? undefined : _data,
body: isGet ? undefined : _data,
...rest,
signal,
method,
Expand Down Expand Up @@ -309,13 +310,15 @@ export class xior {
};
}

createGet<T>(method: string) {
/** create get method */
cG<T>(method: string) {
return (url: string, options?: XiorRequestConfig) => {
return this.request<T>(options ? { ...options, method, url } : { method, url });
};
}

createPost<T>(method: string) {
/** create post method */
cP<T>(method: string) {
return (url: string, data?: any, options?: XiorRequestConfig) => {
return this.request<T>(options ? { ...options, method, url, data } : { method, url, data });
};
Expand All @@ -328,7 +331,7 @@ export class xior {
data?: any;
}
) {
return this.createGet<T>('GET')(url, options);
return this.cG<T>('GET')(url, options);
}
head<T = any>(
url: string,
Expand All @@ -337,23 +340,23 @@ export class xior {
data?: any;
}
) {
return this.createGet<T>('HEAD')(url, options);
return this.cG<T>('HEAD')(url, options);
}

post<T = any>(url: string, data?: any, options?: XiorRequestConfig) {
return this.createPost<T>('POST')(url, data, options);
return this.cP<T>('POST')(url, data, options);
}

put<T = any>(url: string, data?: any, options?: XiorRequestConfig) {
return this.createPost<T>('PUT')(url, data, options);
return this.cP<T>('PUT')(url, data, options);
}

patch<T = any>(url: string, data?: any, options?: XiorRequestConfig) {
return this.createPost<T>('PATCH')(url, data, options);
return this.cP<T>('PATCH')(url, data, options);
}

delete<T = any>(url: string, options?: XiorRequestConfig) {
return this.createGet<T>('DELETE')(url, options);
return this.cG<T>('DELETE')(url, options);
}

options<T = any>(
Expand All @@ -363,6 +366,6 @@ export class xior {
data?: any;
}
) {
return this.createGet<T>('OPTIONS')(url, options);
return this.cG<T>('OPTIONS')(url, options);
}
}

0 comments on commit 270d722

Please sign in to comment.