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.
- 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 +101 -93
- 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 +1 -1
- package/dist/types/types/http.d.ts +2 -0
- package/package.json +1 -1
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import C from "js-md5";
|
|
2
|
-
import
|
|
2
|
+
import m from "crypto-js";
|
|
3
3
|
import N from "pino";
|
|
4
|
-
const
|
|
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
|
-
},
|
|
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
|
-
},
|
|
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
|
-
},
|
|
49
|
-
const n = localStorage.getItem(
|
|
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(
|
|
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 =
|
|
58
|
+
const e = S(), r = t?.heads;
|
|
59
59
|
if (r)
|
|
60
60
|
for (const o in r) {
|
|
61
|
-
const
|
|
62
|
-
|
|
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
|
-
},
|
|
74
|
+
}, O = async (n, t) => {
|
|
75
75
|
const e = t?.data ?? {}, r = t?.heads;
|
|
76
76
|
let s;
|
|
77
|
-
const o = new Headers(
|
|
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
|
|
83
|
+
const a = await fetch(n, {
|
|
84
84
|
method: "POST",
|
|
85
85
|
headers: o,
|
|
86
86
|
body: s
|
|
87
87
|
});
|
|
88
|
-
return
|
|
89
|
-
code:
|
|
88
|
+
return a.ok ? await a.json() : {
|
|
89
|
+
code: a.status,
|
|
90
90
|
success: !1,
|
|
91
|
-
msg:
|
|
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 ??
|
|
98
|
+
t.showError(e.msg ?? T);
|
|
99
99
|
return;
|
|
100
100
|
}
|
|
101
|
-
throw Error(e.msg ??
|
|
101
|
+
throw Error(e.msg ?? T);
|
|
102
102
|
}, K = async (n, t) => {
|
|
103
|
-
const e = await
|
|
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 ??
|
|
107
|
+
t.showError(e.msg ?? E);
|
|
108
108
|
return;
|
|
109
109
|
}
|
|
110
|
-
throw Error(e.msg ??
|
|
110
|
+
throw Error(e.msg ?? E);
|
|
111
111
|
}, F = async (n, t) => {
|
|
112
|
-
const e = await
|
|
113
|
-
return e.success ? (t?.showSuccess && t.showSuccess(e.msg ?? "SUCCESS"), !0) : (t?.showError && t.showError(e.msg ??
|
|
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 =
|
|
136
|
-
name:
|
|
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 =
|
|
146
|
-
return
|
|
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 =
|
|
150
|
-
return
|
|
151
|
-
mode:
|
|
152
|
-
}).toString(
|
|
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 (
|
|
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
|
|
170
|
-
for (const l in
|
|
171
|
-
const i =
|
|
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
|
-
|
|
175
|
-
if (
|
|
176
|
-
for (const
|
|
177
|
-
const u =
|
|
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 + `[${
|
|
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
|
|
186
|
-
e.push(r +
|
|
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
|
|
194
|
-
if (
|
|
195
|
-
const l = `.${o}=${
|
|
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 + "=" +
|
|
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,
|
|
214
|
+
r.sort((i, c) => i.toLowerCase().localeCompare(c.toLowerCase()));
|
|
210
215
|
const s = [];
|
|
211
216
|
r.forEach((i) => {
|
|
212
|
-
let
|
|
213
|
-
typeof
|
|
217
|
+
let c = t[i];
|
|
218
|
+
typeof c == "object" && (c = JSON.stringify(c)), s.push(i + "=" + c);
|
|
214
219
|
});
|
|
215
|
-
const o = s.join("&"),
|
|
220
|
+
const o = s.join("&"), a = C.md5((o + e).toLocaleUpperCase()), l = {
|
|
221
|
+
name: "sort",
|
|
216
222
|
params: o,
|
|
217
|
-
md5:
|
|
223
|
+
md5: a
|
|
218
224
|
};
|
|
219
|
-
return this.logger.debug(l),
|
|
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.
|
|
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),
|
|
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:
|
|
287
|
+
headers: a
|
|
282
288
|
};
|
|
283
289
|
this.logger.info(i);
|
|
284
290
|
try {
|
|
285
|
-
const
|
|
291
|
+
const c = await fetch(l, {
|
|
286
292
|
method: "GET",
|
|
287
|
-
headers:
|
|
293
|
+
headers: a,
|
|
288
294
|
credentials: "include",
|
|
289
295
|
cache: "force-cache"
|
|
290
296
|
});
|
|
291
|
-
if (
|
|
292
|
-
const
|
|
293
|
-
return this.logger.debug({
|
|
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:
|
|
302
|
+
code: c.status,
|
|
297
303
|
success: !1,
|
|
298
|
-
msg:
|
|
304
|
+
msg: c.statusText
|
|
299
305
|
};
|
|
300
|
-
} catch (
|
|
306
|
+
} catch (c) {
|
|
301
307
|
return {
|
|
302
308
|
code: 500,
|
|
303
309
|
success: !1,
|
|
304
|
-
msg:
|
|
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
|
|
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
|
|
321
|
+
const c = this.getApiUrl(t), g = {
|
|
316
322
|
method: "POST",
|
|
317
|
-
url:
|
|
318
|
-
headers: i
|
|
323
|
+
url: c,
|
|
324
|
+
headers: i,
|
|
325
|
+
params: a
|
|
319
326
|
};
|
|
320
|
-
this.logger.info(
|
|
327
|
+
this.logger.info(g);
|
|
321
328
|
try {
|
|
322
|
-
const
|
|
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({
|
|
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 (
|
|
344
|
+
} catch (d) {
|
|
338
345
|
return {
|
|
339
346
|
code: 500,
|
|
340
347
|
success: !1,
|
|
341
|
-
msg:
|
|
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
|
|
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
|
|
359
|
+
const c = this.getApiUrl(t);
|
|
353
360
|
try {
|
|
354
|
-
const
|
|
355
|
-
method: "
|
|
356
|
-
url:
|
|
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(
|
|
360
|
-
const u = await fetch(
|
|
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 (
|
|
378
|
-
throw
|
|
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:
|
|
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:
|
|
425
|
+
nickname: a,
|
|
418
426
|
sex: l,
|
|
419
427
|
headImg: i,
|
|
420
|
-
lastLoginTime:
|
|
421
|
-
tokenExpired:
|
|
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,
|
|
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
|
-
|
|
466
|
+
L as ACCOUNT_TYPE_MAP,
|
|
459
467
|
_ as CURRENT_WEBSITE_KEY,
|
|
460
468
|
U as DEFAULT_LOCALE,
|
|
461
|
-
|
|
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
|
-
|
|
480
|
+
O as post,
|
|
473
481
|
x as setDefaultRequestHeader
|
|
474
482
|
};
|
|
475
483
|
//# sourceMappingURL=iboot-http-client.es.js.map
|