@ichaingo/request 1.2.8 → 1.3.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.js +34 -31
- package/dist/interceptors/refreshToken.d.ts.map +1 -1
- package/package.json +3 -3
package/dist/index.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import { logger as
|
|
5
|
-
import
|
|
6
|
-
const P = () => /^(zh-hans|zh-cn|zh-sg|zh-my)$/.test(window == null ? void 0 : window.navigator.language.toLowerCase()),
|
|
7
|
-
async function
|
|
1
|
+
import $, { isAxiosError as C, AxiosError as w } from "axios";
|
|
2
|
+
import N from "qs";
|
|
3
|
+
import x from "js-cookie";
|
|
4
|
+
import { logger as R } from "@ichaingo/logger";
|
|
5
|
+
import q, { isTokenExpired as O, getToken as b, setToken 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()), S = () => P() ? "zh-hans" : "";
|
|
7
|
+
async function z(e) {
|
|
8
8
|
var n;
|
|
9
9
|
let t = "", o = "";
|
|
10
10
|
if (process.env.IS_SERVER) {
|
|
@@ -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 = x.get("access_token") || "", o = x.get("NEXT_LOCALE") || S() || "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
30
|
function H(e) {
|
|
31
|
-
var g, r,
|
|
32
|
-
const t = (r = (g = e.config) == null ? void 0 : g.method) == null ? void 0 : r.toUpperCase(), o = ((
|
|
31
|
+
var g, r, T, y, L, h, f, _, I, k, v;
|
|
32
|
+
const t = (r = (g = e.config) == null ? void 0 : g.method) == null ? void 0 : r.toUpperCase(), o = ((T = e.config) == null ? void 0 : T.baseURL) || "", i = (y = e.config) != null && y.url ? `${o || ""}${e.config.url}` : "", n = (L = e.response) == null ? void 0 : L.status, c = (h = e.response) == null ? void 0 : h.statusText, s = (f = e.response) == null ? void 0 : f.data, a = ((I = (_ = e.response) == null ? void 0 : _.headers) == null ? void 0 : I["x-request-id"]) || ((k = s == null ? void 0 : s.status) == null ? void 0 : k.requestId), u = (s == null ? void 0 : s.message) || (s == null ? void 0 : s.msg) || ((v = s == null ? void 0 : s.status) == null ? void 0 : v.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 A(e) {
|
|
62
62
|
var n, c, s, a;
|
|
63
|
-
const t = C(e) ? e : new
|
|
63
|
+
const t = C(e) ? e : new w(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)), R.error({ message: l, details: t.details, errType: "timeout" }), Promise.reject(t);
|
|
67
67
|
}
|
|
68
68
|
const i = H(t);
|
|
69
|
-
return t.summary = i, t.details = JSON.stringify(E(t)),
|
|
69
|
+
return t.summary = i, t.details = JSON.stringify(E(t)), R.error({ message: i, details: t.details, errType: "server error" }), Promise.reject(t);
|
|
70
70
|
}
|
|
71
|
-
function
|
|
71
|
+
function j(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 q.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 T = new w(
|
|
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
|
+
), y = `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}`, L = {
|
|
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,24 +104,26 @@ function z(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" && R.error({ summary: y, details: L, errType: "business error" }), T;
|
|
108
108
|
}
|
|
109
109
|
return g;
|
|
110
110
|
}
|
|
111
111
|
return e;
|
|
112
112
|
}
|
|
113
113
|
let p = null;
|
|
114
|
-
const
|
|
115
|
-
baseURL: process.env.
|
|
114
|
+
const M = $.create({
|
|
115
|
+
baseURL: process.env.NEXT_PUBLIC_AUTH_URL,
|
|
116
116
|
timeout: 1e4,
|
|
117
|
+
withCredentials: !0,
|
|
118
|
+
// Always include credentials by default
|
|
117
119
|
paramsSerializer: function(e) {
|
|
118
|
-
return
|
|
120
|
+
return N.stringify(e, { arrayFormat: "brackets" });
|
|
119
121
|
}
|
|
120
122
|
});
|
|
121
|
-
async function
|
|
123
|
+
async function J(e) {
|
|
122
124
|
const t = e.headers;
|
|
123
|
-
return p ? (await p, e) : (
|
|
124
|
-
await
|
|
125
|
+
return p ? (await p, e) : (O() && (p = (async () => {
|
|
126
|
+
const o = await M.get("/api/refresh/token", {
|
|
125
127
|
params: {
|
|
126
128
|
grant_type: "refresh_token",
|
|
127
129
|
refresh_token: b("refresh_token")
|
|
@@ -130,22 +132,23 @@ async function M(e) {
|
|
|
130
132
|
...t
|
|
131
133
|
}
|
|
132
134
|
});
|
|
135
|
+
B(o.data);
|
|
133
136
|
})().finally(() => {
|
|
134
137
|
p = null;
|
|
135
138
|
}), await p), e);
|
|
136
139
|
}
|
|
137
|
-
const
|
|
140
|
+
const U = $.create({
|
|
138
141
|
baseURL: process.env.ICHAINGO_INNER_API_URL || process.env.NEXT_PUBLIC_ICHAINGO_API_URL,
|
|
139
142
|
timeout: 1e4,
|
|
140
143
|
withCredentials: !0,
|
|
141
144
|
// Always include credentials by default
|
|
142
145
|
paramsSerializer: function(e) {
|
|
143
|
-
return
|
|
146
|
+
return N.stringify(e, { arrayFormat: "brackets" });
|
|
144
147
|
}
|
|
145
148
|
});
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
+
U.interceptors.request.use(J);
|
|
150
|
+
U.interceptors.request.use(z, A);
|
|
151
|
+
U.interceptors.response.use(j, A);
|
|
149
152
|
export {
|
|
150
|
-
|
|
153
|
+
U as default
|
|
151
154
|
};
|
|
@@ -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;AAY1D,wBAA8B,YAAY,CACxC,MAAM,EAAE,0BAA0B,4CA2BnC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ichaingo/request",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.0",
|
|
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.
|
|
26
|
-
"@ichaingo/token": "1.
|
|
25
|
+
"@ichaingo/logger": "1.3.0",
|
|
26
|
+
"@ichaingo/token": "1.3.0"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
29
29
|
"@types/js-cookie": "^3.0.6",
|