abbot-http-client 0.0.60 → 0.0.62
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/index.cjs +128 -72
- package/dist/index.d.cts +12 -4
- package/dist/index.d.ts +12 -4
- package/dist/index.js +128 -72
- package/package.json +2 -1
package/dist/index.cjs
CHANGED
|
@@ -112,8 +112,31 @@ var AxiosBase = class {
|
|
|
112
112
|
}
|
|
113
113
|
};
|
|
114
114
|
|
|
115
|
+
// src/encryption/es.ts
|
|
116
|
+
var ES = class {
|
|
117
|
+
static e(value) {
|
|
118
|
+
return Array.from(value).map((c) => c.charCodeAt(0)).reverse().map((e, i) => i % 2 === 0 ? e + 1 : e - 1).map((e, i) => i % 3 === 0 ? e * 2 : e);
|
|
119
|
+
}
|
|
120
|
+
static d(value) {
|
|
121
|
+
const result = value.map((e, i) => i % 3 === 0 ? Math.round(e / 2) : e).map((e, i) => i % 2 === 0 ? e - 1 : e + 1);
|
|
122
|
+
return String.fromCharCode.apply(null, result.reverse());
|
|
123
|
+
}
|
|
124
|
+
static a2b(value) {
|
|
125
|
+
const charactors = "abcdefghijklmnopqrstuvwxyz";
|
|
126
|
+
const randomChar = (max) => Math.floor(Math.random() * max);
|
|
127
|
+
const result = value.map(
|
|
128
|
+
(e, i) => `${String(e)}${i === value.length - 1 ? "" : charactors[randomChar(26)]}`
|
|
129
|
+
).join("");
|
|
130
|
+
return result;
|
|
131
|
+
}
|
|
132
|
+
static b2a(value) {
|
|
133
|
+
if (!value) return [];
|
|
134
|
+
const result = value.split(/[a-z]/);
|
|
135
|
+
return result.map((e) => Number(e));
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
|
|
115
139
|
// src/encryption/cryp.ts
|
|
116
|
-
var crypto = __toESM(require("crypto"), 1);
|
|
117
140
|
var Cryp = class _Cryp {
|
|
118
141
|
static TAG_SIZE = 16;
|
|
119
142
|
// bytes
|
|
@@ -123,51 +146,86 @@ var Cryp = class _Cryp {
|
|
|
123
146
|
static ALGO = "aes-256-gcm";
|
|
124
147
|
key;
|
|
125
148
|
iv;
|
|
126
|
-
constructor(
|
|
127
|
-
this.key = key;
|
|
128
|
-
this.iv = iv ?? _Cryp.genIv();
|
|
149
|
+
constructor(cryptProps, apiKey) {
|
|
150
|
+
this.key = cryptProps.key ? cryptProps.key : ES.d(ES.b2a(apiKey));
|
|
151
|
+
this.iv = cryptProps.iv ?? _Cryp.genIv();
|
|
129
152
|
}
|
|
130
|
-
encrypt(text) {
|
|
153
|
+
async encrypt(text) {
|
|
131
154
|
if (!text) return "";
|
|
132
|
-
const
|
|
133
|
-
const
|
|
134
|
-
const
|
|
135
|
-
const
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
155
|
+
const keyBytes = _Cryp.txtToBytes(this.key);
|
|
156
|
+
const ivBytes = _Cryp.txtToBytes(this.iv);
|
|
157
|
+
const textBytes = _Cryp.txtToBytes(text);
|
|
158
|
+
const cryptoKey = await window.crypto.subtle.importKey(
|
|
159
|
+
"raw",
|
|
160
|
+
keyBytes,
|
|
161
|
+
{ name: "AES-GCM" },
|
|
162
|
+
false,
|
|
163
|
+
["encrypt"]
|
|
164
|
+
);
|
|
165
|
+
const encrypted = new Uint8Array(
|
|
166
|
+
await window.crypto.subtle.encrypt(
|
|
167
|
+
{
|
|
168
|
+
name: "AES-GCM",
|
|
169
|
+
iv: ivBytes,
|
|
170
|
+
tagLength: _Cryp.TAG_SIZE * 8
|
|
171
|
+
// bits
|
|
172
|
+
},
|
|
173
|
+
cryptoKey,
|
|
174
|
+
textBytes
|
|
175
|
+
)
|
|
176
|
+
);
|
|
177
|
+
return _Cryp.bytesToBase64(encrypted);
|
|
141
178
|
}
|
|
142
|
-
decryptText(text) {
|
|
179
|
+
async decryptText(text) {
|
|
143
180
|
if (!text) return "";
|
|
144
|
-
const
|
|
145
|
-
const
|
|
146
|
-
const
|
|
147
|
-
const
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
181
|
+
const keyBytes = _Cryp.txtToBytes(this.key);
|
|
182
|
+
const ivBytes = _Cryp.txtToBytes(this.iv);
|
|
183
|
+
const encryptedBytes = _Cryp.base64ToBytes(text);
|
|
184
|
+
const cryptoKey = await crypto.subtle.importKey(
|
|
185
|
+
"raw",
|
|
186
|
+
keyBytes,
|
|
187
|
+
{ name: "AES-GCM" },
|
|
188
|
+
false,
|
|
189
|
+
["decrypt"]
|
|
190
|
+
);
|
|
191
|
+
const decrypted = new Uint8Array(
|
|
192
|
+
await crypto.subtle.decrypt(
|
|
193
|
+
{
|
|
194
|
+
name: "AES-GCM",
|
|
195
|
+
iv: ivBytes,
|
|
196
|
+
tagLength: _Cryp.TAG_SIZE * 8
|
|
197
|
+
},
|
|
198
|
+
cryptoKey,
|
|
199
|
+
encryptedBytes
|
|
200
|
+
)
|
|
201
|
+
);
|
|
202
|
+
return new TextDecoder().decode(decrypted);
|
|
156
203
|
}
|
|
157
|
-
decrypt(text) {
|
|
204
|
+
async decrypt(text) {
|
|
158
205
|
if (!text) return null;
|
|
159
|
-
const
|
|
160
|
-
const
|
|
161
|
-
const
|
|
162
|
-
const
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
206
|
+
const keyBytes = _Cryp.txtToBytes(this.key);
|
|
207
|
+
const ivBytes = _Cryp.txtToBytes(this.iv);
|
|
208
|
+
const encryptedBytes = _Cryp.base64ToBytes(text);
|
|
209
|
+
const cryptoKey = await window.crypto.subtle.importKey(
|
|
210
|
+
"raw",
|
|
211
|
+
keyBytes,
|
|
212
|
+
{ name: "AES-GCM" },
|
|
213
|
+
false,
|
|
214
|
+
["decrypt"]
|
|
215
|
+
);
|
|
216
|
+
const decrypted = new Uint8Array(
|
|
217
|
+
await crypto.subtle.decrypt(
|
|
218
|
+
{
|
|
219
|
+
name: "AES-GCM",
|
|
220
|
+
iv: ivBytes,
|
|
221
|
+
tagLength: _Cryp.TAG_SIZE * 8
|
|
222
|
+
},
|
|
223
|
+
cryptoKey,
|
|
224
|
+
encryptedBytes
|
|
225
|
+
)
|
|
226
|
+
);
|
|
227
|
+
const result = new TextDecoder().decode(decrypted);
|
|
228
|
+
return JSON.parse(result);
|
|
171
229
|
}
|
|
172
230
|
static genIv() {
|
|
173
231
|
let result = "";
|
|
@@ -177,29 +235,21 @@ var Cryp = class _Cryp {
|
|
|
177
235
|
}
|
|
178
236
|
return result;
|
|
179
237
|
}
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
// src/encryption/es.ts
|
|
183
|
-
var ES = class {
|
|
184
|
-
static e(value) {
|
|
185
|
-
return Array.from(value).map((c) => c.charCodeAt(0)).reverse().map((e, i) => i % 2 === 0 ? e + 1 : e - 1).map((e, i) => i % 3 === 0 ? e * 2 : e);
|
|
238
|
+
static txtToBytes(txt) {
|
|
239
|
+
return new TextEncoder().encode(txt);
|
|
186
240
|
}
|
|
187
|
-
static
|
|
188
|
-
|
|
189
|
-
|
|
241
|
+
static bytesToBase64(bytes) {
|
|
242
|
+
let binary = "";
|
|
243
|
+
bytes.forEach((b) => binary += String.fromCharCode(b));
|
|
244
|
+
return btoa(binary);
|
|
190
245
|
}
|
|
191
|
-
static
|
|
192
|
-
const
|
|
193
|
-
const
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
return
|
|
198
|
-
}
|
|
199
|
-
static b2a(value) {
|
|
200
|
-
if (!value) return [];
|
|
201
|
-
const result = value.split(/[a-z]/);
|
|
202
|
-
return result.map((e) => Number(e));
|
|
246
|
+
static base64ToBytes(base64) {
|
|
247
|
+
const binary = atob(base64);
|
|
248
|
+
const bytes = new Uint8Array(binary.length);
|
|
249
|
+
for (let i = 0; i < binary.length; i++) {
|
|
250
|
+
bytes[i] = binary.charCodeAt(i);
|
|
251
|
+
}
|
|
252
|
+
return bytes;
|
|
203
253
|
}
|
|
204
254
|
};
|
|
205
255
|
|
|
@@ -278,11 +328,11 @@ var AbbotHttp = class extends AxiosBase {
|
|
|
278
328
|
}
|
|
279
329
|
createAxiosInstance(option) {
|
|
280
330
|
const axios2 = this.axiosInstance();
|
|
281
|
-
const { cryp, iv } = this.prepareRequestConfig();
|
|
331
|
+
const { cryp, iv } = this.prepareRequestConfig(option);
|
|
282
332
|
axios2.cryp = cryp;
|
|
283
333
|
axios2.log = new AbbLog(cryp);
|
|
284
334
|
axios2.interceptors.request.use(
|
|
285
|
-
(config) => {
|
|
335
|
+
async (config) => {
|
|
286
336
|
const req = { ...config };
|
|
287
337
|
req.responseType = option?.responseType ?? "json";
|
|
288
338
|
if (this.config.devMode) {
|
|
@@ -294,14 +344,14 @@ var AbbotHttp = class extends AxiosBase {
|
|
|
294
344
|
if (req.data) {
|
|
295
345
|
if (req.data instanceof FormData) {
|
|
296
346
|
if (req.data.get("param")) {
|
|
297
|
-
|
|
298
|
-
"param"
|
|
299
|
-
cryp.encrypt(req.data.get("param")?.toString())
|
|
347
|
+
const param = await cryp.encrypt(
|
|
348
|
+
req.data.get("param")?.toString()
|
|
300
349
|
);
|
|
350
|
+
req.data.set("param", param);
|
|
301
351
|
}
|
|
302
352
|
} else {
|
|
303
353
|
const payload = {};
|
|
304
|
-
payload.param = cryp.encrypt(JSON.stringify(req.data));
|
|
354
|
+
payload.param = await cryp.encrypt(JSON.stringify(req.data));
|
|
305
355
|
const body = payload ? new URLSearchParams(payload).toString() : "";
|
|
306
356
|
req.data = body;
|
|
307
357
|
req.headers.setContentType(
|
|
@@ -328,12 +378,12 @@ var AbbotHttp = class extends AxiosBase {
|
|
|
328
378
|
if (option?.isDownload) {
|
|
329
379
|
const contentTypes = ["application/json", "text/plain"];
|
|
330
380
|
if (contentTypes.includes(contentType) && response.data instanceof Blob) {
|
|
331
|
-
res.data = cryp.decrypt(await response.data.text());
|
|
381
|
+
res.data = await cryp.decrypt(await response.data.text());
|
|
332
382
|
} else {
|
|
333
383
|
res.data = response.data;
|
|
334
384
|
}
|
|
335
385
|
} else {
|
|
336
|
-
res.data = cryp.decrypt(response.data);
|
|
386
|
+
res.data = await cryp.decrypt(response.data);
|
|
337
387
|
}
|
|
338
388
|
if (this.config.devMode) axios2.log.logResponse(res.data);
|
|
339
389
|
return res;
|
|
@@ -347,9 +397,15 @@ var AbbotHttp = class extends AxiosBase {
|
|
|
347
397
|
);
|
|
348
398
|
return axios2;
|
|
349
399
|
}
|
|
350
|
-
prepareRequestConfig() {
|
|
351
|
-
const iv =
|
|
352
|
-
const cryp = new Cryp(
|
|
400
|
+
prepareRequestConfig(option) {
|
|
401
|
+
const iv = Cryp.genIv();
|
|
402
|
+
const cryp = new Cryp(
|
|
403
|
+
{
|
|
404
|
+
iv,
|
|
405
|
+
key: option?.isInit ? null : this.config.app.encKey
|
|
406
|
+
},
|
|
407
|
+
this.config.app.apiKey
|
|
408
|
+
);
|
|
353
409
|
const form = new URLSearchParams();
|
|
354
410
|
return { cryp, iv, form };
|
|
355
411
|
}
|
package/dist/index.d.cts
CHANGED
|
@@ -7,6 +7,7 @@ interface AbbotConfig {
|
|
|
7
7
|
headers: {
|
|
8
8
|
accept: string;
|
|
9
9
|
lang: string;
|
|
10
|
+
app_version?: string;
|
|
10
11
|
post: {
|
|
11
12
|
contentType: string;
|
|
12
13
|
};
|
|
@@ -37,14 +38,21 @@ declare class Cryp {
|
|
|
37
38
|
private static readonly ALGO;
|
|
38
39
|
key: string;
|
|
39
40
|
iv: string;
|
|
40
|
-
constructor(
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
41
|
+
constructor(cryptProps: Partial<{
|
|
42
|
+
iv: string;
|
|
43
|
+
key: string | null;
|
|
44
|
+
}>, apiKey: string);
|
|
45
|
+
encrypt(text?: string): Promise<string>;
|
|
46
|
+
decryptText(text?: string): Promise<string>;
|
|
47
|
+
decrypt<T>(text?: string): Promise<T | null>;
|
|
44
48
|
static genIv(): string;
|
|
49
|
+
private static txtToBytes;
|
|
50
|
+
private static bytesToBase64;
|
|
51
|
+
private static base64ToBytes;
|
|
45
52
|
}
|
|
46
53
|
|
|
47
54
|
interface MethodOption {
|
|
55
|
+
isInit: boolean;
|
|
48
56
|
contentType: string;
|
|
49
57
|
responseType: ResponseType;
|
|
50
58
|
isDownload: boolean;
|
package/dist/index.d.ts
CHANGED
|
@@ -7,6 +7,7 @@ interface AbbotConfig {
|
|
|
7
7
|
headers: {
|
|
8
8
|
accept: string;
|
|
9
9
|
lang: string;
|
|
10
|
+
app_version?: string;
|
|
10
11
|
post: {
|
|
11
12
|
contentType: string;
|
|
12
13
|
};
|
|
@@ -37,14 +38,21 @@ declare class Cryp {
|
|
|
37
38
|
private static readonly ALGO;
|
|
38
39
|
key: string;
|
|
39
40
|
iv: string;
|
|
40
|
-
constructor(
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
41
|
+
constructor(cryptProps: Partial<{
|
|
42
|
+
iv: string;
|
|
43
|
+
key: string | null;
|
|
44
|
+
}>, apiKey: string);
|
|
45
|
+
encrypt(text?: string): Promise<string>;
|
|
46
|
+
decryptText(text?: string): Promise<string>;
|
|
47
|
+
decrypt<T>(text?: string): Promise<T | null>;
|
|
44
48
|
static genIv(): string;
|
|
49
|
+
private static txtToBytes;
|
|
50
|
+
private static bytesToBase64;
|
|
51
|
+
private static base64ToBytes;
|
|
45
52
|
}
|
|
46
53
|
|
|
47
54
|
interface MethodOption {
|
|
55
|
+
isInit: boolean;
|
|
48
56
|
contentType: string;
|
|
49
57
|
responseType: ResponseType;
|
|
50
58
|
isDownload: boolean;
|
package/dist/index.js
CHANGED
|
@@ -65,8 +65,31 @@ var AxiosBase = class {
|
|
|
65
65
|
}
|
|
66
66
|
};
|
|
67
67
|
|
|
68
|
+
// src/encryption/es.ts
|
|
69
|
+
var ES = class {
|
|
70
|
+
static e(value) {
|
|
71
|
+
return Array.from(value).map((c) => c.charCodeAt(0)).reverse().map((e, i) => i % 2 === 0 ? e + 1 : e - 1).map((e, i) => i % 3 === 0 ? e * 2 : e);
|
|
72
|
+
}
|
|
73
|
+
static d(value) {
|
|
74
|
+
const result = value.map((e, i) => i % 3 === 0 ? Math.round(e / 2) : e).map((e, i) => i % 2 === 0 ? e - 1 : e + 1);
|
|
75
|
+
return String.fromCharCode.apply(null, result.reverse());
|
|
76
|
+
}
|
|
77
|
+
static a2b(value) {
|
|
78
|
+
const charactors = "abcdefghijklmnopqrstuvwxyz";
|
|
79
|
+
const randomChar = (max) => Math.floor(Math.random() * max);
|
|
80
|
+
const result = value.map(
|
|
81
|
+
(e, i) => `${String(e)}${i === value.length - 1 ? "" : charactors[randomChar(26)]}`
|
|
82
|
+
).join("");
|
|
83
|
+
return result;
|
|
84
|
+
}
|
|
85
|
+
static b2a(value) {
|
|
86
|
+
if (!value) return [];
|
|
87
|
+
const result = value.split(/[a-z]/);
|
|
88
|
+
return result.map((e) => Number(e));
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
|
|
68
92
|
// src/encryption/cryp.ts
|
|
69
|
-
import * as crypto from "crypto";
|
|
70
93
|
var Cryp = class _Cryp {
|
|
71
94
|
static TAG_SIZE = 16;
|
|
72
95
|
// bytes
|
|
@@ -76,51 +99,86 @@ var Cryp = class _Cryp {
|
|
|
76
99
|
static ALGO = "aes-256-gcm";
|
|
77
100
|
key;
|
|
78
101
|
iv;
|
|
79
|
-
constructor(
|
|
80
|
-
this.key = key;
|
|
81
|
-
this.iv = iv ?? _Cryp.genIv();
|
|
102
|
+
constructor(cryptProps, apiKey) {
|
|
103
|
+
this.key = cryptProps.key ? cryptProps.key : ES.d(ES.b2a(apiKey));
|
|
104
|
+
this.iv = cryptProps.iv ?? _Cryp.genIv();
|
|
82
105
|
}
|
|
83
|
-
encrypt(text) {
|
|
106
|
+
async encrypt(text) {
|
|
84
107
|
if (!text) return "";
|
|
85
|
-
const
|
|
86
|
-
const
|
|
87
|
-
const
|
|
88
|
-
const
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
108
|
+
const keyBytes = _Cryp.txtToBytes(this.key);
|
|
109
|
+
const ivBytes = _Cryp.txtToBytes(this.iv);
|
|
110
|
+
const textBytes = _Cryp.txtToBytes(text);
|
|
111
|
+
const cryptoKey = await window.crypto.subtle.importKey(
|
|
112
|
+
"raw",
|
|
113
|
+
keyBytes,
|
|
114
|
+
{ name: "AES-GCM" },
|
|
115
|
+
false,
|
|
116
|
+
["encrypt"]
|
|
117
|
+
);
|
|
118
|
+
const encrypted = new Uint8Array(
|
|
119
|
+
await window.crypto.subtle.encrypt(
|
|
120
|
+
{
|
|
121
|
+
name: "AES-GCM",
|
|
122
|
+
iv: ivBytes,
|
|
123
|
+
tagLength: _Cryp.TAG_SIZE * 8
|
|
124
|
+
// bits
|
|
125
|
+
},
|
|
126
|
+
cryptoKey,
|
|
127
|
+
textBytes
|
|
128
|
+
)
|
|
129
|
+
);
|
|
130
|
+
return _Cryp.bytesToBase64(encrypted);
|
|
94
131
|
}
|
|
95
|
-
decryptText(text) {
|
|
132
|
+
async decryptText(text) {
|
|
96
133
|
if (!text) return "";
|
|
97
|
-
const
|
|
98
|
-
const
|
|
99
|
-
const
|
|
100
|
-
const
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
134
|
+
const keyBytes = _Cryp.txtToBytes(this.key);
|
|
135
|
+
const ivBytes = _Cryp.txtToBytes(this.iv);
|
|
136
|
+
const encryptedBytes = _Cryp.base64ToBytes(text);
|
|
137
|
+
const cryptoKey = await crypto.subtle.importKey(
|
|
138
|
+
"raw",
|
|
139
|
+
keyBytes,
|
|
140
|
+
{ name: "AES-GCM" },
|
|
141
|
+
false,
|
|
142
|
+
["decrypt"]
|
|
143
|
+
);
|
|
144
|
+
const decrypted = new Uint8Array(
|
|
145
|
+
await crypto.subtle.decrypt(
|
|
146
|
+
{
|
|
147
|
+
name: "AES-GCM",
|
|
148
|
+
iv: ivBytes,
|
|
149
|
+
tagLength: _Cryp.TAG_SIZE * 8
|
|
150
|
+
},
|
|
151
|
+
cryptoKey,
|
|
152
|
+
encryptedBytes
|
|
153
|
+
)
|
|
154
|
+
);
|
|
155
|
+
return new TextDecoder().decode(decrypted);
|
|
109
156
|
}
|
|
110
|
-
decrypt(text) {
|
|
157
|
+
async decrypt(text) {
|
|
111
158
|
if (!text) return null;
|
|
112
|
-
const
|
|
113
|
-
const
|
|
114
|
-
const
|
|
115
|
-
const
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
159
|
+
const keyBytes = _Cryp.txtToBytes(this.key);
|
|
160
|
+
const ivBytes = _Cryp.txtToBytes(this.iv);
|
|
161
|
+
const encryptedBytes = _Cryp.base64ToBytes(text);
|
|
162
|
+
const cryptoKey = await window.crypto.subtle.importKey(
|
|
163
|
+
"raw",
|
|
164
|
+
keyBytes,
|
|
165
|
+
{ name: "AES-GCM" },
|
|
166
|
+
false,
|
|
167
|
+
["decrypt"]
|
|
168
|
+
);
|
|
169
|
+
const decrypted = new Uint8Array(
|
|
170
|
+
await crypto.subtle.decrypt(
|
|
171
|
+
{
|
|
172
|
+
name: "AES-GCM",
|
|
173
|
+
iv: ivBytes,
|
|
174
|
+
tagLength: _Cryp.TAG_SIZE * 8
|
|
175
|
+
},
|
|
176
|
+
cryptoKey,
|
|
177
|
+
encryptedBytes
|
|
178
|
+
)
|
|
179
|
+
);
|
|
180
|
+
const result = new TextDecoder().decode(decrypted);
|
|
181
|
+
return JSON.parse(result);
|
|
124
182
|
}
|
|
125
183
|
static genIv() {
|
|
126
184
|
let result = "";
|
|
@@ -130,29 +188,21 @@ var Cryp = class _Cryp {
|
|
|
130
188
|
}
|
|
131
189
|
return result;
|
|
132
190
|
}
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
// src/encryption/es.ts
|
|
136
|
-
var ES = class {
|
|
137
|
-
static e(value) {
|
|
138
|
-
return Array.from(value).map((c) => c.charCodeAt(0)).reverse().map((e, i) => i % 2 === 0 ? e + 1 : e - 1).map((e, i) => i % 3 === 0 ? e * 2 : e);
|
|
191
|
+
static txtToBytes(txt) {
|
|
192
|
+
return new TextEncoder().encode(txt);
|
|
139
193
|
}
|
|
140
|
-
static
|
|
141
|
-
|
|
142
|
-
|
|
194
|
+
static bytesToBase64(bytes) {
|
|
195
|
+
let binary = "";
|
|
196
|
+
bytes.forEach((b) => binary += String.fromCharCode(b));
|
|
197
|
+
return btoa(binary);
|
|
143
198
|
}
|
|
144
|
-
static
|
|
145
|
-
const
|
|
146
|
-
const
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
return
|
|
151
|
-
}
|
|
152
|
-
static b2a(value) {
|
|
153
|
-
if (!value) return [];
|
|
154
|
-
const result = value.split(/[a-z]/);
|
|
155
|
-
return result.map((e) => Number(e));
|
|
199
|
+
static base64ToBytes(base64) {
|
|
200
|
+
const binary = atob(base64);
|
|
201
|
+
const bytes = new Uint8Array(binary.length);
|
|
202
|
+
for (let i = 0; i < binary.length; i++) {
|
|
203
|
+
bytes[i] = binary.charCodeAt(i);
|
|
204
|
+
}
|
|
205
|
+
return bytes;
|
|
156
206
|
}
|
|
157
207
|
};
|
|
158
208
|
|
|
@@ -231,11 +281,11 @@ var AbbotHttp = class extends AxiosBase {
|
|
|
231
281
|
}
|
|
232
282
|
createAxiosInstance(option) {
|
|
233
283
|
const axios2 = this.axiosInstance();
|
|
234
|
-
const { cryp, iv } = this.prepareRequestConfig();
|
|
284
|
+
const { cryp, iv } = this.prepareRequestConfig(option);
|
|
235
285
|
axios2.cryp = cryp;
|
|
236
286
|
axios2.log = new AbbLog(cryp);
|
|
237
287
|
axios2.interceptors.request.use(
|
|
238
|
-
(config) => {
|
|
288
|
+
async (config) => {
|
|
239
289
|
const req = { ...config };
|
|
240
290
|
req.responseType = option?.responseType ?? "json";
|
|
241
291
|
if (this.config.devMode) {
|
|
@@ -247,14 +297,14 @@ var AbbotHttp = class extends AxiosBase {
|
|
|
247
297
|
if (req.data) {
|
|
248
298
|
if (req.data instanceof FormData) {
|
|
249
299
|
if (req.data.get("param")) {
|
|
250
|
-
|
|
251
|
-
"param"
|
|
252
|
-
cryp.encrypt(req.data.get("param")?.toString())
|
|
300
|
+
const param = await cryp.encrypt(
|
|
301
|
+
req.data.get("param")?.toString()
|
|
253
302
|
);
|
|
303
|
+
req.data.set("param", param);
|
|
254
304
|
}
|
|
255
305
|
} else {
|
|
256
306
|
const payload = {};
|
|
257
|
-
payload.param = cryp.encrypt(JSON.stringify(req.data));
|
|
307
|
+
payload.param = await cryp.encrypt(JSON.stringify(req.data));
|
|
258
308
|
const body = payload ? new URLSearchParams(payload).toString() : "";
|
|
259
309
|
req.data = body;
|
|
260
310
|
req.headers.setContentType(
|
|
@@ -281,12 +331,12 @@ var AbbotHttp = class extends AxiosBase {
|
|
|
281
331
|
if (option?.isDownload) {
|
|
282
332
|
const contentTypes = ["application/json", "text/plain"];
|
|
283
333
|
if (contentTypes.includes(contentType) && response.data instanceof Blob) {
|
|
284
|
-
res.data = cryp.decrypt(await response.data.text());
|
|
334
|
+
res.data = await cryp.decrypt(await response.data.text());
|
|
285
335
|
} else {
|
|
286
336
|
res.data = response.data;
|
|
287
337
|
}
|
|
288
338
|
} else {
|
|
289
|
-
res.data = cryp.decrypt(response.data);
|
|
339
|
+
res.data = await cryp.decrypt(response.data);
|
|
290
340
|
}
|
|
291
341
|
if (this.config.devMode) axios2.log.logResponse(res.data);
|
|
292
342
|
return res;
|
|
@@ -300,9 +350,15 @@ var AbbotHttp = class extends AxiosBase {
|
|
|
300
350
|
);
|
|
301
351
|
return axios2;
|
|
302
352
|
}
|
|
303
|
-
prepareRequestConfig() {
|
|
304
|
-
const iv =
|
|
305
|
-
const cryp = new Cryp(
|
|
353
|
+
prepareRequestConfig(option) {
|
|
354
|
+
const iv = Cryp.genIv();
|
|
355
|
+
const cryp = new Cryp(
|
|
356
|
+
{
|
|
357
|
+
iv,
|
|
358
|
+
key: option?.isInit ? null : this.config.app.encKey
|
|
359
|
+
},
|
|
360
|
+
this.config.app.apiKey
|
|
361
|
+
);
|
|
306
362
|
const form = new URLSearchParams();
|
|
307
363
|
return { cryp, iv, form };
|
|
308
364
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "abbot-http-client",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.62",
|
|
4
4
|
"description": "This package helps Abbot team to handle all the axios requests.",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"module": "./dist/index.mjs",
|
|
@@ -19,6 +19,7 @@
|
|
|
19
19
|
"dependencies": {
|
|
20
20
|
"@types/node": "^24.0.14",
|
|
21
21
|
"axios": "^1.10.0",
|
|
22
|
+
"buffer": "^6.0.3",
|
|
22
23
|
"console-log-colors": "^0.5.0",
|
|
23
24
|
"ts-node": "^10.9.2",
|
|
24
25
|
"typescript": "^5.8.3"
|