Skip to content

Commit

Permalink
feat: improve xior compatible with axios
Browse files Browse the repository at this point in the history
  • Loading branch information
suhaotian committed Feb 20, 2024
1 parent 7515e45 commit e4e6030
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 21 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# CHANGELOG 📝

## v0.0.3 2024-02-20
## v0.0.4 2024-02-20

- feat: support url as first paramter in xiorInstance.request('/url')
- feat: mark `headers` and `params` types as `Record<string, any>` and not undefined in interceptors
- feat: add generic type to `XiorRequestConfig`

## v0.0.3

- Chore: improve README and add more tests
- Feat: `xiorInstance.request` remove first parameter `url`
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "xior",
"version": "0.0.3",
"version": "0.0.4",
"description": "Axios similiar API request library but based on fetch",
"repository": "suhaotian/xior",
"bugs": "https://github.com/suhaotian/xior/issues",
Expand Down
6 changes: 3 additions & 3 deletions src/interceptors.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { XiorRequestConfig } from './types';
import { XiorInterceptorRequestConfig } from './types';
import { encode as liteEncode, merge } from './utils';

const encodeUrlType = 'application/x-www-form-urlencoded';
const jsonType = 'application/json';
// const formType = 'multipart/form-data';

export async function defaultRequestInterceptor(
req: XiorRequestConfig
): Promise<XiorRequestConfig> {
req: XiorInterceptorRequestConfig
): Promise<XiorInterceptorRequestConfig> {
const encode = req.encode || liteEncode;
const encodeURI = req.encodeURI !== false;

Expand Down
8 changes: 6 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export interface XiorRequestConfig extends Omit<RequestInit, 'body'> {
export interface XiorRequestConfig<T = any> extends Omit<RequestInit, 'body'> {
url?: string;
headers?: Record<string, any>;
baseURL?: string;
Expand All @@ -17,12 +17,16 @@ export interface XiorRequestConfig extends Omit<RequestInit, 'body'> {
_url?: string;
}

export type XiorInterceptorRequestConfig<T = any> = XiorRequestConfig & {
headers: Record<string, any>;
params: Record<string, any>;
};
export interface XiorResponse<T = any> {
data: T;
status: number;
statusText: string;
headers: Headers;
response: Response;
config: XiorRequestConfig;
config: XiorInterceptorRequestConfig;
request?: any;
}
33 changes: 19 additions & 14 deletions src/xior.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { defaultRequestInterceptor } from './interceptors';
import { XiorRequestConfig } from './types';
import { XiorInterceptorRequestConfig, XiorRequestConfig } from './types';
import {
ClearableSignal,
XiorTimeoutError,
Expand All @@ -20,18 +20,20 @@ export class xior {
}

private requestInterceptors: ((
config: XiorRequestConfig
) => Promise<XiorRequestConfig> | XiorRequestConfig)[] = [defaultRequestInterceptor];
config: XiorInterceptorRequestConfig
) => Promise<XiorInterceptorRequestConfig> | XiorInterceptorRequestConfig)[] = [
defaultRequestInterceptor,
];
private responseInterceptors: {
fn: (config: { data: any; request: XiorRequestConfig; response: Response }) =>
fn: (config: { data: any; request: XiorInterceptorRequestConfig; response: Response }) =>
| Promise<{
data: any;
request: XiorRequestConfig;
request: XiorInterceptorRequestConfig;
response: Response;
}>
| {
data: any;
request: XiorRequestConfig;
request: XiorInterceptorRequestConfig;
response: Response;
};
onRejected?: (error: any) => any;
Expand All @@ -41,7 +43,9 @@ export class xior {
return {
request: {
use: (
fn: (config: XiorRequestConfig) => Promise<XiorRequestConfig> | XiorRequestConfig,
fn: (
config: XiorInterceptorRequestConfig
) => Promise<XiorInterceptorRequestConfig> | XiorInterceptorRequestConfig,
/** @deprecated no need */
onRejected?: (error: any) => any
) => {
Expand All @@ -50,15 +54,15 @@ export class xior {
},
response: {
use: (
fn: (config: { data: any; request: XiorRequestConfig; response: Response }) =>
fn: (config: { data: any; request: XiorInterceptorRequestConfig; response: Response }) =>
| Promise<{
data: any;
request: XiorRequestConfig;
request: XiorInterceptorRequestConfig;
response: Response;
}>
| {
data: any;
request: XiorRequestConfig;
request: XiorInterceptorRequestConfig;
response: Response;
},
onRejected?: (error: any) => any
Expand All @@ -71,11 +75,12 @@ export class xior {
async request<T>(options?: XiorRequestConfig | string) {
let requestObj: XiorRequestConfig = merge(
this.config || {},
typeof options === 'string' ? { url: options } : options || {}
typeof options === 'string' ? { url: options } : options || {},
{ headers: {}, params: {} }
);

for (const item of this.requestInterceptors) {
requestObj = await item(requestObj);
requestObj = await item(requestObj as XiorInterceptorRequestConfig);
}

const { url: _url, method, headers, timeout, signal: reqSignal, data, ...rest } = requestObj;
Expand Down Expand Up @@ -133,7 +138,7 @@ export class xior {
{
response,
data,
config: requestObj,
config: requestObj as XiorInterceptorRequestConfig,
status: response.status,
statusText: response.statusText,
headers: response.headers,
Expand Down Expand Up @@ -187,7 +192,7 @@ export class xior {
}
let responseObj = {
data: data.data,
request: requestObj,
request: requestObj as XiorInterceptorRequestConfig,
response,
};

Expand Down

0 comments on commit e4e6030

Please sign in to comment.