iboot-http-client 1.1.2 → 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.
- package/dist/iboot-http-client.cjs.js +1 -1
- package/dist/iboot-http-client.cjs.js.map +1 -1
- package/dist/iboot-http-client.es.js +78 -81
- package/dist/iboot-http-client.es.js.map +1 -1
- package/dist/iboot-http-client.umd.js +1 -1
- package/dist/iboot-http-client.umd.js.map +1 -1
- package/dist/types/http-client.d.ts +3 -4
- package/dist/types/types/http.d.ts +0 -5
- package/package.json +1 -1
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import
|
|
1
|
+
import C from "pino";
|
|
2
2
|
import R from "pino-pretty";
|
|
3
|
-
import
|
|
4
|
-
import
|
|
3
|
+
import N from "js-md5";
|
|
4
|
+
import m from "crypto-js";
|
|
5
5
|
const H = {
|
|
6
6
|
GENERAL: 0,
|
|
7
7
|
PHONE: 1,
|
|
@@ -22,30 +22,30 @@ const H = {
|
|
|
22
22
|
unknown: 0,
|
|
23
23
|
male: 1,
|
|
24
24
|
female: 2
|
|
25
|
-
},
|
|
25
|
+
}, M = R({
|
|
26
26
|
colorize: !0,
|
|
27
27
|
destination: process.stdout,
|
|
28
28
|
translateTime: "SYS:yyyy-mm-dd HH:MM:ss",
|
|
29
29
|
ignore: "pid,hostname",
|
|
30
30
|
messageFormat: "{msg}",
|
|
31
31
|
singleLine: !0
|
|
32
|
-
}), u =
|
|
32
|
+
}), u = C({
|
|
33
33
|
name: process.env.APP_NAME ?? "iBoot",
|
|
34
|
-
level: process.env.
|
|
34
|
+
level: process.env.APP_ENV === "production" ? "info" : "debug",
|
|
35
35
|
nestedKey: "payload"
|
|
36
|
-
},
|
|
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++)
|
|
40
40
|
r += e.charAt(Math.floor(Math.random() * s));
|
|
41
41
|
return r;
|
|
42
|
-
},
|
|
42
|
+
}, k = (o, t) => {
|
|
43
43
|
const e = {};
|
|
44
44
|
return o.split("&").forEach((r) => {
|
|
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
|
-
},
|
|
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,29 +76,29 @@ const H = {
|
|
|
76
76
|
}
|
|
77
77
|
}
|
|
78
78
|
return t;
|
|
79
|
-
},
|
|
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
|
-
},
|
|
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
|
-
},
|
|
92
|
-
const o = localStorage.getItem(O) ?? "", t = localStorage.getItem(
|
|
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(
|
|
96
|
-
},
|
|
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 =
|
|
101
|
+
const e = v(), s = t?.headers;
|
|
102
102
|
if (s)
|
|
103
103
|
for (const i in s) {
|
|
104
104
|
const c = s[i];
|
|
@@ -123,10 +123,10 @@ const H = {
|
|
|
123
123
|
msg: r.statusText
|
|
124
124
|
};
|
|
125
125
|
return u.error(n), n;
|
|
126
|
-
},
|
|
126
|
+
}, A = async (o, t) => {
|
|
127
127
|
const e = t?.data ?? {}, s = t?.headers;
|
|
128
128
|
let r;
|
|
129
|
-
const n = new Headers(
|
|
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];
|
|
@@ -151,17 +151,17 @@ const H = {
|
|
|
151
151
|
msg: i.statusText
|
|
152
152
|
};
|
|
153
153
|
return u.error(c), c;
|
|
154
|
-
},
|
|
155
|
-
const e = await
|
|
154
|
+
}, Y = async (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 ??
|
|
159
|
+
t.showError(e.msg ?? _);
|
|
160
160
|
return;
|
|
161
161
|
}
|
|
162
|
-
throw Error(e.msg ??
|
|
163
|
-
},
|
|
164
|
-
const e = await
|
|
162
|
+
throw Error(e.msg ?? _);
|
|
163
|
+
}, J = async (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
|
|
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(
|
|
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(
|
|
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,22 +190,19 @@ class q {
|
|
|
190
190
|
lang;
|
|
191
191
|
websiteId;
|
|
192
192
|
websiteNo;
|
|
193
|
-
|
|
193
|
+
isProduction;
|
|
194
194
|
constructor(t) {
|
|
195
|
-
this.
|
|
196
|
-
}
|
|
197
|
-
isDebug() {
|
|
198
|
-
return this._isDebug;
|
|
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
|
-
const e =
|
|
202
|
-
return
|
|
198
|
+
const e = m.enc.Utf8.parse(this.apiKey);
|
|
199
|
+
return m.AES.encrypt(t, e).toString();
|
|
203
200
|
}
|
|
204
201
|
decrypt(t) {
|
|
205
|
-
const e =
|
|
206
|
-
return
|
|
207
|
-
mode:
|
|
208
|
-
}).toString(
|
|
202
|
+
const e = m.enc.Utf8.parse(this.apiKey);
|
|
203
|
+
return m.AES.decrypt(t, e, {
|
|
204
|
+
mode: m.mode.ECB
|
|
205
|
+
}).toString(m.enc.Utf8);
|
|
209
206
|
}
|
|
210
207
|
getDeviceId() {
|
|
211
208
|
return this.deviceId;
|
|
@@ -230,15 +227,15 @@ class q {
|
|
|
230
227
|
w(a) && a.forEach((l, h) => {
|
|
231
228
|
if (l)
|
|
232
229
|
for (const f in l) {
|
|
233
|
-
const
|
|
234
|
-
if (
|
|
235
|
-
const
|
|
236
|
-
e.push(s +
|
|
230
|
+
const d = l[f];
|
|
231
|
+
if (d && d.toString().length > 0) {
|
|
232
|
+
const p = `[${n.toString()}].${c + `[${h.toString()}].${f}`}=${T(d)}`;
|
|
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}=${
|
|
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}=${
|
|
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 + "=" +
|
|
254
|
+
r != null && r != "" && r.length != 0 && e.push(s + "=" + T(r));
|
|
258
255
|
}
|
|
259
256
|
return e && e.join("&");
|
|
260
257
|
}
|
|
@@ -268,7 +265,7 @@ 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 =
|
|
268
|
+
const n = r.join("&"), i = N.md5((n + e).toLocaleUpperCase()), c = {
|
|
272
269
|
params: n,
|
|
273
270
|
md5: i
|
|
274
271
|
};
|
|
@@ -277,7 +274,7 @@ class q {
|
|
|
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 =
|
|
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 = {
|
|
@@ -289,7 +286,7 @@ class q {
|
|
|
289
286
|
"Api-Key": this.apiKey
|
|
290
287
|
};
|
|
291
288
|
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));
|
|
292
|
-
const r =
|
|
289
|
+
const r = k(t), n = await this.sign(r);
|
|
293
290
|
return s.Sign = n, s;
|
|
294
291
|
}
|
|
295
292
|
async helloIboot() {
|
|
@@ -343,8 +340,8 @@ class q {
|
|
|
343
340
|
cache: r
|
|
344
341
|
});
|
|
345
342
|
if (h.ok) {
|
|
346
|
-
const
|
|
347
|
-
return u.debug(
|
|
343
|
+
const d = await h.json();
|
|
344
|
+
return u.debug(d, "GET_RESULT"), d;
|
|
348
345
|
}
|
|
349
346
|
const f = {
|
|
350
347
|
code: h.status,
|
|
@@ -366,21 +363,21 @@ class q {
|
|
|
366
363
|
params: c
|
|
367
364
|
};
|
|
368
365
|
u.info(f, "POST");
|
|
369
|
-
const
|
|
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(),
|
|
373
370
|
credentials: "include",
|
|
374
371
|
cache: r
|
|
375
372
|
});
|
|
376
|
-
if (
|
|
377
|
-
const E = await
|
|
373
|
+
if (p.ok) {
|
|
374
|
+
const E = await p.json();
|
|
378
375
|
return u.debug(E, "POST_RESULT"), E;
|
|
379
376
|
}
|
|
380
377
|
const y = {
|
|
381
|
-
code:
|
|
378
|
+
code: p.status,
|
|
382
379
|
success: !1,
|
|
383
|
-
msg:
|
|
380
|
+
msg: p.statusText
|
|
384
381
|
};
|
|
385
382
|
return u.error(y), y;
|
|
386
383
|
}
|
|
@@ -399,17 +396,17 @@ class q {
|
|
|
399
396
|
params: i
|
|
400
397
|
};
|
|
401
398
|
u.info(f, "STREAM");
|
|
402
|
-
const
|
|
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 (!
|
|
409
|
-
throw new Error(`HTTP error! status: ${
|
|
410
|
-
if (!
|
|
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(
|
|
409
|
+
return new Response(d.body, {
|
|
413
410
|
headers: {
|
|
414
411
|
"Content-Type": "text/event-stream",
|
|
415
412
|
"Cache-Control": "no-cache",
|
|
@@ -421,7 +418,7 @@ class q {
|
|
|
421
418
|
}
|
|
422
419
|
}
|
|
423
420
|
getUserInfo(t) {
|
|
424
|
-
const e = t.get(
|
|
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(
|
|
434
|
+
token: t.get(g.token)?.value ?? ""
|
|
438
435
|
};
|
|
439
436
|
}
|
|
440
437
|
getTokenByCookies = (t) => {
|
|
@@ -447,12 +444,12 @@ class q {
|
|
|
447
444
|
return {
|
|
448
445
|
username: s,
|
|
449
446
|
utype: r,
|
|
450
|
-
token: t.get(
|
|
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: h, tokenExpired: f, deviceId:
|
|
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,
|
|
@@ -461,8 +458,8 @@ class q {
|
|
|
461
458
|
headImg: a,
|
|
462
459
|
lastLoginTime: h,
|
|
463
460
|
tokenExpired: f,
|
|
464
|
-
deviceId:
|
|
465
|
-
userType:
|
|
461
|
+
deviceId: d,
|
|
462
|
+
userType: p,
|
|
466
463
|
status: 0,
|
|
467
464
|
accountType: 0,
|
|
468
465
|
enabled: !1,
|
|
@@ -472,52 +469,52 @@ class q {
|
|
|
472
469
|
createTime: "",
|
|
473
470
|
mustChangePwd: y
|
|
474
471
|
};
|
|
475
|
-
return e.cookies.set(
|
|
472
|
+
return e.cookies.set(g.token, l, {
|
|
476
473
|
httpOnly: !0,
|
|
477
|
-
secure:
|
|
474
|
+
secure: this.isProduction,
|
|
478
475
|
// 在生产环境中启用Secure
|
|
479
476
|
sameSite: "strict",
|
|
480
477
|
// 防止 CSRF 攻击
|
|
481
478
|
path: "/"
|
|
482
479
|
// Cookie 的路径
|
|
483
|
-
}), e.cookies.set(
|
|
480
|
+
}), e.cookies.set(g.user, JSON.stringify(E)), e;
|
|
484
481
|
}
|
|
485
482
|
cleanToken(t) {
|
|
486
|
-
return t.cookies.set(
|
|
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(
|
|
488
|
+
}), t.cookies.set(g.user, "", {
|
|
492
489
|
maxAge: -1,
|
|
493
490
|
//设置为 -1表示立即过期
|
|
494
491
|
httpOnly: !0,
|
|
495
492
|
path: "/"
|
|
496
|
-
}), t.cookies.delete(
|
|
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
|
-
|
|
502
|
-
|
|
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
|
-
|
|
506
|
+
B as get,
|
|
510
507
|
X as getHttpClientOpts,
|
|
511
508
|
z as getHttpClientOptsByCookie,
|
|
512
|
-
|
|
513
|
-
|
|
509
|
+
Y as iGet,
|
|
510
|
+
J as iPost,
|
|
514
511
|
V as iPostSuccess,
|
|
515
512
|
w as isArray,
|
|
516
513
|
u as logger,
|
|
517
|
-
|
|
518
|
-
|
|
514
|
+
A as post,
|
|
515
|
+
S as randomString,
|
|
519
516
|
G as setDefaultRequestHeader,
|
|
520
|
-
|
|
521
|
-
|
|
517
|
+
T as urlEncode,
|
|
518
|
+
k as urlParamToJson
|
|
522
519
|
};
|
|
523
520
|
//# sourceMappingURL=iboot-http-client.es.js.map
|