iboot-http-client 1.1.1 → 1.1.3

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,6 +1,6 @@
1
- import N from "pino";
1
+ import C from "pino";
2
2
  import R from "pino-pretty";
3
- import A from "js-md5";
3
+ import N from "js-md5";
4
4
  import m from "crypto-js";
5
5
  const H = {
6
6
  GENERAL: 0,
@@ -29,11 +29,11 @@ const H = {
29
29
  ignore: "pid,hostname",
30
30
  messageFormat: "{msg}",
31
31
  singleLine: !0
32
- }), h = N({
32
+ }), u = C({
33
33
  name: process.env.APP_NAME ?? "iBoot",
34
- level: process.env.NODE_ENV !== "production" ? "debug" : "info",
34
+ level: process.env.APP_ENV === "production" ? "info" : "debug",
35
35
  nestedKey: "payload"
36
- }, M), _ = (o) => Array.isArray(o) || typeof o == "object" && Object.prototype.toString.call(o) === "[object Array]", w = (o) => {
36
+ }, M), w = (o) => Array.isArray(o) || typeof o == "object" && Object.prototype.toString.call(o) === "[object Array]", S = (o) => {
37
37
  const t = o || 32, e = "ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678", s = e.length;
38
38
  let r = "";
39
39
  for (let n = 0; n < t; n++)
@@ -45,7 +45,7 @@ const H = {
45
45
  const n = r.split("=");
46
46
  n[1] && (!t || t.indexOf(n[0]) == -1) && (e[n[0]] = n[1]);
47
47
  }), e;
48
- }, b = (o) => !o || o == "" || o.length == 0 ? "" : encodeURIComponent(o).replace(/\(/g, "%28").replace(/\)/g, "%29").replace(/\'/g, "%27").replace(/\!/g, "%21").replace(/\~/g, "%7E"), K = (o, t) => {
48
+ }, T = (o) => !o || o == "" || o.length == 0 ? "" : encodeURIComponent(o).replace(/\(/g, "%28").replace(/\)/g, "%29").replace(/\'/g, "%27").replace(/\!/g, "%21").replace(/\~/g, "%7E"), K = (o, t) => {
49
49
  const e = {
50
50
  "M+": o.getMonth() + 1,
51
51
  //月份
@@ -76,35 +76,35 @@ const H = {
76
76
  }
77
77
  }
78
78
  return t;
79
- }, D = "zh-CN", O = "_device_id_key", v = "_current_website_key", S = "Get request error!", I = "Post request error!", T = {
79
+ }, L = "zh-CN", O = "_device_id_key", P = "_current_website_key", _ = "Get request error!", I = "Post request error!", b = {
80
80
  "Device-Id": "Device-Id",
81
81
  Lang: "Lang",
82
82
  "Website-Id": "Website-Id",
83
83
  "Website-No": "Website-No"
84
- }, d = {
84
+ }, g = {
85
85
  IBOOT_DEVICE_ID: "IBOOT_DEVICE_ID",
86
86
  IBOOT_LOCALE: "IBOOT_LOCALE",
87
87
  IBOOT_WEBSITE_ID: "IBOOT_WEBSITE_ID",
88
88
  IBOOT_WEBSITE_NO: "IBOOT_WEBSITE_NO",
89
89
  token: "token",
90
90
  user: "user"
91
- }, P = () => {
92
- const o = localStorage.getItem(O) ?? "", t = localStorage.getItem(v), e = t != null ? JSON.parse(t) : null, s = {};
91
+ }, v = () => {
92
+ const o = localStorage.getItem(O) ?? "", t = localStorage.getItem(P), e = t != null ? JSON.parse(t) : null, s = {};
93
93
  return o.length > 0 && (s["Device-Id"] = o), e && (s.Lang = e.language, e.websiteId && (s["Website-Id"] = e.websiteId), e.websiteNo && (s["Website-No"] = e.websiteNo)), s;
94
94
  }, G = (o, t) => {
95
- localStorage.setItem(O, o), t && localStorage.setItem(v, JSON.stringify(t));
96
- }, L = async (o, t) => {
95
+ localStorage.setItem(O, o), t && localStorage.setItem(P, JSON.stringify(t));
96
+ }, B = async (o, t) => {
97
97
  if (t?.data) {
98
98
  const i = new URLSearchParams(t.data);
99
99
  o.indexOf("?") != -1 ? o += `&${i}` : o += `?${i}`;
100
100
  }
101
- const e = P(), s = t?.headers;
101
+ const e = v(), s = t?.headers;
102
102
  if (s)
103
103
  for (const i in s) {
104
104
  const c = s[i];
105
105
  c && (e[i] = c);
106
106
  }
107
- h.debug({
107
+ u.debug({
108
108
  url: o,
109
109
  headers: e
110
110
  }, "GET");
@@ -115,24 +115,24 @@ const H = {
115
115
  });
116
116
  if (r.ok) {
117
117
  const i = await r.json();
118
- return h.debug(i, "GET_RESULT"), i;
118
+ return u.debug(i, "GET_RESULT"), i;
119
119
  }
120
120
  const n = {
121
121
  code: r.status,
122
122
  success: !1,
123
123
  msg: r.statusText
124
124
  };
125
- return h.error(n), n;
126
- }, C = async (o, t) => {
125
+ return u.error(n), n;
126
+ }, A = async (o, t) => {
127
127
  const e = t?.data ?? {}, s = t?.headers;
128
128
  let r;
129
- const n = new Headers(P());
129
+ const n = new Headers(v());
130
130
  if (e instanceof FormData ? r = e : (r = JSON.stringify(e), n.set("Content-Type", "application/json")), s)
131
131
  for (let a in s) {
132
132
  const l = s[a];
133
133
  l && n.set(a, l);
134
134
  }
135
- h.debug({
135
+ u.debug({
136
136
  url: o,
137
137
  headers: n
138
138
  }, "POST");
@@ -143,25 +143,25 @@ const H = {
143
143
  });
144
144
  if (i.ok) {
145
145
  const a = await i.json();
146
- return h.debug(a, "POST_RESULT"), a;
146
+ return u.debug(a, "POST_RESULT"), a;
147
147
  }
148
148
  const c = {
149
149
  code: i.status,
150
150
  success: !1,
151
151
  msg: i.statusText
152
152
  };
153
- return h.error(c), c;
153
+ return u.error(c), c;
154
154
  }, Y = async (o, t) => {
155
- const e = await L(o, t);
155
+ const e = await B(o, t);
156
156
  if (e.success)
157
157
  return e.data;
158
158
  if (t?.showError) {
159
- t.showError(e.msg ?? S);
159
+ t.showError(e.msg ?? _);
160
160
  return;
161
161
  }
162
- throw Error(e.msg ?? S);
162
+ throw Error(e.msg ?? _);
163
163
  }, J = async (o, t) => {
164
- const e = await C(o, t);
164
+ const e = await A(o, t);
165
165
  if (e.success)
166
166
  return e.data;
167
167
  if (t?.showError) {
@@ -170,13 +170,13 @@ const H = {
170
170
  }
171
171
  throw Error(e.msg ?? I);
172
172
  }, V = async (o, t) => {
173
- const e = await C(o, t);
173
+ const e = await A(o, t);
174
174
  return e.success ? (t?.showSuccess && t.showSuccess(e.msg ?? "SUCCESS"), !0) : (t?.showError && t.showError(e.msg ?? I), !1);
175
175
  }, X = (o) => {
176
- const t = o.headers.get(T["Device-Id"]), e = o.headers.get(T.Lang), s = o.headers.get(T["Website-Id"]), r = o.headers.get(T["Website-No"]), n = {};
176
+ const t = o.headers.get(b["Device-Id"]), e = o.headers.get(b.Lang), s = o.headers.get(b["Website-Id"]), r = o.headers.get(b["Website-No"]), n = {};
177
177
  return t && t.length > 0 && (n.deviceId = t), e && e.length > 0 && (n.lang = e), s && s.length > 0 && (n.websiteId = s), r && r.length > 0 && (n.websiteNo = r), n;
178
178
  }, z = (o) => {
179
- const t = {}, e = o.get(d.IBOOT_DEVICE_ID)?.value, s = o.get(d.IBOOT_LOCALE)?.value, r = o.get(d.IBOOT_WEBSITE_ID)?.value, n = o.get(d.IBOOT_WEBSITE_NO)?.value;
179
+ const t = {}, e = o.get(g.IBOOT_DEVICE_ID)?.value, s = o.get(g.IBOOT_LOCALE)?.value, r = o.get(g.IBOOT_WEBSITE_ID)?.value, n = o.get(g.IBOOT_WEBSITE_NO)?.value;
180
180
  return e && e.length > 0 && (t.deviceId = e), s && s.length > 0 && (t.lang = s), r && r.length > 0 && (t.websiteId = r), n && n.length > 0 && (t.websiteNo = n), t;
181
181
  };
182
182
  class q {
@@ -190,12 +190,9 @@ class q {
190
190
  lang;
191
191
  websiteId;
192
192
  websiteNo;
193
- _isDebug;
194
- constructor({ deviceId: t, lang: e, websiteId: s, websiteNo: r, userType: n, helloURL: i }) {
195
- this.baseUrl = process.env.BASE_URL ?? "", this.helloMethod = i ?? "", this.apiKey = process.env.API_KEY ?? "", this.userType = n ?? U.TYPE_MGT, this.userFrom = process.env.USER_FROM || "1", this.deviceId = t ?? w(10), this.lang = e ?? D, this.websiteId = s, this.websiteNo = r, this._isDebug = process.env.NODE_ENV != "production";
196
- }
197
- isDebug() {
198
- return this._isDebug;
193
+ isProduction;
194
+ constructor(t) {
195
+ this.isProduction = process.env.APP_ENV === "production", this.isProduction || u.warn("This is currently a development environment!"), this.baseUrl = process.env.APP_BASEURL, this.apiKey = process.env.APP_APIKEY, this.userFrom = process.env.APP_USERFROM ?? "1", this.helloMethod = t.helloURL ?? "", this.userType = t.userType ?? U.TYPE_MGT, this.deviceId = t.deviceId ?? S(10), this.lang = t.lang ?? L, this.websiteId = t.websiteId, this.websiteNo = t.websiteNo;
199
196
  }
200
197
  encrypt(t) {
201
198
  const e = m.enc.Utf8.parse(this.apiKey);
@@ -218,7 +215,7 @@ class q {
218
215
  if (r && typeof r == "string")
219
216
  r = r.trim();
220
217
  else if (r && typeof r == "object") {
221
- if (_(r)) {
218
+ if (w(r)) {
222
219
  if (r.length === 0)
223
220
  continue;
224
221
  for (let n = 0; n < r.length; n++) {
@@ -227,18 +224,18 @@ class q {
227
224
  const a = i[c] ?? void 0;
228
225
  if (a) {
229
226
  if (typeof a == "object")
230
- _(a) && a.forEach((l, u) => {
227
+ w(a) && a.forEach((l, h) => {
231
228
  if (l)
232
229
  for (const f in l) {
233
- const g = l[f];
234
- if (g && g.toString().length > 0) {
235
- const p = `[${n.toString()}].${c + `[${u.toString()}].${f}`}=${b(g)}`;
230
+ const d = l[f];
231
+ if (d && d.toString().length > 0) {
232
+ const p = `[${n.toString()}].${c + `[${h.toString()}].${f}`}=${T(d)}`;
236
233
  e.push(s + p);
237
234
  }
238
235
  }
239
236
  });
240
237
  else if (a.toString().length > 0) {
241
- const l = `[${n.toString()}].${c}=${b(a.toString())}`;
238
+ const l = `[${n.toString()}].${c}=${T(a.toString())}`;
242
239
  e.push(s + l);
243
240
  }
244
241
  }
@@ -248,13 +245,13 @@ class q {
248
245
  for (const n in r) {
249
246
  const i = r[n];
250
247
  if (i != null && i != "" && i.length != 0) {
251
- const c = `.${n}=${b(i)}`;
248
+ const c = `.${n}=${T(i)}`;
252
249
  e.push(s + c);
253
250
  }
254
251
  }
255
252
  continue;
256
253
  } else r && typeof r == "function" && (r = null);
257
- r != null && r != "" && r.length != 0 && e.push(s + "=" + b(r));
254
+ r != null && r != "" && r.length != 0 && e.push(s + "=" + T(r));
258
255
  }
259
256
  return e && e.join("&");
260
257
  }
@@ -268,16 +265,16 @@ class q {
268
265
  let l = t[a];
269
266
  typeof l == "object" && (l = JSON.stringify(l)), r.push(a + "=" + l);
270
267
  });
271
- const n = r.join("&"), i = A.md5((n + e).toLocaleUpperCase()), c = {
268
+ const n = r.join("&"), i = N.md5((n + e).toLocaleUpperCase()), c = {
272
269
  params: n,
273
270
  md5: i
274
271
  };
275
- return h.debug(c, "API_SIGN"), i;
272
+ return u.debug(c, "API_SIGN"), i;
276
273
  }
277
274
  //eslint-disable-next-line @typescript-eslint/no-explicit-any
278
275
  assemblyParameter(t, e) {
279
276
  const s = t ?? {};
280
- 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;
277
+ return s.timestamp = Date.now() + "", s.echostr = S(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;
281
278
  }
282
279
  async assemblyHeader({ urlParams: t, token: e }) {
283
280
  const s = {
@@ -303,7 +300,7 @@ class q {
303
300
  url: t,
304
301
  headers: e
305
302
  };
306
- h.debug(s, "HELLO");
303
+ u.debug(s, "HELLO");
307
304
  const r = await fetch(t, {
308
305
  method: "GET",
309
306
  headers: e,
@@ -312,7 +309,7 @@ class q {
312
309
  });
313
310
  if (r.ok) {
314
311
  const n = await r.json();
315
- return h.debug(n.data, "HELLO RESULT"), n.data;
312
+ return u.debug(n.data, "HELLO RESULT"), n.data;
316
313
  }
317
314
  throw new Error("hello iBoot error!");
318
315
  }
@@ -335,23 +332,23 @@ class q {
335
332
  url: a,
336
333
  headers: c
337
334
  };
338
- h.info(l, "GET");
339
- const u = await fetch(a, {
335
+ u.info(l, "GET");
336
+ const h = await fetch(a, {
340
337
  method: "GET",
341
338
  headers: c,
342
339
  credentials: "include",
343
340
  cache: r
344
341
  });
345
- if (u.ok) {
346
- const g = await u.json();
347
- return h.debug(g, "GET_RESULT"), g;
342
+ if (h.ok) {
343
+ const d = await h.json();
344
+ return u.debug(d, "GET_RESULT"), d;
348
345
  }
349
346
  const f = {
350
- code: u.status,
347
+ code: h.status,
351
348
  success: !1,
352
- msg: u.statusText
349
+ msg: h.statusText
353
350
  };
354
- return h.error(f), f;
351
+ return u.error(f), f;
355
352
  }
356
353
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
357
354
  async post({ url: t, data: e, token: s, cache: r = "default" }) {
@@ -360,13 +357,13 @@ class q {
360
357
  n && delete e.buffer, i && delete e.boundary;
361
358
  const c = this.assemblyParameter(e), a = this.convertUrlParameter(c), l = await this.assemblyHeader({ urlParams: a, token: s });
362
359
  n && i && (l["Content-Type"] = `multipart/form-data; boundary=${i}`);
363
- const u = this.getApiUrl(t), f = {
364
- url: u,
360
+ const h = this.getApiUrl(t), f = {
361
+ url: h,
365
362
  headers: l,
366
363
  params: c
367
364
  };
368
- h.info(f, "POST");
369
- const g = n ? `${u}?${a}` : u, p = await fetch(g, {
365
+ u.info(f, "POST");
366
+ const d = n ? `${h}?${a}` : h, p = await fetch(d, {
370
367
  method: "POST",
371
368
  headers: l,
372
369
  body: n || a.toString(),
@@ -375,14 +372,14 @@ class q {
375
372
  });
376
373
  if (p.ok) {
377
374
  const E = await p.json();
378
- return h.debug(E, "POST_RESULT"), E;
375
+ return u.debug(E, "POST_RESULT"), E;
379
376
  }
380
377
  const y = {
381
378
  code: p.status,
382
379
  success: !1,
383
380
  msg: p.statusText
384
381
  };
385
- return h.error(y), y;
382
+ return u.error(y), y;
386
383
  }
387
384
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
388
385
  async stream({ url: t, data: e, token: s }) {
@@ -393,35 +390,35 @@ class q {
393
390
  r && n && (a["Content-Type"] = `multipart/form-data; boundary=${n}`), a.Connection = "keep-alive", a["X-Accel-Buffering"] = "no", a["Cache-Control"] = "no-cache";
394
391
  const l = this.getApiUrl(t);
395
392
  try {
396
- const u = r ? `${l}?${c}` : l, f = {
393
+ const h = r ? `${l}?${c}` : l, f = {
397
394
  url: l,
398
395
  headers: a,
399
396
  params: i
400
397
  };
401
- h.info(f, "STREAM");
402
- const g = await fetch(u, {
398
+ u.info(f, "STREAM");
399
+ const d = await fetch(h, {
403
400
  method: "POST",
404
401
  headers: a,
405
402
  body: r || c.toString(),
406
403
  credentials: "include"
407
404
  });
408
- if (!g.ok)
409
- throw new Error(`HTTP error! status: ${g.status}`);
410
- if (!g.body)
405
+ if (!d.ok)
406
+ throw new Error(`HTTP error! status: ${d.status}`);
407
+ if (!d.body)
411
408
  throw new Error("No response body");
412
- return new Response(g.body, {
409
+ return new Response(d.body, {
413
410
  headers: {
414
411
  "Content-Type": "text/event-stream",
415
412
  "Cache-Control": "no-cache",
416
413
  Connection: "keep-alive"
417
414
  }
418
415
  });
419
- } catch (u) {
420
- throw u;
416
+ } catch (h) {
417
+ throw h;
421
418
  }
422
419
  }
423
420
  getUserInfo(t) {
424
- const e = t.get(d.user)?.value;
421
+ const e = t.get(g.user)?.value;
425
422
  return e && e.length > 0 ? JSON.parse(e) : null;
426
423
  }
427
424
  getToken(t) {
@@ -434,7 +431,7 @@ class q {
434
431
  return {
435
432
  username: s,
436
433
  utype: r,
437
- token: t.get(d.token)?.value ?? ""
434
+ token: t.get(g.token)?.value ?? ""
438
435
  };
439
436
  }
440
437
  getTokenByCookies = (t) => {
@@ -447,21 +444,21 @@ class q {
447
444
  return {
448
445
  username: s,
449
446
  utype: r,
450
- token: t.get(d.token)?.value ?? ""
447
+ token: t.get(g.token)?.value ?? ""
451
448
  };
452
449
  };
453
450
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
454
451
  setToken(t, e) {
455
- const { id: s, name: r, username: n, nickname: i, sex: c, headImg: a, token: l, lastLoginTime: u, tokenExpired: f, deviceId: g, userType: p, mustChangePwd: y } = t, E = {
452
+ const { id: s, name: r, username: n, nickname: i, sex: c, headImg: a, token: l, lastLoginTime: h, tokenExpired: f, deviceId: d, userType: p, mustChangePwd: y } = t, E = {
456
453
  id: s,
457
454
  name: r,
458
455
  username: n,
459
456
  nickname: i,
460
457
  sex: c,
461
458
  headImg: a,
462
- lastLoginTime: u,
459
+ lastLoginTime: h,
463
460
  tokenExpired: f,
464
- deviceId: g,
461
+ deviceId: d,
465
462
  userType: p,
466
463
  status: 0,
467
464
  accountType: 0,
@@ -472,52 +469,52 @@ class q {
472
469
  createTime: "",
473
470
  mustChangePwd: y
474
471
  };
475
- return e.cookies.set(d.token, l, {
472
+ return e.cookies.set(g.token, l, {
476
473
  httpOnly: !0,
477
- secure: !this._isDebug,
474
+ secure: this.isProduction,
478
475
  // 在生产环境中启用Secure
479
476
  sameSite: "strict",
480
477
  // 防止 CSRF 攻击
481
478
  path: "/"
482
479
  // Cookie 的路径
483
- }), e.cookies.set(d.user, JSON.stringify(E)), e;
480
+ }), e.cookies.set(g.user, JSON.stringify(E)), e;
484
481
  }
485
482
  cleanToken(t) {
486
- return t.cookies.set(d.token, "", {
483
+ return t.cookies.set(g.token, "", {
487
484
  maxAge: -1,
488
485
  //设置为 -1表示立即过期
489
486
  httpOnly: !0,
490
487
  path: "/"
491
- }), t.cookies.set(d.user, "", {
488
+ }), t.cookies.set(g.user, "", {
492
489
  maxAge: -1,
493
490
  //设置为 -1表示立即过期
494
491
  httpOnly: !0,
495
492
  path: "/"
496
- }), t.cookies.delete(d.token), t.cookies.delete(d.user), t;
493
+ }), t.cookies.delete(g.token), t.cookies.delete(g.user), t;
497
494
  }
498
495
  }
499
496
  export {
500
497
  H as ACCOUNT_TYPE_MAP,
501
- v as CURRENT_WEBSITE_KEY,
502
- D as DEFAULT_LOCALE,
498
+ P as CURRENT_WEBSITE_KEY,
499
+ L as DEFAULT_LOCALE,
503
500
  O as DEVICE_ID_KEY,
504
501
  q as HttpClient,
505
502
  j as USER_FORM_MAP,
506
503
  F as USER_SEX_MAP,
507
504
  U as USER_TYPE_MAP,
508
505
  K as dateFormat,
509
- L as get,
506
+ B as get,
510
507
  X as getHttpClientOpts,
511
508
  z as getHttpClientOptsByCookie,
512
509
  Y as iGet,
513
510
  J as iPost,
514
511
  V as iPostSuccess,
515
- _ as isArray,
516
- h as logger,
517
- C as post,
518
- w as randomString,
512
+ w as isArray,
513
+ u as logger,
514
+ A as post,
515
+ S as randomString,
519
516
  G as setDefaultRequestHeader,
520
- b as urlEncode,
517
+ T as urlEncode,
521
518
  k as urlParamToJson
522
519
  };
523
520
  //# sourceMappingURL=iboot-http-client.es.js.map