abbot-http-client 0.0.34 → 0.0.36
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 +176 -201
- package/dist/index.d.cts +55 -37
- package/dist/index.d.ts +55 -37
- package/dist/index.js +168 -202
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -30,62 +30,87 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
30
30
|
// src/index.ts
|
|
31
31
|
var index_exports = {};
|
|
32
32
|
__export(index_exports, {
|
|
33
|
+
AbbotHttp: () => AbbotHttp,
|
|
33
34
|
AppError: () => AppError,
|
|
35
|
+
Axios: () => Axios,
|
|
36
|
+
Cryp: () => Cryp,
|
|
37
|
+
ES: () => ES,
|
|
34
38
|
RestError: () => RestError,
|
|
39
|
+
Secure: () => Secure,
|
|
35
40
|
catchError: () => catchError,
|
|
36
|
-
create: () => create
|
|
41
|
+
create: () => create,
|
|
42
|
+
deepMerge: () => deepMerge,
|
|
43
|
+
defaultConfig: () => defaultConfig
|
|
37
44
|
});
|
|
38
45
|
module.exports = __toCommonJS(index_exports);
|
|
39
46
|
|
|
40
|
-
// src/
|
|
41
|
-
|
|
42
|
-
return promise.then((data) => [void 0, data]).catch((error) => {
|
|
43
|
-
if (errorToCatch === void 0) return [error];
|
|
44
|
-
if (errorToCatch.some((e) => error instanceof e)) return [error];
|
|
45
|
-
throw error;
|
|
46
|
-
});
|
|
47
|
-
}
|
|
47
|
+
// src/config/axiosFn.ts
|
|
48
|
+
var import_axios = __toESM(require("axios"), 1);
|
|
48
49
|
|
|
49
|
-
// src/
|
|
50
|
-
var
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
50
|
+
// src/config/core.ts
|
|
51
|
+
var defaultConfig = {
|
|
52
|
+
axios: {
|
|
53
|
+
baseUrl: "",
|
|
54
|
+
timeout: 5e3,
|
|
55
|
+
headers: {
|
|
56
|
+
accept: "*/*",
|
|
57
|
+
lang: "TH",
|
|
58
|
+
post: {
|
|
59
|
+
contentType: "application/x-www-form-urlencoded"
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
},
|
|
63
|
+
app: {
|
|
64
|
+
redirectUrl: "/login",
|
|
65
|
+
apiKey: "",
|
|
66
|
+
timezone: "Asia/Bangkok",
|
|
67
|
+
encKey: null,
|
|
68
|
+
trackingId: "",
|
|
69
|
+
userJwt: null
|
|
70
|
+
},
|
|
71
|
+
devMode: false
|
|
60
72
|
};
|
|
61
73
|
|
|
62
|
-
// src/
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
static char = "0123456789";
|
|
71
|
-
static createId = () => {
|
|
72
|
-
const keyId = Array(this.keySize).fill("").map(() => {
|
|
73
|
-
const value = Math.floor(Math.random() * 10);
|
|
74
|
-
return this.char[value];
|
|
75
|
-
});
|
|
76
|
-
return keyId.join("");
|
|
77
|
-
};
|
|
78
|
-
static createKey = (keyId) => {
|
|
79
|
-
const crKey = Array(this.keySize).fill("").map((_, i) => Number(this.a ** Number(keyId[i]) % this.b));
|
|
80
|
-
return crKey.join("");
|
|
81
|
-
};
|
|
82
|
-
static toBase64Length16 = (text) => {
|
|
83
|
-
if (text && text.length) {
|
|
84
|
-
const b64 = Buffer.from(encodeURI(text)).toString("base64");
|
|
85
|
-
return b64.substring(0, 16);
|
|
74
|
+
// src/utilities.ts
|
|
75
|
+
function deepMerge(target, source) {
|
|
76
|
+
for (const key in source) {
|
|
77
|
+
const srcVal = source[key];
|
|
78
|
+
if (srcVal && typeof srcVal === "object" && !Array.isArray(srcVal)) {
|
|
79
|
+
target[key] = deepMerge(target[key], srcVal);
|
|
80
|
+
} else if (srcVal !== void 0) {
|
|
81
|
+
target[key] = srcVal;
|
|
86
82
|
}
|
|
87
|
-
|
|
88
|
-
|
|
83
|
+
}
|
|
84
|
+
return target;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// src/config/axiosFn.ts
|
|
88
|
+
function axiosConf(cfg) {
|
|
89
|
+
const axiosObj = import_axios.default.create({
|
|
90
|
+
baseURL: cfg.axios?.baseUrl,
|
|
91
|
+
headers: {
|
|
92
|
+
common: {
|
|
93
|
+
Accept: cfg.axios?.headers?.accept,
|
|
94
|
+
"Accept-Language": cfg.axios?.headers?.lang
|
|
95
|
+
},
|
|
96
|
+
post: {
|
|
97
|
+
"Content-Type": cfg.axios?.headers?.post?.contentType
|
|
98
|
+
}
|
|
99
|
+
},
|
|
100
|
+
timeout: cfg.axios?.timeout,
|
|
101
|
+
withCredentials: true
|
|
102
|
+
});
|
|
103
|
+
return axiosObj;
|
|
104
|
+
}
|
|
105
|
+
var Axios = class {
|
|
106
|
+
config = defaultConfig;
|
|
107
|
+
constructor(config) {
|
|
108
|
+
this.config = deepMerge(this.config, config);
|
|
109
|
+
}
|
|
110
|
+
axiosInstance() {
|
|
111
|
+
const axios2 = axiosConf(this.config);
|
|
112
|
+
return axios2;
|
|
113
|
+
}
|
|
89
114
|
};
|
|
90
115
|
|
|
91
116
|
// src/encryption/cryp.ts
|
|
@@ -153,119 +178,88 @@ var Cryp = class {
|
|
|
153
178
|
}
|
|
154
179
|
};
|
|
155
180
|
|
|
156
|
-
// src/
|
|
157
|
-
var
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
181
|
+
// src/encryption/secure.ts
|
|
182
|
+
var Secure = class {
|
|
183
|
+
static keySize = 16;
|
|
184
|
+
static a = 5;
|
|
185
|
+
static b = 9;
|
|
186
|
+
static char = "0123456789";
|
|
187
|
+
static createId = () => {
|
|
188
|
+
const keyId = Array(this.keySize).fill("").map(() => {
|
|
189
|
+
const value = Math.floor(Math.random() * 10);
|
|
190
|
+
return this.char[value];
|
|
191
|
+
});
|
|
192
|
+
return keyId.join("");
|
|
193
|
+
};
|
|
194
|
+
static createKey = (keyId) => {
|
|
195
|
+
const crKey = Array(this.keySize).fill("").map((_, i) => Number(this.a ** Number(keyId[i]) % this.b));
|
|
196
|
+
return crKey.join("");
|
|
197
|
+
};
|
|
198
|
+
static toBase64Length16 = (text) => {
|
|
199
|
+
if (text && text.length) {
|
|
200
|
+
const b64 = Buffer.from(encodeURI(text)).toString("base64");
|
|
201
|
+
return b64.substring(0, 16);
|
|
167
202
|
}
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
redirectUrl: "/login",
|
|
171
|
-
apiKey: "",
|
|
172
|
-
timezone: "Asia/Bangkok",
|
|
173
|
-
encKey: "",
|
|
174
|
-
trackingId: "",
|
|
175
|
-
userJwt: ""
|
|
176
|
-
},
|
|
177
|
-
devMode: false,
|
|
178
|
-
useAxiosInterceptors: false
|
|
203
|
+
return "";
|
|
204
|
+
};
|
|
179
205
|
};
|
|
180
206
|
|
|
181
|
-
// src/
|
|
182
|
-
var
|
|
183
|
-
|
|
184
|
-
const axiosObj = import_axios.default.create({
|
|
185
|
-
baseURL: cfg.axios?.baseUrl,
|
|
186
|
-
headers: {
|
|
187
|
-
common: {
|
|
188
|
-
Accept: cfg.axios?.headers?.accept,
|
|
189
|
-
"Accept-Language": cfg.axios?.headers?.lang
|
|
190
|
-
},
|
|
191
|
-
post: {
|
|
192
|
-
"Content-Type": cfg.axios?.headers?.post?.contentType
|
|
193
|
-
}
|
|
194
|
-
},
|
|
195
|
-
timeout: cfg.axios?.timeout,
|
|
196
|
-
withCredentials: true
|
|
197
|
-
});
|
|
198
|
-
return axiosObj;
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
// src/http.ts
|
|
202
|
-
var AbbotHttp = class {
|
|
203
|
-
config = defaultConfig;
|
|
207
|
+
// src/http/core.ts
|
|
208
|
+
var import_axios2 = require("axios");
|
|
209
|
+
var AbbotHttp = class extends Axios {
|
|
204
210
|
constructor(config) {
|
|
205
|
-
|
|
211
|
+
super(config);
|
|
206
212
|
}
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
const srcVal = source[key];
|
|
210
|
-
if (srcVal && typeof srcVal === "object" && !Array.isArray(srcVal)) {
|
|
211
|
-
target[key] = deepMerge(target[key], srcVal);
|
|
212
|
-
} else if (srcVal !== void 0) {
|
|
213
|
-
target[key] = srcVal;
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
return target;
|
|
213
|
+
getConfig() {
|
|
214
|
+
return this.config;
|
|
217
215
|
}
|
|
218
216
|
createAxiosInstance(option) {
|
|
219
|
-
const axios2 =
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
if (config.
|
|
226
|
-
if (config.data) {
|
|
227
|
-
if (config.data
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
cryp.encrypt(config.data.get("param")?.toString())
|
|
232
|
-
);
|
|
233
|
-
}
|
|
234
|
-
} else {
|
|
235
|
-
const payload = {};
|
|
236
|
-
payload.param = cryp.encrypt(JSON.stringify(config.data));
|
|
237
|
-
const body = payload ? new URLSearchParams(payload).toString() : "";
|
|
238
|
-
req.data = body;
|
|
239
|
-
req.headers.setContentType(
|
|
240
|
-
option?.contentType ?? this.config.axios.headers.post.contentType
|
|
217
|
+
const axios2 = this.axiosInstance();
|
|
218
|
+
const { cryp, iv } = this.prepareRequestConfig(option);
|
|
219
|
+
axios2.interceptors.request.use(
|
|
220
|
+
(config) => {
|
|
221
|
+
const req = { ...config };
|
|
222
|
+
if (config.method && config.method.toLowerCase() === "post") {
|
|
223
|
+
if (config.data) {
|
|
224
|
+
if (config.data instanceof FormData) {
|
|
225
|
+
if (config.data.get("param")) {
|
|
226
|
+
config.data.set(
|
|
227
|
+
"param",
|
|
228
|
+
cryp.encrypt(config.data.get("param")?.toString())
|
|
241
229
|
);
|
|
242
230
|
}
|
|
231
|
+
} else {
|
|
232
|
+
const payload = {};
|
|
233
|
+
payload.param = cryp.encrypt(JSON.stringify(config.data));
|
|
234
|
+
const body = payload ? new URLSearchParams(payload).toString() : "";
|
|
235
|
+
req.data = body;
|
|
236
|
+
req.headers.setContentType(
|
|
237
|
+
option?.contentType ?? this.config.axios.headers.post.contentType
|
|
238
|
+
);
|
|
243
239
|
}
|
|
244
240
|
}
|
|
245
|
-
req.headers.set("data-code", iv);
|
|
246
|
-
if (this.config.app.trackingId) {
|
|
247
|
-
config.headers.set("X-Trace-Id", this.config.app.trackingId);
|
|
248
|
-
}
|
|
249
|
-
if (this.config.app.userJwt) {
|
|
250
|
-
config.headers.setAuthorization(
|
|
251
|
-
`Bearer ${this.config.app.userJwt}`
|
|
252
|
-
);
|
|
253
|
-
}
|
|
254
|
-
return req;
|
|
255
|
-
},
|
|
256
|
-
(error) => Promise.reject(error)
|
|
257
|
-
);
|
|
258
|
-
axios2.interceptors.response.use(
|
|
259
|
-
(response) => {
|
|
260
|
-
const res = { ...response };
|
|
261
|
-
res.data = cryp.decrypt(response.data);
|
|
262
|
-
return res.data;
|
|
263
|
-
},
|
|
264
|
-
(error) => {
|
|
265
|
-
return this.habdleError(error, cryp);
|
|
266
241
|
}
|
|
267
|
-
|
|
268
|
-
|
|
242
|
+
req.headers.set("data-code", iv);
|
|
243
|
+
if (this.config.app.trackingId) {
|
|
244
|
+
config.headers.set("X-Trace-Id", this.config.app.trackingId);
|
|
245
|
+
}
|
|
246
|
+
if (this.config.app.userJwt) {
|
|
247
|
+
config.headers.setAuthorization(`Bearer ${this.config.app.userJwt}`);
|
|
248
|
+
}
|
|
249
|
+
return req;
|
|
250
|
+
},
|
|
251
|
+
(error) => Promise.reject(error)
|
|
252
|
+
);
|
|
253
|
+
axios2.interceptors.response.use(
|
|
254
|
+
(response) => {
|
|
255
|
+
const res = { ...response };
|
|
256
|
+
res.data = cryp.decrypt(response.data);
|
|
257
|
+
return res.data;
|
|
258
|
+
},
|
|
259
|
+
(error) => {
|
|
260
|
+
return this.habdleError(error, cryp);
|
|
261
|
+
}
|
|
262
|
+
);
|
|
269
263
|
return axios2;
|
|
270
264
|
}
|
|
271
265
|
prepareRequestConfig(option) {
|
|
@@ -279,25 +273,13 @@ var AbbotHttp = class {
|
|
|
279
273
|
const cryp = new Cryp(
|
|
280
274
|
{
|
|
281
275
|
iv,
|
|
282
|
-
keyValue: option?.isInit ?
|
|
276
|
+
keyValue: option?.isInit ? null : this.config.app.encKey
|
|
283
277
|
},
|
|
284
278
|
this.config.app.apiKey
|
|
285
279
|
);
|
|
286
280
|
const form = new URLSearchParams();
|
|
287
281
|
return { cryp, iv, form };
|
|
288
282
|
}
|
|
289
|
-
createAxiosRequestObj(iv) {
|
|
290
|
-
const headers = {
|
|
291
|
-
"data-code": iv
|
|
292
|
-
};
|
|
293
|
-
if (this.config.app.userJwt) {
|
|
294
|
-
headers.Authorization = `Bearer ${this.config.app.userJwt}`;
|
|
295
|
-
}
|
|
296
|
-
if (this.config.app.trackingId) {
|
|
297
|
-
headers["X-Trace-Id"] = this.config.app.trackingId;
|
|
298
|
-
}
|
|
299
|
-
return headers;
|
|
300
|
-
}
|
|
301
283
|
habdleError(error, cryp) {
|
|
302
284
|
const err = {
|
|
303
285
|
message: error.message,
|
|
@@ -371,46 +353,13 @@ var AbbotHttp = class {
|
|
|
371
353
|
}
|
|
372
354
|
async post(url, param, option) {
|
|
373
355
|
const axios2 = this.createAxiosInstance(option);
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
const config = {
|
|
377
|
-
headers: this.createAxiosRequestObj(iv)
|
|
378
|
-
};
|
|
379
|
-
if (param) {
|
|
380
|
-
form.append("param", encodeURI(cryp.encrypt(JSON.stringify(param))));
|
|
381
|
-
}
|
|
382
|
-
try {
|
|
383
|
-
const response = await axios2.post(url, form, config);
|
|
384
|
-
const res = { ...response };
|
|
385
|
-
res.data = cryp.decrypt(response.data);
|
|
386
|
-
return res.data;
|
|
387
|
-
} catch (err) {
|
|
388
|
-
return this.habdleError(err, cryp);
|
|
389
|
-
}
|
|
390
|
-
} else {
|
|
391
|
-
const response = await axios2.post(url, param);
|
|
392
|
-
return response;
|
|
393
|
-
}
|
|
356
|
+
const response = await axios2.post(url, param);
|
|
357
|
+
return response;
|
|
394
358
|
}
|
|
395
359
|
async get(url, option) {
|
|
396
360
|
const axios2 = this.createAxiosInstance(option);
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
const config = {
|
|
400
|
-
headers: this.createAxiosRequestObj(iv)
|
|
401
|
-
};
|
|
402
|
-
try {
|
|
403
|
-
const response = await axios2.get(url, config);
|
|
404
|
-
const res = { ...response };
|
|
405
|
-
res.data = cryp.decrypt(response.data);
|
|
406
|
-
return res.data;
|
|
407
|
-
} catch (err) {
|
|
408
|
-
return this.habdleError(err, cryp);
|
|
409
|
-
}
|
|
410
|
-
} else {
|
|
411
|
-
const response = await axios2.get(url);
|
|
412
|
-
return response;
|
|
413
|
-
}
|
|
361
|
+
const response = await axios2.get(url);
|
|
362
|
+
return response;
|
|
414
363
|
}
|
|
415
364
|
async uploadFile(url, file, param1, param2, param3) {
|
|
416
365
|
let response;
|
|
@@ -441,20 +390,46 @@ var AbbotHttp = class {
|
|
|
441
390
|
}
|
|
442
391
|
return response;
|
|
443
392
|
}
|
|
444
|
-
|
|
445
|
-
|
|
393
|
+
};
|
|
394
|
+
|
|
395
|
+
// src/catchError.ts
|
|
396
|
+
async function catchError(promise, errorToCatch) {
|
|
397
|
+
return promise.then((data) => [void 0, data]).catch((error) => {
|
|
398
|
+
if (errorToCatch === void 0) return [error];
|
|
399
|
+
if (errorToCatch.some((e) => error instanceof e)) return [error];
|
|
400
|
+
throw error;
|
|
401
|
+
});
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
// src/error.ts
|
|
405
|
+
var AppError = class extends Error {
|
|
406
|
+
name = "App Error";
|
|
407
|
+
};
|
|
408
|
+
var RestError = class extends Error {
|
|
409
|
+
name = "Rest Error";
|
|
410
|
+
res;
|
|
411
|
+
constructor(rs) {
|
|
412
|
+
super(rs.message);
|
|
413
|
+
this.res = rs;
|
|
446
414
|
}
|
|
447
415
|
};
|
|
448
416
|
|
|
449
|
-
// src/
|
|
417
|
+
// src/core.ts
|
|
450
418
|
function create(config) {
|
|
451
419
|
const http = new AbbotHttp(config);
|
|
452
420
|
return http;
|
|
453
421
|
}
|
|
454
422
|
// Annotate the CommonJS export names for ESM import in node:
|
|
455
423
|
0 && (module.exports = {
|
|
424
|
+
AbbotHttp,
|
|
456
425
|
AppError,
|
|
426
|
+
Axios,
|
|
427
|
+
Cryp,
|
|
428
|
+
ES,
|
|
457
429
|
RestError,
|
|
430
|
+
Secure,
|
|
458
431
|
catchError,
|
|
459
|
-
create
|
|
432
|
+
create,
|
|
433
|
+
deepMerge,
|
|
434
|
+
defaultConfig
|
|
460
435
|
});
|
package/dist/index.d.cts
CHANGED
|
@@ -1,18 +1,5 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
declare class Cryp {
|
|
4
|
-
ivText: string;
|
|
5
|
-
key: string;
|
|
6
|
-
private keyBuffer;
|
|
7
|
-
private iv;
|
|
8
|
-
private algConfig;
|
|
9
|
-
constructor(cryptProps: {
|
|
10
|
-
iv: string;
|
|
11
|
-
keyValue?: string;
|
|
12
|
-
}, apiKey: string);
|
|
13
|
-
encrypt(textValue?: string): string;
|
|
14
|
-
decrypt<T>(encryptedValue?: string): T | null;
|
|
15
|
-
}
|
|
1
|
+
import * as axios from 'axios';
|
|
2
|
+
import { AxiosResponse, AxiosError } from 'axios';
|
|
16
3
|
|
|
17
4
|
interface AbbotConfig {
|
|
18
5
|
axios: {
|
|
@@ -31,44 +18,77 @@ interface AbbotConfig {
|
|
|
31
18
|
apiKey: string;
|
|
32
19
|
timezone: string;
|
|
33
20
|
trackingId: string;
|
|
34
|
-
encKey: string;
|
|
35
|
-
userJwt: string;
|
|
21
|
+
encKey: string | null;
|
|
22
|
+
userJwt: string | null;
|
|
36
23
|
};
|
|
37
24
|
devMode: boolean;
|
|
38
|
-
useAxiosInterceptors: boolean;
|
|
39
25
|
}
|
|
26
|
+
declare const defaultConfig: AbbotConfig;
|
|
40
27
|
|
|
41
28
|
type DeepPartial<T> = T extends object ? {
|
|
42
29
|
[P in keyof T]?: DeepPartial<T[P]>;
|
|
43
30
|
} : T;
|
|
31
|
+
declare function deepMerge<T>(target: T, source: DeepPartial<T>): T;
|
|
32
|
+
|
|
33
|
+
declare class Axios {
|
|
34
|
+
config: AbbotConfig;
|
|
35
|
+
constructor(config: DeepPartial<AbbotConfig>);
|
|
36
|
+
axiosInstance(): axios.AxiosInstance;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
declare class Cryp {
|
|
40
|
+
ivText: string;
|
|
41
|
+
key: string;
|
|
42
|
+
private keyBuffer;
|
|
43
|
+
private iv;
|
|
44
|
+
private algConfig;
|
|
45
|
+
constructor(cryptProps: {
|
|
46
|
+
iv: string;
|
|
47
|
+
keyValue?: string | null;
|
|
48
|
+
}, apiKey: string);
|
|
49
|
+
encrypt(textValue?: string): string;
|
|
50
|
+
decrypt<T>(encryptedValue?: string): T | null;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
declare class ES {
|
|
54
|
+
static e(value: string): number[];
|
|
55
|
+
static d(value: number[]): string;
|
|
56
|
+
static a2b(value: number[]): string;
|
|
57
|
+
static b2a(value: string | undefined): number[];
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
declare class Secure {
|
|
61
|
+
private static readonly keySize;
|
|
62
|
+
private static readonly a;
|
|
63
|
+
private static readonly b;
|
|
64
|
+
private static readonly char;
|
|
65
|
+
static createId: () => string;
|
|
66
|
+
static createKey: (keyId: string) => string;
|
|
67
|
+
static toBase64Length16: (text: string | undefined) => string;
|
|
68
|
+
}
|
|
69
|
+
|
|
44
70
|
interface MethodOption {
|
|
45
71
|
isInit: boolean;
|
|
46
72
|
contentType: string;
|
|
47
73
|
}
|
|
48
|
-
interface AbbotResponse
|
|
74
|
+
interface AbbotResponse<T> {
|
|
49
75
|
code: AxiosResponse["status"];
|
|
50
76
|
detail: string | null;
|
|
51
77
|
message: string;
|
|
52
78
|
status: string;
|
|
53
79
|
data: T;
|
|
54
80
|
}
|
|
55
|
-
|
|
56
|
-
|
|
81
|
+
|
|
82
|
+
declare class AbbotHttp extends Axios {
|
|
57
83
|
constructor(config: DeepPartial<AbbotConfig>);
|
|
58
|
-
protected deepMerge<T>(target: T, source: DeepPartial<T>): T;
|
|
59
|
-
protected createAxiosInstance(option?: Partial<MethodOption>): AxiosInstance;
|
|
60
|
-
protected prepareRequestConfig(option?: Partial<MethodOption>): {
|
|
61
|
-
cryp: Cryp;
|
|
62
|
-
iv: string;
|
|
63
|
-
form: URLSearchParams;
|
|
64
|
-
};
|
|
65
|
-
protected createAxiosRequestObj(iv: string): Record<string, any>;
|
|
66
|
-
protected habdleError(error: any, cryp: Cryp): Promise<never>;
|
|
67
|
-
post<T>(url: string, param?: Record<string, any> | null, option?: Partial<MethodOption>): Promise<AbbotResponse$1<T>>;
|
|
68
|
-
get<T>(url: string, option?: Partial<MethodOption>): Promise<AbbotResponse$1<T>>;
|
|
69
|
-
uploadFile<T>(url: string, file: File, param?: Record<string, any> | null, option?: Partial<MethodOption>): Promise<AbbotResponse$1<T>>;
|
|
70
|
-
uploadFile<T>(url: string, file: File[], fileKeys: string[], param?: Record<string, any> | null, option?: Partial<MethodOption>): Promise<AbbotResponse$1<T>>;
|
|
71
84
|
getConfig(): AbbotConfig;
|
|
85
|
+
private createAxiosInstance;
|
|
86
|
+
private prepareRequestConfig;
|
|
87
|
+
private habdleError;
|
|
88
|
+
post<T>(url: string, param?: Record<string, any> | null, option?: Partial<MethodOption>): Promise<AbbotResponse<T>>;
|
|
89
|
+
get<T>(url: string, option?: Partial<MethodOption>): Promise<AbbotResponse<T>>;
|
|
90
|
+
uploadFile<T>(url: string, file: File, param?: Record<string, any> | null, option?: Partial<MethodOption>): Promise<AbbotResponse<T>>;
|
|
91
|
+
uploadFile<T>(url: string, file: File[], fileKeys: string[], param?: Record<string, any> | null, option?: Partial<MethodOption>): Promise<AbbotResponse<T>>;
|
|
72
92
|
}
|
|
73
93
|
|
|
74
94
|
declare function catchError<T, E extends new (err?: any) => Error>(promise: Promise<T>, errorToCatch?: E[]): Promise<[undefined, T] | [InstanceType<E>]>;
|
|
@@ -84,6 +104,4 @@ declare class RestError<T extends AxiosError> extends Error {
|
|
|
84
104
|
|
|
85
105
|
declare function create(config: DeepPartial<AbbotConfig>): AbbotHttp;
|
|
86
106
|
|
|
87
|
-
type AbbotResponse
|
|
88
|
-
|
|
89
|
-
export { type AbbotConfig, type AbbotResponse, AppError, RestError, catchError, create };
|
|
107
|
+
export { type AbbotConfig, AbbotHttp, type AbbotResponse, AppError, Axios, Cryp, type DeepPartial, ES, type MethodOption, RestError, Secure, catchError, create, deepMerge, defaultConfig };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,18 +1,5 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
declare class Cryp {
|
|
4
|
-
ivText: string;
|
|
5
|
-
key: string;
|
|
6
|
-
private keyBuffer;
|
|
7
|
-
private iv;
|
|
8
|
-
private algConfig;
|
|
9
|
-
constructor(cryptProps: {
|
|
10
|
-
iv: string;
|
|
11
|
-
keyValue?: string;
|
|
12
|
-
}, apiKey: string);
|
|
13
|
-
encrypt(textValue?: string): string;
|
|
14
|
-
decrypt<T>(encryptedValue?: string): T | null;
|
|
15
|
-
}
|
|
1
|
+
import * as axios from 'axios';
|
|
2
|
+
import { AxiosResponse, AxiosError } from 'axios';
|
|
16
3
|
|
|
17
4
|
interface AbbotConfig {
|
|
18
5
|
axios: {
|
|
@@ -31,44 +18,77 @@ interface AbbotConfig {
|
|
|
31
18
|
apiKey: string;
|
|
32
19
|
timezone: string;
|
|
33
20
|
trackingId: string;
|
|
34
|
-
encKey: string;
|
|
35
|
-
userJwt: string;
|
|
21
|
+
encKey: string | null;
|
|
22
|
+
userJwt: string | null;
|
|
36
23
|
};
|
|
37
24
|
devMode: boolean;
|
|
38
|
-
useAxiosInterceptors: boolean;
|
|
39
25
|
}
|
|
26
|
+
declare const defaultConfig: AbbotConfig;
|
|
40
27
|
|
|
41
28
|
type DeepPartial<T> = T extends object ? {
|
|
42
29
|
[P in keyof T]?: DeepPartial<T[P]>;
|
|
43
30
|
} : T;
|
|
31
|
+
declare function deepMerge<T>(target: T, source: DeepPartial<T>): T;
|
|
32
|
+
|
|
33
|
+
declare class Axios {
|
|
34
|
+
config: AbbotConfig;
|
|
35
|
+
constructor(config: DeepPartial<AbbotConfig>);
|
|
36
|
+
axiosInstance(): axios.AxiosInstance;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
declare class Cryp {
|
|
40
|
+
ivText: string;
|
|
41
|
+
key: string;
|
|
42
|
+
private keyBuffer;
|
|
43
|
+
private iv;
|
|
44
|
+
private algConfig;
|
|
45
|
+
constructor(cryptProps: {
|
|
46
|
+
iv: string;
|
|
47
|
+
keyValue?: string | null;
|
|
48
|
+
}, apiKey: string);
|
|
49
|
+
encrypt(textValue?: string): string;
|
|
50
|
+
decrypt<T>(encryptedValue?: string): T | null;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
declare class ES {
|
|
54
|
+
static e(value: string): number[];
|
|
55
|
+
static d(value: number[]): string;
|
|
56
|
+
static a2b(value: number[]): string;
|
|
57
|
+
static b2a(value: string | undefined): number[];
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
declare class Secure {
|
|
61
|
+
private static readonly keySize;
|
|
62
|
+
private static readonly a;
|
|
63
|
+
private static readonly b;
|
|
64
|
+
private static readonly char;
|
|
65
|
+
static createId: () => string;
|
|
66
|
+
static createKey: (keyId: string) => string;
|
|
67
|
+
static toBase64Length16: (text: string | undefined) => string;
|
|
68
|
+
}
|
|
69
|
+
|
|
44
70
|
interface MethodOption {
|
|
45
71
|
isInit: boolean;
|
|
46
72
|
contentType: string;
|
|
47
73
|
}
|
|
48
|
-
interface AbbotResponse
|
|
74
|
+
interface AbbotResponse<T> {
|
|
49
75
|
code: AxiosResponse["status"];
|
|
50
76
|
detail: string | null;
|
|
51
77
|
message: string;
|
|
52
78
|
status: string;
|
|
53
79
|
data: T;
|
|
54
80
|
}
|
|
55
|
-
|
|
56
|
-
|
|
81
|
+
|
|
82
|
+
declare class AbbotHttp extends Axios {
|
|
57
83
|
constructor(config: DeepPartial<AbbotConfig>);
|
|
58
|
-
protected deepMerge<T>(target: T, source: DeepPartial<T>): T;
|
|
59
|
-
protected createAxiosInstance(option?: Partial<MethodOption>): AxiosInstance;
|
|
60
|
-
protected prepareRequestConfig(option?: Partial<MethodOption>): {
|
|
61
|
-
cryp: Cryp;
|
|
62
|
-
iv: string;
|
|
63
|
-
form: URLSearchParams;
|
|
64
|
-
};
|
|
65
|
-
protected createAxiosRequestObj(iv: string): Record<string, any>;
|
|
66
|
-
protected habdleError(error: any, cryp: Cryp): Promise<never>;
|
|
67
|
-
post<T>(url: string, param?: Record<string, any> | null, option?: Partial<MethodOption>): Promise<AbbotResponse$1<T>>;
|
|
68
|
-
get<T>(url: string, option?: Partial<MethodOption>): Promise<AbbotResponse$1<T>>;
|
|
69
|
-
uploadFile<T>(url: string, file: File, param?: Record<string, any> | null, option?: Partial<MethodOption>): Promise<AbbotResponse$1<T>>;
|
|
70
|
-
uploadFile<T>(url: string, file: File[], fileKeys: string[], param?: Record<string, any> | null, option?: Partial<MethodOption>): Promise<AbbotResponse$1<T>>;
|
|
71
84
|
getConfig(): AbbotConfig;
|
|
85
|
+
private createAxiosInstance;
|
|
86
|
+
private prepareRequestConfig;
|
|
87
|
+
private habdleError;
|
|
88
|
+
post<T>(url: string, param?: Record<string, any> | null, option?: Partial<MethodOption>): Promise<AbbotResponse<T>>;
|
|
89
|
+
get<T>(url: string, option?: Partial<MethodOption>): Promise<AbbotResponse<T>>;
|
|
90
|
+
uploadFile<T>(url: string, file: File, param?: Record<string, any> | null, option?: Partial<MethodOption>): Promise<AbbotResponse<T>>;
|
|
91
|
+
uploadFile<T>(url: string, file: File[], fileKeys: string[], param?: Record<string, any> | null, option?: Partial<MethodOption>): Promise<AbbotResponse<T>>;
|
|
72
92
|
}
|
|
73
93
|
|
|
74
94
|
declare function catchError<T, E extends new (err?: any) => Error>(promise: Promise<T>, errorToCatch?: E[]): Promise<[undefined, T] | [InstanceType<E>]>;
|
|
@@ -84,6 +104,4 @@ declare class RestError<T extends AxiosError> extends Error {
|
|
|
84
104
|
|
|
85
105
|
declare function create(config: DeepPartial<AbbotConfig>): AbbotHttp;
|
|
86
106
|
|
|
87
|
-
type AbbotResponse
|
|
88
|
-
|
|
89
|
-
export { type AbbotConfig, type AbbotResponse, AppError, RestError, catchError, create };
|
|
107
|
+
export { type AbbotConfig, AbbotHttp, type AbbotResponse, AppError, Axios, Cryp, type DeepPartial, ES, type MethodOption, RestError, Secure, catchError, create, deepMerge, defaultConfig };
|
package/dist/index.js
CHANGED
|
@@ -1,54 +1,70 @@
|
|
|
1
|
-
// src/
|
|
2
|
-
|
|
3
|
-
return promise.then((data) => [void 0, data]).catch((error) => {
|
|
4
|
-
if (errorToCatch === void 0) return [error];
|
|
5
|
-
if (errorToCatch.some((e) => error instanceof e)) return [error];
|
|
6
|
-
throw error;
|
|
7
|
-
});
|
|
8
|
-
}
|
|
1
|
+
// src/config/axiosFn.ts
|
|
2
|
+
import axios from "axios";
|
|
9
3
|
|
|
10
|
-
// src/
|
|
11
|
-
var
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
4
|
+
// src/config/core.ts
|
|
5
|
+
var defaultConfig = {
|
|
6
|
+
axios: {
|
|
7
|
+
baseUrl: "",
|
|
8
|
+
timeout: 5e3,
|
|
9
|
+
headers: {
|
|
10
|
+
accept: "*/*",
|
|
11
|
+
lang: "TH",
|
|
12
|
+
post: {
|
|
13
|
+
contentType: "application/x-www-form-urlencoded"
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
app: {
|
|
18
|
+
redirectUrl: "/login",
|
|
19
|
+
apiKey: "",
|
|
20
|
+
timezone: "Asia/Bangkok",
|
|
21
|
+
encKey: null,
|
|
22
|
+
trackingId: "",
|
|
23
|
+
userJwt: null
|
|
24
|
+
},
|
|
25
|
+
devMode: false
|
|
21
26
|
};
|
|
22
27
|
|
|
23
|
-
// src/
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
static a = 5;
|
|
32
|
-
static b = 9;
|
|
33
|
-
static char = "0123456789";
|
|
34
|
-
static createId = () => {
|
|
35
|
-
const keyId = Array(this.keySize).fill("").map(() => {
|
|
36
|
-
const value = Math.floor(Math.random() * 10);
|
|
37
|
-
return this.char[value];
|
|
38
|
-
});
|
|
39
|
-
return keyId.join("");
|
|
40
|
-
};
|
|
41
|
-
static createKey = (keyId) => {
|
|
42
|
-
const crKey = Array(this.keySize).fill("").map((_, i) => Number(this.a ** Number(keyId[i]) % this.b));
|
|
43
|
-
return crKey.join("");
|
|
44
|
-
};
|
|
45
|
-
static toBase64Length16 = (text) => {
|
|
46
|
-
if (text && text.length) {
|
|
47
|
-
const b64 = Buffer.from(encodeURI(text)).toString("base64");
|
|
48
|
-
return b64.substring(0, 16);
|
|
28
|
+
// src/utilities.ts
|
|
29
|
+
function deepMerge(target, source) {
|
|
30
|
+
for (const key in source) {
|
|
31
|
+
const srcVal = source[key];
|
|
32
|
+
if (srcVal && typeof srcVal === "object" && !Array.isArray(srcVal)) {
|
|
33
|
+
target[key] = deepMerge(target[key], srcVal);
|
|
34
|
+
} else if (srcVal !== void 0) {
|
|
35
|
+
target[key] = srcVal;
|
|
49
36
|
}
|
|
50
|
-
|
|
51
|
-
|
|
37
|
+
}
|
|
38
|
+
return target;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// src/config/axiosFn.ts
|
|
42
|
+
function axiosConf(cfg) {
|
|
43
|
+
const axiosObj = axios.create({
|
|
44
|
+
baseURL: cfg.axios?.baseUrl,
|
|
45
|
+
headers: {
|
|
46
|
+
common: {
|
|
47
|
+
Accept: cfg.axios?.headers?.accept,
|
|
48
|
+
"Accept-Language": cfg.axios?.headers?.lang
|
|
49
|
+
},
|
|
50
|
+
post: {
|
|
51
|
+
"Content-Type": cfg.axios?.headers?.post?.contentType
|
|
52
|
+
}
|
|
53
|
+
},
|
|
54
|
+
timeout: cfg.axios?.timeout,
|
|
55
|
+
withCredentials: true
|
|
56
|
+
});
|
|
57
|
+
return axiosObj;
|
|
58
|
+
}
|
|
59
|
+
var Axios = class {
|
|
60
|
+
config = defaultConfig;
|
|
61
|
+
constructor(config) {
|
|
62
|
+
this.config = deepMerge(this.config, config);
|
|
63
|
+
}
|
|
64
|
+
axiosInstance() {
|
|
65
|
+
const axios2 = axiosConf(this.config);
|
|
66
|
+
return axios2;
|
|
67
|
+
}
|
|
52
68
|
};
|
|
53
69
|
|
|
54
70
|
// src/encryption/cryp.ts
|
|
@@ -116,119 +132,88 @@ var Cryp = class {
|
|
|
116
132
|
}
|
|
117
133
|
};
|
|
118
134
|
|
|
119
|
-
// src/
|
|
120
|
-
var
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
135
|
+
// src/encryption/secure.ts
|
|
136
|
+
var Secure = class {
|
|
137
|
+
static keySize = 16;
|
|
138
|
+
static a = 5;
|
|
139
|
+
static b = 9;
|
|
140
|
+
static char = "0123456789";
|
|
141
|
+
static createId = () => {
|
|
142
|
+
const keyId = Array(this.keySize).fill("").map(() => {
|
|
143
|
+
const value = Math.floor(Math.random() * 10);
|
|
144
|
+
return this.char[value];
|
|
145
|
+
});
|
|
146
|
+
return keyId.join("");
|
|
147
|
+
};
|
|
148
|
+
static createKey = (keyId) => {
|
|
149
|
+
const crKey = Array(this.keySize).fill("").map((_, i) => Number(this.a ** Number(keyId[i]) % this.b));
|
|
150
|
+
return crKey.join("");
|
|
151
|
+
};
|
|
152
|
+
static toBase64Length16 = (text) => {
|
|
153
|
+
if (text && text.length) {
|
|
154
|
+
const b64 = Buffer.from(encodeURI(text)).toString("base64");
|
|
155
|
+
return b64.substring(0, 16);
|
|
130
156
|
}
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
redirectUrl: "/login",
|
|
134
|
-
apiKey: "",
|
|
135
|
-
timezone: "Asia/Bangkok",
|
|
136
|
-
encKey: "",
|
|
137
|
-
trackingId: "",
|
|
138
|
-
userJwt: ""
|
|
139
|
-
},
|
|
140
|
-
devMode: false,
|
|
141
|
-
useAxiosInterceptors: false
|
|
157
|
+
return "";
|
|
158
|
+
};
|
|
142
159
|
};
|
|
143
160
|
|
|
144
|
-
// src/
|
|
145
|
-
import
|
|
146
|
-
|
|
147
|
-
const axiosObj = axios.create({
|
|
148
|
-
baseURL: cfg.axios?.baseUrl,
|
|
149
|
-
headers: {
|
|
150
|
-
common: {
|
|
151
|
-
Accept: cfg.axios?.headers?.accept,
|
|
152
|
-
"Accept-Language": cfg.axios?.headers?.lang
|
|
153
|
-
},
|
|
154
|
-
post: {
|
|
155
|
-
"Content-Type": cfg.axios?.headers?.post?.contentType
|
|
156
|
-
}
|
|
157
|
-
},
|
|
158
|
-
timeout: cfg.axios?.timeout,
|
|
159
|
-
withCredentials: true
|
|
160
|
-
});
|
|
161
|
-
return axiosObj;
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
// src/http.ts
|
|
165
|
-
var AbbotHttp = class {
|
|
166
|
-
config = defaultConfig;
|
|
161
|
+
// src/http/core.ts
|
|
162
|
+
import { AxiosError } from "axios";
|
|
163
|
+
var AbbotHttp = class extends Axios {
|
|
167
164
|
constructor(config) {
|
|
168
|
-
|
|
165
|
+
super(config);
|
|
169
166
|
}
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
const srcVal = source[key];
|
|
173
|
-
if (srcVal && typeof srcVal === "object" && !Array.isArray(srcVal)) {
|
|
174
|
-
target[key] = deepMerge(target[key], srcVal);
|
|
175
|
-
} else if (srcVal !== void 0) {
|
|
176
|
-
target[key] = srcVal;
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
return target;
|
|
167
|
+
getConfig() {
|
|
168
|
+
return this.config;
|
|
180
169
|
}
|
|
181
170
|
createAxiosInstance(option) {
|
|
182
|
-
const axios2 =
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
if (config.
|
|
189
|
-
if (config.data) {
|
|
190
|
-
if (config.data
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
cryp.encrypt(config.data.get("param")?.toString())
|
|
195
|
-
);
|
|
196
|
-
}
|
|
197
|
-
} else {
|
|
198
|
-
const payload = {};
|
|
199
|
-
payload.param = cryp.encrypt(JSON.stringify(config.data));
|
|
200
|
-
const body = payload ? new URLSearchParams(payload).toString() : "";
|
|
201
|
-
req.data = body;
|
|
202
|
-
req.headers.setContentType(
|
|
203
|
-
option?.contentType ?? this.config.axios.headers.post.contentType
|
|
171
|
+
const axios2 = this.axiosInstance();
|
|
172
|
+
const { cryp, iv } = this.prepareRequestConfig(option);
|
|
173
|
+
axios2.interceptors.request.use(
|
|
174
|
+
(config) => {
|
|
175
|
+
const req = { ...config };
|
|
176
|
+
if (config.method && config.method.toLowerCase() === "post") {
|
|
177
|
+
if (config.data) {
|
|
178
|
+
if (config.data instanceof FormData) {
|
|
179
|
+
if (config.data.get("param")) {
|
|
180
|
+
config.data.set(
|
|
181
|
+
"param",
|
|
182
|
+
cryp.encrypt(config.data.get("param")?.toString())
|
|
204
183
|
);
|
|
205
184
|
}
|
|
185
|
+
} else {
|
|
186
|
+
const payload = {};
|
|
187
|
+
payload.param = cryp.encrypt(JSON.stringify(config.data));
|
|
188
|
+
const body = payload ? new URLSearchParams(payload).toString() : "";
|
|
189
|
+
req.data = body;
|
|
190
|
+
req.headers.setContentType(
|
|
191
|
+
option?.contentType ?? this.config.axios.headers.post.contentType
|
|
192
|
+
);
|
|
206
193
|
}
|
|
207
194
|
}
|
|
208
|
-
req.headers.set("data-code", iv);
|
|
209
|
-
if (this.config.app.trackingId) {
|
|
210
|
-
config.headers.set("X-Trace-Id", this.config.app.trackingId);
|
|
211
|
-
}
|
|
212
|
-
if (this.config.app.userJwt) {
|
|
213
|
-
config.headers.setAuthorization(
|
|
214
|
-
`Bearer ${this.config.app.userJwt}`
|
|
215
|
-
);
|
|
216
|
-
}
|
|
217
|
-
return req;
|
|
218
|
-
},
|
|
219
|
-
(error) => Promise.reject(error)
|
|
220
|
-
);
|
|
221
|
-
axios2.interceptors.response.use(
|
|
222
|
-
(response) => {
|
|
223
|
-
const res = { ...response };
|
|
224
|
-
res.data = cryp.decrypt(response.data);
|
|
225
|
-
return res.data;
|
|
226
|
-
},
|
|
227
|
-
(error) => {
|
|
228
|
-
return this.habdleError(error, cryp);
|
|
229
195
|
}
|
|
230
|
-
|
|
231
|
-
|
|
196
|
+
req.headers.set("data-code", iv);
|
|
197
|
+
if (this.config.app.trackingId) {
|
|
198
|
+
config.headers.set("X-Trace-Id", this.config.app.trackingId);
|
|
199
|
+
}
|
|
200
|
+
if (this.config.app.userJwt) {
|
|
201
|
+
config.headers.setAuthorization(`Bearer ${this.config.app.userJwt}`);
|
|
202
|
+
}
|
|
203
|
+
return req;
|
|
204
|
+
},
|
|
205
|
+
(error) => Promise.reject(error)
|
|
206
|
+
);
|
|
207
|
+
axios2.interceptors.response.use(
|
|
208
|
+
(response) => {
|
|
209
|
+
const res = { ...response };
|
|
210
|
+
res.data = cryp.decrypt(response.data);
|
|
211
|
+
return res.data;
|
|
212
|
+
},
|
|
213
|
+
(error) => {
|
|
214
|
+
return this.habdleError(error, cryp);
|
|
215
|
+
}
|
|
216
|
+
);
|
|
232
217
|
return axios2;
|
|
233
218
|
}
|
|
234
219
|
prepareRequestConfig(option) {
|
|
@@ -242,25 +227,13 @@ var AbbotHttp = class {
|
|
|
242
227
|
const cryp = new Cryp(
|
|
243
228
|
{
|
|
244
229
|
iv,
|
|
245
|
-
keyValue: option?.isInit ?
|
|
230
|
+
keyValue: option?.isInit ? null : this.config.app.encKey
|
|
246
231
|
},
|
|
247
232
|
this.config.app.apiKey
|
|
248
233
|
);
|
|
249
234
|
const form = new URLSearchParams();
|
|
250
235
|
return { cryp, iv, form };
|
|
251
236
|
}
|
|
252
|
-
createAxiosRequestObj(iv) {
|
|
253
|
-
const headers = {
|
|
254
|
-
"data-code": iv
|
|
255
|
-
};
|
|
256
|
-
if (this.config.app.userJwt) {
|
|
257
|
-
headers.Authorization = `Bearer ${this.config.app.userJwt}`;
|
|
258
|
-
}
|
|
259
|
-
if (this.config.app.trackingId) {
|
|
260
|
-
headers["X-Trace-Id"] = this.config.app.trackingId;
|
|
261
|
-
}
|
|
262
|
-
return headers;
|
|
263
|
-
}
|
|
264
237
|
habdleError(error, cryp) {
|
|
265
238
|
const err = {
|
|
266
239
|
message: error.message,
|
|
@@ -334,46 +307,13 @@ var AbbotHttp = class {
|
|
|
334
307
|
}
|
|
335
308
|
async post(url, param, option) {
|
|
336
309
|
const axios2 = this.createAxiosInstance(option);
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
const config = {
|
|
340
|
-
headers: this.createAxiosRequestObj(iv)
|
|
341
|
-
};
|
|
342
|
-
if (param) {
|
|
343
|
-
form.append("param", encodeURI(cryp.encrypt(JSON.stringify(param))));
|
|
344
|
-
}
|
|
345
|
-
try {
|
|
346
|
-
const response = await axios2.post(url, form, config);
|
|
347
|
-
const res = { ...response };
|
|
348
|
-
res.data = cryp.decrypt(response.data);
|
|
349
|
-
return res.data;
|
|
350
|
-
} catch (err) {
|
|
351
|
-
return this.habdleError(err, cryp);
|
|
352
|
-
}
|
|
353
|
-
} else {
|
|
354
|
-
const response = await axios2.post(url, param);
|
|
355
|
-
return response;
|
|
356
|
-
}
|
|
310
|
+
const response = await axios2.post(url, param);
|
|
311
|
+
return response;
|
|
357
312
|
}
|
|
358
313
|
async get(url, option) {
|
|
359
314
|
const axios2 = this.createAxiosInstance(option);
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
const config = {
|
|
363
|
-
headers: this.createAxiosRequestObj(iv)
|
|
364
|
-
};
|
|
365
|
-
try {
|
|
366
|
-
const response = await axios2.get(url, config);
|
|
367
|
-
const res = { ...response };
|
|
368
|
-
res.data = cryp.decrypt(response.data);
|
|
369
|
-
return res.data;
|
|
370
|
-
} catch (err) {
|
|
371
|
-
return this.habdleError(err, cryp);
|
|
372
|
-
}
|
|
373
|
-
} else {
|
|
374
|
-
const response = await axios2.get(url);
|
|
375
|
-
return response;
|
|
376
|
-
}
|
|
315
|
+
const response = await axios2.get(url);
|
|
316
|
+
return response;
|
|
377
317
|
}
|
|
378
318
|
async uploadFile(url, file, param1, param2, param3) {
|
|
379
319
|
let response;
|
|
@@ -404,19 +344,45 @@ var AbbotHttp = class {
|
|
|
404
344
|
}
|
|
405
345
|
return response;
|
|
406
346
|
}
|
|
407
|
-
|
|
408
|
-
|
|
347
|
+
};
|
|
348
|
+
|
|
349
|
+
// src/catchError.ts
|
|
350
|
+
async function catchError(promise, errorToCatch) {
|
|
351
|
+
return promise.then((data) => [void 0, data]).catch((error) => {
|
|
352
|
+
if (errorToCatch === void 0) return [error];
|
|
353
|
+
if (errorToCatch.some((e) => error instanceof e)) return [error];
|
|
354
|
+
throw error;
|
|
355
|
+
});
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
// src/error.ts
|
|
359
|
+
var AppError = class extends Error {
|
|
360
|
+
name = "App Error";
|
|
361
|
+
};
|
|
362
|
+
var RestError = class extends Error {
|
|
363
|
+
name = "Rest Error";
|
|
364
|
+
res;
|
|
365
|
+
constructor(rs) {
|
|
366
|
+
super(rs.message);
|
|
367
|
+
this.res = rs;
|
|
409
368
|
}
|
|
410
369
|
};
|
|
411
370
|
|
|
412
|
-
// src/
|
|
371
|
+
// src/core.ts
|
|
413
372
|
function create(config) {
|
|
414
373
|
const http = new AbbotHttp(config);
|
|
415
374
|
return http;
|
|
416
375
|
}
|
|
417
376
|
export {
|
|
377
|
+
AbbotHttp,
|
|
418
378
|
AppError,
|
|
379
|
+
Axios,
|
|
380
|
+
Cryp,
|
|
381
|
+
ES,
|
|
419
382
|
RestError,
|
|
383
|
+
Secure,
|
|
420
384
|
catchError,
|
|
421
|
-
create
|
|
385
|
+
create,
|
|
386
|
+
deepMerge,
|
|
387
|
+
defaultConfig
|
|
422
388
|
};
|