iboot-http-client 1.0.4 → 1.0.6

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,7 +1,7 @@
1
1
  import C from "js-md5";
2
- import p from "crypto-js";
2
+ import m from "crypto-js";
3
3
  import N from "pino";
4
- const M = {
4
+ const L = {
5
5
  GENERAL: 0,
6
6
  PHONE: 1,
7
7
  EMAIL: 2
@@ -21,7 +21,7 @@ const M = {
21
21
  unknown: 0,
22
22
  male: 1,
23
23
  female: 2
24
- }, E = (n) => Array.isArray(n) || typeof n == "object" && Object.prototype.toString.call(n) === "[object Array]", I = (n) => {
24
+ }, b = (n) => Array.isArray(n) || typeof n == "object" && Object.prototype.toString.call(n) === "[object Array]", I = (n) => {
25
25
  const t = n, e = "ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678", r = e.length;
26
26
  let s = "";
27
27
  for (let o = 0; o < t; o++)
@@ -33,7 +33,7 @@ const M = {
33
33
  const o = s.split("=");
34
34
  o[1] && (e[o[0]] = o[1]);
35
35
  }), e;
36
- }, m = (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", T = "_device_id_key", _ = "_current_website_key", w = "Get request error!", b = "Post request error!", y = {
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 = {
37
37
  "Device-Id": "Device-Id",
38
38
  Lang: "Lang",
39
39
  "Website-Id": "Website-Id",
@@ -45,21 +45,21 @@ const M = {
45
45
  IBOOT_WEBSITE_NO: "IBOOT_WEBSITE_NO",
46
46
  token: "token",
47
47
  user: "user"
48
- }, O = () => {
49
- const n = localStorage.getItem(T) ?? "", t = localStorage.getItem(_), e = t != null ? JSON.parse(t) : null, r = {};
48
+ }, S = () => {
49
+ const n = localStorage.getItem(w) ?? "", t = localStorage.getItem(_), e = t != null ? JSON.parse(t) : null, r = {};
50
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;
51
51
  }, x = (n, t) => {
52
- localStorage.setItem(T, n), t && localStorage.setItem(_, JSON.stringify(t));
52
+ localStorage.setItem(w, n), t && localStorage.setItem(_, JSON.stringify(t));
53
53
  }, k = async (n, t) => {
54
54
  if (t?.data) {
55
55
  const o = new URLSearchParams(t.data);
56
56
  n.indexOf("?") != -1 ? n += `&${o}` : n += `?${o}`;
57
57
  }
58
- const e = O(), r = t?.heads;
58
+ const e = S(), r = t?.heads;
59
59
  if (r)
60
60
  for (const o in r) {
61
- const c = r[o];
62
- c && (e[o] = c);
61
+ const a = r[o];
62
+ a && (e[o] = a);
63
63
  }
64
64
  const s = await fetch(n, {
65
65
  method: "GET",
@@ -71,46 +71,46 @@ const M = {
71
71
  success: !1,
72
72
  msg: s.statusText
73
73
  };
74
- }, S = async (n, t) => {
74
+ }, O = async (n, t) => {
75
75
  const e = t?.data ?? {}, r = t?.heads;
76
76
  let s;
77
- const o = new Headers(O());
77
+ const o = new Headers(S());
78
78
  if (e instanceof FormData ? s = e : (s = JSON.stringify(e), o.set("Content-Type", "application/json")), r)
79
79
  for (let l in r) {
80
80
  const i = r[l];
81
81
  i && o.set(l, i);
82
82
  }
83
- const c = await fetch(n, {
83
+ const a = await fetch(n, {
84
84
  method: "POST",
85
85
  headers: o,
86
86
  body: s
87
87
  });
88
- return c.ok ? await c.json() : {
89
- code: c.status,
88
+ return a.ok ? await a.json() : {
89
+ code: a.status,
90
90
  success: !1,
91
- msg: c.statusText
91
+ msg: a.statusText
92
92
  };
93
93
  }, j = async (n, t) => {
94
94
  const e = await k(n, t);
95
95
  if (e.success)
96
96
  return e.data;
97
97
  if (t?.showError) {
98
- t.showError(e.msg ?? w);
98
+ t.showError(e.msg ?? T);
99
99
  return;
100
100
  }
101
- throw Error(e.msg ?? w);
101
+ throw Error(e.msg ?? T);
102
102
  }, K = async (n, t) => {
103
- const e = await S(n, t);
103
+ const e = await O(n, t);
104
104
  if (e.success)
105
105
  return e.data;
106
106
  if (t?.showError) {
107
- t.showError(e.msg ?? b);
107
+ t.showError(e.msg ?? E);
108
108
  return;
109
109
  }
110
- throw Error(e.msg ?? b);
110
+ throw Error(e.msg ?? E);
111
111
  }, F = async (n, t) => {
112
- const e = await S(n, t);
113
- return e.success ? (t?.showSuccess && t.showSuccess(e.msg ?? "SUCCESS"), !0) : (t?.showError && t.showError(e.msg ?? b), !1);
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
114
  }, H = (n) => {
115
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
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;
@@ -131,25 +131,30 @@ class J {
131
131
  websiteNo;
132
132
  _isDebug;
133
133
  logger;
134
- constructor({ deviceId: t, lang: e, websiteId: r, websiteNo: s, userType: o }) {
135
- this.baseUrl = process.env.BASE_URL ?? "", this.helloMethod = process.env.HELLO_URL ?? "", 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: "iboot",
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
137
  level: this._isDebug ? "debug" : "info",
138
- nestedKey: "payload"
138
+ nestedKey: "payload",
139
+ messageKey: "msg",
140
+ //明确指定消息键,可解决乱码问题
141
+ formatters: {
142
+ level: (i) => ({ level: i })
143
+ }
139
144
  });
140
145
  }
141
146
  isDebug() {
142
147
  return this._isDebug;
143
148
  }
144
149
  encrypt(t) {
145
- const e = p.enc.Utf8.parse(this.apiKey);
146
- return p.AES.encrypt(t, e).toString();
150
+ const e = m.enc.Utf8.parse(this.apiKey);
151
+ return m.AES.encrypt(t, e).toString();
147
152
  }
148
153
  decrypt(t) {
149
- const e = p.enc.Utf8.parse(this.apiKey);
150
- return p.AES.decrypt(t, e, {
151
- mode: p.mode.ECB
152
- }).toString(p.enc.Utf8);
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);
153
158
  }
154
159
  getDeviceId() {
155
160
  return this.deviceId;
@@ -162,43 +167,43 @@ class J {
162
167
  if (s && typeof s == "string")
163
168
  s = s.trim();
164
169
  else if (s && typeof s == "object") {
165
- if (E(s)) {
170
+ if (b(s)) {
166
171
  if (s.length === 0)
167
172
  continue;
168
173
  for (let o = 0; o < s.length; o++) {
169
- const c = s[o];
170
- for (const l in c) {
171
- const i = c[l] ?? void 0;
174
+ const a = s[o];
175
+ for (const l in a) {
176
+ const i = a[l] ?? void 0;
172
177
  if (i) {
173
178
  if (typeof i == "object")
174
- E(i) && i.forEach((a, d) => {
175
- if (a)
176
- for (const g in a) {
177
- const u = a[g];
179
+ b(i) && i.forEach((c, g) => {
180
+ if (c)
181
+ for (const d in c) {
182
+ const u = c[d];
178
183
  if (u && u.toString().length > 0) {
179
- const f = `[${o.toString()}].${l + `[${d.toString()}].${g}`}=${m(u)}`;
184
+ const f = `[${o.toString()}].${l + `[${g.toString()}].${d}`}=${p(u)}`;
180
185
  e.push(r + f);
181
186
  }
182
187
  }
183
188
  });
184
189
  else if (i.toString().length > 0) {
185
- const a = `[${o.toString()}].${l}=${m(i.toString())}`;
186
- e.push(r + a);
190
+ const c = `[${o.toString()}].${l}=${p(i.toString())}`;
191
+ e.push(r + c);
187
192
  }
188
193
  }
189
194
  }
190
195
  }
191
196
  } else
192
197
  for (const o in s) {
193
- const c = s[o];
194
- if (c != null && c != "" && c.length != 0) {
195
- const l = `.${o}=${m(c)}`;
198
+ const a = s[o];
199
+ if (a != null && a != "" && a.length != 0) {
200
+ const l = `.${o}=${p(a)}`;
196
201
  e.push(r + l);
197
202
  }
198
203
  }
199
204
  continue;
200
205
  } else s && typeof s == "function" && (s = null);
201
- s != null && s != "" && s.length != 0 && e.push(r + "=" + m(s));
206
+ s != null && s != "" && s.length != 0 && e.push(r + "=" + p(s));
202
207
  }
203
208
  return e && e.join("&");
204
209
  }
@@ -206,17 +211,18 @@ class J {
206
211
  const e = "&key=" + await this.helloIboot(), r = [];
207
212
  for (const i in t)
208
213
  r.push(i);
209
- r.sort((i, a) => i.toLowerCase().localeCompare(a.toLowerCase()));
214
+ r.sort((i, c) => i.toLowerCase().localeCompare(c.toLowerCase()));
210
215
  const s = [];
211
216
  r.forEach((i) => {
212
- let a = t[i];
213
- typeof a == "object" && (a = JSON.stringify(a)), s.push(i + "=" + a);
217
+ let c = t[i];
218
+ typeof c == "object" && (c = JSON.stringify(c)), s.push(i + "=" + c);
214
219
  });
215
- const o = s.join("&"), c = C.md5((o + e).toLocaleUpperCase()), l = {
220
+ const o = s.join("&"), a = C.md5((o + e).toLocaleUpperCase()), l = {
221
+ name: "sort",
216
222
  params: o,
217
- md5: c
223
+ md5: a
218
224
  };
219
- return this.logger.debug(l), c;
225
+ return this.logger.debug(l), a;
220
226
  }
221
227
  //eslint-disable-next-line @typescript-eslint/no-explicit-any
222
228
  assemblyParameter(t, e) {
@@ -237,7 +243,7 @@ class J {
237
243
  return r.Sign = o, r;
238
244
  }
239
245
  async helloIboot() {
240
- if (this.helloIboot.length === 0)
246
+ if (this.helloMethod.length <= 0)
241
247
  return this.apiKey;
242
248
  const t = this.getApiUrl(this.helloMethod), e = {
243
249
  "Content-Type": "application/x-www-form-urlencoded",
@@ -275,33 +281,33 @@ class J {
275
281
  }
276
282
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
277
283
  async get({ url: t, data: e, token: r }) {
278
- const s = this.assemblyParameter(e), o = this.convertUrlParameter(s), c = await this.assemblyHeader({ urlParams: o, token: r }), l = `${this.getApiUrl(t)}?${o.toString()}`, i = {
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 = {
279
285
  method: "GET",
280
286
  url: l,
281
- headers: c
287
+ headers: a
282
288
  };
283
289
  this.logger.info(i);
284
290
  try {
285
- const a = await fetch(l, {
291
+ const c = await fetch(l, {
286
292
  method: "GET",
287
- headers: c,
293
+ headers: a,
288
294
  credentials: "include",
289
295
  cache: "force-cache"
290
296
  });
291
- if (a.ok) {
292
- const d = await a.json();
293
- return this.logger.debug({ result: d.data }), d;
297
+ if (c.ok) {
298
+ const g = await c.json();
299
+ return this.logger.debug({ GET_RESULT: g }), g;
294
300
  }
295
301
  return {
296
- code: a.status,
302
+ code: c.status,
297
303
  success: !1,
298
- msg: a.statusText
304
+ msg: c.statusText
299
305
  };
300
- } catch (a) {
306
+ } catch (c) {
301
307
  return {
302
308
  code: 500,
303
309
  success: !1,
304
- msg: a?.toString() ?? "Server exception!"
310
+ msg: c?.toString() ?? "Server exception!"
305
311
  };
306
312
  }
307
313
  }
@@ -310,16 +316,17 @@ class J {
310
316
  e = e ? { ...e } : {};
311
317
  const s = e.buffer ?? void 0, o = e.boundary ?? void 0;
312
318
  s && delete e.buffer, o && delete e.boundary;
313
- const c = this.assemblyParameter(e), l = this.convertUrlParameter(c), i = await this.assemblyHeader({ urlParams: l, token: r });
319
+ const a = this.assemblyParameter(e), l = this.convertUrlParameter(a), i = await this.assemblyHeader({ urlParams: l, token: r });
314
320
  s && o && (i["Content-Type"] = `multipart/form-data; boundary=${o}`);
315
- const a = this.getApiUrl(t), d = {
321
+ const c = this.getApiUrl(t), g = {
316
322
  method: "POST",
317
- url: a,
318
- headers: i
323
+ url: c,
324
+ headers: i,
325
+ params: a
319
326
  };
320
- this.logger.info(d);
327
+ this.logger.info(g);
321
328
  try {
322
- const g = s ? `${a}?${l}` : a, u = await fetch(g, {
329
+ const d = s ? `${c}?${l}` : c, u = await fetch(d, {
323
330
  method: "POST",
324
331
  headers: i,
325
332
  body: s || l.toString(),
@@ -327,18 +334,18 @@ class J {
327
334
  });
328
335
  if (u.ok) {
329
336
  const f = await u.json();
330
- return this.logger.debug({ result: f.data }), f;
337
+ return this.logger.debug({ POST_RESULT: f }), f;
331
338
  }
332
339
  return {
333
340
  code: u.status,
334
341
  success: !1,
335
342
  msg: u.statusText
336
343
  };
337
- } catch (g) {
344
+ } catch (d) {
338
345
  return {
339
346
  code: 500,
340
347
  success: !1,
341
- msg: g?.toString() ?? "Server exception!"
348
+ msg: d?.toString() ?? "Server exception!"
342
349
  };
343
350
  }
344
351
  }
@@ -347,17 +354,18 @@ class J {
347
354
  e = e ? { ...e } : {};
348
355
  const s = e.buffer ?? void 0, o = e.boundary ?? void 0;
349
356
  s && delete e.buffer, o && delete e.boundary;
350
- const c = this.assemblyParameter(e), l = this.convertUrlParameter(c), i = await this.assemblyHeader({ urlParams: l, token: r });
357
+ const a = this.assemblyParameter(e), l = this.convertUrlParameter(a), i = await this.assemblyHeader({ urlParams: l, token: r });
351
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";
352
- const a = this.getApiUrl(t);
359
+ const c = this.getApiUrl(t);
353
360
  try {
354
- const d = s ? `${a}?${l}` : a, g = {
355
- method: "POST",
356
- url: a,
357
- headers: i
361
+ const g = s ? `${c}?${l}` : c, d = {
362
+ method: "STREAM",
363
+ url: c,
364
+ headers: i,
365
+ params: a
358
366
  };
359
- this.logger.info(g);
360
- const u = await fetch(d, {
367
+ this.logger.info(d);
368
+ const u = await fetch(g, {
361
369
  method: "POST",
362
370
  headers: i,
363
371
  body: s || l.toString(),
@@ -374,8 +382,8 @@ class J {
374
382
  Connection: "keep-alive"
375
383
  }
376
384
  });
377
- } catch (d) {
378
- throw d;
385
+ } catch (g) {
386
+ throw g;
379
387
  }
380
388
  }
381
389
  getUserInfo(t) {
@@ -410,15 +418,15 @@ class J {
410
418
  };
411
419
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
412
420
  setToken(t, e) {
413
- const { id: r, name: s, username: o, nickname: c, sex: l, headImg: i, token: a, lastLoginTime: d, tokenExpired: g, deviceId: u, userType: f, mustChangePwd: v } = t, P = {
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 = {
414
422
  id: r,
415
423
  name: s,
416
424
  username: o,
417
- nickname: c,
425
+ nickname: a,
418
426
  sex: l,
419
427
  headImg: i,
420
- lastLoginTime: d,
421
- tokenExpired: g,
428
+ lastLoginTime: g,
429
+ tokenExpired: d,
422
430
  deviceId: u,
423
431
  userType: f,
424
432
  status: 0,
@@ -430,7 +438,7 @@ class J {
430
438
  createTime: "",
431
439
  mustChangePwd: v
432
440
  };
433
- return e.cookies.set(h.token, a, {
441
+ return e.cookies.set(h.token, c, {
434
442
  httpOnly: !0,
435
443
  secure: !this._isDebug,
436
444
  // 在生产环境中启用Secure
@@ -455,10 +463,10 @@ class J {
455
463
  }
456
464
  }
457
465
  export {
458
- M as ACCOUNT_TYPE_MAP,
466
+ L as ACCOUNT_TYPE_MAP,
459
467
  _ as CURRENT_WEBSITE_KEY,
460
468
  U as DEFAULT_LOCALE,
461
- T as DEVICE_ID_KEY,
469
+ w as DEVICE_ID_KEY,
462
470
  J as HttpClient,
463
471
  $ as USER_FORM_MAP,
464
472
  W as USER_SEX_MAP,
@@ -469,7 +477,7 @@ export {
469
477
  j as iGet,
470
478
  K as iPost,
471
479
  F as iPostSuccess,
472
- S as post,
480
+ O as post,
473
481
  x as setDefaultRequestHeader
474
482
  };
475
483
  //# sourceMappingURL=iboot-http-client.es.js.map