@ichaingo/request 1.5.34 → 1.5.36
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.
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
package/dist/index.js
CHANGED
|
@@ -9,18 +9,40 @@ const isSimplifiedChinese = () => {
|
|
|
9
9
|
const matchSystemLanguage = () => {
|
|
10
10
|
return isSimplifiedChinese() ? "zh-hans" : "";
|
|
11
11
|
};
|
|
12
|
+
let httpAgent;
|
|
13
|
+
let httpsAgent;
|
|
14
|
+
function isServerRuntime() {
|
|
15
|
+
return typeof window === "undefined" || process.env.IS_SERVER === "true";
|
|
16
|
+
}
|
|
17
|
+
function getHeader(config, key) {
|
|
18
|
+
const h = config.headers;
|
|
19
|
+
if ((h == null ? void 0 : h.get) && typeof h.get === "function") return h.get(key);
|
|
20
|
+
return h == null ? void 0 : h[key];
|
|
21
|
+
}
|
|
22
|
+
function setHeader(config, key, value) {
|
|
23
|
+
const h = config.headers;
|
|
24
|
+
if ((h == null ? void 0 : h.set) && typeof h.set === "function") {
|
|
25
|
+
h.set(key, value);
|
|
26
|
+
} else {
|
|
27
|
+
h[key] = value;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
12
30
|
async function commonHeader(config) {
|
|
13
|
-
var _a;
|
|
31
|
+
var _a, _b;
|
|
14
32
|
let access_token = "";
|
|
15
33
|
let language = "";
|
|
16
|
-
if (
|
|
34
|
+
if (isServerRuntime()) {
|
|
17
35
|
const http = await import("http");
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
36
|
+
const https = await import("./__vite-browser-external-l0sNRNKZ.js");
|
|
37
|
+
const baseURL = config == null ? void 0 : config.baseURL;
|
|
38
|
+
const isHttps = typeof baseURL === "string" ? baseURL.startsWith("https://") : false;
|
|
39
|
+
if (!httpAgent) httpAgent = new http.Agent({ keepAlive: true });
|
|
40
|
+
if (!httpsAgent) httpsAgent = new https.Agent({ keepAlive: true });
|
|
41
|
+
config.httpAgent = isHttps ? void 0 : httpAgent;
|
|
42
|
+
config.httpsAgent = isHttps ? httpsAgent : void 0;
|
|
21
43
|
try {
|
|
22
|
-
const startLanguage = config
|
|
23
|
-
config
|
|
44
|
+
const startLanguage = getHeader(config, "Accept-Language");
|
|
45
|
+
setHeader(config, "x-whistle-nohost-env", process.env.NO_HOST_ENV || "ichaingo");
|
|
24
46
|
const { cookies, headers } = await import("next/headers");
|
|
25
47
|
const cookieStore = await cookies();
|
|
26
48
|
access_token = ((_a = cookieStore.get("access_token")) == null ? void 0 : _a.value) || "";
|
|
@@ -33,12 +55,12 @@ async function commonHeader(config) {
|
|
|
33
55
|
access_token = jsCookie.get("access_token") || "";
|
|
34
56
|
language = jsCookie.get("NEXT_LOCALE") || matchSystemLanguage() || "en";
|
|
35
57
|
}
|
|
36
|
-
const isRefreshToken = config.headers["isRefreshToken"];
|
|
37
|
-
config
|
|
58
|
+
const isRefreshToken = (_b = config.headers) == null ? void 0 : _b["isRefreshToken"];
|
|
59
|
+
setHeader(config, "Accept-Language", language);
|
|
38
60
|
if (access_token && !isRefreshToken) {
|
|
39
|
-
config
|
|
61
|
+
setHeader(config, "Authorization", `Bearer ${access_token}`);
|
|
40
62
|
}
|
|
41
|
-
config
|
|
63
|
+
setHeader(config, "devicetype", 1);
|
|
42
64
|
return config;
|
|
43
65
|
}
|
|
44
66
|
function buildErrorMessage(err) {
|
|
@@ -111,7 +133,9 @@ function ResponseInterceptor(response) {
|
|
|
111
133
|
return response;
|
|
112
134
|
}
|
|
113
135
|
if (response.status === 401) {
|
|
114
|
-
|
|
136
|
+
if (typeof window !== "undefined") {
|
|
137
|
+
TokenManager.removeAllToken();
|
|
138
|
+
}
|
|
115
139
|
return response;
|
|
116
140
|
}
|
|
117
141
|
if (response.status === 200) {
|
|
@@ -169,23 +193,44 @@ const axiosInstance$1 = axios.create({
|
|
|
169
193
|
}
|
|
170
194
|
});
|
|
171
195
|
async function refreshToken(config) {
|
|
196
|
+
if (typeof window === "undefined" || process.env.IS_SERVER === "true") {
|
|
197
|
+
return config;
|
|
198
|
+
}
|
|
199
|
+
if (typeof config.url === "string" && config.url.includes("/api/refresh/token")) {
|
|
200
|
+
return config;
|
|
201
|
+
}
|
|
172
202
|
const headers = config.headers;
|
|
173
203
|
if (refreshPromise) {
|
|
174
204
|
await refreshPromise;
|
|
175
205
|
return config;
|
|
176
206
|
}
|
|
177
|
-
|
|
207
|
+
let expired = false;
|
|
208
|
+
try {
|
|
209
|
+
expired = await isTokenExpired();
|
|
210
|
+
} catch {
|
|
211
|
+
expired = false;
|
|
212
|
+
}
|
|
213
|
+
if (expired) {
|
|
214
|
+
const refresh_token = getToken("refresh_token");
|
|
215
|
+
if (!refresh_token) {
|
|
216
|
+
removeAllToken();
|
|
217
|
+
return config;
|
|
218
|
+
}
|
|
178
219
|
if (refreshPromise) {
|
|
179
220
|
await refreshPromise;
|
|
180
221
|
return config;
|
|
181
222
|
}
|
|
223
|
+
const refreshHeaders = { ...headers };
|
|
224
|
+
delete refreshHeaders.Authorization;
|
|
225
|
+
delete refreshHeaders.authorization;
|
|
226
|
+
refreshHeaders.isRefreshToken = true;
|
|
182
227
|
refreshPromise = axiosInstance$1.get(`/api/refresh/token`, {
|
|
183
228
|
params: {
|
|
184
229
|
grant_type: "refresh_token",
|
|
185
|
-
refresh_token
|
|
230
|
+
refresh_token
|
|
186
231
|
},
|
|
187
232
|
headers: {
|
|
188
|
-
...
|
|
233
|
+
...refreshHeaders
|
|
189
234
|
}
|
|
190
235
|
}).then((res) => {
|
|
191
236
|
var _a;
|
|
@@ -207,7 +252,7 @@ async function refreshToken(config) {
|
|
|
207
252
|
}
|
|
208
253
|
const axiosInstance = axios.create({
|
|
209
254
|
baseURL: process.env.INNER_API_URL || process.env.NEXT_PUBLIC_API_URL,
|
|
210
|
-
timeout: 1e4,
|
|
255
|
+
timeout: Number(process.env.NEXT_PUBLIC_REQUEST_TIMEOUT_MS) || 1e4,
|
|
211
256
|
withCredentials: true,
|
|
212
257
|
// Always include credentials by default
|
|
213
258
|
// Treat HTTP 401 as a non-error response so it goes through responseInterceptor.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"commonHeader.d.ts","sourceRoot":"","sources":["../../src/interceptors/commonHeader.ts"],"names":[],"mappings":"AAEA,OAAO,EAAoB,0BAA0B,EAAE,MAAM,OAAO,CAAC;
|
|
1
|
+
{"version":3,"file":"commonHeader.d.ts","sourceRoot":"","sources":["../../src/interceptors/commonHeader.ts"],"names":[],"mappings":"AAEA,OAAO,EAAoB,0BAA0B,EAAE,MAAM,OAAO,CAAC;AAwBrE,wBAAsB,YAAY,CAChC,MAAM,EAAE,0BAA0B,4CAwCnC"}
|
|
@@ -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,4CAqEnC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"response.d.ts","sourceRoot":"","sources":["../../src/interceptors/response.ts"],"names":[],"mappings":"AAAA,OAAO,EAAc,aAAa,EAAE,MAAM,OAAO,CAAC;AAKlD,MAAM,CAAC,OAAO,UAAU,mBAAmB,CAAC,QAAQ,EAAE,aAAa,GAAG,GAAG,
|
|
1
|
+
{"version":3,"file":"response.d.ts","sourceRoot":"","sources":["../../src/interceptors/response.ts"],"names":[],"mappings":"AAAA,OAAO,EAAc,aAAa,EAAE,MAAM,OAAO,CAAC;AAKlD,MAAM,CAAC,OAAO,UAAU,mBAAmB,CAAC,QAAQ,EAAE,aAAa,GAAG,GAAG,CAuDxE"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ichaingo/request",
|
|
3
|
-
"version": "1.5.
|
|
3
|
+
"version": "1.5.36",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"module": "./dist/index.js",
|
|
@@ -26,7 +26,7 @@
|
|
|
26
26
|
"js-cookie": "3.0.5",
|
|
27
27
|
"next": "15.5.6",
|
|
28
28
|
"qs": "6.14.0",
|
|
29
|
-
"@ichaingo/
|
|
30
|
-
"@ichaingo/
|
|
29
|
+
"@ichaingo/logger": "1.5.36",
|
|
30
|
+
"@ichaingo/token": "1.5.36"
|
|
31
31
|
}
|
|
32
32
|
}
|