iboot-http-client 1.0.5 → 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 p from "crypto-js";
3
1
  import N from "pino";
4
- const M = {
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 M = {
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
- }, E = (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
- }, 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 = {
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
- }, O = () => {
49
- const n = localStorage.getItem(T) ?? "", 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(T, 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 = O(), r = t?.heads;
59
- if (r)
60
- for (const o in r) {
61
- const c = r[o];
62
- c && (e[o] = c);
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
- }, S = async (n, t) => {
75
- const e = t?.data ?? {}, r = t?.heads;
76
- let s;
77
- const o = new Headers(O());
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 c = 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 c.ok ? await c.json() : {
89
- code: c.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: c.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 ?? w);
131
+ t.showError(e.msg ?? O);
99
132
  return;
100
133
  }
101
- throw Error(e.msg ?? w);
102
- }, K = async (n, t) => {
103
- const e = await S(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 ?? b);
140
+ t.showError(e.msg ?? I);
108
141
  return;
109
142
  }
110
- throw Error(e.msg ?? b);
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);
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,13 +163,8 @@ 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 }) {
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",
137
- level: this._isDebug ? "debug" : "info",
138
- nestedKey: "payload"
139
- });
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";
140
168
  }
141
169
  isDebug() {
142
170
  return this._isDebug;
@@ -157,74 +185,75 @@ class J {
157
185
  //eslint-disable-next-line @typescript-eslint/no-explicit-any
158
186
  convertUrlParameter(t) {
159
187
  const e = [];
160
- for (const r in t) {
161
- let s = t[r];
162
- if (s && typeof s == "string")
163
- s = s.trim();
164
- else if (s && typeof s == "object") {
165
- if (E(s)) {
166
- 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)
167
195
  continue;
168
- 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;
172
- if (i) {
173
- 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];
178
- if (u && u.toString().length > 0) {
179
- const f = `[${o.toString()}].${l + `[${d.toString()}].${g}`}=${m(u)}`;
180
- e.push(r + f);
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) => {
203
+ if (c)
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);
181
209
  }
182
210
  }
183
211
  });
184
- else if (i.toString().length > 0) {
185
- const a = `[${o.toString()}].${l}=${m(i.toString())}`;
186
- e.push(r + a);
212
+ else if (a.toString().length > 0) {
213
+ const c = `[${o.toString()}].${l}=${b(a.toString())}`;
214
+ e.push(s + c);
187
215
  }
188
216
  }
189
217
  }
190
218
  }
191
219
  } else
192
- for (const o in s) {
193
- const c = s[o];
194
- if (c != null && c != "" && c.length != 0) {
195
- const l = `.${o}=${m(c)}`;
196
- 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);
197
225
  }
198
226
  }
199
227
  continue;
200
- } else s && typeof s == "function" && (s = null);
201
- s != null && s != "" && s.length != 0 && e.push(r + "=" + m(s));
228
+ } else r && typeof r == "function" && (r = null);
229
+ r != null && r != "" && r.length != 0 && e.push(s + "=" + b(r));
202
230
  }
203
231
  return e && e.join("&");
204
232
  }
205
233
  async sign(t) {
206
- const e = "&key=" + await this.helloIboot(), r = [];
207
- for (const i in t)
208
- r.push(i);
209
- r.sort((i, a) => i.toLowerCase().localeCompare(a.toLowerCase()));
210
- const s = [];
211
- r.forEach((i) => {
212
- let a = t[i];
213
- typeof a == "object" && (a = JSON.stringify(a)), s.push(i + "=" + a);
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);
214
242
  });
215
- const o = s.join("&"), c = C.md5((o + e).toLocaleUpperCase()), l = {
243
+ const o = r.join("&"), i = A.md5((o + e).toLocaleUpperCase()), l = {
244
+ name: "sort",
216
245
  params: o,
217
- md5: c
246
+ md5: i
218
247
  };
219
- return this.logger.debug(l), c;
248
+ return h.debug(l), i;
220
249
  }
221
250
  //eslint-disable-next-line @typescript-eslint/no-explicit-any
222
251
  assemblyParameter(t, e) {
223
- const r = t ?? {};
224
- 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;
225
254
  }
226
255
  async assemblyHeader({ urlParams: t, token: e }) {
227
- const r = {
256
+ const s = {
228
257
  "Content-Type": "application/x-www-form-urlencoded",
229
258
  "Res-Type": "json",
230
259
  "Device-Id": this.deviceId,
@@ -232,9 +261,9 @@ class J {
232
261
  "User-From": this.userFrom,
233
262
  "Api-Key": this.apiKey
234
263
  };
235
- 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));
236
- const s = R(t), o = await this.sign(s);
237
- 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;
238
267
  }
239
268
  async helloIboot() {
240
269
  if (this.helloMethod.length <= 0)
@@ -243,20 +272,20 @@ class J {
243
272
  "Content-Type": "application/x-www-form-urlencoded",
244
273
  "Res-Type": "json",
245
274
  "Api-Key": this.apiKey
246
- }, r = {
275
+ }, s = {
247
276
  url: t,
248
277
  headers: e
249
278
  };
250
- this.logger.debug(r);
251
- const s = await fetch(t, {
279
+ h.debug(s);
280
+ const r = await fetch(t, {
252
281
  method: "GET",
253
282
  headers: e,
254
283
  credentials: "include",
255
284
  cache: "force-cache"
256
285
  });
257
- if (s.ok) {
258
- const o = await s.json();
259
- 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;
260
289
  }
261
290
  throw new Error("hello iBoot error!");
262
291
  }
@@ -274,153 +303,142 @@ class J {
274
303
  }
275
304
  }
276
305
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
277
- 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 = {
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 = {
279
308
  method: "GET",
280
- url: l,
281
- headers: c
309
+ url: a,
310
+ headers: l
282
311
  };
283
- this.logger.info(i);
284
- try {
285
- const a = await fetch(l, {
286
- method: "GET",
287
- headers: c,
288
- credentials: "include",
289
- cache: "force-cache"
290
- });
291
- if (a.ok) {
292
- const d = await a.json();
293
- return this.logger.debug({ result: d.data }), d;
294
- }
295
- return {
296
- code: a.status,
297
- success: !1,
298
- msg: a.statusText
299
- };
300
- } catch (a) {
301
- return {
302
- code: 500,
303
- success: !1,
304
- msg: a?.toString() ?? "Server exception!"
305
- };
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;
306
322
  }
323
+ const g = {
324
+ code: u.status,
325
+ success: !1,
326
+ msg: u.statusText
327
+ };
328
+ return h.error(g), g;
307
329
  }
308
330
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
309
- async post({ url: t, data: e, token: r }) {
331
+ async post({ url: t, data: e, token: s, cache: r = "default" }) {
310
332
  e = e ? { ...e } : {};
311
- const s = e.buffer ?? void 0, o = e.boundary ?? void 0;
312
- 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 });
314
- s && o && (i["Content-Type"] = `multipart/form-data; boundary=${o}`);
315
- const a = this.getApiUrl(t), d = {
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 = {
316
338
  method: "POST",
317
- url: a,
318
- headers: i
339
+ url: u,
340
+ headers: c,
341
+ params: l
319
342
  };
320
- this.logger.info(d);
321
- try {
322
- const g = s ? `${a}?${l}` : a, u = await fetch(g, {
323
- method: "POST",
324
- headers: i,
325
- body: s || l.toString(),
326
- credentials: "include"
327
- });
328
- if (u.ok) {
329
- const f = await u.json();
330
- return this.logger.debug({ result: f.data }), f;
331
- }
332
- return {
333
- code: u.status,
334
- success: !1,
335
- msg: u.statusText
336
- };
337
- } catch (g) {
338
- return {
339
- code: 500,
340
- success: !1,
341
- msg: g?.toString() ?? "Server exception!"
342
- };
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;
343
354
  }
355
+ const y = {
356
+ code: m.status,
357
+ success: !1,
358
+ msg: m.statusText
359
+ };
360
+ return h.error(y), y;
344
361
  }
345
362
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
346
- async stream({ url: t, data: e, token: r }) {
363
+ async stream({ url: t, data: e, token: s }) {
347
364
  e = e ? { ...e } : {};
348
- const s = e.buffer ?? void 0, o = e.boundary ?? void 0;
349
- 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 });
351
- 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);
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";
369
+ const c = this.getApiUrl(t);
353
370
  try {
354
- const d = s ? `${a}?${l}` : a, g = {
355
- method: "POST",
356
- url: a,
357
- headers: i
371
+ const u = r ? `${c}?${l}` : c, g = {
372
+ method: "STREAM",
373
+ url: c,
374
+ headers: a,
375
+ params: i
358
376
  };
359
- this.logger.info(g);
360
- const u = await fetch(d, {
377
+ h.info(g);
378
+ const d = await fetch(u, {
361
379
  method: "POST",
362
- headers: i,
363
- body: s || l.toString(),
380
+ headers: a,
381
+ body: r || l.toString(),
364
382
  credentials: "include"
365
383
  });
366
- if (!u.ok)
367
- throw new Error(`HTTP error! status: ${u.status}`);
368
- if (!u.body)
384
+ if (!d.ok)
385
+ throw new Error(`HTTP error! status: ${d.status}`);
386
+ if (!d.body)
369
387
  throw new Error("No response body");
370
- return new Response(u.body, {
388
+ return new Response(d.body, {
371
389
  headers: {
372
390
  "Content-Type": "text/event-stream",
373
391
  "Cache-Control": "no-cache",
374
392
  Connection: "keep-alive"
375
393
  }
376
394
  });
377
- } catch (d) {
378
- throw d;
395
+ } catch (u) {
396
+ throw u;
379
397
  }
380
398
  }
381
399
  getUserInfo(t) {
382
- const e = t.get(h.user)?.value;
400
+ const e = t.get(f.user)?.value;
383
401
  return e && e.length > 0 ? JSON.parse(e) : null;
384
402
  }
385
403
  getToken(t) {
386
404
  const e = t.get("user")?.value;
387
- let r = "", s = this.userType;
405
+ let s = "", r = this.userType;
388
406
  if (e && e.length > 0) {
389
407
  const o = JSON.parse(e);
390
- r = o.username, s = o.userType.toString();
408
+ s = o.username, r = o.userType.toString();
391
409
  }
392
410
  return {
393
- username: r,
394
- utype: s,
395
- token: t.get(h.token)?.value ?? ""
411
+ username: s,
412
+ utype: r,
413
+ token: t.get(f.token)?.value ?? ""
396
414
  };
397
415
  }
398
416
  getTokenByCookies = (t) => {
399
417
  const e = t.get("user")?.value;
400
- let r = "", s = this.userType;
418
+ let s = "", r = this.userType;
401
419
  if (e && e.length > 0) {
402
420
  const o = JSON.parse(e);
403
- r = o.username, s = o.userType.toString();
421
+ s = o.username, r = o.userType.toString();
404
422
  }
405
423
  return {
406
- username: r,
407
- utype: s,
408
- token: t.get(h.token)?.value ?? ""
424
+ username: s,
425
+ utype: r,
426
+ token: t.get(f.token)?.value ?? ""
409
427
  };
410
428
  };
411
429
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
412
430
  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 = {
414
- id: r,
415
- 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,
416
434
  username: o,
417
- nickname: c,
435
+ nickname: i,
418
436
  sex: l,
419
- headImg: i,
420
- lastLoginTime: d,
437
+ headImg: a,
438
+ lastLoginTime: u,
421
439
  tokenExpired: g,
422
- deviceId: u,
423
- userType: f,
440
+ deviceId: d,
441
+ userType: m,
424
442
  status: 0,
425
443
  accountType: 0,
426
444
  enabled: !1,
@@ -428,9 +446,9 @@ class J {
428
446
  needToReview: !1,
429
447
  socketOnline: !1,
430
448
  createTime: "",
431
- mustChangePwd: v
449
+ mustChangePwd: y
432
450
  };
433
- return e.cookies.set(h.token, a, {
451
+ return e.cookies.set(f.token, c, {
434
452
  httpOnly: !0,
435
453
  secure: !this._isDebug,
436
454
  // 在生产环境中启用Secure
@@ -438,38 +456,43 @@ class J {
438
456
  // 防止 CSRF 攻击
439
457
  path: "/"
440
458
  // Cookie 的路径
441
- }), e.cookies.set(h.user, JSON.stringify(P)), e;
459
+ }), e.cookies.set(f.user, JSON.stringify(E)), e;
442
460
  }
443
461
  cleanToken(t) {
444
- return t.cookies.set(h.token, "", {
462
+ return t.cookies.set(f.token, "", {
445
463
  maxAge: -1,
446
464
  //设置为 -1表示立即过期
447
465
  httpOnly: !0,
448
466
  path: "/"
449
- }), t.cookies.set(h.user, "", {
467
+ }), t.cookies.set(f.user, "", {
450
468
  maxAge: -1,
451
469
  //设置为 -1表示立即过期
452
470
  httpOnly: !0,
453
471
  path: "/"
454
- }), t.cookies.delete(h.token), t.cookies.delete(h.user), t;
472
+ }), t.cookies.delete(f.token), t.cookies.delete(f.user), t;
455
473
  }
456
474
  }
457
475
  export {
458
- M as ACCOUNT_TYPE_MAP,
459
- _ as CURRENT_WEBSITE_KEY,
460
- U as DEFAULT_LOCALE,
461
- T as DEVICE_ID_KEY,
462
- J as HttpClient,
463
- $ as USER_FORM_MAP,
464
- W as USER_SEX_MAP,
465
- A as USER_TYPE_MAP,
466
- k as get,
467
- H as getHttpClientOpts,
468
- G as getHttpClientOptsByCookie,
469
- j as iGet,
470
- K as iPost,
471
- F as iPostSuccess,
472
- S as post,
473
- 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
474
497
  };
475
498
  //# sourceMappingURL=iboot-http-client.es.js.map