@ichaingo/request 1.2.7 → 1.2.8
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.js +30 -32
- package/dist/interceptors/refreshToken.d.ts.map +1 -1
- package/package.json +3 -3
package/dist/index.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import { logger as
|
|
5
|
-
import
|
|
6
|
-
const
|
|
1
|
+
import x, { isAxiosError as C, AxiosError as A } from "axios";
|
|
2
|
+
import $ from "qs";
|
|
3
|
+
import N from "js-cookie";
|
|
4
|
+
import { logger as _ } from "@ichaingo/logger";
|
|
5
|
+
import O, { isTokenExpired as q, getToken as b } from "@ichaingo/token";
|
|
6
|
+
const P = () => /^(zh-hans|zh-cn|zh-sg|zh-my)$/.test(window == null ? void 0 : window.navigator.language.toLowerCase()), B = () => P() ? "zh-hans" : "";
|
|
7
7
|
async function S(e) {
|
|
8
8
|
var n;
|
|
9
9
|
let t = "", o = "";
|
|
@@ -23,13 +23,13 @@ async function S(e) {
|
|
|
23
23
|
console.error("Error accessing cookies in server component:", s);
|
|
24
24
|
}
|
|
25
25
|
} else
|
|
26
|
-
t =
|
|
26
|
+
t = N.get("access_token") || "", o = N.get("NEXT_LOCALE") || B() || "en";
|
|
27
27
|
const i = e.headers.isRefreshToken;
|
|
28
28
|
return e.headers["Accept-Language"] = o, t && !i && (e.headers.Authorization = `Bearer ${t}`), e.headers.devicetype = 1, e;
|
|
29
29
|
}
|
|
30
|
-
function
|
|
31
|
-
var g, r, T, y,
|
|
32
|
-
const t = (r = (g = e.config) == null ? void 0 : g.method) == null ? void 0 : r.toUpperCase(), o = ((
|
|
30
|
+
function H(e) {
|
|
31
|
+
var g, r, I, T, y, h, f, L, U, v, k;
|
|
32
|
+
const t = (r = (g = e.config) == null ? void 0 : g.method) == null ? void 0 : r.toUpperCase(), o = ((I = e.config) == null ? void 0 : I.baseURL) || "", i = (T = e.config) != null && T.url ? `${o || ""}${e.config.url}` : "", n = (y = e.response) == null ? void 0 : y.status, c = (h = e.response) == null ? void 0 : h.statusText, s = (f = e.response) == null ? void 0 : f.data, a = ((U = (L = e.response) == null ? void 0 : L.headers) == null ? void 0 : U["x-request-id"]) || ((v = s == null ? void 0 : s.status) == null ? void 0 : v.requestId), u = (s == null ? void 0 : s.message) || (s == null ? void 0 : s.msg) || ((k = s == null ? void 0 : s.status) == null ? void 0 : k.msg), m = [
|
|
33
33
|
n ? `HTTP ${n}` : "HTTP ERROR",
|
|
34
34
|
t,
|
|
35
35
|
i,
|
|
@@ -58,34 +58,34 @@ function E(e) {
|
|
|
58
58
|
responseData: (r = e.response) == null ? void 0 : r.data
|
|
59
59
|
};
|
|
60
60
|
}
|
|
61
|
-
function
|
|
61
|
+
function w(e) {
|
|
62
62
|
var n, c, s, a;
|
|
63
|
-
const t = C(e) ? e : new
|
|
63
|
+
const t = C(e) ? e : new A(String(e));
|
|
64
64
|
if (t.code === "ECONNABORTED" || /timeout/i.test(t.message || "")) {
|
|
65
65
|
const u = (c = (n = t.config) == null ? void 0 : n.method) == null ? void 0 : c.toUpperCase(), m = ((s = t.config) == null ? void 0 : s.baseURL) || "", d = (a = t.config) != null && a.url ? `${m || ""}${t.config.url}` : "", l = ["TIMEOUT", u, d].filter(Boolean).join(" | ");
|
|
66
|
-
return t.summary = l, t.details = JSON.stringify(E(t)),
|
|
66
|
+
return t.summary = l, t.details = JSON.stringify(E(t)), _.error({ message: l, details: t.details, errType: "timeout" }), Promise.reject(t);
|
|
67
67
|
}
|
|
68
|
-
const i =
|
|
69
|
-
return t.summary = i, t.details = JSON.stringify(E(t)),
|
|
68
|
+
const i = H(t);
|
|
69
|
+
return t.summary = i, t.details = JSON.stringify(E(t)), _.error({ message: i, details: t.details, errType: "server error" }), Promise.reject(t);
|
|
70
70
|
}
|
|
71
|
-
function
|
|
71
|
+
function z(e) {
|
|
72
72
|
var o, i, n, c, s, a, u, m, d, l;
|
|
73
73
|
if (e.config.returnFullResponse)
|
|
74
74
|
return e;
|
|
75
75
|
if (e.status === 401)
|
|
76
|
-
return
|
|
76
|
+
return O.removeAllToken(), window.location.href = `${process.env.NEXT_PUBLIC_AUTH_URL}/login`, e;
|
|
77
77
|
if (e.status === 200) {
|
|
78
78
|
if ((o = e.headers["content-type"]) != null && o.includes("text/event-stream"))
|
|
79
79
|
return e;
|
|
80
80
|
const g = e.data, r = g.status;
|
|
81
81
|
if (r && typeof r.code == "number" && ![0, 200].includes(r.code)) {
|
|
82
|
-
const
|
|
82
|
+
const I = new A(
|
|
83
83
|
(r == null ? void 0 : r.msg) || "Business error",
|
|
84
84
|
String(r.code),
|
|
85
85
|
e.config,
|
|
86
86
|
e.request,
|
|
87
87
|
e
|
|
88
|
-
),
|
|
88
|
+
), T = `BIZ ${r.code} | ${(n = (i = e.config) == null ? void 0 : i.method) == null ? void 0 : n.toUpperCase()} | ${((c = e.config) == null ? void 0 : c.baseURL) || ""}${((s = e.config) == null ? void 0 : s.url) || ""} | server: ${r == null ? void 0 : r.msg}`, y = {
|
|
89
89
|
method: (u = (a = e.config) == null ? void 0 : a.method) == null ? void 0 : u.toUpperCase(),
|
|
90
90
|
url: (m = e.config) != null && m.baseURL ? `${e.config.baseURL}${e.config.url || ""}` : e.config.url,
|
|
91
91
|
params: (d = e.config) == null ? void 0 : d.params,
|
|
@@ -104,25 +104,23 @@ function H(e) {
|
|
|
104
104
|
requestId: ((l = e.headers) == null ? void 0 : l["x-request-id"]) || (r == null ? void 0 : r.requestId),
|
|
105
105
|
responseData: g
|
|
106
106
|
};
|
|
107
|
-
throw process.env.NODE_ENV !== "development" &&
|
|
107
|
+
throw process.env.NODE_ENV !== "development" && _.error({ summary: T, details: y, errType: "business error" }), I;
|
|
108
108
|
}
|
|
109
109
|
return g;
|
|
110
110
|
}
|
|
111
111
|
return e;
|
|
112
112
|
}
|
|
113
113
|
let p = null;
|
|
114
|
-
const j =
|
|
115
|
-
baseURL: process.env.
|
|
114
|
+
const j = x.create({
|
|
115
|
+
baseURL: process.env.ICHAINGO_INNER_API_URL || process.env.NEXT_PUBLIC_ICHAINGO_API_URL,
|
|
116
116
|
timeout: 1e4,
|
|
117
|
-
withCredentials: !0,
|
|
118
|
-
// Always include credentials by default
|
|
119
117
|
paramsSerializer: function(e) {
|
|
120
|
-
return
|
|
118
|
+
return $.stringify(e, { arrayFormat: "brackets" });
|
|
121
119
|
}
|
|
122
120
|
});
|
|
123
121
|
async function M(e) {
|
|
124
122
|
const t = e.headers;
|
|
125
|
-
return p ? (await p, e) : (
|
|
123
|
+
return p ? (await p, e) : (q() && (p = (async () => {
|
|
126
124
|
await j.get("/api/refresh/token", {
|
|
127
125
|
params: {
|
|
128
126
|
grant_type: "refresh_token",
|
|
@@ -136,18 +134,18 @@ async function M(e) {
|
|
|
136
134
|
p = null;
|
|
137
135
|
}), await p), e);
|
|
138
136
|
}
|
|
139
|
-
const
|
|
137
|
+
const R = x.create({
|
|
140
138
|
baseURL: process.env.ICHAINGO_INNER_API_URL || process.env.NEXT_PUBLIC_ICHAINGO_API_URL,
|
|
141
139
|
timeout: 1e4,
|
|
142
140
|
withCredentials: !0,
|
|
143
141
|
// Always include credentials by default
|
|
144
142
|
paramsSerializer: function(e) {
|
|
145
|
-
return
|
|
143
|
+
return $.stringify(e, { arrayFormat: "brackets" });
|
|
146
144
|
}
|
|
147
145
|
});
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
146
|
+
R.interceptors.request.use(M);
|
|
147
|
+
R.interceptors.request.use(S, w);
|
|
148
|
+
R.interceptors.response.use(z, w);
|
|
151
149
|
export {
|
|
152
|
-
|
|
150
|
+
R as default
|
|
153
151
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"refreshToken.d.ts","sourceRoot":"","sources":["../../src/interceptors/refreshToken.ts"],"names":[],"mappings":"AAAA,OAAc,EAAE,0BAA0B,EAAE,MAAM,OAAO,CAAC;
|
|
1
|
+
{"version":3,"file":"refreshToken.d.ts","sourceRoot":"","sources":["../../src/interceptors/refreshToken.ts"],"names":[],"mappings":"AAAA,OAAc,EAAE,0BAA0B,EAAE,MAAM,OAAO,CAAC;AAW1D,wBAA8B,YAAY,CACxC,MAAM,EAAE,0BAA0B,4CA0BnC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ichaingo/request",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.8",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"module": "./dist/index.js",
|
|
@@ -22,8 +22,8 @@
|
|
|
22
22
|
"js-cookie": "3.0.5",
|
|
23
23
|
"next": "^15.4.5",
|
|
24
24
|
"qs": "6.14.0",
|
|
25
|
-
"@ichaingo/logger": "1.2.
|
|
26
|
-
"@ichaingo/token": "1.2.
|
|
25
|
+
"@ichaingo/logger": "1.2.8",
|
|
26
|
+
"@ichaingo/token": "1.2.8"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
29
29
|
"@types/js-cookie": "^3.0.6",
|