@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 CHANGED
@@ -1,12 +1,12 @@
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) {
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 = "", o = "";
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: a, headers: u } = await import("next/headers");
19
- t = ((n = (await a()).get("access_token")) == null ? void 0 : n.value) || "";
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
- o = s || d.get("x-next-intl-locale") || "en";
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 = x.get("access_token") || "", o = x.get("NEXT_LOCALE") || S() || "en";
27
- const i = e.headers.isRefreshToken;
28
- return e.headers["Accept-Language"] = o, t && !i && (e.headers.Authorization = `Bearer ${t}`), e.headers.devicetype = 1, e;
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 H(e) {
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 = [
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
- i,
35
+ a,
36
36
  c
37
- ].filter(Boolean).join(" | "), d = u ? `server: ${u}` : void 0, l = a ? `requestId=${a}` : void 0;
38
- return [m, d, l].filter(Boolean).join(" | ");
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 E(e) {
41
- var c, s, a, u, m, d, l, g, r;
40
+ function x(e) {
41
+ var c, s, i, u, m, d, h, g, l;
42
42
  const t = e.config;
43
- let o = t == null ? void 0 : t.data;
44
- if (typeof o == "string")
43
+ let r = t == null ? void 0 : t.data;
44
+ if (typeof r == "string")
45
45
  try {
46
- o = JSON.parse(o);
46
+ r = JSON.parse(r);
47
47
  } catch {
48
48
  }
49
- const i = t == null ? void 0 : t.baseURL, n = t != null && t.url ? i ? `${i}${t.url}` : t.url : void 0;
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: o,
54
+ data: r,
55
55
  status: (s = e.response) == null ? void 0 : s.status,
56
- statusText: (a = e.response) == null ? void 0 : a.statusText,
57
- requestId: ((m = (u = e.response) == null ? void 0 : u.headers) == null ? void 0 : m["x-request-id"]) || ((g = (l = (d = e.response) == null ? void 0 : d.data) == null ? void 0 : l.status) == null ? void 0 : g.requestId),
58
- responseData: (r = e.response) == null ? void 0 : r.data
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 A(e) {
62
- var n, c, s, a;
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 = (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)), R.error({ message: l, details: t.details, errType: "timeout" }), Promise.reject(t);
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 i = H(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);
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 j(e) {
72
- var o, i, n, c, s, a, u, m, d, l;
73
- if (e.config.returnFullResponse)
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
- return q.removeAllToken(), window.location.href = `${process.env.NEXT_PUBLIC_AUTH_URL}/login`, e;
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 ((o = e.headers["content-type"]) != null && o.includes("text/event-stream"))
82
+ if ((a = e.headers["content-type"]) != null && a.includes("text/event-stream"))
79
83
  return e;
80
- const g = e.data, r = g.status;
81
- if (r && typeof r.code == "number" && ![0, 200].includes(r.code)) {
82
- const T = new w(
83
- (r == null ? void 0 : r.msg) || "Business error",
84
- String(r.code),
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
- ), 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
- method: (u = (a = e.config) == null ? void 0 : a.method) == null ? void 0 : u.toUpperCase(),
90
- url: (m = e.config) != null && m.baseURL ? `${e.config.baseURL}${e.config.url || ""}` : e.config.url,
91
- params: (d = e.config) == null ? void 0 : d.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 f;
94
- let h = (f = e.config) == null ? void 0 : f.data;
95
- if (typeof h == "string")
97
+ var p;
98
+ let f = (p = e.config) == null ? void 0 : p.data;
99
+ if (typeof f == "string")
96
100
  try {
97
- h = JSON.parse(h);
101
+ f = JSON.parse(f);
98
102
  } catch {
99
103
  }
100
- return h;
104
+ return f;
101
105
  })(),
102
106
  status: e.status,
103
107
  statusText: e.statusText,
104
- requestId: ((l = e.headers) == null ? void 0 : l["x-request-id"]) || (r == null ? void 0 : r.requestId),
105
- responseData: g
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" && R.error({ summary: y, details: L, errType: "business error" }), T;
111
+ throw process.env.NODE_ENV !== "development" && U.error({ summary: L, details: R, errType: "business error" }), y;
108
112
  }
109
- return g;
113
+ return l;
110
114
  }
111
115
  return e;
112
116
  }
113
- let p = null;
114
- const M = $.create({
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 N.stringify(e, { arrayFormat: "brackets" });
124
+ return A.stringify(e, { arrayFormat: "brackets" });
121
125
  }
122
126
  });
123
- async function J(e) {
127
+ async function X(e) {
124
128
  const t = e.headers;
125
- return p ? (await p, e) : (O() && (p = (async () => {
126
- const o = await M.get("/api/refresh/token", {
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
- B(o.data);
140
+ return (a = r == null ? void 0 : r.data) != null && a.data ? P(r.data.data) : B(), Promise.resolve();
136
141
  })().finally(() => {
137
- p = null;
138
- }), await p), e);
142
+ T = null;
143
+ }), await T), e);
139
144
  }
140
- const U = $.create({
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 N.stringify(e, { arrayFormat: "brackets" });
151
+ return A.stringify(e, { arrayFormat: "brackets" });
147
152
  }
148
153
  });
149
- U.interceptors.request.use(J);
150
- U.interceptors.request.use(z, A);
151
- U.interceptors.response.use(j, A);
154
+ v.interceptors.request.use(X);
155
+ v.interceptors.request.use(H, N);
156
+ v.interceptors.response.use(M, N);
152
157
  export {
153
- U as default
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,4CA2BnC"}
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",
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.3",
26
- "@ichaingo/token": "1.3.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",