iboot-http-client 1.0.6 → 1.0.7

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.
@@ -1,15 +1,15 @@
1
- import C from "js-md5";
2
- import m from "crypto-js";
3
1
  import N from "pino";
4
- const L = {
2
+ import A from "js-md5";
3
+ import p from "crypto-js";
4
+ const $ = {
5
5
  GENERAL: 0,
6
6
  PHONE: 1,
7
7
  EMAIL: 2
8
- }, A = {
8
+ }, R = {
9
9
  TYPE_MGT: 0,
10
10
  TYPE_C: 1,
11
11
  TYPE_B: 2
12
- }, $ = {
12
+ }, W = {
13
13
  FROM_WEB: 1,
14
14
  FROM_PC: 2,
15
15
  FROM_WX_MINI_PRO: 3,
@@ -17,108 +17,141 @@ const L = {
17
17
  FROM_WX_E: 5,
18
18
  FROM_APP: 6,
19
19
  FROM_DEVICE: 7
20
- }, W = {
20
+ }, j = {
21
21
  unknown: 0,
22
22
  male: 1,
23
23
  female: 2
24
- }, b = (n) => Array.isArray(n) || typeof n == "object" && Object.prototype.toString.call(n) === "[object Array]", I = (n) => {
25
- const t = n, e = "ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678", r = e.length;
26
- let s = "";
24
+ }, h = N({
25
+ name: process.env.APP_NAME ?? "iBoot",
26
+ level: process.env.NODE_ENV != "production" ? "debug" : "info",
27
+ nestedKey: "payload",
28
+ messageKey: "msg",
29
+ //明确指定消息键,可解决乱码问题
30
+ formatters: {
31
+ level: (n) => ({ level: n })
32
+ }
33
+ }), _ = (n) => Array.isArray(n) || typeof n == "object" && Object.prototype.toString.call(n) === "[object Array]", w = (n) => {
34
+ const t = n || 32, e = "ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678", s = e.length;
35
+ let r = "";
27
36
  for (let o = 0; o < t; o++)
28
- s += e.charAt(Math.floor(Math.random() * r));
29
- return s;
30
- }, R = (n, t) => {
37
+ r += e.charAt(Math.floor(Math.random() * s));
38
+ return r;
39
+ }, U = (n, t) => {
31
40
  const e = {};
32
- return n.split("&").forEach((s) => {
33
- const o = s.split("=");
34
- o[1] && (e[o[0]] = o[1]);
41
+ return n.split("&").forEach((r) => {
42
+ const o = r.split("=");
43
+ o[1] && (!t || t.indexOf(o[0]) == -1) && (e[o[0]] = o[1]);
35
44
  }), e;
36
- }, p = (n) => n == null || n == "" || n.length == 0 ? "" : encodeURIComponent(n).replace(/\(/g, "%28").replace(/\)/g, "%29").replace(/\'/g, "%27").replace(/\!/g, "%21").replace(/\~/g, "%7E"), U = "zh-CN", w = "_device_id_key", _ = "_current_website_key", T = "Get request error!", E = "Post request error!", y = {
45
+ }, b = (n) => !n || n == "" || n.length == 0 ? "" : encodeURIComponent(n).replace(/\(/g, "%28").replace(/\)/g, "%29").replace(/\'/g, "%27").replace(/\!/g, "%21").replace(/\~/g, "%7E"), k = "zh-CN", S = "_device_id_key", v = "_current_website_key", O = "Get request error!", I = "Post request error!", T = {
37
46
  "Device-Id": "Device-Id",
38
47
  Lang: "Lang",
39
48
  "Website-Id": "Website-Id",
40
49
  "Website-No": "Website-No"
41
- }, h = {
50
+ }, f = {
42
51
  IBOOT_DEVICE_ID: "IBOOT_DEVICE_ID",
43
52
  IBOOT_LOCALE: "IBOOT_LOCALE",
44
53
  IBOOT_WEBSITE_ID: "IBOOT_WEBSITE_ID",
45
54
  IBOOT_WEBSITE_NO: "IBOOT_WEBSITE_NO",
46
55
  token: "token",
47
56
  user: "user"
48
- }, S = () => {
49
- const n = localStorage.getItem(w) ?? "", t = localStorage.getItem(_), e = t != null ? JSON.parse(t) : null, r = {};
50
- return n.length > 0 && (r["Device-Id"] = n), e && (r.Lang = e.language, e.websiteId && (r["Website-Id"] = e.websiteId), e.websiteNo && (r["Website-No"] = e.websiteNo)), r;
57
+ }, P = () => {
58
+ const n = localStorage.getItem(S) ?? "", t = localStorage.getItem(v), e = t != null ? JSON.parse(t) : null, s = {};
59
+ return n.length > 0 && (s["Device-Id"] = n), e && (s.Lang = e.language, e.websiteId && (s["Website-Id"] = e.websiteId), e.websiteNo && (s["Website-No"] = e.websiteNo)), s;
51
60
  }, x = (n, t) => {
52
- localStorage.setItem(w, n), t && localStorage.setItem(_, JSON.stringify(t));
53
- }, k = async (n, t) => {
61
+ localStorage.setItem(S, n), t && localStorage.setItem(v, JSON.stringify(t));
62
+ }, D = async (n, t) => {
54
63
  if (t?.data) {
55
- const o = new URLSearchParams(t.data);
56
- n.indexOf("?") != -1 ? n += `&${o}` : n += `?${o}`;
64
+ const i = new URLSearchParams(t.data);
65
+ n.indexOf("?") != -1 ? n += `&${i}` : n += `?${i}`;
57
66
  }
58
- const e = S(), r = t?.heads;
59
- if (r)
60
- for (const o in r) {
61
- const a = r[o];
62
- a && (e[o] = a);
67
+ const e = P(), s = t?.headers;
68
+ if (s)
69
+ for (const i in s) {
70
+ const l = s[i];
71
+ l && (e[i] = l);
63
72
  }
64
- const s = await fetch(n, {
73
+ h.debug({
74
+ method: "GET",
75
+ url: n,
76
+ headers: e
77
+ });
78
+ const r = await fetch(n, {
65
79
  method: "GET",
66
80
  headers: e,
67
81
  cache: t?.useCache ? "force-cache" : "default"
68
82
  });
69
- return s.ok ? await s.json() : {
70
- code: s.status,
83
+ if (r.ok) {
84
+ const i = await r.json();
85
+ return h.debug({
86
+ GET_RESULT: i
87
+ }), i;
88
+ }
89
+ const o = {
90
+ code: r.status,
71
91
  success: !1,
72
- msg: s.statusText
92
+ msg: r.statusText
73
93
  };
74
- }, O = async (n, t) => {
75
- const e = t?.data ?? {}, r = t?.heads;
76
- let s;
77
- const o = new Headers(S());
78
- if (e instanceof FormData ? s = e : (s = JSON.stringify(e), o.set("Content-Type", "application/json")), r)
79
- for (let l in r) {
80
- const i = r[l];
81
- i && o.set(l, i);
94
+ return h.error(o), o;
95
+ }, C = async (n, t) => {
96
+ const e = t?.data ?? {}, s = t?.headers;
97
+ let r;
98
+ const o = new Headers(P());
99
+ if (e instanceof FormData ? r = e : (r = JSON.stringify(e), o.set("Content-Type", "application/json")), s)
100
+ for (let a in s) {
101
+ const c = s[a];
102
+ c && o.set(a, c);
82
103
  }
83
- const a = await fetch(n, {
104
+ h.debug({
105
+ method: "GET",
106
+ url: n,
107
+ headers: o
108
+ });
109
+ const i = await fetch(n, {
84
110
  method: "POST",
85
111
  headers: o,
86
- body: s
112
+ body: r
87
113
  });
88
- return a.ok ? await a.json() : {
89
- code: a.status,
114
+ if (i.ok) {
115
+ const a = await i.json();
116
+ return h.debug({
117
+ POST_RESULT: a
118
+ }), a;
119
+ }
120
+ const l = {
121
+ code: i.status,
90
122
  success: !1,
91
- msg: a.statusText
123
+ msg: i.statusText
92
124
  };
93
- }, j = async (n, t) => {
94
- const e = await k(n, t);
125
+ return h.error(l), l;
126
+ }, K = async (n, t) => {
127
+ const e = await D(n, t);
95
128
  if (e.success)
96
129
  return e.data;
97
130
  if (t?.showError) {
98
- t.showError(e.msg ?? T);
131
+ t.showError(e.msg ?? O);
99
132
  return;
100
133
  }
101
- throw Error(e.msg ?? T);
102
- }, K = async (n, t) => {
103
- const e = await O(n, t);
134
+ throw Error(e.msg ?? O);
135
+ }, F = async (n, t) => {
136
+ const e = await C(n, t);
104
137
  if (e.success)
105
138
  return e.data;
106
139
  if (t?.showError) {
107
- t.showError(e.msg ?? E);
140
+ t.showError(e.msg ?? I);
108
141
  return;
109
142
  }
110
- throw Error(e.msg ?? E);
111
- }, F = async (n, t) => {
112
- const e = await O(n, t);
113
- return e.success ? (t?.showSuccess && t.showSuccess(e.msg ?? "SUCCESS"), !0) : (t?.showError && t.showError(e.msg ?? E), !1);
114
- }, H = (n) => {
115
- const t = n.headers.get(y["Device-Id"]), e = n.headers.get(y.Lang), r = n.headers.get(y["Website-Id"]), s = n.headers.get(y["Website-No"]), o = {};
116
- return t && t.length > 0 && (o.deviceId = t), e && e.length > 0 && (o.lang = e), r && r.length > 0 && (o.websiteId = r), s && s.length > 0 && (o.websiteNo = s), o;
143
+ throw Error(e.msg ?? I);
144
+ }, H = async (n, t) => {
145
+ const e = await C(n, t);
146
+ return e.success ? (t?.showSuccess && t.showSuccess(e.msg ?? "SUCCESS"), !0) : (t?.showError && t.showError(e.msg ?? I), !1);
117
147
  }, G = (n) => {
118
- const t = {}, e = n.get(h.IBOOT_DEVICE_ID)?.value, r = n.get(h.IBOOT_LOCALE)?.value, s = n.get(h.IBOOT_WEBSITE_ID)?.value, o = n.get(h.IBOOT_WEBSITE_NO)?.value;
119
- return e && e.length > 0 && (t.deviceId = e), r && r.length > 0 && (t.lang = r), s && s.length > 0 && (t.websiteId = s), o && o.length > 0 && (t.websiteNo = o), t;
148
+ const t = n.headers.get(T["Device-Id"]), e = n.headers.get(T.Lang), s = n.headers.get(T["Website-Id"]), r = n.headers.get(T["Website-No"]), o = {};
149
+ return t && t.length > 0 && (o.deviceId = t), e && e.length > 0 && (o.lang = e), s && s.length > 0 && (o.websiteId = s), r && r.length > 0 && (o.websiteNo = r), o;
150
+ }, J = (n) => {
151
+ const t = {}, e = n.get(f.IBOOT_DEVICE_ID)?.value, s = n.get(f.IBOOT_LOCALE)?.value, r = n.get(f.IBOOT_WEBSITE_ID)?.value, o = n.get(f.IBOOT_WEBSITE_NO)?.value;
152
+ return e && e.length > 0 && (t.deviceId = e), s && s.length > 0 && (t.lang = s), r && r.length > 0 && (t.websiteId = r), o && o.length > 0 && (t.websiteNo = o), t;
120
153
  };
121
- class J {
154
+ class Y {
122
155
  baseUrl;
123
156
  helloMethod;
124
157
  apiKey;
@@ -130,31 +163,21 @@ class J {
130
163
  websiteId;
131
164
  websiteNo;
132
165
  _isDebug;
133
- logger;
134
- constructor({ deviceId: t, lang: e, websiteId: r, websiteNo: s, userType: o, helloURL: a, appName: l }) {
135
- this.baseUrl = process.env.BASE_URL ?? "", this.helloMethod = a ?? "", this.apiKey = process.env.API_KEY ?? "", this.userType = o ?? A.TYPE_MGT, this.userFrom = process.env.USER_FROM || "1", this.deviceId = t ?? I(10), this.lang = e ?? U, this.websiteId = r, this.websiteNo = s, this._isDebug = process.env.NODE_ENV != "production", this.logger = N({
136
- name: l,
137
- level: this._isDebug ? "debug" : "info",
138
- nestedKey: "payload",
139
- messageKey: "msg",
140
- //明确指定消息键,可解决乱码问题
141
- formatters: {
142
- level: (i) => ({ level: i })
143
- }
144
- });
166
+ constructor({ deviceId: t, lang: e, websiteId: s, websiteNo: r, userType: o, helloURL: i }) {
167
+ this.baseUrl = process.env.BASE_URL ?? "", this.helloMethod = i ?? "", this.apiKey = process.env.API_KEY ?? "", this.userType = o ?? R.TYPE_MGT, this.userFrom = process.env.USER_FROM || "1", this.deviceId = t ?? w(10), this.lang = e ?? k, this.websiteId = s, this.websiteNo = r, this._isDebug = process.env.NODE_ENV != "production";
145
168
  }
146
169
  isDebug() {
147
170
  return this._isDebug;
148
171
  }
149
172
  encrypt(t) {
150
- const e = m.enc.Utf8.parse(this.apiKey);
151
- return m.AES.encrypt(t, e).toString();
173
+ const e = p.enc.Utf8.parse(this.apiKey);
174
+ return p.AES.encrypt(t, e).toString();
152
175
  }
153
176
  decrypt(t) {
154
- const e = m.enc.Utf8.parse(this.apiKey);
155
- return m.AES.decrypt(t, e, {
156
- mode: m.mode.ECB
157
- }).toString(m.enc.Utf8);
177
+ const e = p.enc.Utf8.parse(this.apiKey);
178
+ return p.AES.decrypt(t, e, {
179
+ mode: p.mode.ECB
180
+ }).toString(p.enc.Utf8);
158
181
  }
159
182
  getDeviceId() {
160
183
  return this.deviceId;
@@ -162,75 +185,75 @@ class J {
162
185
  //eslint-disable-next-line @typescript-eslint/no-explicit-any
163
186
  convertUrlParameter(t) {
164
187
  const e = [];
165
- for (const r in t) {
166
- let s = t[r];
167
- if (s && typeof s == "string")
168
- s = s.trim();
169
- else if (s && typeof s == "object") {
170
- if (b(s)) {
171
- if (s.length === 0)
188
+ for (const s in t) {
189
+ let r = t[s];
190
+ if (r && typeof r == "string")
191
+ r = r.trim();
192
+ else if (r && typeof r == "object") {
193
+ if (_(r)) {
194
+ if (r.length === 0)
172
195
  continue;
173
- for (let o = 0; o < s.length; o++) {
174
- const a = s[o];
175
- for (const l in a) {
176
- const i = a[l] ?? void 0;
177
- if (i) {
178
- if (typeof i == "object")
179
- b(i) && i.forEach((c, g) => {
196
+ for (let o = 0; o < r.length; o++) {
197
+ const i = r[o];
198
+ for (const l in i) {
199
+ const a = i[l] ?? void 0;
200
+ if (a) {
201
+ if (typeof a == "object")
202
+ _(a) && a.forEach((c, u) => {
180
203
  if (c)
181
- for (const d in c) {
182
- const u = c[d];
183
- if (u && u.toString().length > 0) {
184
- const f = `[${o.toString()}].${l + `[${g.toString()}].${d}`}=${p(u)}`;
185
- e.push(r + f);
204
+ for (const g in c) {
205
+ const d = c[g];
206
+ if (d && d.toString().length > 0) {
207
+ const m = `[${o.toString()}].${l + `[${u.toString()}].${g}`}=${b(d)}`;
208
+ e.push(s + m);
186
209
  }
187
210
  }
188
211
  });
189
- else if (i.toString().length > 0) {
190
- const c = `[${o.toString()}].${l}=${p(i.toString())}`;
191
- e.push(r + c);
212
+ else if (a.toString().length > 0) {
213
+ const c = `[${o.toString()}].${l}=${b(a.toString())}`;
214
+ e.push(s + c);
192
215
  }
193
216
  }
194
217
  }
195
218
  }
196
219
  } else
197
- for (const o in s) {
198
- const a = s[o];
199
- if (a != null && a != "" && a.length != 0) {
200
- const l = `.${o}=${p(a)}`;
201
- e.push(r + l);
220
+ for (const o in r) {
221
+ const i = r[o];
222
+ if (i != null && i != "" && i.length != 0) {
223
+ const l = `.${o}=${b(i)}`;
224
+ e.push(s + l);
202
225
  }
203
226
  }
204
227
  continue;
205
- } else s && typeof s == "function" && (s = null);
206
- s != null && s != "" && s.length != 0 && e.push(r + "=" + p(s));
228
+ } else r && typeof r == "function" && (r = null);
229
+ r != null && r != "" && r.length != 0 && e.push(s + "=" + b(r));
207
230
  }
208
231
  return e && e.join("&");
209
232
  }
210
233
  async sign(t) {
211
- const e = "&key=" + await this.helloIboot(), r = [];
212
- for (const i in t)
213
- r.push(i);
214
- r.sort((i, c) => i.toLowerCase().localeCompare(c.toLowerCase()));
215
- const s = [];
216
- r.forEach((i) => {
217
- let c = t[i];
218
- typeof c == "object" && (c = JSON.stringify(c)), s.push(i + "=" + c);
234
+ const e = "&key=" + await this.helloIboot(), s = [];
235
+ for (const a in t)
236
+ s.push(a);
237
+ s.sort((a, c) => a.toLowerCase().localeCompare(c.toLowerCase()));
238
+ const r = [];
239
+ s.forEach((a) => {
240
+ let c = t[a];
241
+ typeof c == "object" && (c = JSON.stringify(c)), r.push(a + "=" + c);
219
242
  });
220
- const o = s.join("&"), a = C.md5((o + e).toLocaleUpperCase()), l = {
243
+ const o = r.join("&"), i = A.md5((o + e).toLocaleUpperCase()), l = {
221
244
  name: "sort",
222
245
  params: o,
223
- md5: a
246
+ md5: i
224
247
  };
225
- return this.logger.debug(l), a;
248
+ return h.debug(l), i;
226
249
  }
227
250
  //eslint-disable-next-line @typescript-eslint/no-explicit-any
228
251
  assemblyParameter(t, e) {
229
- const r = t ?? {};
230
- return r.timestamp = Date.now() + "", r.echostr = I(10), r.version = this.version, r.deviceId = this.deviceId, r.webid = this.websiteId, r.apiKey = this.apiKey, e && e.length > 0 && (r.username = e), r;
252
+ const s = t ?? {};
253
+ return s.timestamp = Date.now() + "", s.echostr = w(10), s.version = this.version, s.deviceId = this.deviceId, s.webid = this.websiteId, s.apiKey = this.apiKey, e && e.length > 0 && (s.username = e), s;
231
254
  }
232
255
  async assemblyHeader({ urlParams: t, token: e }) {
233
- const r = {
256
+ const s = {
234
257
  "Content-Type": "application/x-www-form-urlencoded",
235
258
  "Res-Type": "json",
236
259
  "Device-Id": this.deviceId,
@@ -238,9 +261,9 @@ class J {
238
261
  "User-From": this.userFrom,
239
262
  "Api-Key": this.apiKey
240
263
  };
241
- this.lang && this.lang.length > 0 && (r.Lang = this.lang), this.websiteId && this.websiteId.length > 0 && (r["Web-Id"] = this.websiteId), this.websiteNo && this.websiteNo.length > 0 && (r["Web-No"] = this.websiteNo), e && (e.token && e.token.length > 0 && e.username && e.username.length > 0 && (r.Authorization = e.token, r.Username = e.username), e.xcsrf && (r[e.xcsrf.csrfHeader] = e.xcsrf.csrfToken));
242
- const s = R(t), o = await this.sign(s);
243
- return r.Sign = o, r;
264
+ this.lang && this.lang.length > 0 && (s.Lang = this.lang), this.websiteId && this.websiteId.length > 0 && (s["Web-Id"] = this.websiteId), this.websiteNo && this.websiteNo.length > 0 && (s["Web-No"] = this.websiteNo), e && (e.token && e.token.length > 0 && e.username && e.username.length > 0 && (s.Authorization = e.token, s.Username = e.username), e.xcsrf && (s[e.xcsrf.csrfHeader] = e.xcsrf.csrfToken));
265
+ const r = U(t), o = await this.sign(r);
266
+ return s.Sign = o, s;
244
267
  }
245
268
  async helloIboot() {
246
269
  if (this.helloMethod.length <= 0)
@@ -249,20 +272,20 @@ class J {
249
272
  "Content-Type": "application/x-www-form-urlencoded",
250
273
  "Res-Type": "json",
251
274
  "Api-Key": this.apiKey
252
- }, r = {
275
+ }, s = {
253
276
  url: t,
254
277
  headers: e
255
278
  };
256
- this.logger.debug(r);
257
- const s = await fetch(t, {
279
+ h.debug(s);
280
+ const r = await fetch(t, {
258
281
  method: "GET",
259
282
  headers: e,
260
283
  credentials: "include",
261
284
  cache: "force-cache"
262
285
  });
263
- if (s.ok) {
264
- const o = await s.json();
265
- return this.logger.debug({ result: o.data }), o.data;
286
+ if (r.ok) {
287
+ const o = await r.json();
288
+ return h.debug({ result: o.data }), o.data;
266
289
  }
267
290
  throw new Error("hello iBoot error!");
268
291
  }
@@ -280,155 +303,142 @@ class J {
280
303
  }
281
304
  }
282
305
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
283
- async get({ url: t, data: e, token: r }) {
284
- const s = this.assemblyParameter(e), o = this.convertUrlParameter(s), a = await this.assemblyHeader({ urlParams: o, token: r }), l = `${this.getApiUrl(t)}?${o.toString()}`, i = {
306
+ async get({ url: t, data: e, token: s, cache: r = "default" }) {
307
+ const o = this.assemblyParameter(e), i = this.convertUrlParameter(o), l = await this.assemblyHeader({ urlParams: i, token: s }), a = `${this.getApiUrl(t)}?${i.toString()}`, c = {
285
308
  method: "GET",
286
- url: l,
287
- headers: a
309
+ url: a,
310
+ headers: l
288
311
  };
289
- this.logger.info(i);
290
- try {
291
- const c = await fetch(l, {
292
- method: "GET",
293
- headers: a,
294
- credentials: "include",
295
- cache: "force-cache"
296
- });
297
- if (c.ok) {
298
- const g = await c.json();
299
- return this.logger.debug({ GET_RESULT: g }), g;
300
- }
301
- return {
302
- code: c.status,
303
- success: !1,
304
- msg: c.statusText
305
- };
306
- } catch (c) {
307
- return {
308
- code: 500,
309
- success: !1,
310
- msg: c?.toString() ?? "Server exception!"
311
- };
312
+ h.info(c);
313
+ const u = await fetch(a, {
314
+ method: "GET",
315
+ headers: l,
316
+ credentials: "include",
317
+ cache: r
318
+ });
319
+ if (u.ok) {
320
+ const d = await u.json();
321
+ return h.debug({ GET_RESULT: d }), d;
312
322
  }
323
+ const g = {
324
+ code: u.status,
325
+ success: !1,
326
+ msg: u.statusText
327
+ };
328
+ return h.error(g), g;
313
329
  }
314
330
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
315
- async post({ url: t, data: e, token: r }) {
331
+ async post({ url: t, data: e, token: s, cache: r = "default" }) {
316
332
  e = e ? { ...e } : {};
317
- const s = e.buffer ?? void 0, o = e.boundary ?? void 0;
318
- s && delete e.buffer, o && delete e.boundary;
319
- const a = this.assemblyParameter(e), l = this.convertUrlParameter(a), i = await this.assemblyHeader({ urlParams: l, token: r });
320
- s && o && (i["Content-Type"] = `multipart/form-data; boundary=${o}`);
321
- const c = this.getApiUrl(t), g = {
333
+ const o = e.buffer ?? void 0, i = e.boundary ?? void 0;
334
+ o && delete e.buffer, i && delete e.boundary;
335
+ const l = this.assemblyParameter(e), a = this.convertUrlParameter(l), c = await this.assemblyHeader({ urlParams: a, token: s });
336
+ o && i && (c["Content-Type"] = `multipart/form-data; boundary=${i}`);
337
+ const u = this.getApiUrl(t), g = {
322
338
  method: "POST",
323
- url: c,
324
- headers: i,
325
- params: a
339
+ url: u,
340
+ headers: c,
341
+ params: l
326
342
  };
327
- this.logger.info(g);
328
- try {
329
- const d = s ? `${c}?${l}` : c, u = await fetch(d, {
330
- method: "POST",
331
- headers: i,
332
- body: s || l.toString(),
333
- credentials: "include"
334
- });
335
- if (u.ok) {
336
- const f = await u.json();
337
- return this.logger.debug({ POST_RESULT: f }), f;
338
- }
339
- return {
340
- code: u.status,
341
- success: !1,
342
- msg: u.statusText
343
- };
344
- } catch (d) {
345
- return {
346
- code: 500,
347
- success: !1,
348
- msg: d?.toString() ?? "Server exception!"
349
- };
343
+ h.info(g);
344
+ const d = o ? `${u}?${a}` : u, m = await fetch(d, {
345
+ method: "POST",
346
+ headers: c,
347
+ body: o || a.toString(),
348
+ credentials: "include",
349
+ cache: r
350
+ });
351
+ if (m.ok) {
352
+ const E = await m.json();
353
+ return h.debug({ POST_RESULT: E }), E;
350
354
  }
355
+ const y = {
356
+ code: m.status,
357
+ success: !1,
358
+ msg: m.statusText
359
+ };
360
+ return h.error(y), y;
351
361
  }
352
362
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
353
- async stream({ url: t, data: e, token: r }) {
363
+ async stream({ url: t, data: e, token: s }) {
354
364
  e = e ? { ...e } : {};
355
- const s = e.buffer ?? void 0, o = e.boundary ?? void 0;
356
- s && delete e.buffer, o && delete e.boundary;
357
- const a = this.assemblyParameter(e), l = this.convertUrlParameter(a), i = await this.assemblyHeader({ urlParams: l, token: r });
358
- s && o && (i["Content-Type"] = `multipart/form-data; boundary=${o}`), i.Connection = "keep-alive", i["X-Accel-Buffering"] = "no", i["Cache-Control"] = "no-cache";
365
+ const r = e.buffer ?? void 0, o = e.boundary ?? void 0;
366
+ r && delete e.buffer, o && delete e.boundary;
367
+ const i = this.assemblyParameter(e), l = this.convertUrlParameter(i), a = await this.assemblyHeader({ urlParams: l, token: s });
368
+ r && o && (a["Content-Type"] = `multipart/form-data; boundary=${o}`), a.Connection = "keep-alive", a["X-Accel-Buffering"] = "no", a["Cache-Control"] = "no-cache";
359
369
  const c = this.getApiUrl(t);
360
370
  try {
361
- const g = s ? `${c}?${l}` : c, d = {
371
+ const u = r ? `${c}?${l}` : c, g = {
362
372
  method: "STREAM",
363
373
  url: c,
364
- headers: i,
365
- params: a
374
+ headers: a,
375
+ params: i
366
376
  };
367
- this.logger.info(d);
368
- const u = await fetch(g, {
377
+ h.info(g);
378
+ const d = await fetch(u, {
369
379
  method: "POST",
370
- headers: i,
371
- body: s || l.toString(),
380
+ headers: a,
381
+ body: r || l.toString(),
372
382
  credentials: "include"
373
383
  });
374
- if (!u.ok)
375
- throw new Error(`HTTP error! status: ${u.status}`);
376
- if (!u.body)
384
+ if (!d.ok)
385
+ throw new Error(`HTTP error! status: ${d.status}`);
386
+ if (!d.body)
377
387
  throw new Error("No response body");
378
- return new Response(u.body, {
388
+ return new Response(d.body, {
379
389
  headers: {
380
390
  "Content-Type": "text/event-stream",
381
391
  "Cache-Control": "no-cache",
382
392
  Connection: "keep-alive"
383
393
  }
384
394
  });
385
- } catch (g) {
386
- throw g;
395
+ } catch (u) {
396
+ throw u;
387
397
  }
388
398
  }
389
399
  getUserInfo(t) {
390
- const e = t.get(h.user)?.value;
400
+ const e = t.get(f.user)?.value;
391
401
  return e && e.length > 0 ? JSON.parse(e) : null;
392
402
  }
393
403
  getToken(t) {
394
404
  const e = t.get("user")?.value;
395
- let r = "", s = this.userType;
405
+ let s = "", r = this.userType;
396
406
  if (e && e.length > 0) {
397
407
  const o = JSON.parse(e);
398
- r = o.username, s = o.userType.toString();
408
+ s = o.username, r = o.userType.toString();
399
409
  }
400
410
  return {
401
- username: r,
402
- utype: s,
403
- token: t.get(h.token)?.value ?? ""
411
+ username: s,
412
+ utype: r,
413
+ token: t.get(f.token)?.value ?? ""
404
414
  };
405
415
  }
406
416
  getTokenByCookies = (t) => {
407
417
  const e = t.get("user")?.value;
408
- let r = "", s = this.userType;
418
+ let s = "", r = this.userType;
409
419
  if (e && e.length > 0) {
410
420
  const o = JSON.parse(e);
411
- r = o.username, s = o.userType.toString();
421
+ s = o.username, r = o.userType.toString();
412
422
  }
413
423
  return {
414
- username: r,
415
- utype: s,
416
- token: t.get(h.token)?.value ?? ""
424
+ username: s,
425
+ utype: r,
426
+ token: t.get(f.token)?.value ?? ""
417
427
  };
418
428
  };
419
429
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
420
430
  setToken(t, e) {
421
- const { id: r, name: s, username: o, nickname: a, sex: l, headImg: i, token: c, lastLoginTime: g, tokenExpired: d, deviceId: u, userType: f, mustChangePwd: v } = t, P = {
422
- id: r,
423
- name: s,
431
+ const { id: s, name: r, username: o, nickname: i, sex: l, headImg: a, token: c, lastLoginTime: u, tokenExpired: g, deviceId: d, userType: m, mustChangePwd: y } = t, E = {
432
+ id: s,
433
+ name: r,
424
434
  username: o,
425
- nickname: a,
435
+ nickname: i,
426
436
  sex: l,
427
- headImg: i,
428
- lastLoginTime: g,
429
- tokenExpired: d,
430
- deviceId: u,
431
- userType: f,
437
+ headImg: a,
438
+ lastLoginTime: u,
439
+ tokenExpired: g,
440
+ deviceId: d,
441
+ userType: m,
432
442
  status: 0,
433
443
  accountType: 0,
434
444
  enabled: !1,
@@ -436,9 +446,9 @@ class J {
436
446
  needToReview: !1,
437
447
  socketOnline: !1,
438
448
  createTime: "",
439
- mustChangePwd: v
449
+ mustChangePwd: y
440
450
  };
441
- return e.cookies.set(h.token, c, {
451
+ return e.cookies.set(f.token, c, {
442
452
  httpOnly: !0,
443
453
  secure: !this._isDebug,
444
454
  // 在生产环境中启用Secure
@@ -446,38 +456,43 @@ class J {
446
456
  // 防止 CSRF 攻击
447
457
  path: "/"
448
458
  // Cookie 的路径
449
- }), e.cookies.set(h.user, JSON.stringify(P)), e;
459
+ }), e.cookies.set(f.user, JSON.stringify(E)), e;
450
460
  }
451
461
  cleanToken(t) {
452
- return t.cookies.set(h.token, "", {
462
+ return t.cookies.set(f.token, "", {
453
463
  maxAge: -1,
454
464
  //设置为 -1表示立即过期
455
465
  httpOnly: !0,
456
466
  path: "/"
457
- }), t.cookies.set(h.user, "", {
467
+ }), t.cookies.set(f.user, "", {
458
468
  maxAge: -1,
459
469
  //设置为 -1表示立即过期
460
470
  httpOnly: !0,
461
471
  path: "/"
462
- }), t.cookies.delete(h.token), t.cookies.delete(h.user), t;
472
+ }), t.cookies.delete(f.token), t.cookies.delete(f.user), t;
463
473
  }
464
474
  }
465
475
  export {
466
- L as ACCOUNT_TYPE_MAP,
467
- _ as CURRENT_WEBSITE_KEY,
468
- U as DEFAULT_LOCALE,
469
- w as DEVICE_ID_KEY,
470
- J as HttpClient,
471
- $ as USER_FORM_MAP,
472
- W as USER_SEX_MAP,
473
- A as USER_TYPE_MAP,
474
- k as get,
475
- H as getHttpClientOpts,
476
- G as getHttpClientOptsByCookie,
477
- j as iGet,
478
- K as iPost,
479
- F as iPostSuccess,
480
- O as post,
481
- x as setDefaultRequestHeader
476
+ $ as ACCOUNT_TYPE_MAP,
477
+ v as CURRENT_WEBSITE_KEY,
478
+ k as DEFAULT_LOCALE,
479
+ S as DEVICE_ID_KEY,
480
+ Y as HttpClient,
481
+ W as USER_FORM_MAP,
482
+ j as USER_SEX_MAP,
483
+ R as USER_TYPE_MAP,
484
+ D as get,
485
+ G as getHttpClientOpts,
486
+ J as getHttpClientOptsByCookie,
487
+ K as iGet,
488
+ F as iPost,
489
+ H as iPostSuccess,
490
+ _ as isArray,
491
+ h as logger,
492
+ C as post,
493
+ w as randomString,
494
+ x as setDefaultRequestHeader,
495
+ b as urlEncode,
496
+ U as urlParamToJson
482
497
  };
483
498
  //# sourceMappingURL=iboot-http-client.es.js.map