@ichaingo/request 1.4.12 → 1.4.14
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 +177 -120
- package/package.json +3 -3
package/dist/index.js
CHANGED
|
@@ -1,159 +1,216 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import { logger
|
|
5
|
-
import
|
|
6
|
-
const
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
1
|
+
import axios, { isAxiosError, AxiosError } from "axios";
|
|
2
|
+
import qs from "qs";
|
|
3
|
+
import jsCookie from "js-cookie";
|
|
4
|
+
import { logger } from "@ichaingo/logger";
|
|
5
|
+
import TokenManager, { isTokenExpired, getToken, removeAllToken } from "@ichaingo/token";
|
|
6
|
+
const isSimplifiedChinese = () => {
|
|
7
|
+
return /^(zh-hans|zh-cn|zh-sg|zh-my)$/.test(window == null ? void 0 : window.navigator.language.toLowerCase());
|
|
8
|
+
};
|
|
9
|
+
const matchSystemLanguage = () => {
|
|
10
|
+
return isSimplifiedChinese() ? "zh-hans" : "";
|
|
11
|
+
};
|
|
12
|
+
async function commonHeader(config) {
|
|
13
|
+
var _a;
|
|
14
|
+
let access_token = "";
|
|
15
|
+
let language = "";
|
|
10
16
|
if (process.env.IS_SERVER) {
|
|
11
|
-
const
|
|
12
|
-
|
|
13
|
-
keepAlive:
|
|
17
|
+
const http = await import("http");
|
|
18
|
+
config.httpAgent = new http.Agent({
|
|
19
|
+
keepAlive: true
|
|
14
20
|
});
|
|
15
21
|
try {
|
|
16
|
-
const
|
|
17
|
-
|
|
18
|
-
const { cookies
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
22
|
+
const startLanguage = config.headers.get("Accept-Language");
|
|
23
|
+
config.headers["x-whistle-nohost-env"] = process.env.NO_HOST_ENV || "ichaingo";
|
|
24
|
+
const { cookies, headers } = await import("next/headers");
|
|
25
|
+
const cookieStore = await cookies();
|
|
26
|
+
access_token = ((_a = cookieStore.get("access_token")) == null ? void 0 : _a.value) || "";
|
|
27
|
+
const hList = await headers();
|
|
28
|
+
language = startLanguage || hList.get("x-next-intl-locale") || "en";
|
|
29
|
+
} catch (error) {
|
|
30
|
+
console.error("Error accessing cookies in server component:", error);
|
|
24
31
|
}
|
|
25
|
-
} else
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
32
|
+
} else {
|
|
33
|
+
access_token = jsCookie.get("access_token") || "";
|
|
34
|
+
language = jsCookie.get("NEXT_LOCALE") || matchSystemLanguage() || "en";
|
|
35
|
+
}
|
|
36
|
+
const isRefreshToken = config.headers["isRefreshToken"];
|
|
37
|
+
config.headers["Accept-Language"] = language;
|
|
38
|
+
if (access_token && !isRefreshToken) {
|
|
39
|
+
config.headers["Authorization"] = `Bearer ${access_token}`;
|
|
40
|
+
}
|
|
41
|
+
config.headers["devicetype"] = 1;
|
|
42
|
+
return config;
|
|
29
43
|
}
|
|
30
|
-
function
|
|
31
|
-
var
|
|
32
|
-
const
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
44
|
+
function buildErrorMessage(err) {
|
|
45
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k;
|
|
46
|
+
const method = (_b = (_a = err.config) == null ? void 0 : _a.method) == null ? void 0 : _b.toUpperCase();
|
|
47
|
+
const baseURL = ((_c = err.config) == null ? void 0 : _c.baseURL) || "";
|
|
48
|
+
const url = ((_d = err.config) == null ? void 0 : _d.url) ? `${baseURL || ""}${err.config.url}` : "";
|
|
49
|
+
const status = (_e = err.response) == null ? void 0 : _e.status;
|
|
50
|
+
const statusText = (_f = err.response) == null ? void 0 : _f.statusText;
|
|
51
|
+
const respData = (_g = err.response) == null ? void 0 : _g.data;
|
|
52
|
+
const requestId = ((_i = (_h = err.response) == null ? void 0 : _h.headers) == null ? void 0 : _i["x-request-id"]) || ((_j = respData == null ? void 0 : respData.status) == null ? void 0 : _j.requestId);
|
|
53
|
+
const serverMsg = (respData == null ? void 0 : respData.message) || (respData == null ? void 0 : respData.msg) || ((_k = respData == null ? void 0 : respData.status) == null ? void 0 : _k.msg);
|
|
54
|
+
const summary = [
|
|
55
|
+
status ? `HTTP ${status}` : "HTTP ERROR",
|
|
56
|
+
method,
|
|
57
|
+
url,
|
|
58
|
+
statusText
|
|
59
|
+
].filter(Boolean).join(" | ");
|
|
60
|
+
const shortMsg = serverMsg ? `server: ${serverMsg}` : void 0;
|
|
61
|
+
const ridMsg = requestId ? `requestId=${requestId}` : void 0;
|
|
62
|
+
return [summary, shortMsg, ridMsg].filter(Boolean).join(" | ");
|
|
39
63
|
}
|
|
40
|
-
function
|
|
41
|
-
var
|
|
42
|
-
const
|
|
43
|
-
let
|
|
44
|
-
if (typeof
|
|
64
|
+
function buildDetails(err) {
|
|
65
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i;
|
|
66
|
+
const cfg = err.config;
|
|
67
|
+
let body = cfg == null ? void 0 : cfg.data;
|
|
68
|
+
if (typeof body === "string") {
|
|
45
69
|
try {
|
|
46
|
-
|
|
70
|
+
body = JSON.parse(body);
|
|
47
71
|
} catch {
|
|
48
72
|
}
|
|
49
|
-
|
|
73
|
+
}
|
|
74
|
+
const baseURL = cfg == null ? void 0 : cfg.baseURL;
|
|
75
|
+
const url = (cfg == null ? void 0 : cfg.url) ? baseURL ? `${baseURL}${cfg.url}` : cfg.url : void 0;
|
|
50
76
|
return {
|
|
51
|
-
method: (
|
|
52
|
-
url
|
|
53
|
-
params:
|
|
54
|
-
data:
|
|
55
|
-
status: (
|
|
56
|
-
statusText: (
|
|
57
|
-
requestId: ((
|
|
58
|
-
responseData: (
|
|
77
|
+
method: (_a = cfg == null ? void 0 : cfg.method) == null ? void 0 : _a.toUpperCase(),
|
|
78
|
+
url,
|
|
79
|
+
params: cfg == null ? void 0 : cfg.params,
|
|
80
|
+
data: body,
|
|
81
|
+
status: (_b = err.response) == null ? void 0 : _b.status,
|
|
82
|
+
statusText: (_c = err.response) == null ? void 0 : _c.statusText,
|
|
83
|
+
requestId: ((_e = (_d = err.response) == null ? void 0 : _d.headers) == null ? void 0 : _e["x-request-id"]) || ((_h = (_g = (_f = err.response) == null ? void 0 : _f.data) == null ? void 0 : _g.status) == null ? void 0 : _h.requestId),
|
|
84
|
+
responseData: (_i = err.response) == null ? void 0 : _i.data
|
|
59
85
|
};
|
|
60
86
|
}
|
|
61
|
-
function
|
|
62
|
-
var
|
|
63
|
-
const
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
87
|
+
function errorInterceptor(error) {
|
|
88
|
+
var _a, _b, _c, _d;
|
|
89
|
+
const err = isAxiosError(error) ? error : new AxiosError(String(error));
|
|
90
|
+
const isTimeout = err.code === "ECONNABORTED" || /timeout/i.test(err.message || "");
|
|
91
|
+
if (isTimeout) {
|
|
92
|
+
const method = (_b = (_a = err.config) == null ? void 0 : _a.method) == null ? void 0 : _b.toUpperCase();
|
|
93
|
+
const baseURL = ((_c = err.config) == null ? void 0 : _c.baseURL) || "";
|
|
94
|
+
const url = ((_d = err.config) == null ? void 0 : _d.url) ? `${baseURL || ""}${err.config.url}` : "";
|
|
95
|
+
const summary = [`TIMEOUT`, method, url].filter(Boolean).join(" | ");
|
|
96
|
+
err.summary = summary;
|
|
97
|
+
err.details = JSON.stringify(buildDetails(err));
|
|
98
|
+
logger.error({ message: summary, details: err.details, errType: "timeout" });
|
|
99
|
+
return Promise.reject(err);
|
|
67
100
|
}
|
|
68
|
-
const
|
|
69
|
-
|
|
101
|
+
const message = buildErrorMessage(err);
|
|
102
|
+
err.summary = message;
|
|
103
|
+
err.details = JSON.stringify(buildDetails(err));
|
|
104
|
+
logger.error({ message, details: err.details, errType: "server error" });
|
|
105
|
+
return Promise.reject(err);
|
|
70
106
|
}
|
|
71
|
-
function
|
|
72
|
-
var
|
|
73
|
-
const
|
|
74
|
-
if (
|
|
75
|
-
return
|
|
76
|
-
if (e.status === 401) {
|
|
77
|
-
q.removeAllToken();
|
|
78
|
-
const l = ((s = t == null ? void 0 : t.headers) == null ? void 0 : s["Accept-Language"]) || "en";
|
|
79
|
-
return window.location.href = `${process.env.NEXT_PUBLIC_AUTH_URL}/${l}/login`, e;
|
|
107
|
+
function ResponseInterceptor(response) {
|
|
108
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k;
|
|
109
|
+
const config = response.config;
|
|
110
|
+
if (config.returnFullResponse) {
|
|
111
|
+
return response;
|
|
80
112
|
}
|
|
81
|
-
if (
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
113
|
+
if (response.status === 401) {
|
|
114
|
+
TokenManager.removeAllToken();
|
|
115
|
+
const locale = ((_a = config == null ? void 0 : config.headers) == null ? void 0 : _a["Accept-Language"]) || "en";
|
|
116
|
+
window.location.href = `${process.env.NEXT_PUBLIC_AUTH_URL}/${locale}/login`;
|
|
117
|
+
return response;
|
|
118
|
+
}
|
|
119
|
+
if (response.status === 200) {
|
|
120
|
+
if ((_b = response.headers["content-type"]) == null ? void 0 : _b.includes("text/event-stream")) {
|
|
121
|
+
return response;
|
|
122
|
+
}
|
|
123
|
+
const data = response.data;
|
|
124
|
+
const businessStatus = data.status;
|
|
125
|
+
if (businessStatus && typeof businessStatus.code === "number" && ![0, 200].includes(businessStatus.code) && businessStatus.code !== 401) {
|
|
126
|
+
const err = new AxiosError(
|
|
127
|
+
(businessStatus == null ? void 0 : businessStatus.msg) || "Business error",
|
|
128
|
+
String(businessStatus.code),
|
|
129
|
+
response.config,
|
|
130
|
+
response.request,
|
|
131
|
+
response
|
|
132
|
+
);
|
|
133
|
+
const summary = `BIZ ${businessStatus.code} | ${(_d = (_c = response.config) == null ? void 0 : _c.method) == null ? void 0 : _d.toUpperCase()} | ${((_e = response.config) == null ? void 0 : _e.baseURL) || ""}${((_f = response.config) == null ? void 0 : _f.url) || ""} | server: ${businessStatus == null ? void 0 : businessStatus.msg}`;
|
|
134
|
+
const details = {
|
|
135
|
+
method: (_h = (_g = response.config) == null ? void 0 : _g.method) == null ? void 0 : _h.toUpperCase(),
|
|
136
|
+
url: ((_i = response.config) == null ? void 0 : _i.baseURL) ? `${response.config.baseURL}${response.config.url || ""}` : response.config.url,
|
|
137
|
+
params: (_j = response.config) == null ? void 0 : _j.params,
|
|
96
138
|
data: (() => {
|
|
97
|
-
var
|
|
98
|
-
let
|
|
99
|
-
if (typeof
|
|
139
|
+
var _a2;
|
|
140
|
+
let d = (_a2 = response.config) == null ? void 0 : _a2.data;
|
|
141
|
+
if (typeof d === "string") {
|
|
100
142
|
try {
|
|
101
|
-
|
|
143
|
+
d = JSON.parse(d);
|
|
102
144
|
} catch {
|
|
103
145
|
}
|
|
104
|
-
|
|
146
|
+
}
|
|
147
|
+
return d;
|
|
105
148
|
})(),
|
|
106
|
-
status:
|
|
107
|
-
statusText:
|
|
108
|
-
requestId: ((
|
|
109
|
-
responseData:
|
|
149
|
+
status: response.status,
|
|
150
|
+
statusText: response.statusText,
|
|
151
|
+
requestId: ((_k = response.headers) == null ? void 0 : _k["x-request-id"]) || (businessStatus == null ? void 0 : businessStatus.requestId),
|
|
152
|
+
responseData: data
|
|
110
153
|
};
|
|
111
|
-
|
|
154
|
+
if (process.env.NODE_ENV !== "development") {
|
|
155
|
+
logger.error({ summary, details, errType: "business error" });
|
|
156
|
+
}
|
|
157
|
+
throw err;
|
|
112
158
|
}
|
|
113
|
-
return
|
|
159
|
+
return data;
|
|
114
160
|
}
|
|
115
|
-
return
|
|
161
|
+
return response;
|
|
116
162
|
}
|
|
117
|
-
let
|
|
118
|
-
const
|
|
163
|
+
let refreshPromise = null;
|
|
164
|
+
const axiosInstance$1 = axios.create({
|
|
119
165
|
baseURL: process.env.NEXT_PUBLIC_AUTH_URL,
|
|
120
166
|
timeout: 1e4,
|
|
121
|
-
withCredentials:
|
|
167
|
+
withCredentials: true,
|
|
122
168
|
// Always include credentials by default
|
|
123
|
-
paramsSerializer: function(
|
|
124
|
-
return
|
|
169
|
+
paramsSerializer: function(params) {
|
|
170
|
+
return qs.stringify(params, { arrayFormat: "brackets" });
|
|
125
171
|
}
|
|
126
172
|
});
|
|
127
|
-
async function
|
|
128
|
-
const
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
173
|
+
async function refreshToken(config) {
|
|
174
|
+
const headers = config.headers;
|
|
175
|
+
if (refreshPromise) {
|
|
176
|
+
await refreshPromise;
|
|
177
|
+
return config;
|
|
178
|
+
}
|
|
179
|
+
if (isTokenExpired()) {
|
|
180
|
+
refreshPromise = (async () => {
|
|
181
|
+
var _a;
|
|
182
|
+
const res = await axiosInstance$1.get(`/api/refresh/token`, {
|
|
183
|
+
params: {
|
|
184
|
+
grant_type: "refresh_token",
|
|
185
|
+
refresh_token: getToken("refresh_token")
|
|
186
|
+
},
|
|
187
|
+
headers: {
|
|
188
|
+
...headers
|
|
189
|
+
}
|
|
190
|
+
});
|
|
191
|
+
if (!((_a = res == null ? void 0 : res.data) == null ? void 0 : _a.data)) {
|
|
192
|
+
removeAllToken();
|
|
138
193
|
}
|
|
194
|
+
return Promise.resolve();
|
|
195
|
+
})().finally(() => {
|
|
196
|
+
refreshPromise = null;
|
|
139
197
|
});
|
|
140
|
-
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
}), await T), e);
|
|
198
|
+
await refreshPromise;
|
|
199
|
+
}
|
|
200
|
+
return config;
|
|
144
201
|
}
|
|
145
|
-
const
|
|
202
|
+
const axiosInstance = axios.create({
|
|
146
203
|
baseURL: process.env.ICHAINGO_INNER_API_URL || process.env.NEXT_PUBLIC_ICHAINGO_API_URL,
|
|
147
204
|
timeout: 1e4,
|
|
148
|
-
withCredentials:
|
|
205
|
+
withCredentials: true,
|
|
149
206
|
// Always include credentials by default
|
|
150
|
-
paramsSerializer: function(
|
|
151
|
-
return
|
|
207
|
+
paramsSerializer: function(params) {
|
|
208
|
+
return qs.stringify(params, { arrayFormat: "brackets" });
|
|
152
209
|
}
|
|
153
210
|
});
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
211
|
+
axiosInstance.interceptors.request.use(refreshToken);
|
|
212
|
+
axiosInstance.interceptors.request.use(commonHeader, errorInterceptor);
|
|
213
|
+
axiosInstance.interceptors.response.use(ResponseInterceptor, errorInterceptor);
|
|
157
214
|
export {
|
|
158
|
-
|
|
215
|
+
axiosInstance as default
|
|
159
216
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ichaingo/request",
|
|
3
|
-
"version": "1.4.
|
|
3
|
+
"version": "1.4.14",
|
|
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.4.
|
|
26
|
-
"@ichaingo/token": "1.4.
|
|
25
|
+
"@ichaingo/logger": "1.4.14",
|
|
26
|
+
"@ichaingo/token": "1.4.14"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
29
29
|
"@types/js-cookie": "^3.0.6",
|