abbot-http-client 0.0.57 → 0.0.59

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 CHANGED
@@ -113,7 +113,71 @@ var AxiosBase = class {
113
113
  };
114
114
 
115
115
  // src/encryption/cryp.ts
116
- var import_crypto_ts = require("crypto-ts");
116
+ var crypto = __toESM(require("crypto"), 1);
117
+ var Cryp = class _Cryp {
118
+ static TAG_SIZE = 16;
119
+ // bytes
120
+ static NONCE_SIZE = 12;
121
+ // bytes
122
+ static CHAR = "0123456789";
123
+ static ALGO = "aes-256-gcm";
124
+ key;
125
+ iv;
126
+ constructor(key, iv) {
127
+ this.key = key;
128
+ this.iv = iv ?? _Cryp.genIv();
129
+ }
130
+ encrypt(text) {
131
+ if (!text) return "";
132
+ const keyBuf = Buffer.from(this.key, "utf8");
133
+ const ivBuf = Buffer.from(this.iv, "utf8");
134
+ const cipher = crypto.createCipheriv(_Cryp.ALGO, keyBuf, ivBuf);
135
+ const encrypted = Buffer.concat([
136
+ cipher.update(text, "utf8"),
137
+ cipher.final()
138
+ ]);
139
+ const tag = cipher.getAuthTag();
140
+ return Buffer.concat([encrypted, tag]).toString("base64");
141
+ }
142
+ decryptText(text) {
143
+ if (!text) return "";
144
+ const data = Buffer.from(text, "base64");
145
+ const ciphertext = data.subarray(0, data.length - _Cryp.TAG_SIZE);
146
+ const tag = data.subarray(data.length - _Cryp.TAG_SIZE);
147
+ const keyBuf = Buffer.from(this.key, "utf8");
148
+ const ivBuf = Buffer.from(this.iv, "utf8");
149
+ const decipher = crypto.createDecipheriv(_Cryp.ALGO, keyBuf, ivBuf);
150
+ decipher.setAuthTag(tag);
151
+ const decrypted = Buffer.concat([
152
+ decipher.update(ciphertext),
153
+ decipher.final()
154
+ ]);
155
+ return decrypted.toString("utf8");
156
+ }
157
+ decrypt(text) {
158
+ if (!text) return null;
159
+ const data = Buffer.from(text, "base64");
160
+ const ciphertext = data.subarray(0, data.length - _Cryp.TAG_SIZE);
161
+ const tag = data.subarray(data.length - _Cryp.TAG_SIZE);
162
+ const keyBuf = Buffer.from(this.key, "utf8");
163
+ const ivBuf = Buffer.from(this.iv, "utf8");
164
+ const decipher = crypto.createDecipheriv(_Cryp.ALGO, keyBuf, ivBuf);
165
+ decipher.setAuthTag(tag);
166
+ const decrypted = Buffer.concat([
167
+ decipher.update(ciphertext),
168
+ decipher.final()
169
+ ]).toString("utf8");
170
+ return JSON.parse(decrypted);
171
+ }
172
+ static genIv() {
173
+ let result = "";
174
+ for (let i = 0; i < _Cryp.NONCE_SIZE; i++) {
175
+ const index = Math.floor(Math.random() * _Cryp.CHAR.length);
176
+ result += _Cryp.CHAR[index];
177
+ }
178
+ return result;
179
+ }
180
+ };
117
181
 
118
182
  // src/encryption/es.ts
119
183
  var ES = class {
@@ -139,44 +203,6 @@ var ES = class {
139
203
  }
140
204
  };
141
205
 
142
- // src/encryption/cryp.ts
143
- var Cryp = class {
144
- ivText;
145
- key;
146
- keyBuffer;
147
- iv;
148
- algConfig;
149
- constructor(cryptProps, apiKey) {
150
- this.ivText = cryptProps.iv;
151
- this.key = cryptProps.keyValue ? cryptProps.keyValue : ES.d(ES.b2a(apiKey));
152
- this.keyBuffer = import_crypto_ts.enc.Utf8.parse(this.key);
153
- this.iv = import_crypto_ts.enc.Utf8.parse(this.ivText);
154
- this.algConfig = { mode: import_crypto_ts.mode.CBC, padding: import_crypto_ts.pad.PKCS7, iv: this.iv };
155
- }
156
- encrypt(textValue) {
157
- if (!textValue) return "";
158
- if (!textValue.length) return textValue;
159
- const cipher = import_crypto_ts.AES.encrypt(textValue, this.keyBuffer, this.algConfig);
160
- return cipher.toString();
161
- }
162
- decrypt(encryptedValue) {
163
- try {
164
- if (!encryptedValue || !encryptedValue.length) return null;
165
- const decryptValue = import_crypto_ts.AES.decrypt(
166
- encryptedValue,
167
- this.keyBuffer,
168
- this.algConfig
169
- );
170
- const decryptData = decryptValue.toString(import_crypto_ts.enc.Utf8);
171
- const jsonData = JSON.parse(decryptData);
172
- return jsonData;
173
- } catch (err) {
174
- console.error(err);
175
- return null;
176
- }
177
- }
178
- };
179
-
180
206
  // src/encryption/secure.ts
181
207
  var Secure = class {
182
208
  static keySize = 16;
@@ -213,7 +239,7 @@ var AbbLog = class {
213
239
  log = [];
214
240
  constructor(cryp) {
215
241
  this.cryp = cryp;
216
- this.log.push(`[${(0, import_console_log_colors.blueBright)(cryp.ivText)}]`);
242
+ this.log.push(`[${(0, import_console_log_colors.blueBright)(cryp.iv)}]`);
217
243
  this.log.push(`[${(0, import_console_log_colors.blueBright)(cryp.key)}]`);
218
244
  }
219
245
  logRequest(path, req) {
@@ -252,17 +278,19 @@ var AbbotHttp = class extends AxiosBase {
252
278
  }
253
279
  createAxiosInstance(option) {
254
280
  const axios2 = this.axiosInstance();
255
- const { cryp, iv } = this.prepareRequestConfig(option);
281
+ const { cryp, iv } = this.prepareRequestConfig();
256
282
  axios2.cryp = cryp;
257
283
  axios2.log = new AbbLog(cryp);
258
284
  axios2.interceptors.request.use(
259
285
  (config) => {
260
286
  const req = { ...config };
287
+ req.responseType = option?.responseType ?? "json";
288
+ if (this.config.devMode) {
289
+ const method = req.method?.toLowerCase() || "";
290
+ const payload = method === "get" ? req.params : req.data;
291
+ axios2.log.logRequest(req.url ?? "", payload);
292
+ }
261
293
  if (req.method && req.method.toLowerCase() === "post") {
262
- req.responseType = option?.responseType ?? "json";
263
- if (this.config.devMode) {
264
- axios2.log.logRequest(config.url ?? "", req.data);
265
- }
266
294
  if (req.data) {
267
295
  if (req.data instanceof FormData) {
268
296
  if (req.data.get("param")) {
@@ -319,15 +347,9 @@ var AbbotHttp = class extends AxiosBase {
319
347
  );
320
348
  return axios2;
321
349
  }
322
- prepareRequestConfig(option) {
350
+ prepareRequestConfig() {
323
351
  const iv = Secure.createId();
324
- const cryp = new Cryp(
325
- {
326
- iv,
327
- keyValue: option?.isInit ? null : this.config.app.encKey
328
- },
329
- this.config.app.apiKey
330
- );
352
+ const cryp = new Cryp(this.config.app.apiKey, iv);
331
353
  const form = new URLSearchParams();
332
354
  return { cryp, iv, form };
333
355
  }
@@ -393,7 +415,7 @@ var AbbotHttp = class extends AxiosBase {
393
415
  };
394
416
  if (error.code === "ECONNABORTED") {
395
417
  err.response.code = -1;
396
- err.response.message = "\u0E01\u0E32\u0E23\u0E14\u0E33\u0E40\u0E19\u0E34\u0E19\u0E01\u0E32\u0E23\u0E43\u0E0A\u0E49\u0E40\u0E27\u0E25\u0E32\u0E19\u0E32\u0E40\u0E01\u0E34\u0E19\u0E44\u0E1B";
418
+ err.response.message = "\u0E01\u0E32\u0E23\u0E14\u0E33\u0E40\u0E19\u0E34\u0E19\u0E01\u0E32\u0E23\u0E43\u0E0A\u0E49\u0E40\u0E27\u0E25\u0E32\u0E19\u0E32\u0E19\u0E40\u0E01\u0E34\u0E19\u0E44\u0E1B";
397
419
  err.response.detail = "\u0E01\u0E23\u0E38\u0E13\u0E32\u0E25\u0E2D\u0E07\u0E43\u0E2B\u0E21\u0E48\u0E2D\u0E35\u0E01\u0E04\u0E23\u0E31\u0E49\u0E07";
398
420
  } else if (error.code === "ERR_NETWORK") {
399
421
  err.response.code = -2;
package/dist/index.d.cts CHANGED
@@ -31,21 +31,20 @@ type DeepPartial<T> = T extends object ? {
31
31
  declare function deepMerge<T>(target: T, source: DeepPartial<T>): T;
32
32
 
33
33
  declare class Cryp {
34
- ivText: string;
34
+ private static readonly TAG_SIZE;
35
+ private static readonly NONCE_SIZE;
36
+ private static readonly CHAR;
37
+ private static readonly ALGO;
35
38
  key: string;
36
- private keyBuffer;
37
- private iv;
38
- private algConfig;
39
- constructor(cryptProps: {
40
- iv: string;
41
- keyValue?: string | null;
42
- }, apiKey: string);
43
- encrypt(textValue?: string): string;
44
- decrypt<T>(encryptedValue?: string): T | null;
39
+ iv: string;
40
+ constructor(key: string, iv?: string);
41
+ encrypt(text?: string): string;
42
+ decryptText(text?: string): string;
43
+ decrypt<T = any>(text?: string): T | null;
44
+ static genIv(): string;
45
45
  }
46
46
 
47
47
  interface MethodOption {
48
- isInit: boolean;
49
48
  contentType: string;
50
49
  responseType: ResponseType;
51
50
  isDownload: boolean;
package/dist/index.d.ts CHANGED
@@ -31,21 +31,20 @@ type DeepPartial<T> = T extends object ? {
31
31
  declare function deepMerge<T>(target: T, source: DeepPartial<T>): T;
32
32
 
33
33
  declare class Cryp {
34
- ivText: string;
34
+ private static readonly TAG_SIZE;
35
+ private static readonly NONCE_SIZE;
36
+ private static readonly CHAR;
37
+ private static readonly ALGO;
35
38
  key: string;
36
- private keyBuffer;
37
- private iv;
38
- private algConfig;
39
- constructor(cryptProps: {
40
- iv: string;
41
- keyValue?: string | null;
42
- }, apiKey: string);
43
- encrypt(textValue?: string): string;
44
- decrypt<T>(encryptedValue?: string): T | null;
39
+ iv: string;
40
+ constructor(key: string, iv?: string);
41
+ encrypt(text?: string): string;
42
+ decryptText(text?: string): string;
43
+ decrypt<T = any>(text?: string): T | null;
44
+ static genIv(): string;
45
45
  }
46
46
 
47
47
  interface MethodOption {
48
- isInit: boolean;
49
48
  contentType: string;
50
49
  responseType: ResponseType;
51
50
  isDownload: boolean;
package/dist/index.js CHANGED
@@ -66,7 +66,71 @@ var AxiosBase = class {
66
66
  };
67
67
 
68
68
  // src/encryption/cryp.ts
69
- import { AES, enc, mode, pad } from "crypto-ts";
69
+ import * as crypto from "crypto";
70
+ var Cryp = class _Cryp {
71
+ static TAG_SIZE = 16;
72
+ // bytes
73
+ static NONCE_SIZE = 12;
74
+ // bytes
75
+ static CHAR = "0123456789";
76
+ static ALGO = "aes-256-gcm";
77
+ key;
78
+ iv;
79
+ constructor(key, iv) {
80
+ this.key = key;
81
+ this.iv = iv ?? _Cryp.genIv();
82
+ }
83
+ encrypt(text) {
84
+ if (!text) return "";
85
+ const keyBuf = Buffer.from(this.key, "utf8");
86
+ const ivBuf = Buffer.from(this.iv, "utf8");
87
+ const cipher = crypto.createCipheriv(_Cryp.ALGO, keyBuf, ivBuf);
88
+ const encrypted = Buffer.concat([
89
+ cipher.update(text, "utf8"),
90
+ cipher.final()
91
+ ]);
92
+ const tag = cipher.getAuthTag();
93
+ return Buffer.concat([encrypted, tag]).toString("base64");
94
+ }
95
+ decryptText(text) {
96
+ if (!text) return "";
97
+ const data = Buffer.from(text, "base64");
98
+ const ciphertext = data.subarray(0, data.length - _Cryp.TAG_SIZE);
99
+ const tag = data.subarray(data.length - _Cryp.TAG_SIZE);
100
+ const keyBuf = Buffer.from(this.key, "utf8");
101
+ const ivBuf = Buffer.from(this.iv, "utf8");
102
+ const decipher = crypto.createDecipheriv(_Cryp.ALGO, keyBuf, ivBuf);
103
+ decipher.setAuthTag(tag);
104
+ const decrypted = Buffer.concat([
105
+ decipher.update(ciphertext),
106
+ decipher.final()
107
+ ]);
108
+ return decrypted.toString("utf8");
109
+ }
110
+ decrypt(text) {
111
+ if (!text) return null;
112
+ const data = Buffer.from(text, "base64");
113
+ const ciphertext = data.subarray(0, data.length - _Cryp.TAG_SIZE);
114
+ const tag = data.subarray(data.length - _Cryp.TAG_SIZE);
115
+ const keyBuf = Buffer.from(this.key, "utf8");
116
+ const ivBuf = Buffer.from(this.iv, "utf8");
117
+ const decipher = crypto.createDecipheriv(_Cryp.ALGO, keyBuf, ivBuf);
118
+ decipher.setAuthTag(tag);
119
+ const decrypted = Buffer.concat([
120
+ decipher.update(ciphertext),
121
+ decipher.final()
122
+ ]).toString("utf8");
123
+ return JSON.parse(decrypted);
124
+ }
125
+ static genIv() {
126
+ let result = "";
127
+ for (let i = 0; i < _Cryp.NONCE_SIZE; i++) {
128
+ const index = Math.floor(Math.random() * _Cryp.CHAR.length);
129
+ result += _Cryp.CHAR[index];
130
+ }
131
+ return result;
132
+ }
133
+ };
70
134
 
71
135
  // src/encryption/es.ts
72
136
  var ES = class {
@@ -92,44 +156,6 @@ var ES = class {
92
156
  }
93
157
  };
94
158
 
95
- // src/encryption/cryp.ts
96
- var Cryp = class {
97
- ivText;
98
- key;
99
- keyBuffer;
100
- iv;
101
- algConfig;
102
- constructor(cryptProps, apiKey) {
103
- this.ivText = cryptProps.iv;
104
- this.key = cryptProps.keyValue ? cryptProps.keyValue : ES.d(ES.b2a(apiKey));
105
- this.keyBuffer = enc.Utf8.parse(this.key);
106
- this.iv = enc.Utf8.parse(this.ivText);
107
- this.algConfig = { mode: mode.CBC, padding: pad.PKCS7, iv: this.iv };
108
- }
109
- encrypt(textValue) {
110
- if (!textValue) return "";
111
- if (!textValue.length) return textValue;
112
- const cipher = AES.encrypt(textValue, this.keyBuffer, this.algConfig);
113
- return cipher.toString();
114
- }
115
- decrypt(encryptedValue) {
116
- try {
117
- if (!encryptedValue || !encryptedValue.length) return null;
118
- const decryptValue = AES.decrypt(
119
- encryptedValue,
120
- this.keyBuffer,
121
- this.algConfig
122
- );
123
- const decryptData = decryptValue.toString(enc.Utf8);
124
- const jsonData = JSON.parse(decryptData);
125
- return jsonData;
126
- } catch (err) {
127
- console.error(err);
128
- return null;
129
- }
130
- }
131
- };
132
-
133
159
  // src/encryption/secure.ts
134
160
  var Secure = class {
135
161
  static keySize = 16;
@@ -166,7 +192,7 @@ var AbbLog = class {
166
192
  log = [];
167
193
  constructor(cryp) {
168
194
  this.cryp = cryp;
169
- this.log.push(`[${blueBright(cryp.ivText)}]`);
195
+ this.log.push(`[${blueBright(cryp.iv)}]`);
170
196
  this.log.push(`[${blueBright(cryp.key)}]`);
171
197
  }
172
198
  logRequest(path, req) {
@@ -205,17 +231,19 @@ var AbbotHttp = class extends AxiosBase {
205
231
  }
206
232
  createAxiosInstance(option) {
207
233
  const axios2 = this.axiosInstance();
208
- const { cryp, iv } = this.prepareRequestConfig(option);
234
+ const { cryp, iv } = this.prepareRequestConfig();
209
235
  axios2.cryp = cryp;
210
236
  axios2.log = new AbbLog(cryp);
211
237
  axios2.interceptors.request.use(
212
238
  (config) => {
213
239
  const req = { ...config };
240
+ req.responseType = option?.responseType ?? "json";
241
+ if (this.config.devMode) {
242
+ const method = req.method?.toLowerCase() || "";
243
+ const payload = method === "get" ? req.params : req.data;
244
+ axios2.log.logRequest(req.url ?? "", payload);
245
+ }
214
246
  if (req.method && req.method.toLowerCase() === "post") {
215
- req.responseType = option?.responseType ?? "json";
216
- if (this.config.devMode) {
217
- axios2.log.logRequest(config.url ?? "", req.data);
218
- }
219
247
  if (req.data) {
220
248
  if (req.data instanceof FormData) {
221
249
  if (req.data.get("param")) {
@@ -272,15 +300,9 @@ var AbbotHttp = class extends AxiosBase {
272
300
  );
273
301
  return axios2;
274
302
  }
275
- prepareRequestConfig(option) {
303
+ prepareRequestConfig() {
276
304
  const iv = Secure.createId();
277
- const cryp = new Cryp(
278
- {
279
- iv,
280
- keyValue: option?.isInit ? null : this.config.app.encKey
281
- },
282
- this.config.app.apiKey
283
- );
305
+ const cryp = new Cryp(this.config.app.apiKey, iv);
284
306
  const form = new URLSearchParams();
285
307
  return { cryp, iv, form };
286
308
  }
@@ -346,7 +368,7 @@ var AbbotHttp = class extends AxiosBase {
346
368
  };
347
369
  if (error.code === "ECONNABORTED") {
348
370
  err.response.code = -1;
349
- err.response.message = "\u0E01\u0E32\u0E23\u0E14\u0E33\u0E40\u0E19\u0E34\u0E19\u0E01\u0E32\u0E23\u0E43\u0E0A\u0E49\u0E40\u0E27\u0E25\u0E32\u0E19\u0E32\u0E40\u0E01\u0E34\u0E19\u0E44\u0E1B";
371
+ err.response.message = "\u0E01\u0E32\u0E23\u0E14\u0E33\u0E40\u0E19\u0E34\u0E19\u0E01\u0E32\u0E23\u0E43\u0E0A\u0E49\u0E40\u0E27\u0E25\u0E32\u0E19\u0E32\u0E19\u0E40\u0E01\u0E34\u0E19\u0E44\u0E1B";
350
372
  err.response.detail = "\u0E01\u0E23\u0E38\u0E13\u0E32\u0E25\u0E2D\u0E07\u0E43\u0E2B\u0E21\u0E48\u0E2D\u0E35\u0E01\u0E04\u0E23\u0E31\u0E49\u0E07";
351
373
  } else if (error.code === "ERR_NETWORK") {
352
374
  err.response.code = -2;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "abbot-http-client",
3
- "version": "0.0.57",
3
+ "version": "0.0.59",
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",
@@ -20,7 +20,6 @@
20
20
  "@types/node": "^24.0.14",
21
21
  "axios": "^1.10.0",
22
22
  "console-log-colors": "^0.5.0",
23
- "crypto-ts": "^1.0.2",
24
23
  "ts-node": "^10.9.2",
25
24
  "typescript": "^5.8.3"
26
25
  },