abbot-http-client 0.0.28 → 0.0.30

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