iboot-http-client 1.0.6 → 1.0.8

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