@ichaingo/request 1.2.8 → 1.2.9
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 +32 -30
- 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 $, { 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 } from "@ichaingo/token";
|
|
6
|
+
const B = () => /^(zh-hans|zh-cn|zh-sg|zh-my)$/.test(window == null ? void 0 : window.navigator.language.toLowerCase()), P = () => B() ? "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 = x.get("access_token") || "", o = x.get("NEXT_LOCALE") || P() || "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,
|
|
32
|
-
const t = (r = (g = e.config) == null ? void 0 : g.method) == null ? void 0 : r.toUpperCase(), o = ((
|
|
30
|
+
function z(e) {
|
|
31
|
+
var g, r, T, y, L, h, f, _, I, v, k;
|
|
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"]) || ((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 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
|
-
const i =
|
|
69
|
-
return t.summary = i, t.details = JSON.stringify(E(t)),
|
|
68
|
+
const i = z(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 H(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,23 +104,25 @@ 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 j =
|
|
115
|
-
baseURL: process.env.
|
|
114
|
+
const j = $.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
123
|
async function M(e) {
|
|
122
124
|
const t = e.headers;
|
|
123
|
-
return p ? (await p, e) : (
|
|
125
|
+
return p ? (await p, e) : (O() && (p = (async () => {
|
|
124
126
|
await j.get("/api/refresh/token", {
|
|
125
127
|
params: {
|
|
126
128
|
grant_type: "refresh_token",
|
|
@@ -134,18 +136,18 @@ async function M(e) {
|
|
|
134
136
|
p = null;
|
|
135
137
|
}), await p), e);
|
|
136
138
|
}
|
|
137
|
-
const
|
|
139
|
+
const U = $.create({
|
|
138
140
|
baseURL: process.env.ICHAINGO_INNER_API_URL || process.env.NEXT_PUBLIC_ICHAINGO_API_URL,
|
|
139
141
|
timeout: 1e4,
|
|
140
142
|
withCredentials: !0,
|
|
141
143
|
// Always include credentials by default
|
|
142
144
|
paramsSerializer: function(e) {
|
|
143
|
-
return
|
|
145
|
+
return N.stringify(e, { arrayFormat: "brackets" });
|
|
144
146
|
}
|
|
145
147
|
});
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
148
|
+
U.interceptors.request.use(M);
|
|
149
|
+
U.interceptors.request.use(S, A);
|
|
150
|
+
U.interceptors.response.use(H, A);
|
|
149
151
|
export {
|
|
150
|
-
|
|
152
|
+
U as default
|
|
151
153
|
};
|
|
@@ -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,4CA0BnC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ichaingo/request",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.9",
|
|
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.9",
|
|
26
|
+
"@ichaingo/token": "1.2.9"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
29
29
|
"@types/js-cookie": "^3.0.6",
|