@swiftcrab/request 1.0.1

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 ADDED
@@ -0,0 +1,78 @@
1
+ ## 使用示例
2
+
3
+ - 安装: pnpm add @swiftcrab/request
4
+
5
+ #### 创建 request.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(baseURL: string) {
19
+ const client = new RequestClient({
20
+ baseURL,
21
+ })
22
+
23
+ /** 处理重新认证 */
24
+ async function doReAuthenticate() {
25
+ message.error('Access token or refresh token is invalid or expired. ')
26
+ }
27
+
28
+ /** 处理刷新令牌 */
29
+ async function doRefreshToken() {}
30
+
31
+ // 处理请求头处理
32
+ client.addRequestInterceptor({
33
+ fulfilled: config => {},
34
+ })
35
+
36
+ // 返回数据格式 { data: any, info: { code: number, message: string, status: boolean } }
37
+ client.addResponseInterceptor(
38
+ defaultResponseInterceptor({
39
+ /** 响应数据中代表访问结果的字段名 */
40
+ codeField: 'code',
41
+ /** 响应数据中装载实际数据的字段名,或者提供一个函数从响应数据中解析需要返回的数据 */
42
+ dataField: 'data',
43
+ /** 当codeField所指定的字段值与successCode相同时,代表接口访问成功。如果提供一个函数,则返回true代表接口访问成功 */
44
+ successCode: [200],
45
+ }),
46
+ )
47
+
48
+ client.addResponseInterceptor(
49
+ authenticateResponseInterceptor({
50
+ client,
51
+ doReAuthenticate,
52
+ doRefreshToken,
53
+ /** 是否启用刷新令牌 */
54
+ enableRefreshToken: true,
55
+ }),
56
+ )
57
+
58
+ client.addResponseInterceptor(
59
+ errorMessageResponseInterceptor((msg: string, error) => {
60
+ /** 处理错误消息 */
61
+ }),
62
+ )
63
+
64
+ return client
65
+ }
66
+
67
+ export const requestClient = createRequestClient(baseURL)
68
+ export const baseRequestClient = new RequestClient({ baseURL })
69
+ ```
70
+
71
+ #### 使用 requestClient
72
+
73
+ ```typescript
74
+ import { requestClient } from './request'
75
+ requestClient.get('/user').then(res => {
76
+ console.log(res)
77
+ })
78
+ ```
@@ -0,0 +1,89 @@
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 };
@@ -0,0 +1,89 @@
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 };
package/dist/index.mjs ADDED
@@ -0,0 +1,273 @@
1
+ import axios from 'axios';
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 };
package/package.json ADDED
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "@swiftcrab/request",
3
+ "version": "1.0.1",
4
+ "license": "MIT",
5
+ "type": "module",
6
+ "main": "./dist/index.mjs",
7
+ "module": "./dist/index.mjs",
8
+ "exports": {
9
+ ".": {
10
+ "default": "./dist/index.mjs"
11
+ },
12
+ "./package.json": "./package.json"
13
+ },
14
+ "files": [
15
+ "dist"
16
+ ],
17
+ "dependencies": {
18
+ "defu": "^6.1.4",
19
+ "axios": "^1.9.0"
20
+ },
21
+ "devDependencies": {
22
+ "unbuild": "^3.5.0",
23
+ "typescript": "^5.8.3",
24
+ "@swiftcrab/tsconfig": "1.0.0"
25
+ },
26
+ "publishConfig": {
27
+ "access": "public",
28
+ "registry": "https://registry.npmjs.org/"
29
+ },
30
+ "scripts": {
31
+ "build": "pnpm unbuild"
32
+ }
33
+ }