@brostark/solutions-client 1.1.14 → 1.1.15

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/helper.d.ts CHANGED
@@ -13,4 +13,10 @@ export declare const setLimit: (limit: number, min: number, max: number) => numb
13
13
  * @returns The URL with the query string
14
14
  */
15
15
  export declare const buildUrlWithParams: (baseUrl: string, params?: Record<string, unknown>) => string;
16
+ /**
17
+ * Remove undefined values of an objet
18
+ * @param record - The record
19
+ * @returns The record filtered
20
+ */
21
+ export declare const removeUndefinedValues: <T extends object>(record: T) => T;
16
22
  //# sourceMappingURL=helper.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"helper.d.ts","sourceRoot":"","sources":["../src/helper.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,eAAO,MAAM,QAAQ,GAAI,OAAO,MAAM,EAAE,KAAK,MAAM,EAAE,KAAK,MAAM,WAE/D,CAAA;AAGD;;;;;GAKG;AACH,eAAO,MAAM,kBAAkB,GAAI,SAAS,MAAM,EAAE,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAAG,MAiBtF,CAAA"}
1
+ {"version":3,"file":"helper.d.ts","sourceRoot":"","sources":["../src/helper.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,eAAO,MAAM,QAAQ,GAAI,OAAO,MAAM,EAAE,KAAK,MAAM,EAAE,KAAK,MAAM,WAE/D,CAAA;AAGD;;;;;GAKG;AACH,eAAO,MAAM,kBAAkB,GAAI,SAAS,MAAM,EAAE,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAAG,MAiBtF,CAAA;AAGD;;;;GAIG;AACH,eAAO,MAAM,qBAAqB,GAAI,CAAC,SAAS,MAAM,EAAE,QAAQ,CAAC,KAAG,CAUnE,CAAA"}
package/dist/helper.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.buildUrlWithParams = exports.setLimit = void 0;
3
+ exports.removeUndefinedValues = exports.buildUrlWithParams = exports.setLimit = void 0;
4
4
  /**
5
5
  * Sets a limit between a minimum and maximum value
6
6
  * @param limit - The desired limit
@@ -33,3 +33,18 @@ const buildUrlWithParams = (baseUrl, params) => {
33
33
  return `${baseUrl}${separator}${search}`;
34
34
  };
35
35
  exports.buildUrlWithParams = buildUrlWithParams;
36
+ /**
37
+ * Remove undefined values of an objet
38
+ * @param record - The record
39
+ * @returns The record filtered
40
+ */
41
+ const removeUndefinedValues = (record) => {
42
+ Object.keys(record).forEach((key) => {
43
+ const value = record[key];
44
+ if (typeof value === "undefined") {
45
+ delete record[key];
46
+ }
47
+ });
48
+ return record;
49
+ };
50
+ exports.removeUndefinedValues = removeUndefinedValues;
@@ -2,6 +2,7 @@ import { ListOptions } from "./types";
2
2
  type RequestOptions = Omit<RequestInit, "body"> & {
3
3
  params?: Record<string, unknown>;
4
4
  data?: unknown;
5
+ timeout?: number;
5
6
  };
6
7
  /**
7
8
  * Base function to make an HTTP request
@@ -1 +1 @@
1
- {"version":3,"file":"httpRequest.d.ts","sourceRoot":"","sources":["../../src/lib/httpRequest.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AAItC,KAAK,cAAc,GAAG,IAAI,CAAC,WAAW,EAAE,MAAM,CAAC,GAAG;IAChD,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IACjC,IAAI,CAAC,EAAE,OAAO,CAAC;CAChB,CAAC;AAGF;;;;;;GAMG;AACH,eAAO,MAAM,WAAW,GAAU,QAAQ,MAAM,EAAE,QAAQ,MAAM,EAAE,SAAS,cAAc,iBA2CxF,CAAA;AAED;;;;;;GAMG;AACH,eAAO,MAAM,eAAe,GAAI,QAAQ,MAAM,EAAE,QAAQ,MAAM,EAAE,SAAS,WAAW,iBAOnF,CAAA;AAED;;;;;GAKG;AACH,eAAO,MAAM,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,iBAI/D,CAAA;AAED;;;;;;GAMG;AACH,eAAO,MAAM,iBAAiB,GAAI,QAAQ,MAAM,EAAE,QAAQ,MAAM,EAAE,MAAM,IAAI,iBAQ3E,CAAA"}
@@ -11,13 +11,16 @@ const constant_1 = require("../constant");
11
11
  * @returns A promise with the request response
12
12
  */
13
13
  const httpRequest = async (apiUrl, apiKey, options) => {
14
- const { headers, params, data, ...rest } = options;
14
+ const { headers, params, data, timeout: timeoutValue, ...rest } = options;
15
15
  const url = (0, helper_1.buildUrlWithParams)(apiUrl, params);
16
- const config = {
16
+ const controller = typeof timeoutValue === "number" ? new AbortController() : undefined;
17
+ const timeoutId = typeof timeoutValue === "number" ? setTimeout(() => controller.abort(), timeoutValue) : undefined;
18
+ const config = (0, helper_1.removeUndefinedValues)({
17
19
  mode: "cors",
18
20
  method: "GET",
19
21
  ...rest,
20
- };
22
+ signal: controller?.signal,
23
+ });
21
24
  const finalHeaders = {
22
25
  "x-api-key": apiKey,
23
26
  ...headers,
@@ -47,6 +50,11 @@ const httpRequest = async (apiUrl, apiKey, options) => {
47
50
  catch (e) {
48
51
  return responseText;
49
52
  }
53
+ finally {
54
+ if (timeoutId !== undefined) {
55
+ clearTimeout(timeoutId);
56
+ }
57
+ }
50
58
  };
51
59
  exports.httpRequest = httpRequest;
52
60
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@brostark/solutions-client",
3
- "version": "1.1.14",
3
+ "version": "1.1.15",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "scripts": {
package/src/helper.ts CHANGED
@@ -33,4 +33,22 @@ export const buildUrlWithParams = (baseUrl: string, params?: Record<string, unkn
33
33
  const separator = baseUrl.includes("?") ? "&" : "?";
34
34
 
35
35
  return `${baseUrl}${separator}${search}`;
36
- }
36
+ }
37
+
38
+
39
+ /**
40
+ * Remove undefined values of an objet
41
+ * @param record - The record
42
+ * @returns The record filtered
43
+ */
44
+ export const removeUndefinedValues = <T extends object>(record: T): T => {
45
+ Object.keys(record).forEach((key) => {
46
+ const value = record[key as keyof typeof record];
47
+
48
+ if (typeof value === "undefined") {
49
+ delete record[key as keyof typeof record];
50
+ }
51
+ });
52
+
53
+ return record as T;
54
+ }
@@ -1,10 +1,11 @@
1
1
  import { ListOptions } from "./types";
2
- import { buildUrlWithParams, setLimit } from "../helper";
2
+ import { buildUrlWithParams, removeUndefinedValues, setLimit } from "../helper";
3
3
  import { MAX_LIMIT } from "../constant";
4
4
 
5
5
  type RequestOptions = Omit<RequestInit, "body"> & {
6
6
  params?: Record<string, unknown>;
7
7
  data?: unknown;
8
+ timeout?: number;
8
9
  };
9
10
 
10
11
 
@@ -16,15 +17,18 @@ type RequestOptions = Omit<RequestInit, "body"> & {
16
17
  * @returns A promise with the request response
17
18
  */
18
19
  export const httpRequest = async (apiUrl: string, apiKey: string, options: RequestOptions) => {
19
- const { headers, params, data, ...rest } = options;
20
+ const { headers, params, data, timeout: timeoutValue,...rest } = options;
20
21
 
21
22
  const url = buildUrlWithParams(apiUrl, params);
23
+ const controller = typeof timeoutValue === "number" ? new AbortController() : undefined;
24
+ const timeoutId = typeof timeoutValue === "number" ? setTimeout(() => controller!.abort(), timeoutValue) : undefined;
22
25
 
23
- const config: RequestInit = {
26
+ const config: RequestInit = removeUndefinedValues({
24
27
  mode: "cors",
25
28
  method: "GET",
26
29
  ...rest,
27
- };
30
+ signal: controller?.signal,
31
+ });
28
32
 
29
33
  const finalHeaders: Record<string, string> = {
30
34
  "x-api-key": apiKey,
@@ -57,6 +61,10 @@ export const httpRequest = async (apiUrl: string, apiKey: string, options: Reque
57
61
  return JSON.parse(responseText);
58
62
  } catch (e) {
59
63
  return responseText;
64
+ } finally {
65
+ if (timeoutId !== undefined) {
66
+ clearTimeout(timeoutId);
67
+ }
60
68
  }
61
69
  }
62
70