@ichaingo/request 1.3.3 → 1.3.4
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 +80 -75
- package/dist/interceptors/refreshToken.d.ts.map +1 -1
- package/package.json +3 -3
package/dist/index.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import { logger as
|
|
5
|
-
import q, { isTokenExpired as O, getToken as b, setToken as B } from "@ichaingo/token";
|
|
6
|
-
const
|
|
7
|
-
async function
|
|
1
|
+
import E, { isAxiosError as C, AxiosError as w } from "axios";
|
|
2
|
+
import A from "qs";
|
|
3
|
+
import $ from "js-cookie";
|
|
4
|
+
import { logger as U } from "@ichaingo/logger";
|
|
5
|
+
import q, { isTokenExpired as O, getToken as b, setToken as P, removeAllToken as B } from "@ichaingo/token";
|
|
6
|
+
const S = () => /^(zh-hans|zh-cn|zh-sg|zh-my)$/.test(window == null ? void 0 : window.navigator.language.toLowerCase()), z = () => S() ? "zh-hans" : "";
|
|
7
|
+
async function H(e) {
|
|
8
8
|
var n;
|
|
9
|
-
let t = "",
|
|
9
|
+
let t = "", r = "";
|
|
10
10
|
if (process.env.IS_SERVER) {
|
|
11
11
|
const c = await import("http");
|
|
12
12
|
e.httpAgent = new c.Agent({
|
|
@@ -15,115 +15,120 @@ async function z(e) {
|
|
|
15
15
|
try {
|
|
16
16
|
const s = e.headers.get("Accept-Language");
|
|
17
17
|
e.headers["x-whistle-nohost-env"] = process.env.NO_HOST_ENV || "ichaingo";
|
|
18
|
-
const { cookies:
|
|
19
|
-
t = ((n = (await
|
|
18
|
+
const { cookies: i, headers: u } = await import("next/headers");
|
|
19
|
+
t = ((n = (await i()).get("access_token")) == null ? void 0 : n.value) || "";
|
|
20
20
|
const d = await u();
|
|
21
|
-
|
|
21
|
+
r = s || d.get("x-next-intl-locale") || "en";
|
|
22
22
|
} catch (s) {
|
|
23
23
|
console.error("Error accessing cookies in server component:", s);
|
|
24
24
|
}
|
|
25
25
|
} else
|
|
26
|
-
t =
|
|
27
|
-
const
|
|
28
|
-
return e.headers["Accept-Language"] =
|
|
26
|
+
t = $.get("access_token") || "", r = $.get("NEXT_LOCALE") || z() || "en";
|
|
27
|
+
const a = e.headers.isRefreshToken;
|
|
28
|
+
return e.headers["Accept-Language"] = r, t && !a && (e.headers.Authorization = `Bearer ${t}`), e.headers.devicetype = 1, e;
|
|
29
29
|
}
|
|
30
|
-
function
|
|
31
|
-
var g,
|
|
32
|
-
const t = (
|
|
30
|
+
function j(e) {
|
|
31
|
+
var g, l, o, y, L, R, f, p, _, k, I;
|
|
32
|
+
const t = (l = (g = e.config) == null ? void 0 : g.method) == null ? void 0 : l.toUpperCase(), r = ((o = e.config) == null ? void 0 : o.baseURL) || "", a = (y = e.config) != null && y.url ? `${r || ""}${e.config.url}` : "", n = (L = e.response) == null ? void 0 : L.status, c = (R = e.response) == null ? void 0 : R.statusText, s = (f = e.response) == null ? void 0 : f.data, i = ((_ = (p = e.response) == null ? void 0 : p.headers) == null ? void 0 : _["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) || ((I = s == null ? void 0 : s.status) == null ? void 0 : I.msg), m = [
|
|
33
33
|
n ? `HTTP ${n}` : "HTTP ERROR",
|
|
34
34
|
t,
|
|
35
|
-
|
|
35
|
+
a,
|
|
36
36
|
c
|
|
37
|
-
].filter(Boolean).join(" | "), d = u ? `server: ${u}` : void 0,
|
|
38
|
-
return [m, d,
|
|
37
|
+
].filter(Boolean).join(" | "), d = u ? `server: ${u}` : void 0, h = i ? `requestId=${i}` : void 0;
|
|
38
|
+
return [m, d, h].filter(Boolean).join(" | ");
|
|
39
39
|
}
|
|
40
|
-
function
|
|
41
|
-
var c, s,
|
|
40
|
+
function x(e) {
|
|
41
|
+
var c, s, i, u, m, d, h, g, l;
|
|
42
42
|
const t = e.config;
|
|
43
|
-
let
|
|
44
|
-
if (typeof
|
|
43
|
+
let r = t == null ? void 0 : t.data;
|
|
44
|
+
if (typeof r == "string")
|
|
45
45
|
try {
|
|
46
|
-
|
|
46
|
+
r = JSON.parse(r);
|
|
47
47
|
} catch {
|
|
48
48
|
}
|
|
49
|
-
const
|
|
49
|
+
const a = t == null ? void 0 : t.baseURL, n = t != null && t.url ? a ? `${a}${t.url}` : t.url : void 0;
|
|
50
50
|
return {
|
|
51
51
|
method: (c = t == null ? void 0 : t.method) == null ? void 0 : c.toUpperCase(),
|
|
52
52
|
url: n,
|
|
53
53
|
params: t == null ? void 0 : t.params,
|
|
54
|
-
data:
|
|
54
|
+
data: r,
|
|
55
55
|
status: (s = e.response) == null ? void 0 : s.status,
|
|
56
|
-
statusText: (
|
|
57
|
-
requestId: ((m = (u = e.response) == null ? void 0 : u.headers) == null ? void 0 : m["x-request-id"]) || ((g = (
|
|
58
|
-
responseData: (
|
|
56
|
+
statusText: (i = e.response) == null ? void 0 : i.statusText,
|
|
57
|
+
requestId: ((m = (u = e.response) == null ? void 0 : u.headers) == null ? void 0 : m["x-request-id"]) || ((g = (h = (d = e.response) == null ? void 0 : d.data) == null ? void 0 : h.status) == null ? void 0 : g.requestId),
|
|
58
|
+
responseData: (l = e.response) == null ? void 0 : l.data
|
|
59
59
|
};
|
|
60
60
|
}
|
|
61
|
-
function
|
|
62
|
-
var n, c, s,
|
|
61
|
+
function N(e) {
|
|
62
|
+
var n, c, s, i;
|
|
63
63
|
const t = C(e) ? e : new w(String(e));
|
|
64
64
|
if (t.code === "ECONNABORTED" || /timeout/i.test(t.message || "")) {
|
|
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 = (
|
|
66
|
-
return t.summary =
|
|
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 = (i = t.config) != null && i.url ? `${m || ""}${t.config.url}` : "", h = ["TIMEOUT", u, d].filter(Boolean).join(" | ");
|
|
66
|
+
return t.summary = h, t.details = JSON.stringify(x(t)), U.error({ message: h, details: t.details, errType: "timeout" }), Promise.reject(t);
|
|
67
67
|
}
|
|
68
|
-
const
|
|
69
|
-
return t.summary =
|
|
68
|
+
const a = j(t);
|
|
69
|
+
return t.summary = a, t.details = JSON.stringify(x(t)), U.error({ message: a, details: t.details, errType: "server error" }), Promise.reject(t);
|
|
70
70
|
}
|
|
71
|
-
function
|
|
72
|
-
var
|
|
73
|
-
|
|
71
|
+
function M(e) {
|
|
72
|
+
var r, a, n, c, s, i, u, m, d, h, g;
|
|
73
|
+
const t = e.config;
|
|
74
|
+
if (t.returnFullResponse)
|
|
74
75
|
return e;
|
|
75
|
-
if (e.status === 401)
|
|
76
|
-
|
|
76
|
+
if (e.status === 401) {
|
|
77
|
+
q.removeAllToken();
|
|
78
|
+
const l = ((r = t == null ? void 0 : t.headers) == null ? void 0 : r["Accept-Language"]) || "en";
|
|
79
|
+
return window.location.href = `${process.env.NEXT_PUBLIC_AUTH_URL}/${l}/login`, e;
|
|
80
|
+
}
|
|
77
81
|
if (e.status === 200) {
|
|
78
|
-
if ((
|
|
82
|
+
if ((a = e.headers["content-type"]) != null && a.includes("text/event-stream"))
|
|
79
83
|
return e;
|
|
80
|
-
const
|
|
81
|
-
if (
|
|
82
|
-
const
|
|
83
|
-
(
|
|
84
|
-
String(
|
|
84
|
+
const l = e.data, o = l.status;
|
|
85
|
+
if (o && typeof o.code == "number" && ![0, 200].includes(o.code)) {
|
|
86
|
+
const y = new w(
|
|
87
|
+
(o == null ? void 0 : o.msg) || "Business error",
|
|
88
|
+
String(o.code),
|
|
85
89
|
e.config,
|
|
86
90
|
e.request,
|
|
87
91
|
e
|
|
88
|
-
),
|
|
89
|
-
method: (
|
|
90
|
-
url: (
|
|
91
|
-
params: (
|
|
92
|
+
), L = `BIZ ${o.code} | ${(c = (n = e.config) == null ? void 0 : n.method) == null ? void 0 : c.toUpperCase()} | ${((s = e.config) == null ? void 0 : s.baseURL) || ""}${((i = e.config) == null ? void 0 : i.url) || ""} | server: ${o == null ? void 0 : o.msg}`, R = {
|
|
93
|
+
method: (m = (u = e.config) == null ? void 0 : u.method) == null ? void 0 : m.toUpperCase(),
|
|
94
|
+
url: (d = e.config) != null && d.baseURL ? `${e.config.baseURL}${e.config.url || ""}` : e.config.url,
|
|
95
|
+
params: (h = e.config) == null ? void 0 : h.params,
|
|
92
96
|
data: (() => {
|
|
93
|
-
var
|
|
94
|
-
let
|
|
95
|
-
if (typeof
|
|
97
|
+
var p;
|
|
98
|
+
let f = (p = e.config) == null ? void 0 : p.data;
|
|
99
|
+
if (typeof f == "string")
|
|
96
100
|
try {
|
|
97
|
-
|
|
101
|
+
f = JSON.parse(f);
|
|
98
102
|
} catch {
|
|
99
103
|
}
|
|
100
|
-
return
|
|
104
|
+
return f;
|
|
101
105
|
})(),
|
|
102
106
|
status: e.status,
|
|
103
107
|
statusText: e.statusText,
|
|
104
|
-
requestId: ((
|
|
105
|
-
responseData:
|
|
108
|
+
requestId: ((g = e.headers) == null ? void 0 : g["x-request-id"]) || (o == null ? void 0 : o.requestId),
|
|
109
|
+
responseData: l
|
|
106
110
|
};
|
|
107
|
-
throw process.env.NODE_ENV !== "development" &&
|
|
111
|
+
throw process.env.NODE_ENV !== "development" && U.error({ summary: L, details: R, errType: "business error" }), y;
|
|
108
112
|
}
|
|
109
|
-
return
|
|
113
|
+
return l;
|
|
110
114
|
}
|
|
111
115
|
return e;
|
|
112
116
|
}
|
|
113
|
-
let
|
|
114
|
-
const
|
|
117
|
+
let T = null;
|
|
118
|
+
const J = E.create({
|
|
115
119
|
baseURL: process.env.NEXT_PUBLIC_AUTH_URL,
|
|
116
120
|
timeout: 1e4,
|
|
117
121
|
withCredentials: !0,
|
|
118
122
|
// Always include credentials by default
|
|
119
123
|
paramsSerializer: function(e) {
|
|
120
|
-
return
|
|
124
|
+
return A.stringify(e, { arrayFormat: "brackets" });
|
|
121
125
|
}
|
|
122
126
|
});
|
|
123
|
-
async function
|
|
127
|
+
async function X(e) {
|
|
124
128
|
const t = e.headers;
|
|
125
|
-
return
|
|
126
|
-
|
|
129
|
+
return T ? (await T, e) : (O() && (T = (async () => {
|
|
130
|
+
var a;
|
|
131
|
+
const r = await J.get("/api/refresh/token", {
|
|
127
132
|
params: {
|
|
128
133
|
grant_type: "refresh_token",
|
|
129
134
|
refresh_token: b("refresh_token")
|
|
@@ -132,23 +137,23 @@ async function J(e) {
|
|
|
132
137
|
...t
|
|
133
138
|
}
|
|
134
139
|
});
|
|
135
|
-
|
|
140
|
+
return (a = r == null ? void 0 : r.data) != null && a.data ? P(r.data.data) : B(), Promise.resolve();
|
|
136
141
|
})().finally(() => {
|
|
137
|
-
|
|
138
|
-
}), await
|
|
142
|
+
T = null;
|
|
143
|
+
}), await T), e);
|
|
139
144
|
}
|
|
140
|
-
const
|
|
145
|
+
const v = E.create({
|
|
141
146
|
baseURL: process.env.ICHAINGO_INNER_API_URL || process.env.NEXT_PUBLIC_ICHAINGO_API_URL,
|
|
142
147
|
timeout: 1e4,
|
|
143
148
|
withCredentials: !0,
|
|
144
149
|
// Always include credentials by default
|
|
145
150
|
paramsSerializer: function(e) {
|
|
146
|
-
return
|
|
151
|
+
return A.stringify(e, { arrayFormat: "brackets" });
|
|
147
152
|
}
|
|
148
153
|
});
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
154
|
+
v.interceptors.request.use(X);
|
|
155
|
+
v.interceptors.request.use(H, N);
|
|
156
|
+
v.interceptors.response.use(M, N);
|
|
152
157
|
export {
|
|
153
|
-
|
|
158
|
+
v as default
|
|
154
159
|
};
|
|
@@ -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;AAY1D,wBAA8B,YAAY,CACxC,MAAM,EAAE,0BAA0B,
|
|
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,4CAgCnC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ichaingo/request",
|
|
3
|
-
"version": "1.3.
|
|
3
|
+
"version": "1.3.4",
|
|
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.3.
|
|
26
|
-
"@ichaingo/token": "1.3.
|
|
25
|
+
"@ichaingo/logger": "1.3.4",
|
|
26
|
+
"@ichaingo/token": "1.3.4"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
29
29
|
"@types/js-cookie": "^3.0.6",
|