abbot-http-client 0.0.9 → 0.0.11

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
@@ -164,12 +164,11 @@ var CoreHttp = class {
164
164
  constructor(config) {
165
165
  this.config = config;
166
166
  }
167
- createAxiosInstance(user) {
167
+ createAxiosInstance(userJWT) {
168
168
  const axios2 = axiosConf(this.config);
169
169
  const iv = Secure.createId();
170
170
  const cryp = new Cryp({
171
- iv,
172
- keyValue: user.key
171
+ iv
173
172
  });
174
173
  axios2.interceptors.request.use(
175
174
  (config) => {
@@ -193,11 +192,11 @@ var CoreHttp = class {
193
192
  }
194
193
  }
195
194
  req.headers.set("data-code", iv);
196
- if (user.trackingId) {
197
- req.headers.set("X-Trace-Id", user.trackingId);
195
+ if (this.config.app?.trackingId) {
196
+ req.headers.set("X-Trace-Id", this.config.app.trackingId);
198
197
  }
199
- if (user.token) {
200
- req.headers.setAuthorization(`Bearer ${user.token}`);
198
+ if (userJWT) {
199
+ req.headers.setAuthorization(`Bearer ${userJWT}`);
201
200
  }
202
201
  return req;
203
202
  },
@@ -283,78 +282,40 @@ var CoreHttp = class {
283
282
  );
284
283
  return axios2;
285
284
  }
286
- async post(param1, url, param) {
287
- let axios2;
288
- if (typeof param1 === "function") {
289
- const user = param1();
290
- axios2 = this.createAxiosInstance({
291
- key: user?.key,
292
- token: user?.token,
293
- trackingId: user?.trackingId
294
- });
295
- } else {
296
- axios2 = this.createAxiosInstance({
297
- key: param1?.key,
298
- token: param1?.token,
299
- trackingId: param1?.trackingId
300
- });
301
- }
285
+ async post(url, param, jwt) {
286
+ const axios2 = this.createAxiosInstance(jwt);
302
287
  const response = await axios2.post(url, param);
303
288
  return response;
304
289
  }
305
- async get(param1, url) {
306
- let axios2;
307
- if (typeof param1 === "function") {
308
- const user = param1();
309
- axios2 = this.createAxiosInstance({
310
- key: user?.key,
311
- token: user?.token,
312
- trackingId: user?.trackingId
313
- });
314
- } else {
315
- axios2 = this.createAxiosInstance({
316
- key: param1?.key,
317
- token: param1?.token,
318
- trackingId: param1?.trackingId
319
- });
320
- }
290
+ async get(url, jwt) {
291
+ const axios2 = this.createAxiosInstance(jwt);
321
292
  const response = await axios2.get(url);
322
293
  return response;
323
294
  }
324
- async uploadFile(param1, url, file, param2, param3) {
325
- let axios2;
326
- if (typeof param1 === "function") {
327
- const user = param1();
328
- axios2 = this.createAxiosInstance({
329
- key: user?.key,
330
- token: user?.token,
331
- trackingId: user?.trackingId
332
- });
333
- } else {
334
- axios2 = this.createAxiosInstance({
335
- key: param1?.key,
336
- token: param1?.token,
337
- trackingId: param1?.trackingId
338
- });
339
- }
295
+ async uploadFile(url, file, param1, param2, param3) {
296
+ let response;
340
297
  const formData = new FormData();
341
- if (param2 instanceof Array) {
342
- if (file instanceof Array) {
343
- param2.forEach((k, i) => {
344
- formData.append(k, file[i]);
345
- });
346
- } else {
347
- formData.append("file", file);
298
+ if (file instanceof Array) {
299
+ const fileKeys = param1;
300
+ const param = param2;
301
+ const jwt = param3;
302
+ const axios2 = this.createAxiosInstance(jwt);
303
+ fileKeys.forEach((k, i) => {
304
+ formData.append(k, file[i]);
305
+ });
306
+ if (param) {
307
+ formData.append("param", JSON.stringify(param));
348
308
  }
309
+ response = await axios2.post(url, formData);
349
310
  } else {
350
- if (param2) {
351
- formData.append("param", JSON.stringify(param2));
311
+ const param = param1;
312
+ const jwt = param2;
313
+ const axios2 = this.createAxiosInstance(jwt);
314
+ if (param) {
315
+ formData.append("param", JSON.stringify(param));
352
316
  }
317
+ response = await axios2.post(url, formData);
353
318
  }
354
- if (param3) {
355
- formData.append("param", JSON.stringify(param3));
356
- }
357
- const response = await axios2.post(url, formData);
358
319
  return response;
359
320
  }
360
321
  getConfig() {
@@ -378,7 +339,8 @@ var cfg = {
378
339
  app: {
379
340
  redirectUrl: "/login",
380
341
  apiKey: "",
381
- timezone: "Asia/Bangkok"
342
+ timezone: "Asia/Bangkok",
343
+ trackingId: ""
382
344
  }
383
345
  };
384
346
  function getConfig() {
package/dist/index.d.cts CHANGED
@@ -2,23 +2,14 @@ import { AxiosResponse } from 'axios';
2
2
 
3
3
  declare function catchError<T, E extends new (err?: any) => Error>(promise: Promise<T>, errorToCatch?: E[]): Promise<[undefined, T] | [InstanceType<E>]>;
4
4
 
5
- interface credential {
6
- key: string | undefined;
7
- token: string | undefined;
8
- trackingId?: string;
9
- }
10
5
  declare class CoreHttp {
11
6
  private config;
12
7
  constructor(config: AbbotConfig);
13
8
  private createAxiosInstance;
14
- post<T, U extends credential = credential>(middleware: () => U | null, url: string, param?: Record<string, any> | null): Promise<AxiosResponse<T>>;
15
- post<T, U extends credential = credential>(user: U | null, url: string, param?: Record<string, any> | null): Promise<AxiosResponse<T>>;
16
- get<T, U extends credential = credential>(middleware: () => U | null, url: string): Promise<AxiosResponse<T>>;
17
- get<T, U extends credential = credential>(user: U | null, url: string): Promise<AxiosResponse<T>>;
18
- uploadFile<T, U extends credential = credential>(middleware: () => U | null, url: string, file: File, param?: Record<string, any> | null): Promise<AxiosResponse<T>>;
19
- uploadFile<T, U extends credential = credential>(middleware: () => U | null, url: string, file: File[], fileKeys: string[], param?: Record<string, any> | null): Promise<AxiosResponse<T>>;
20
- uploadFile<T, U extends credential = credential>(user: U | null, url: string, file: File, param?: Record<string, any> | null): Promise<AxiosResponse<T>>;
21
- uploadFile<T, U extends credential = credential>(user: U | null, url: string, file: File[], fileKeys: string[], param?: Record<string, any> | null): Promise<AxiosResponse<T>>;
9
+ post<T>(url: string, param?: Record<string, any> | null, jwt?: string): Promise<AxiosResponse<T>>;
10
+ get<T>(url: string, jwt?: string): Promise<AxiosResponse<T>>;
11
+ uploadFile<T>(url: string, file: File, param?: Record<string, any> | null, jwt?: string): Promise<AxiosResponse<T>>;
12
+ uploadFile<T>(url: string, file: File[], fileKeys: string[], param?: Record<string, any> | null, jwt?: string): Promise<AxiosResponse<T>>;
22
13
  getConfig(): AbbotConfig;
23
14
  }
24
15
 
@@ -38,6 +29,7 @@ interface AbbotConfig {
38
29
  redirectUrl?: string;
39
30
  apiKey?: string;
40
31
  timezone?: string;
32
+ trackingId?: string;
41
33
  };
42
34
  }
43
35
  declare function create(config: AbbotConfig): CoreHttp;
package/dist/index.d.ts CHANGED
@@ -2,23 +2,14 @@ import { AxiosResponse } from 'axios';
2
2
 
3
3
  declare function catchError<T, E extends new (err?: any) => Error>(promise: Promise<T>, errorToCatch?: E[]): Promise<[undefined, T] | [InstanceType<E>]>;
4
4
 
5
- interface credential {
6
- key: string | undefined;
7
- token: string | undefined;
8
- trackingId?: string;
9
- }
10
5
  declare class CoreHttp {
11
6
  private config;
12
7
  constructor(config: AbbotConfig);
13
8
  private createAxiosInstance;
14
- post<T, U extends credential = credential>(middleware: () => U | null, url: string, param?: Record<string, any> | null): Promise<AxiosResponse<T>>;
15
- post<T, U extends credential = credential>(user: U | null, url: string, param?: Record<string, any> | null): Promise<AxiosResponse<T>>;
16
- get<T, U extends credential = credential>(middleware: () => U | null, url: string): Promise<AxiosResponse<T>>;
17
- get<T, U extends credential = credential>(user: U | null, url: string): Promise<AxiosResponse<T>>;
18
- uploadFile<T, U extends credential = credential>(middleware: () => U | null, url: string, file: File, param?: Record<string, any> | null): Promise<AxiosResponse<T>>;
19
- uploadFile<T, U extends credential = credential>(middleware: () => U | null, url: string, file: File[], fileKeys: string[], param?: Record<string, any> | null): Promise<AxiosResponse<T>>;
20
- uploadFile<T, U extends credential = credential>(user: U | null, url: string, file: File, param?: Record<string, any> | null): Promise<AxiosResponse<T>>;
21
- uploadFile<T, U extends credential = credential>(user: U | null, url: string, file: File[], fileKeys: string[], param?: Record<string, any> | null): Promise<AxiosResponse<T>>;
9
+ post<T>(url: string, param?: Record<string, any> | null, jwt?: string): Promise<AxiosResponse<T>>;
10
+ get<T>(url: string, jwt?: string): Promise<AxiosResponse<T>>;
11
+ uploadFile<T>(url: string, file: File, param?: Record<string, any> | null, jwt?: string): Promise<AxiosResponse<T>>;
12
+ uploadFile<T>(url: string, file: File[], fileKeys: string[], param?: Record<string, any> | null, jwt?: string): Promise<AxiosResponse<T>>;
22
13
  getConfig(): AbbotConfig;
23
14
  }
24
15
 
@@ -38,6 +29,7 @@ interface AbbotConfig {
38
29
  redirectUrl?: string;
39
30
  apiKey?: string;
40
31
  timezone?: string;
32
+ trackingId?: string;
41
33
  };
42
34
  }
43
35
  declare function create(config: AbbotConfig): CoreHttp;
package/dist/index.js CHANGED
@@ -127,12 +127,11 @@ var CoreHttp = class {
127
127
  constructor(config) {
128
128
  this.config = config;
129
129
  }
130
- createAxiosInstance(user) {
130
+ createAxiosInstance(userJWT) {
131
131
  const axios2 = axiosConf(this.config);
132
132
  const iv = Secure.createId();
133
133
  const cryp = new Cryp({
134
- iv,
135
- keyValue: user.key
134
+ iv
136
135
  });
137
136
  axios2.interceptors.request.use(
138
137
  (config) => {
@@ -156,11 +155,11 @@ var CoreHttp = class {
156
155
  }
157
156
  }
158
157
  req.headers.set("data-code", iv);
159
- if (user.trackingId) {
160
- req.headers.set("X-Trace-Id", user.trackingId);
158
+ if (this.config.app?.trackingId) {
159
+ req.headers.set("X-Trace-Id", this.config.app.trackingId);
161
160
  }
162
- if (user.token) {
163
- req.headers.setAuthorization(`Bearer ${user.token}`);
161
+ if (userJWT) {
162
+ req.headers.setAuthorization(`Bearer ${userJWT}`);
164
163
  }
165
164
  return req;
166
165
  },
@@ -246,78 +245,40 @@ var CoreHttp = class {
246
245
  );
247
246
  return axios2;
248
247
  }
249
- async post(param1, url, param) {
250
- let axios2;
251
- if (typeof param1 === "function") {
252
- const user = param1();
253
- axios2 = this.createAxiosInstance({
254
- key: user?.key,
255
- token: user?.token,
256
- trackingId: user?.trackingId
257
- });
258
- } else {
259
- axios2 = this.createAxiosInstance({
260
- key: param1?.key,
261
- token: param1?.token,
262
- trackingId: param1?.trackingId
263
- });
264
- }
248
+ async post(url, param, jwt) {
249
+ const axios2 = this.createAxiosInstance(jwt);
265
250
  const response = await axios2.post(url, param);
266
251
  return response;
267
252
  }
268
- async get(param1, url) {
269
- let axios2;
270
- if (typeof param1 === "function") {
271
- const user = param1();
272
- axios2 = this.createAxiosInstance({
273
- key: user?.key,
274
- token: user?.token,
275
- trackingId: user?.trackingId
276
- });
277
- } else {
278
- axios2 = this.createAxiosInstance({
279
- key: param1?.key,
280
- token: param1?.token,
281
- trackingId: param1?.trackingId
282
- });
283
- }
253
+ async get(url, jwt) {
254
+ const axios2 = this.createAxiosInstance(jwt);
284
255
  const response = await axios2.get(url);
285
256
  return response;
286
257
  }
287
- async uploadFile(param1, url, file, param2, param3) {
288
- let axios2;
289
- if (typeof param1 === "function") {
290
- const user = param1();
291
- axios2 = this.createAxiosInstance({
292
- key: user?.key,
293
- token: user?.token,
294
- trackingId: user?.trackingId
295
- });
296
- } else {
297
- axios2 = this.createAxiosInstance({
298
- key: param1?.key,
299
- token: param1?.token,
300
- trackingId: param1?.trackingId
301
- });
302
- }
258
+ async uploadFile(url, file, param1, param2, param3) {
259
+ let response;
303
260
  const formData = new FormData();
304
- if (param2 instanceof Array) {
305
- if (file instanceof Array) {
306
- param2.forEach((k, i) => {
307
- formData.append(k, file[i]);
308
- });
309
- } else {
310
- formData.append("file", file);
261
+ if (file instanceof Array) {
262
+ const fileKeys = param1;
263
+ const param = param2;
264
+ const jwt = param3;
265
+ const axios2 = this.createAxiosInstance(jwt);
266
+ fileKeys.forEach((k, i) => {
267
+ formData.append(k, file[i]);
268
+ });
269
+ if (param) {
270
+ formData.append("param", JSON.stringify(param));
311
271
  }
272
+ response = await axios2.post(url, formData);
312
273
  } else {
313
- if (param2) {
314
- formData.append("param", JSON.stringify(param2));
274
+ const param = param1;
275
+ const jwt = param2;
276
+ const axios2 = this.createAxiosInstance(jwt);
277
+ if (param) {
278
+ formData.append("param", JSON.stringify(param));
315
279
  }
280
+ response = await axios2.post(url, formData);
316
281
  }
317
- if (param3) {
318
- formData.append("param", JSON.stringify(param3));
319
- }
320
- const response = await axios2.post(url, formData);
321
282
  return response;
322
283
  }
323
284
  getConfig() {
@@ -341,7 +302,8 @@ var cfg = {
341
302
  app: {
342
303
  redirectUrl: "/login",
343
304
  apiKey: "",
344
- timezone: "Asia/Bangkok"
305
+ timezone: "Asia/Bangkok",
306
+ trackingId: ""
345
307
  }
346
308
  };
347
309
  function getConfig() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "abbot-http-client",
3
- "version": "0.0.9",
3
+ "version": "0.0.11",
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",