@yeepay/client-utils 3.1.1 → 4.0.0
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/dist/index.d.ts +20 -4
- package/dist/index.js +51 -39
- package/package.json +2 -2
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import debug from "debug";
|
|
2
|
-
import { AxiosInstance, AxiosResponse, InternalAxiosRequestConfig } from "axios";
|
|
2
|
+
import { AxiosError, AxiosInstance, AxiosResponse, InternalAxiosRequestConfig } from "axios";
|
|
3
3
|
|
|
4
4
|
//#region src/debug.d.ts
|
|
5
5
|
declare const logger: Record<string, debug.Debugger> & debug.Debugger;
|
|
@@ -13,6 +13,23 @@ declare global {
|
|
|
13
13
|
}
|
|
14
14
|
}
|
|
15
15
|
//#endregion
|
|
16
|
+
//#region src/is.d.ts
|
|
17
|
+
declare function isTouchDevice(): boolean;
|
|
18
|
+
declare const BREAKPOINTS: {
|
|
19
|
+
MOBILE_MAX: number;
|
|
20
|
+
TABLET_MIN: number;
|
|
21
|
+
TABLET_MAX: number;
|
|
22
|
+
DESKTOP_MIN: number;
|
|
23
|
+
};
|
|
24
|
+
declare const MEDIA_QUERIES: {
|
|
25
|
+
MOBILE: string;
|
|
26
|
+
TABLET: string;
|
|
27
|
+
DESKTOP: string;
|
|
28
|
+
};
|
|
29
|
+
declare function isMobile(): boolean;
|
|
30
|
+
declare function isTablet(): boolean;
|
|
31
|
+
declare function isDesktop(): boolean;
|
|
32
|
+
//#endregion
|
|
16
33
|
//#region src/request/mock.d.ts
|
|
17
34
|
/**
|
|
18
35
|
* A valid `picomatch` glob pattern, or array of patterns.
|
|
@@ -76,11 +93,10 @@ interface ServiceFactoryCallbacks {
|
|
|
76
93
|
notfoundCallback?: (response: AxiosResponse<any> | any) => any;
|
|
77
94
|
requestInterceptor?: (config: InternalAxiosRequestConfig) => InternalAxiosRequestConfig;
|
|
78
95
|
responseInterceptor?: (response: AxiosResponse<any>) => AxiosResponse<any>;
|
|
96
|
+
responseInterceptorFailCallback?: (error: AxiosError) => any;
|
|
79
97
|
}
|
|
80
|
-
type ServiceFactoryCallbacksCompatible = [ServiceFactoryCallbacks['successCallback']?, ServiceFactoryCallbacks['failCallback']?, ServiceFactoryCallbacks['unauthorizedCallback']?, ServiceFactoryCallbacks['forbiddenCallback']?, ServiceFactoryCallbacks['notfoundCallback']?, ServiceFactoryCallbacks['requestInterceptor']?, ServiceFactoryCallbacks['responseInterceptor']?];
|
|
81
98
|
declare function verifySuccessCode<T extends string | number>(code: T, customCode?: T): boolean;
|
|
82
99
|
declare function serviceFactory(options: ServiceFactoryOptions, callbacks: ServiceFactoryCallbacks): AxiosInstance;
|
|
83
|
-
declare function serviceFactory(options: ServiceFactoryOptions, ...callbacks: ServiceFactoryCallbacksCompatible): AxiosInstance;
|
|
84
100
|
//#endregion
|
|
85
101
|
//#region src/request/token.d.ts
|
|
86
102
|
declare function getToken(): string | undefined;
|
|
@@ -93,4 +109,4 @@ declare function removeToken(): void;
|
|
|
93
109
|
//#region src/utils.d.ts
|
|
94
110
|
declare function getQueryObject(url?: string): Record<string, string>;
|
|
95
111
|
//#endregion
|
|
96
|
-
export { logger as debug, getQueryObject, getToken, removeToken, serviceFactory, setTokenFromUrl, verifySuccessCode };
|
|
112
|
+
export { BREAKPOINTS, MEDIA_QUERIES, logger as debug, getQueryObject, getToken, isDesktop, isMobile, isTablet, isTouchDevice, removeToken, serviceFactory, setTokenFromUrl, verifySuccessCode };
|
package/dist/index.js
CHANGED
|
@@ -54,6 +54,41 @@ try {
|
|
|
54
54
|
console.error("Error enabling logger:", error);
|
|
55
55
|
}
|
|
56
56
|
|
|
57
|
+
//#endregion
|
|
58
|
+
//#region src/is.ts
|
|
59
|
+
function isTouchDevice() {
|
|
60
|
+
if (typeof navigator === "undefined" || typeof window === "undefined") return false;
|
|
61
|
+
if ("maxTouchPoints" in navigator) return navigator.maxTouchPoints > 0;
|
|
62
|
+
if (typeof window.matchMedia === "function") return window.matchMedia("(pointer: coarse)").matches;
|
|
63
|
+
const ua = navigator.userAgent || "";
|
|
64
|
+
return /android|iphone|ipad|ipod|windows phone|blackberry|mobile/i.test(ua);
|
|
65
|
+
}
|
|
66
|
+
const BREAKPOINTS = {
|
|
67
|
+
MOBILE_MAX: 767,
|
|
68
|
+
TABLET_MIN: 768,
|
|
69
|
+
TABLET_MAX: 1023,
|
|
70
|
+
DESKTOP_MIN: 1024
|
|
71
|
+
};
|
|
72
|
+
const MEDIA_QUERIES = {
|
|
73
|
+
MOBILE: `(max-width: ${BREAKPOINTS.MOBILE_MAX}px)`,
|
|
74
|
+
TABLET: `(min-width: ${BREAKPOINTS.TABLET_MIN}px) and (max-width: ${BREAKPOINTS.TABLET_MAX}px)`,
|
|
75
|
+
DESKTOP: `(min-width: ${BREAKPOINTS.DESKTOP_MIN}px)`
|
|
76
|
+
};
|
|
77
|
+
function isMobile() {
|
|
78
|
+
if (!isTouchDevice()) return false;
|
|
79
|
+
if (typeof window.matchMedia === "function") return window.matchMedia(MEDIA_QUERIES.MOBILE).matches;
|
|
80
|
+
return false;
|
|
81
|
+
}
|
|
82
|
+
function isTablet() {
|
|
83
|
+
if (!isTouchDevice()) return false;
|
|
84
|
+
if (typeof window.matchMedia === "function") return window.matchMedia(MEDIA_QUERIES.TABLET).matches;
|
|
85
|
+
return false;
|
|
86
|
+
}
|
|
87
|
+
function isDesktop() {
|
|
88
|
+
if (typeof navigator === "undefined" || typeof window === "undefined") return false;
|
|
89
|
+
return window.matchMedia(MEDIA_QUERIES.DESKTOP).matches;
|
|
90
|
+
}
|
|
91
|
+
|
|
57
92
|
//#endregion
|
|
58
93
|
//#region src/request/mock.ts
|
|
59
94
|
/**
|
|
@@ -177,26 +212,13 @@ function verifySuccessCode(code, customCode) {
|
|
|
177
212
|
if (customCode) return toArray(customCode).includes(code) || !code;
|
|
178
213
|
return code === 0 || code === "0" || code === "000000" || code === 200 || !code;
|
|
179
214
|
}
|
|
180
|
-
function compatibleCallback(compatibleCallbacks = []) {
|
|
181
|
-
if (Array.isArray(compatibleCallbacks) && compatibleCallbacks.length && typeof compatibleCallbacks[0] === "function") return {
|
|
182
|
-
successCallback: compatibleCallbacks[0] || (() => ({})),
|
|
183
|
-
failCallback: compatibleCallbacks[1] || (() => ({})),
|
|
184
|
-
unauthorizedCallback: compatibleCallbacks[2] || (() => ({})),
|
|
185
|
-
forbiddenCallback: compatibleCallbacks[3] || (() => ({})),
|
|
186
|
-
notfoundCallback: compatibleCallbacks[4] || (() => ({})),
|
|
187
|
-
requestInterceptor: compatibleCallbacks[5] || ((c) => c),
|
|
188
|
-
responseInterceptor: compatibleCallbacks[6] || ((r) => r)
|
|
189
|
-
};
|
|
190
|
-
else return compatibleCallbacks[0];
|
|
191
|
-
}
|
|
192
215
|
function blobToJson(blob) {
|
|
193
216
|
return new Promise((resolve, reject) => {
|
|
194
217
|
const reader = new FileReader();
|
|
195
218
|
reader.onload = (event) => {
|
|
196
219
|
try {
|
|
197
220
|
const jsonString = event.target?.result;
|
|
198
|
-
|
|
199
|
-
resolve(jsonObject);
|
|
221
|
+
resolve(JSON.parse(jsonString));
|
|
200
222
|
} catch (error) {
|
|
201
223
|
reject(error);
|
|
202
224
|
}
|
|
@@ -207,16 +229,13 @@ function blobToJson(blob) {
|
|
|
207
229
|
reader.readAsText(blob);
|
|
208
230
|
});
|
|
209
231
|
}
|
|
210
|
-
function serviceFactory(options,
|
|
232
|
+
function serviceFactory(options, callbacks) {
|
|
211
233
|
const { baseUrl = "/", timeout = 12e5, headers, code, debug: debug$1, mock } = options;
|
|
212
|
-
const { successCallback = (_response) => ({}), failCallback = (_error) => ({}), unauthorizedCallback = (_response) => ({}), forbiddenCallback = (_response) => ({}), notfoundCallback = (_response) => ({}), requestInterceptor = (c) => c, responseInterceptor = (r) => r } =
|
|
234
|
+
const { successCallback = (_response) => ({}), failCallback = (_error) => ({}), unauthorizedCallback = (_response) => ({}), forbiddenCallback = (_response) => ({}), notfoundCallback = (_response) => ({}), requestInterceptor = (c) => c, responseInterceptor = (r) => r, responseInterceptorFailCallback = (error) => Promise.reject(error) } = callbacks;
|
|
213
235
|
const service = axios.create({
|
|
214
236
|
baseURL: baseUrl,
|
|
215
237
|
timeout,
|
|
216
|
-
headers
|
|
217
|
-
validateStatus(status) {
|
|
218
|
-
return status >= 200 && status < 300 || status === 401 || status === 403 || status === 400 || status === 500;
|
|
219
|
-
}
|
|
238
|
+
headers
|
|
220
239
|
});
|
|
221
240
|
/**
|
|
222
241
|
* request拦截器, 先入栈后执行
|
|
@@ -248,16 +267,12 @@ function serviceFactory(options, ...callbacks) {
|
|
|
248
267
|
}
|
|
249
268
|
return response;
|
|
250
269
|
}, async (error) => {
|
|
251
|
-
|
|
270
|
+
try {
|
|
271
|
+
if (error.response.data instanceof Blob && error.response.headers["content-type"]?.split(";")[0]?.trim() === "application/json") error.response.data = await blobToJson(error.response.data);
|
|
272
|
+
} catch (error$1) {}
|
|
252
273
|
return Promise.reject(error);
|
|
253
274
|
});
|
|
254
275
|
service.interceptors.response.use((response) => {
|
|
255
|
-
if (response.status && response.status === 401) return unauthorizedCallback(response);
|
|
256
|
-
if (response.status && response.status === 403) return forbiddenCallback(response);
|
|
257
|
-
if (response.status && (response.status === 400 || response.status === 500)) {
|
|
258
|
-
failCallback(response);
|
|
259
|
-
return Promise.reject(response);
|
|
260
|
-
}
|
|
261
276
|
if (verifySuccessCode(response.data.code, code)) {
|
|
262
277
|
successCallback(response);
|
|
263
278
|
return response;
|
|
@@ -267,24 +282,21 @@ function serviceFactory(options, ...callbacks) {
|
|
|
267
282
|
}
|
|
268
283
|
}, (error) => {
|
|
269
284
|
if (axios.isCancel(error)) return Promise.reject(error);
|
|
270
|
-
let status = 0;
|
|
271
285
|
try {
|
|
272
|
-
status = error.response.status || error.response.data.status;
|
|
273
|
-
|
|
274
|
-
if (
|
|
286
|
+
const status = error.response.status || error.response.data.status;
|
|
287
|
+
if (status === 401) unauthorizedCallback(error);
|
|
288
|
+
else if (status === 403) forbiddenCallback(error);
|
|
289
|
+
else if (status === 404) notfoundCallback(error);
|
|
290
|
+
else failCallback(error);
|
|
291
|
+
} catch (_) {
|
|
292
|
+
failCallback(error);
|
|
275
293
|
}
|
|
276
|
-
if (status) if (status === 403) forbiddenCallback(error);
|
|
277
|
-
else if (status === 404) notfoundCallback(error);
|
|
278
|
-
else failCallback(error);
|
|
279
|
-
else failCallback(error);
|
|
280
|
-
return Promise.reject(error);
|
|
281
|
-
});
|
|
282
|
-
service.interceptors.response.use(responseInterceptor, (error) => {
|
|
283
294
|
return Promise.reject(error);
|
|
284
295
|
});
|
|
296
|
+
service.interceptors.response.use(responseInterceptor, responseInterceptorFailCallback);
|
|
285
297
|
if (mock) setupMockAdapter(service, mock);
|
|
286
298
|
return service;
|
|
287
299
|
}
|
|
288
300
|
|
|
289
301
|
//#endregion
|
|
290
|
-
export { logger as debug, getQueryObject, getToken, removeToken, serviceFactory, setTokenFromUrl, verifySuccessCode };
|
|
302
|
+
export { BREAKPOINTS, MEDIA_QUERIES, logger as debug, getQueryObject, getToken, isDesktop, isMobile, isTablet, isTouchDevice, removeToken, serviceFactory, setTokenFromUrl, verifySuccessCode };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@yeepay/client-utils",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "
|
|
4
|
+
"version": "4.0.0",
|
|
5
5
|
"description": "shared utilities for yeepay client packages",
|
|
6
6
|
"author": "Yong Yang",
|
|
7
7
|
"homepage": "http://gitlab.yeepay.com/ued/client-utils#readme",
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
"@imyangyong/utils": "^0.8.0",
|
|
24
24
|
"@yeepay/client-utils": "link:",
|
|
25
25
|
"ansis": "^4.1.0",
|
|
26
|
-
"axios": "^1.
|
|
26
|
+
"axios": "^1.13.2",
|
|
27
27
|
"axios-mock-adapter": "^2.1.0",
|
|
28
28
|
"debug": "^4.4.3",
|
|
29
29
|
"js-cookie": "^3.0.5",
|