@swiftcrab/request 1.0.2 → 1.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +81 -80
- package/dist/index.cjs +1 -0
- package/dist/index.d.cts +110 -0
- package/dist/index.d.mts +80 -59
- package/dist/index.mjs +1 -273
- package/package.json +15 -10
- package/dist/index.d.ts +0 -89
package/README.md
CHANGED
|
@@ -1,80 +1,81 @@
|
|
|
1
|
-
## 使用示例
|
|
2
|
-
|
|
3
|
-
- 安装: pnpm add @swiftcrab/request
|
|
4
|
-
|
|
5
|
-
#### 创建 overFetch.ts
|
|
6
|
-
|
|
7
|
-
```typescript
|
|
8
|
-
import {
|
|
9
|
-
RequestClient,
|
|
10
|
-
formatToken,
|
|
11
|
-
defaultResponseInterceptor,
|
|
12
|
-
authenticateResponseInterceptor,
|
|
13
|
-
errorMessageResponseInterceptor,
|
|
14
|
-
} from '@swiftcrab/request'
|
|
15
|
-
|
|
16
|
-
const baseURL = '/api'
|
|
17
|
-
|
|
18
|
-
function createRequestClient(
|
|
19
|
-
const client = new RequestClient({
|
|
20
|
-
baseURL,
|
|
21
|
-
})
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
async function doReAuthenticate() {}
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
async function doRefreshToken() {
|
|
28
|
-
return ''
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
//
|
|
32
|
-
client.addRequestInterceptor({
|
|
33
|
-
fulfilled: config => {
|
|
34
|
-
return config
|
|
35
|
-
},
|
|
36
|
-
})
|
|
37
|
-
|
|
38
|
-
// 返回数据格式 { data: any, info: { code: number, message: string, status: boolean } }
|
|
39
|
-
client.addResponseInterceptor(
|
|
40
|
-
defaultResponseInterceptor({
|
|
41
|
-
/** 响应数据中代表访问结果的字段名 */
|
|
42
|
-
codeField: 'code',
|
|
43
|
-
/** 响应数据中装载实际数据的字段名,或者提供一个函数从响应数据中解析需要返回的数据 */
|
|
44
|
-
dataField: 'data',
|
|
45
|
-
/** 当codeField所指定的字段值与successCode相同时,代表接口访问成功。如果提供一个函数,则返回true代表接口访问成功 */
|
|
46
|
-
successCode: 200,
|
|
47
|
-
}),
|
|
48
|
-
)
|
|
49
|
-
|
|
50
|
-
client.addResponseInterceptor(
|
|
51
|
-
authenticateResponseInterceptor({
|
|
52
|
-
client,
|
|
53
|
-
doReAuthenticate,
|
|
54
|
-
doRefreshToken,
|
|
55
|
-
/** 是否启用刷新令牌 */
|
|
56
|
-
enableRefreshToken: true,
|
|
57
|
-
}),
|
|
58
|
-
)
|
|
59
|
-
|
|
60
|
-
client.addResponseInterceptor(
|
|
61
|
-
errorMessageResponseInterceptor((msg: string, error) => {
|
|
62
|
-
/** 处理错误消息 */
|
|
63
|
-
}),
|
|
64
|
-
)
|
|
65
|
-
|
|
66
|
-
return client
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
export const requestClient = createRequestClient(
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
requestClient
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
1
|
+
## 使用示例
|
|
2
|
+
|
|
3
|
+
- 安装: pnpm add @swiftcrab/request
|
|
4
|
+
|
|
5
|
+
#### 创建 overFetch.ts
|
|
6
|
+
|
|
7
|
+
```typescript
|
|
8
|
+
import {
|
|
9
|
+
RequestClient,
|
|
10
|
+
formatToken,
|
|
11
|
+
defaultResponseInterceptor,
|
|
12
|
+
authenticateResponseInterceptor,
|
|
13
|
+
errorMessageResponseInterceptor,
|
|
14
|
+
} from '@swiftcrab/request'
|
|
15
|
+
|
|
16
|
+
const baseURL = '/api'
|
|
17
|
+
|
|
18
|
+
function createRequestClient() {
|
|
19
|
+
const client = new RequestClient({
|
|
20
|
+
baseURL,
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
/** 处理重新认证, 常用于回到登录页 */
|
|
24
|
+
async function doReAuthenticate() {}
|
|
25
|
+
|
|
26
|
+
/** 处理刷新token */
|
|
27
|
+
async function doRefreshToken() {
|
|
28
|
+
return ''
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// 请求头处理
|
|
32
|
+
client.addRequestInterceptor({
|
|
33
|
+
fulfilled: config => {
|
|
34
|
+
return config
|
|
35
|
+
},
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
// 返回数据格式 { data: any, info: { code: number, message: string, status: boolean } }
|
|
39
|
+
client.addResponseInterceptor(
|
|
40
|
+
defaultResponseInterceptor({
|
|
41
|
+
/** 响应数据中代表访问结果的字段名 */
|
|
42
|
+
codeField: 'code',
|
|
43
|
+
/** 响应数据中装载实际数据的字段名,或者提供一个函数从响应数据中解析需要返回的数据 */
|
|
44
|
+
dataField: 'data',
|
|
45
|
+
/** 当codeField所指定的字段值与successCode相同时,代表接口访问成功。如果提供一个函数,则返回true代表接口访问成功 */
|
|
46
|
+
successCode: 200,
|
|
47
|
+
}),
|
|
48
|
+
)
|
|
49
|
+
|
|
50
|
+
client.addResponseInterceptor(
|
|
51
|
+
authenticateResponseInterceptor({
|
|
52
|
+
client,
|
|
53
|
+
doReAuthenticate,
|
|
54
|
+
doRefreshToken,
|
|
55
|
+
/** 是否启用刷新令牌 */
|
|
56
|
+
enableRefreshToken: true,
|
|
57
|
+
}),
|
|
58
|
+
)
|
|
59
|
+
|
|
60
|
+
client.addResponseInterceptor(
|
|
61
|
+
errorMessageResponseInterceptor((msg: string, error) => {
|
|
62
|
+
/** 处理错误消息 */
|
|
63
|
+
}),
|
|
64
|
+
)
|
|
65
|
+
|
|
66
|
+
return client
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export const requestClient = createRequestClient()
|
|
70
|
+
|
|
71
|
+
export const baseRequestClient = new RequestClient({ baseURL })
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
#### 使用 requestClient
|
|
75
|
+
|
|
76
|
+
```typescript
|
|
77
|
+
import { requestClient } from './request'
|
|
78
|
+
requestClient.get('/user').then(res => {
|
|
79
|
+
console.log(res)
|
|
80
|
+
})
|
|
81
|
+
```
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
var e=Object.create,t=Object.defineProperty,n=Object.getOwnPropertyDescriptor,r=Object.getOwnPropertyNames,i=Object.getPrototypeOf,a=Object.prototype.hasOwnProperty,o=(e,i,o,s)=>{if(i&&typeof i==`object`||typeof i==`function`)for(var c=r(i),l=0,u=c.length,d;l<u;l++)d=c[l],!a.call(e,d)&&d!==o&&t(e,d,{get:(e=>i[e]).bind(null,d),enumerable:!(s=n(i,d))||s.enumerable});return e},s=(n,r,a)=>(a=n==null?{}:e(i(n)),o(r||!n||!n.__esModule?t(a,`default`,{value:n,enumerable:!0}):a,n));let c=require(`axios`);c=s(c);let l=require(`defu`);var u=class{client;constructor(e){this.client=e}async download(e,t){let n={...t,responseType:`blob`};return await this.client.get(e,n)}};const d={fulfilled:e=>e,rejected:e=>Promise.reject(e)},f={fulfilled:e=>e,rejected:e=>Promise.reject(e)};var p=class{axiosInstance;constructor(e){this.axiosInstance=e}addRequestInterceptor({fulfilled:e,rejected:t}=d){this.axiosInstance.interceptors.request.use(e,t)}addResponseInterceptor({fulfilled:e,rejected:t}=f){this.axiosInstance.interceptors.response.use(e,t)}},m=class{client;constructor(e){this.client=e}async upload(e,t,n){let r=new FormData;Object.entries(t).forEach(([e,t])=>{r.append(e,t)});let i={...n,headers:{"Content-Type":`multipart/form-data`,...n?.headers}};return this.client.post(e,r,i)}},h=class{instance;addRequestInterceptor;addResponseInterceptor;download;isRefreshing=!1;refreshTokenQueue=[];upload;constructor(e={}){let t={headers:{"Content-Type":`application/json;charset=utf-8`},timeout:1e4},{...n}=e,r=(0,l.defu)(n,t);this.instance=c.default.create(r);let i=new p(this.instance);this.addRequestInterceptor=i.addRequestInterceptor.bind(i),this.addResponseInterceptor=i.addResponseInterceptor.bind(i);let a=new m(this);this.upload=a.upload.bind(a);let o=new u(this);this.download=o.download.bind(o)}delete(e,t){return this.request(e,{...t,method:`DELETE`})}get(e,t){return this.request(e,{...t,method:`GET`})}post(e,t,n){return this.request(e,{...n,data:t,method:`POST`})}put(e,t,n){return this.request(e,{...n,data:t,method:`PUT`})}setDefaultHeader(e,t){this.instance.defaults.headers.common[e]=t}async request(e,t){try{return await this.instance({url:e,...t})}catch(e){throw e.response?e.response.data:e}}};const g=e=>typeof e==`function`;function _(e){return e?`Bearer ${e}`:null}const v=({token:e})=>({fulfilled:t=>(t.headers.authorization=_(e),t)}),y=({codeField:e=`code`,dataField:t=`data`,successCode:n=200})=>({fulfilled:r=>{let{config:i,data:a,status:o}=r;if(i.responseReturn===`raw`)return r;if(o>=200&&o<400){if(i.responseReturn===`body`)return a;if(g(n)?n(a[e]):a.info[e]===n)return g(t)?t(a):a[t]}throw Object.assign({},r,{response:r})}}),b=({client:e,doReAuthenticate:t,doRefreshToken:n,enableRefreshToken:r})=>({rejected:async i=>{let{config:a,response:o}=i;if(o?.status!==401)throw i;if(!r||a.__isRetryRequest)throw await t(),i;if(e.isRefreshing)return new Promise(t=>{e.refreshTokenQueue.push(n=>{a.headers.Authorization=_(n),t(e.request(a.url,{...a}))})});e.isRefreshing=!0,a.__isRetryRequest=!0;try{let t=await n();return e.refreshTokenQueue.forEach(e=>e(t)),e.refreshTokenQueue=[],e.request(i.config.url,{...i.config})}catch(n){throw console.log(`object`,n),e.refreshTokenQueue.forEach(e=>e(``)),e.refreshTokenQueue=[],await t(),n}finally{e.isRefreshing=!1}}}),x=e=>({rejected:t=>{if(c.default.isCancel(t))return Promise.reject(t);let n=t?.toString?.()??``,r=``;if(n?.includes(`Network Error`)?r=`网络异常,请检查您的网络连接后重试。`:t?.message?.includes?.(`timeout`)&&(r=`请求超时,请稍后再试。`),r)return e?.(r,t),Promise.reject(t);let i=``;switch(t?.response?.status){case 400:i=`请求错误。请检查您的输入并重试。`;break;case 401:i=`登录认证过期,请重新登录后继续。`;break;case 403:i=`禁止访问, 您没有权限访问此资源。`;break;case 404:i=`未找到, 请求的资源不存在。`;break;case 408:i=`请求超时,请稍后再试。`;break;default:i=`内部服务器错误,请稍后再试。`}return e?.(i,t),Promise.reject(t)}});exports.RequestClient=h,exports.authenticateResponseInterceptor=b,exports.defaultRequestInterceptor=v,exports.defaultResponseInterceptor=y,exports.errorMessageResponseInterceptor=x,exports.formatToken=_;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { AxiosInstance, AxiosRequestConfig, AxiosRequestConfig as AxiosRequestConfig$1, AxiosResponse, CreateAxiosDefaults, InternalAxiosRequestConfig } from "axios";
|
|
2
|
+
|
|
3
|
+
//#region src/types.d.ts
|
|
4
|
+
type ExtendOptions<T = any> = {
|
|
5
|
+
paramsSerializer?: 'brackets' | 'comma' | 'indices' | 'repeat' | AxiosRequestConfig<T>['paramsSerializer'];
|
|
6
|
+
responseReturn?: 'body' | 'data' | 'raw';
|
|
7
|
+
};
|
|
8
|
+
type RequestClientConfig<T = any> = AxiosRequestConfig<T> & ExtendOptions<T>;
|
|
9
|
+
type RequestResponse<T = any> = AxiosResponse<T> & {
|
|
10
|
+
config: RequestClientConfig<T>;
|
|
11
|
+
};
|
|
12
|
+
type RequestContentType = 'application/json;charset=utf-8' | 'application/octet-stream;charset=utf-8' | 'application/x-www-form-urlencoded;charset=utf-8' | 'multipart/form-data;charset=utf-8';
|
|
13
|
+
type RequestClientOptions = CreateAxiosDefaults & ExtendOptions;
|
|
14
|
+
interface SseRequestOptions extends RequestInit {
|
|
15
|
+
onMessage?: (message: string) => void;
|
|
16
|
+
onEnd?: () => void;
|
|
17
|
+
}
|
|
18
|
+
interface RequestInterceptorConfig {
|
|
19
|
+
fulfilled?: (config: ExtendOptions & InternalAxiosRequestConfig) => (ExtendOptions & InternalAxiosRequestConfig<any>) | Promise<ExtendOptions & InternalAxiosRequestConfig<any>>;
|
|
20
|
+
rejected?: (error: any) => any;
|
|
21
|
+
}
|
|
22
|
+
interface ResponseInterceptorConfig<T = any> {
|
|
23
|
+
fulfilled?: (response: RequestResponse<T>) => Promise<RequestResponse> | RequestResponse;
|
|
24
|
+
rejected?: (error: any) => any;
|
|
25
|
+
}
|
|
26
|
+
type MakeErrorMessageFn = (message: string, error: any) => void;
|
|
27
|
+
interface HttpResponse<T = any> {
|
|
28
|
+
code: number;
|
|
29
|
+
data: T;
|
|
30
|
+
message: string;
|
|
31
|
+
}
|
|
32
|
+
//#endregion
|
|
33
|
+
//#region src/modules/downloader.d.ts
|
|
34
|
+
declare class FileDownloader {
|
|
35
|
+
private client;
|
|
36
|
+
constructor(client: RequestClient);
|
|
37
|
+
download(url: string, config?: AxiosRequestConfig$1): Promise<RequestResponse<Blob>>;
|
|
38
|
+
}
|
|
39
|
+
//#endregion
|
|
40
|
+
//#region src/modules/interceptor.d.ts
|
|
41
|
+
declare class InterceptorManager {
|
|
42
|
+
private axiosInstance;
|
|
43
|
+
constructor(instance: AxiosInstance);
|
|
44
|
+
addRequestInterceptor({
|
|
45
|
+
fulfilled,
|
|
46
|
+
rejected
|
|
47
|
+
}?: RequestInterceptorConfig): void;
|
|
48
|
+
addResponseInterceptor<T = any>({
|
|
49
|
+
fulfilled,
|
|
50
|
+
rejected
|
|
51
|
+
}?: ResponseInterceptorConfig<T>): void;
|
|
52
|
+
}
|
|
53
|
+
//#endregion
|
|
54
|
+
//#region src/modules/uploader.d.ts
|
|
55
|
+
declare class FileUploader {
|
|
56
|
+
private client;
|
|
57
|
+
constructor(client: RequestClient);
|
|
58
|
+
upload(url: string, data: {
|
|
59
|
+
file: Blob | File;
|
|
60
|
+
} & Record<string, any>, config?: AxiosRequestConfig$1): Promise<AxiosResponse>;
|
|
61
|
+
}
|
|
62
|
+
//#endregion
|
|
63
|
+
//#region src/request-client.d.ts
|
|
64
|
+
declare class RequestClient {
|
|
65
|
+
private readonly instance;
|
|
66
|
+
addRequestInterceptor: InterceptorManager['addRequestInterceptor'];
|
|
67
|
+
addResponseInterceptor: InterceptorManager['addResponseInterceptor'];
|
|
68
|
+
download: FileDownloader['download'];
|
|
69
|
+
isRefreshing: boolean;
|
|
70
|
+
refreshTokenQueue: ((token: string) => void)[];
|
|
71
|
+
upload: FileUploader['upload'];
|
|
72
|
+
constructor(options?: RequestClientOptions);
|
|
73
|
+
delete<T = any>(url: string, config?: AxiosRequestConfig$1): Promise<T>;
|
|
74
|
+
get<T = any>(url: string, config?: AxiosRequestConfig$1): Promise<T>;
|
|
75
|
+
post<T = any>(url: string, data?: any, config?: AxiosRequestConfig$1): Promise<T>;
|
|
76
|
+
put<T = any>(url: string, data?: any, config?: AxiosRequestConfig$1): Promise<T>;
|
|
77
|
+
setDefaultHeader(name: string, value: string): void;
|
|
78
|
+
request<T>(url: string, config: AxiosRequestConfig$1): Promise<T>;
|
|
79
|
+
}
|
|
80
|
+
//#endregion
|
|
81
|
+
//#region src/preset-interceptors.d.ts
|
|
82
|
+
declare function formatToken(token: null | string): string | null;
|
|
83
|
+
declare const defaultRequestInterceptor: ({
|
|
84
|
+
token
|
|
85
|
+
}: {
|
|
86
|
+
token: string;
|
|
87
|
+
}) => RequestInterceptorConfig;
|
|
88
|
+
declare const defaultResponseInterceptor: ({
|
|
89
|
+
codeField,
|
|
90
|
+
dataField,
|
|
91
|
+
successCode
|
|
92
|
+
}: {
|
|
93
|
+
codeField: string;
|
|
94
|
+
dataField: ((response: any) => any) | string;
|
|
95
|
+
successCode: ((code: any) => boolean) | number | string;
|
|
96
|
+
}) => ResponseInterceptorConfig;
|
|
97
|
+
declare const authenticateResponseInterceptor: ({
|
|
98
|
+
client,
|
|
99
|
+
doReAuthenticate,
|
|
100
|
+
doRefreshToken,
|
|
101
|
+
enableRefreshToken
|
|
102
|
+
}: {
|
|
103
|
+
client: RequestClient;
|
|
104
|
+
doReAuthenticate: () => Promise<void>;
|
|
105
|
+
doRefreshToken: () => Promise<string>;
|
|
106
|
+
enableRefreshToken: boolean;
|
|
107
|
+
}) => ResponseInterceptorConfig;
|
|
108
|
+
declare const errorMessageResponseInterceptor: (makeErrorMessage?: MakeErrorMessageFn) => ResponseInterceptorConfig;
|
|
109
|
+
//#endregion
|
|
110
|
+
export { type AxiosRequestConfig, type HttpResponse, type MakeErrorMessageFn, RequestClient, type RequestClientConfig, type RequestClientOptions, type RequestContentType, type RequestInterceptorConfig, type RequestResponse, type ResponseInterceptorConfig, type SseRequestOptions, authenticateResponseInterceptor, defaultRequestInterceptor, defaultResponseInterceptor, errorMessageResponseInterceptor, formatToken };
|
package/dist/index.d.mts
CHANGED
|
@@ -1,89 +1,110 @@
|
|
|
1
|
-
import { AxiosRequestConfig,
|
|
2
|
-
export { AxiosRequestConfig } from 'axios';
|
|
1
|
+
import { AxiosInstance, AxiosRequestConfig, AxiosRequestConfig as AxiosRequestConfig$1, AxiosResponse, CreateAxiosDefaults, InternalAxiosRequestConfig } from "axios";
|
|
3
2
|
|
|
3
|
+
//#region src/types.d.ts
|
|
4
4
|
type ExtendOptions<T = any> = {
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
paramsSerializer?: 'brackets' | 'comma' | 'indices' | 'repeat' | AxiosRequestConfig<T>['paramsSerializer'];
|
|
6
|
+
responseReturn?: 'body' | 'data' | 'raw';
|
|
7
7
|
};
|
|
8
8
|
type RequestClientConfig<T = any> = AxiosRequestConfig<T> & ExtendOptions<T>;
|
|
9
9
|
type RequestResponse<T = any> = AxiosResponse<T> & {
|
|
10
|
-
|
|
10
|
+
config: RequestClientConfig<T>;
|
|
11
11
|
};
|
|
12
12
|
type RequestContentType = 'application/json;charset=utf-8' | 'application/octet-stream;charset=utf-8' | 'application/x-www-form-urlencoded;charset=utf-8' | 'multipart/form-data;charset=utf-8';
|
|
13
13
|
type RequestClientOptions = CreateAxiosDefaults & ExtendOptions;
|
|
14
14
|
interface SseRequestOptions extends RequestInit {
|
|
15
|
-
|
|
16
|
-
|
|
15
|
+
onMessage?: (message: string) => void;
|
|
16
|
+
onEnd?: () => void;
|
|
17
17
|
}
|
|
18
18
|
interface RequestInterceptorConfig {
|
|
19
|
-
|
|
20
|
-
|
|
19
|
+
fulfilled?: (config: ExtendOptions & InternalAxiosRequestConfig) => (ExtendOptions & InternalAxiosRequestConfig<any>) | Promise<ExtendOptions & InternalAxiosRequestConfig<any>>;
|
|
20
|
+
rejected?: (error: any) => any;
|
|
21
21
|
}
|
|
22
22
|
interface ResponseInterceptorConfig<T = any> {
|
|
23
|
-
|
|
24
|
-
|
|
23
|
+
fulfilled?: (response: RequestResponse<T>) => Promise<RequestResponse> | RequestResponse;
|
|
24
|
+
rejected?: (error: any) => any;
|
|
25
25
|
}
|
|
26
26
|
type MakeErrorMessageFn = (message: string, error: any) => void;
|
|
27
27
|
interface HttpResponse<T = any> {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
28
|
+
code: number;
|
|
29
|
+
data: T;
|
|
30
|
+
message: string;
|
|
31
31
|
}
|
|
32
|
-
|
|
32
|
+
//#endregion
|
|
33
|
+
//#region src/modules/downloader.d.ts
|
|
33
34
|
declare class FileDownloader {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
35
|
+
private client;
|
|
36
|
+
constructor(client: RequestClient);
|
|
37
|
+
download(url: string, config?: AxiosRequestConfig$1): Promise<RequestResponse<Blob>>;
|
|
37
38
|
}
|
|
38
|
-
|
|
39
|
+
//#endregion
|
|
40
|
+
//#region src/modules/interceptor.d.ts
|
|
39
41
|
declare class InterceptorManager {
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
42
|
+
private axiosInstance;
|
|
43
|
+
constructor(instance: AxiosInstance);
|
|
44
|
+
addRequestInterceptor({
|
|
45
|
+
fulfilled,
|
|
46
|
+
rejected
|
|
47
|
+
}?: RequestInterceptorConfig): void;
|
|
48
|
+
addResponseInterceptor<T = any>({
|
|
49
|
+
fulfilled,
|
|
50
|
+
rejected
|
|
51
|
+
}?: ResponseInterceptorConfig<T>): void;
|
|
44
52
|
}
|
|
45
|
-
|
|
53
|
+
//#endregion
|
|
54
|
+
//#region src/modules/uploader.d.ts
|
|
46
55
|
declare class FileUploader {
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
56
|
+
private client;
|
|
57
|
+
constructor(client: RequestClient);
|
|
58
|
+
upload(url: string, data: {
|
|
59
|
+
file: Blob | File;
|
|
60
|
+
} & Record<string, any>, config?: AxiosRequestConfig$1): Promise<AxiosResponse>;
|
|
52
61
|
}
|
|
53
|
-
|
|
62
|
+
//#endregion
|
|
63
|
+
//#region src/request-client.d.ts
|
|
54
64
|
declare class RequestClient {
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
65
|
+
private readonly instance;
|
|
66
|
+
addRequestInterceptor: InterceptorManager['addRequestInterceptor'];
|
|
67
|
+
addResponseInterceptor: InterceptorManager['addResponseInterceptor'];
|
|
68
|
+
download: FileDownloader['download'];
|
|
69
|
+
isRefreshing: boolean;
|
|
70
|
+
refreshTokenQueue: ((token: string) => void)[];
|
|
71
|
+
upload: FileUploader['upload'];
|
|
72
|
+
constructor(options?: RequestClientOptions);
|
|
73
|
+
delete<T = any>(url: string, config?: AxiosRequestConfig$1): Promise<T>;
|
|
74
|
+
get<T = any>(url: string, config?: AxiosRequestConfig$1): Promise<T>;
|
|
75
|
+
post<T = any>(url: string, data?: any, config?: AxiosRequestConfig$1): Promise<T>;
|
|
76
|
+
put<T = any>(url: string, data?: any, config?: AxiosRequestConfig$1): Promise<T>;
|
|
77
|
+
setDefaultHeader(name: string, value: string): void;
|
|
78
|
+
request<T>(url: string, config: AxiosRequestConfig$1): Promise<T>;
|
|
69
79
|
}
|
|
70
|
-
|
|
80
|
+
//#endregion
|
|
81
|
+
//#region src/preset-interceptors.d.ts
|
|
71
82
|
declare function formatToken(token: null | string): string | null;
|
|
72
|
-
declare const defaultRequestInterceptor: ({
|
|
73
|
-
|
|
83
|
+
declare const defaultRequestInterceptor: ({
|
|
84
|
+
token
|
|
85
|
+
}: {
|
|
86
|
+
token: string;
|
|
74
87
|
}) => RequestInterceptorConfig;
|
|
75
|
-
declare const defaultResponseInterceptor: ({
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
88
|
+
declare const defaultResponseInterceptor: ({
|
|
89
|
+
codeField,
|
|
90
|
+
dataField,
|
|
91
|
+
successCode
|
|
92
|
+
}: {
|
|
93
|
+
codeField: string;
|
|
94
|
+
dataField: ((response: any) => any) | string;
|
|
95
|
+
successCode: ((code: any) => boolean) | number | string;
|
|
79
96
|
}) => ResponseInterceptorConfig;
|
|
80
|
-
declare const authenticateResponseInterceptor: ({
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
97
|
+
declare const authenticateResponseInterceptor: ({
|
|
98
|
+
client,
|
|
99
|
+
doReAuthenticate,
|
|
100
|
+
doRefreshToken,
|
|
101
|
+
enableRefreshToken
|
|
102
|
+
}: {
|
|
103
|
+
client: RequestClient;
|
|
104
|
+
doReAuthenticate: () => Promise<void>;
|
|
105
|
+
doRefreshToken: () => Promise<string>;
|
|
106
|
+
enableRefreshToken: boolean;
|
|
85
107
|
}) => ResponseInterceptorConfig;
|
|
86
108
|
declare const errorMessageResponseInterceptor: (makeErrorMessage?: MakeErrorMessageFn) => ResponseInterceptorConfig;
|
|
87
|
-
|
|
88
|
-
export { RequestClient, authenticateResponseInterceptor, defaultRequestInterceptor, defaultResponseInterceptor, errorMessageResponseInterceptor, formatToken };
|
|
89
|
-
export type { HttpResponse, MakeErrorMessageFn, RequestClientConfig, RequestClientOptions, RequestContentType, RequestInterceptorConfig, RequestResponse, ResponseInterceptorConfig, SseRequestOptions };
|
|
109
|
+
//#endregion
|
|
110
|
+
export { type AxiosRequestConfig, type HttpResponse, type MakeErrorMessageFn, RequestClient, type RequestClientConfig, type RequestClientOptions, type RequestContentType, type RequestInterceptorConfig, type RequestResponse, type ResponseInterceptorConfig, type SseRequestOptions, authenticateResponseInterceptor, defaultRequestInterceptor, defaultResponseInterceptor, errorMessageResponseInterceptor, formatToken };
|
package/dist/index.mjs
CHANGED
|
@@ -1,273 +1 @@
|
|
|
1
|
-
import axios from
|
|
2
|
-
import { defu } from 'defu';
|
|
3
|
-
|
|
4
|
-
class FileDownloader {
|
|
5
|
-
client;
|
|
6
|
-
constructor(client) {
|
|
7
|
-
this.client = client;
|
|
8
|
-
}
|
|
9
|
-
async download(url, config) {
|
|
10
|
-
const finalConfig = {
|
|
11
|
-
...config,
|
|
12
|
-
responseType: "blob"
|
|
13
|
-
};
|
|
14
|
-
const response = await this.client.get(
|
|
15
|
-
url,
|
|
16
|
-
finalConfig
|
|
17
|
-
);
|
|
18
|
-
return response;
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
const defaultRequestInterceptorConfig = {
|
|
23
|
-
fulfilled: (response) => response,
|
|
24
|
-
rejected: (error) => Promise.reject(error)
|
|
25
|
-
};
|
|
26
|
-
const defaultResponseInterceptorConfig = {
|
|
27
|
-
fulfilled: (response) => response,
|
|
28
|
-
rejected: (error) => Promise.reject(error)
|
|
29
|
-
};
|
|
30
|
-
class InterceptorManager {
|
|
31
|
-
axiosInstance;
|
|
32
|
-
constructor(instance) {
|
|
33
|
-
this.axiosInstance = instance;
|
|
34
|
-
}
|
|
35
|
-
addRequestInterceptor({
|
|
36
|
-
fulfilled,
|
|
37
|
-
rejected
|
|
38
|
-
} = defaultRequestInterceptorConfig) {
|
|
39
|
-
this.axiosInstance.interceptors.request.use(fulfilled, rejected);
|
|
40
|
-
}
|
|
41
|
-
addResponseInterceptor({
|
|
42
|
-
fulfilled,
|
|
43
|
-
rejected
|
|
44
|
-
} = defaultResponseInterceptorConfig) {
|
|
45
|
-
this.axiosInstance.interceptors.response.use(fulfilled, rejected);
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
class FileUploader {
|
|
50
|
-
client;
|
|
51
|
-
constructor(client) {
|
|
52
|
-
this.client = client;
|
|
53
|
-
}
|
|
54
|
-
async upload(url, data, config) {
|
|
55
|
-
const formData = new FormData();
|
|
56
|
-
Object.entries(data).forEach(([key, value]) => {
|
|
57
|
-
formData.append(key, value);
|
|
58
|
-
});
|
|
59
|
-
const finalConfig = {
|
|
60
|
-
...config,
|
|
61
|
-
headers: {
|
|
62
|
-
"Content-Type": "multipart/form-data",
|
|
63
|
-
...config?.headers
|
|
64
|
-
}
|
|
65
|
-
};
|
|
66
|
-
return this.client.post(url, formData, finalConfig);
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
class RequestClient {
|
|
71
|
-
instance;
|
|
72
|
-
addRequestInterceptor;
|
|
73
|
-
addResponseInterceptor;
|
|
74
|
-
download;
|
|
75
|
-
// 是否正在刷新token
|
|
76
|
-
isRefreshing = false;
|
|
77
|
-
// 刷新token队列
|
|
78
|
-
refreshTokenQueue = [];
|
|
79
|
-
upload;
|
|
80
|
-
/**
|
|
81
|
-
* 构造函数,用于创建Axios实例
|
|
82
|
-
* @param options - Axios请求配置,可选
|
|
83
|
-
*/
|
|
84
|
-
constructor(options = {}) {
|
|
85
|
-
const defaultConfig = {
|
|
86
|
-
headers: {
|
|
87
|
-
"Content-Type": "application/json;charset=utf-8"
|
|
88
|
-
},
|
|
89
|
-
// 默认超时时间
|
|
90
|
-
timeout: 1e4
|
|
91
|
-
};
|
|
92
|
-
const { ...axiosConfig } = options;
|
|
93
|
-
const requestConfig = defu(axiosConfig, defaultConfig);
|
|
94
|
-
this.instance = axios.create(requestConfig);
|
|
95
|
-
const interceptorManager = new InterceptorManager(this.instance);
|
|
96
|
-
this.addRequestInterceptor = interceptorManager.addRequestInterceptor.bind(interceptorManager);
|
|
97
|
-
this.addResponseInterceptor = interceptorManager.addResponseInterceptor.bind(interceptorManager);
|
|
98
|
-
const fileUploader = new FileUploader(this);
|
|
99
|
-
this.upload = fileUploader.upload.bind(fileUploader);
|
|
100
|
-
const fileDownloader = new FileDownloader(this);
|
|
101
|
-
this.download = fileDownloader.download.bind(fileDownloader);
|
|
102
|
-
}
|
|
103
|
-
/**
|
|
104
|
-
* DELETE请求方法
|
|
105
|
-
*/
|
|
106
|
-
delete(url, config) {
|
|
107
|
-
return this.request(url, { ...config, method: "DELETE" });
|
|
108
|
-
}
|
|
109
|
-
/**
|
|
110
|
-
* GET请求方法
|
|
111
|
-
*/
|
|
112
|
-
get(url, config) {
|
|
113
|
-
return this.request(url, { ...config, method: "GET" });
|
|
114
|
-
}
|
|
115
|
-
/**
|
|
116
|
-
* POST请求方法
|
|
117
|
-
*/
|
|
118
|
-
post(url, data, config) {
|
|
119
|
-
return this.request(url, { ...config, data, method: "POST" });
|
|
120
|
-
}
|
|
121
|
-
/**
|
|
122
|
-
* PUT请求方法
|
|
123
|
-
*/
|
|
124
|
-
put(url, data, config) {
|
|
125
|
-
return this.request(url, { ...config, data, method: "PUT" });
|
|
126
|
-
}
|
|
127
|
-
setDefaultHeader(name, value) {
|
|
128
|
-
this.instance.defaults.headers.common[name] = value;
|
|
129
|
-
}
|
|
130
|
-
/**
|
|
131
|
-
* 通用的请求方法
|
|
132
|
-
*/
|
|
133
|
-
async request(url, config) {
|
|
134
|
-
try {
|
|
135
|
-
const response = await this.instance({
|
|
136
|
-
url,
|
|
137
|
-
...config
|
|
138
|
-
});
|
|
139
|
-
return response;
|
|
140
|
-
} catch (error) {
|
|
141
|
-
throw error.response ? error.response.data : error;
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
const isFunction = (val) => typeof val === "function";
|
|
147
|
-
function formatToken(token) {
|
|
148
|
-
return token ? `Bearer ${token}` : null;
|
|
149
|
-
}
|
|
150
|
-
const defaultRequestInterceptor = ({ token }) => {
|
|
151
|
-
return {
|
|
152
|
-
fulfilled: (config) => {
|
|
153
|
-
config.headers.authorization = formatToken(token);
|
|
154
|
-
return config;
|
|
155
|
-
}
|
|
156
|
-
};
|
|
157
|
-
};
|
|
158
|
-
const defaultResponseInterceptor = ({
|
|
159
|
-
codeField = "code",
|
|
160
|
-
dataField = "data",
|
|
161
|
-
successCode = 200
|
|
162
|
-
}) => {
|
|
163
|
-
return {
|
|
164
|
-
fulfilled: (response) => {
|
|
165
|
-
const { config, data: responseData, status } = response;
|
|
166
|
-
if (config.responseReturn === "raw") {
|
|
167
|
-
return response;
|
|
168
|
-
}
|
|
169
|
-
if (status >= 200 && status < 400) {
|
|
170
|
-
if (config.responseReturn === "body") {
|
|
171
|
-
return responseData;
|
|
172
|
-
} else if (isFunction(successCode) ? successCode(responseData[codeField]) : responseData["info"][codeField] === successCode) {
|
|
173
|
-
return isFunction(dataField) ? dataField(responseData) : responseData[dataField];
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
throw Object.assign({}, response, { response });
|
|
177
|
-
}
|
|
178
|
-
};
|
|
179
|
-
};
|
|
180
|
-
const authenticateResponseInterceptor = ({
|
|
181
|
-
client,
|
|
182
|
-
doReAuthenticate,
|
|
183
|
-
doRefreshToken,
|
|
184
|
-
enableRefreshToken
|
|
185
|
-
}) => {
|
|
186
|
-
return {
|
|
187
|
-
rejected: async (error) => {
|
|
188
|
-
const { config, response } = error;
|
|
189
|
-
if (response?.status !== 401) {
|
|
190
|
-
throw error;
|
|
191
|
-
}
|
|
192
|
-
if (!enableRefreshToken || config.__isRetryRequest) {
|
|
193
|
-
await doReAuthenticate();
|
|
194
|
-
throw error;
|
|
195
|
-
}
|
|
196
|
-
if (client.isRefreshing) {
|
|
197
|
-
return new Promise((resolve) => {
|
|
198
|
-
client.refreshTokenQueue.push((newToken) => {
|
|
199
|
-
config.headers.Authorization = formatToken(newToken);
|
|
200
|
-
resolve(client.request(config.url, { ...config }));
|
|
201
|
-
});
|
|
202
|
-
});
|
|
203
|
-
}
|
|
204
|
-
client.isRefreshing = true;
|
|
205
|
-
config.__isRetryRequest = true;
|
|
206
|
-
try {
|
|
207
|
-
const newToken = await doRefreshToken();
|
|
208
|
-
client.refreshTokenQueue.forEach((callback) => callback(newToken));
|
|
209
|
-
client.refreshTokenQueue = [];
|
|
210
|
-
return client.request(error.config.url, { ...error.config });
|
|
211
|
-
} catch (refreshError) {
|
|
212
|
-
console.log("object", refreshError);
|
|
213
|
-
client.refreshTokenQueue.forEach((callback) => callback(""));
|
|
214
|
-
client.refreshTokenQueue = [];
|
|
215
|
-
await doReAuthenticate();
|
|
216
|
-
throw refreshError;
|
|
217
|
-
} finally {
|
|
218
|
-
client.isRefreshing = false;
|
|
219
|
-
}
|
|
220
|
-
}
|
|
221
|
-
};
|
|
222
|
-
};
|
|
223
|
-
const errorMessageResponseInterceptor = (makeErrorMessage) => {
|
|
224
|
-
return {
|
|
225
|
-
rejected: (error) => {
|
|
226
|
-
if (axios.isCancel(error)) {
|
|
227
|
-
return Promise.reject(error);
|
|
228
|
-
}
|
|
229
|
-
const err = error?.toString?.() ?? "";
|
|
230
|
-
let errMsg = "";
|
|
231
|
-
if (err?.includes("Network Error")) {
|
|
232
|
-
errMsg = "\u7F51\u7EDC\u5F02\u5E38\uFF0C\u8BF7\u68C0\u67E5\u60A8\u7684\u7F51\u7EDC\u8FDE\u63A5\u540E\u91CD\u8BD5\u3002";
|
|
233
|
-
} else if (error?.message?.includes?.("timeout")) {
|
|
234
|
-
errMsg = "\u8BF7\u6C42\u8D85\u65F6\uFF0C\u8BF7\u7A0D\u540E\u518D\u8BD5\u3002";
|
|
235
|
-
}
|
|
236
|
-
if (errMsg) {
|
|
237
|
-
makeErrorMessage?.(errMsg, error);
|
|
238
|
-
return Promise.reject(error);
|
|
239
|
-
}
|
|
240
|
-
let errorMessage = "";
|
|
241
|
-
const status = error?.response?.status;
|
|
242
|
-
switch (status) {
|
|
243
|
-
case 400: {
|
|
244
|
-
errorMessage = "\u8BF7\u6C42\u9519\u8BEF\u3002\u8BF7\u68C0\u67E5\u60A8\u7684\u8F93\u5165\u5E76\u91CD\u8BD5\u3002";
|
|
245
|
-
break;
|
|
246
|
-
}
|
|
247
|
-
case 401: {
|
|
248
|
-
errorMessage = "\u767B\u5F55\u8BA4\u8BC1\u8FC7\u671F\uFF0C\u8BF7\u91CD\u65B0\u767B\u5F55\u540E\u7EE7\u7EED\u3002";
|
|
249
|
-
break;
|
|
250
|
-
}
|
|
251
|
-
case 403: {
|
|
252
|
-
errorMessage = "\u7981\u6B62\u8BBF\u95EE, \u60A8\u6CA1\u6709\u6743\u9650\u8BBF\u95EE\u6B64\u8D44\u6E90\u3002";
|
|
253
|
-
break;
|
|
254
|
-
}
|
|
255
|
-
case 404: {
|
|
256
|
-
errorMessage = "\u672A\u627E\u5230, \u8BF7\u6C42\u7684\u8D44\u6E90\u4E0D\u5B58\u5728\u3002";
|
|
257
|
-
break;
|
|
258
|
-
}
|
|
259
|
-
case 408: {
|
|
260
|
-
errorMessage = "\u8BF7\u6C42\u8D85\u65F6\uFF0C\u8BF7\u7A0D\u540E\u518D\u8BD5\u3002";
|
|
261
|
-
break;
|
|
262
|
-
}
|
|
263
|
-
default: {
|
|
264
|
-
errorMessage = "\u5185\u90E8\u670D\u52A1\u5668\u9519\u8BEF\uFF0C\u8BF7\u7A0D\u540E\u518D\u8BD5\u3002";
|
|
265
|
-
}
|
|
266
|
-
}
|
|
267
|
-
makeErrorMessage?.(errorMessage, error);
|
|
268
|
-
return Promise.reject(error);
|
|
269
|
-
}
|
|
270
|
-
};
|
|
271
|
-
};
|
|
272
|
-
|
|
273
|
-
export { RequestClient, authenticateResponseInterceptor, defaultRequestInterceptor, defaultResponseInterceptor, errorMessageResponseInterceptor, formatToken };
|
|
1
|
+
import e from"axios";import{defu as t}from"defu";var n=class{client;constructor(e){this.client=e}async download(e,t){let n={...t,responseType:`blob`};return await this.client.get(e,n)}};const r={fulfilled:e=>e,rejected:e=>Promise.reject(e)},i={fulfilled:e=>e,rejected:e=>Promise.reject(e)};var a=class{axiosInstance;constructor(e){this.axiosInstance=e}addRequestInterceptor({fulfilled:e,rejected:t}=r){this.axiosInstance.interceptors.request.use(e,t)}addResponseInterceptor({fulfilled:e,rejected:t}=i){this.axiosInstance.interceptors.response.use(e,t)}},o=class{client;constructor(e){this.client=e}async upload(e,t,n){let r=new FormData;Object.entries(t).forEach(([e,t])=>{r.append(e,t)});let i={...n,headers:{"Content-Type":`multipart/form-data`,...n?.headers}};return this.client.post(e,r,i)}},s=class{instance;addRequestInterceptor;addResponseInterceptor;download;isRefreshing=!1;refreshTokenQueue=[];upload;constructor(r={}){let i={headers:{"Content-Type":`application/json;charset=utf-8`},timeout:1e4},{...s}=r,c=t(s,i);this.instance=e.create(c);let l=new a(this.instance);this.addRequestInterceptor=l.addRequestInterceptor.bind(l),this.addResponseInterceptor=l.addResponseInterceptor.bind(l);let u=new o(this);this.upload=u.upload.bind(u);let d=new n(this);this.download=d.download.bind(d)}delete(e,t){return this.request(e,{...t,method:`DELETE`})}get(e,t){return this.request(e,{...t,method:`GET`})}post(e,t,n){return this.request(e,{...n,data:t,method:`POST`})}put(e,t,n){return this.request(e,{...n,data:t,method:`PUT`})}setDefaultHeader(e,t){this.instance.defaults.headers.common[e]=t}async request(e,t){try{return await this.instance({url:e,...t})}catch(e){throw e.response?e.response.data:e}}};const c=e=>typeof e==`function`;function l(e){return e?`Bearer ${e}`:null}const u=({token:e})=>({fulfilled:t=>(t.headers.authorization=l(e),t)}),d=({codeField:e=`code`,dataField:t=`data`,successCode:n=200})=>({fulfilled:r=>{let{config:i,data:a,status:o}=r;if(i.responseReturn===`raw`)return r;if(o>=200&&o<400){if(i.responseReturn===`body`)return a;if(c(n)?n(a[e]):a.info[e]===n)return c(t)?t(a):a[t]}throw Object.assign({},r,{response:r})}}),f=({client:e,doReAuthenticate:t,doRefreshToken:n,enableRefreshToken:r})=>({rejected:async i=>{let{config:a,response:o}=i;if(o?.status!==401)throw i;if(!r||a.__isRetryRequest)throw await t(),i;if(e.isRefreshing)return new Promise(t=>{e.refreshTokenQueue.push(n=>{a.headers.Authorization=l(n),t(e.request(a.url,{...a}))})});e.isRefreshing=!0,a.__isRetryRequest=!0;try{let t=await n();return e.refreshTokenQueue.forEach(e=>e(t)),e.refreshTokenQueue=[],e.request(i.config.url,{...i.config})}catch(n){throw console.log(`object`,n),e.refreshTokenQueue.forEach(e=>e(``)),e.refreshTokenQueue=[],await t(),n}finally{e.isRefreshing=!1}}}),p=t=>({rejected:n=>{if(e.isCancel(n))return Promise.reject(n);let r=n?.toString?.()??``,i=``;if(r?.includes(`Network Error`)?i=`网络异常,请检查您的网络连接后重试。`:n?.message?.includes?.(`timeout`)&&(i=`请求超时,请稍后再试。`),i)return t?.(i,n),Promise.reject(n);let a=``;switch(n?.response?.status){case 400:a=`请求错误。请检查您的输入并重试。`;break;case 401:a=`登录认证过期,请重新登录后继续。`;break;case 403:a=`禁止访问, 您没有权限访问此资源。`;break;case 404:a=`未找到, 请求的资源不存在。`;break;case 408:a=`请求超时,请稍后再试。`;break;default:a=`内部服务器错误,请稍后再试。`}return t?.(a,n),Promise.reject(n)}});export{s as RequestClient,f as authenticateResponseInterceptor,u as defaultRequestInterceptor,d as defaultResponseInterceptor,p as errorMessageResponseInterceptor,l as formatToken};
|
package/package.json
CHANGED
|
@@ -1,25 +1,30 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@swiftcrab/request",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.4",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"type": "module",
|
|
6
|
-
"
|
|
7
|
-
"module": "./dist/index.mjs",
|
|
6
|
+
"types": "./dist/index.d.mts",
|
|
8
7
|
"exports": {
|
|
9
8
|
".": {
|
|
10
|
-
"
|
|
11
|
-
|
|
12
|
-
|
|
9
|
+
"import": {
|
|
10
|
+
"types": "./dist/index.d.mts",
|
|
11
|
+
"default": "./dist/index.mjs"
|
|
12
|
+
},
|
|
13
|
+
"require": {
|
|
14
|
+
"types": "./dist/index.d.cts",
|
|
15
|
+
"default": "./dist/index.cjs"
|
|
16
|
+
}
|
|
17
|
+
}
|
|
13
18
|
},
|
|
14
19
|
"files": [
|
|
15
20
|
"dist"
|
|
16
21
|
],
|
|
17
22
|
"dependencies": {
|
|
18
|
-
"
|
|
19
|
-
"
|
|
23
|
+
"axios": "^1.9.0",
|
|
24
|
+
"defu": "^6.1.4"
|
|
20
25
|
},
|
|
21
26
|
"devDependencies": {
|
|
22
|
-
"
|
|
27
|
+
"tsdown": "^0.17.0-beta.6",
|
|
23
28
|
"typescript": "^5.8.3",
|
|
24
29
|
"@swiftcrab/tsconfig": "1.0.1"
|
|
25
30
|
},
|
|
@@ -28,6 +33,6 @@
|
|
|
28
33
|
"registry": "https://registry.npmjs.org/"
|
|
29
34
|
},
|
|
30
35
|
"scripts": {
|
|
31
|
-
"build": "pnpm
|
|
36
|
+
"build": "pnpm tsdown"
|
|
32
37
|
}
|
|
33
38
|
}
|
package/dist/index.d.ts
DELETED
|
@@ -1,89 +0,0 @@
|
|
|
1
|
-
import { AxiosRequestConfig, InternalAxiosRequestConfig, AxiosResponse, CreateAxiosDefaults, AxiosInstance } from 'axios';
|
|
2
|
-
export { AxiosRequestConfig } from 'axios';
|
|
3
|
-
|
|
4
|
-
type ExtendOptions<T = any> = {
|
|
5
|
-
paramsSerializer?: 'brackets' | 'comma' | 'indices' | 'repeat' | AxiosRequestConfig<T>['paramsSerializer'];
|
|
6
|
-
responseReturn?: 'body' | 'data' | 'raw';
|
|
7
|
-
};
|
|
8
|
-
type RequestClientConfig<T = any> = AxiosRequestConfig<T> & ExtendOptions<T>;
|
|
9
|
-
type RequestResponse<T = any> = AxiosResponse<T> & {
|
|
10
|
-
config: RequestClientConfig<T>;
|
|
11
|
-
};
|
|
12
|
-
type RequestContentType = 'application/json;charset=utf-8' | 'application/octet-stream;charset=utf-8' | 'application/x-www-form-urlencoded;charset=utf-8' | 'multipart/form-data;charset=utf-8';
|
|
13
|
-
type RequestClientOptions = CreateAxiosDefaults & ExtendOptions;
|
|
14
|
-
interface SseRequestOptions extends RequestInit {
|
|
15
|
-
onMessage?: (message: string) => void;
|
|
16
|
-
onEnd?: () => void;
|
|
17
|
-
}
|
|
18
|
-
interface RequestInterceptorConfig {
|
|
19
|
-
fulfilled?: (config: ExtendOptions & InternalAxiosRequestConfig) => (ExtendOptions & InternalAxiosRequestConfig<any>) | Promise<ExtendOptions & InternalAxiosRequestConfig<any>>;
|
|
20
|
-
rejected?: (error: any) => any;
|
|
21
|
-
}
|
|
22
|
-
interface ResponseInterceptorConfig<T = any> {
|
|
23
|
-
fulfilled?: (response: RequestResponse<T>) => Promise<RequestResponse> | RequestResponse;
|
|
24
|
-
rejected?: (error: any) => any;
|
|
25
|
-
}
|
|
26
|
-
type MakeErrorMessageFn = (message: string, error: any) => void;
|
|
27
|
-
interface HttpResponse<T = any> {
|
|
28
|
-
code: number;
|
|
29
|
-
data: T;
|
|
30
|
-
message: string;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
declare class FileDownloader {
|
|
34
|
-
private client;
|
|
35
|
-
constructor(client: RequestClient);
|
|
36
|
-
download(url: string, config?: AxiosRequestConfig): Promise<RequestResponse<Blob>>;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
declare class InterceptorManager {
|
|
40
|
-
private axiosInstance;
|
|
41
|
-
constructor(instance: AxiosInstance);
|
|
42
|
-
addRequestInterceptor({ fulfilled, rejected, }?: RequestInterceptorConfig): void;
|
|
43
|
-
addResponseInterceptor<T = any>({ fulfilled, rejected, }?: ResponseInterceptorConfig<T>): void;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
declare class FileUploader {
|
|
47
|
-
private client;
|
|
48
|
-
constructor(client: RequestClient);
|
|
49
|
-
upload(url: string, data: {
|
|
50
|
-
file: Blob | File;
|
|
51
|
-
} & Record<string, any>, config?: AxiosRequestConfig): Promise<AxiosResponse>;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
declare class RequestClient {
|
|
55
|
-
private readonly instance;
|
|
56
|
-
addRequestInterceptor: InterceptorManager['addRequestInterceptor'];
|
|
57
|
-
addResponseInterceptor: InterceptorManager['addResponseInterceptor'];
|
|
58
|
-
download: FileDownloader['download'];
|
|
59
|
-
isRefreshing: boolean;
|
|
60
|
-
refreshTokenQueue: ((token: string) => void)[];
|
|
61
|
-
upload: FileUploader['upload'];
|
|
62
|
-
constructor(options?: RequestClientOptions);
|
|
63
|
-
delete<T = any>(url: string, config?: AxiosRequestConfig): Promise<T>;
|
|
64
|
-
get<T = any>(url: string, config?: AxiosRequestConfig): Promise<T>;
|
|
65
|
-
post<T = any>(url: string, data?: any, config?: AxiosRequestConfig): Promise<T>;
|
|
66
|
-
put<T = any>(url: string, data?: any, config?: AxiosRequestConfig): Promise<T>;
|
|
67
|
-
setDefaultHeader(name: string, value: string): void;
|
|
68
|
-
request<T>(url: string, config: AxiosRequestConfig): Promise<T>;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
declare function formatToken(token: null | string): string | null;
|
|
72
|
-
declare const defaultRequestInterceptor: ({ token }: {
|
|
73
|
-
token: string;
|
|
74
|
-
}) => RequestInterceptorConfig;
|
|
75
|
-
declare const defaultResponseInterceptor: ({ codeField, dataField, successCode, }: {
|
|
76
|
-
codeField: string;
|
|
77
|
-
dataField: ((response: any) => any) | string;
|
|
78
|
-
successCode: ((code: any) => boolean) | number | string;
|
|
79
|
-
}) => ResponseInterceptorConfig;
|
|
80
|
-
declare const authenticateResponseInterceptor: ({ client, doReAuthenticate, doRefreshToken, enableRefreshToken, }: {
|
|
81
|
-
client: RequestClient;
|
|
82
|
-
doReAuthenticate: () => Promise<void>;
|
|
83
|
-
doRefreshToken: () => Promise<string>;
|
|
84
|
-
enableRefreshToken: boolean;
|
|
85
|
-
}) => ResponseInterceptorConfig;
|
|
86
|
-
declare const errorMessageResponseInterceptor: (makeErrorMessage?: MakeErrorMessageFn) => ResponseInterceptorConfig;
|
|
87
|
-
|
|
88
|
-
export { RequestClient, authenticateResponseInterceptor, defaultRequestInterceptor, defaultResponseInterceptor, errorMessageResponseInterceptor, formatToken };
|
|
89
|
-
export type { HttpResponse, MakeErrorMessageFn, RequestClientConfig, RequestClientOptions, RequestContentType, RequestInterceptorConfig, RequestResponse, ResponseInterceptorConfig, SseRequestOptions };
|