@brostark/solutions-client 1.1.8 → 1.1.9
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 +7 -0
- package/dist/helper.d.ts.map +1 -1
- package/dist/helper.js +22 -1
- package/dist/lib/httpRequest.d.ts.map +1 -1
- package/dist/lib/httpRequest.js +2 -9
- package/package.json +1 -1
- package/src/helper.ts +26 -0
- package/src/lib/httpRequest.ts +4 -10
package/dist/helper.d.ts
CHANGED
|
@@ -6,4 +6,11 @@
|
|
|
6
6
|
* @returns The adjusted limit
|
|
7
7
|
*/
|
|
8
8
|
export declare const setLimit: (limit: number, min: number, max: number) => number;
|
|
9
|
+
/**
|
|
10
|
+
* Builds URL with query string without relying on URL/URLSearchParams (React Native compatible)
|
|
11
|
+
* @param baseUrl - The base URL
|
|
12
|
+
* @param params - The parameters to add to the URL
|
|
13
|
+
* @returns The URL with the query string
|
|
14
|
+
*/
|
|
15
|
+
export declare const buildUrlWithParams: (baseUrl: string, params?: Record<string, unknown>) => string;
|
|
9
16
|
//# sourceMappingURL=helper.d.ts.map
|
package/dist/helper.d.ts.map
CHANGED
|
@@ -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"}
|
|
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"}
|
package/dist/helper.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.setLimit = void 0;
|
|
3
|
+
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
|
|
@@ -12,3 +12,24 @@ const setLimit = (limit, min, max) => {
|
|
|
12
12
|
return Math.min(Math.max(limit, min), max);
|
|
13
13
|
};
|
|
14
14
|
exports.setLimit = setLimit;
|
|
15
|
+
/**
|
|
16
|
+
* Builds URL with query string without relying on URL/URLSearchParams (React Native compatible)
|
|
17
|
+
* @param baseUrl - The base URL
|
|
18
|
+
* @param params - The parameters to add to the URL
|
|
19
|
+
* @returns The URL with the query string
|
|
20
|
+
*/
|
|
21
|
+
const buildUrlWithParams = (baseUrl, params) => {
|
|
22
|
+
if (!params || Object.keys(params).length === 0) {
|
|
23
|
+
return baseUrl;
|
|
24
|
+
}
|
|
25
|
+
const search = Object.entries(params)
|
|
26
|
+
.filter(([, value]) => value !== undefined && value !== null)
|
|
27
|
+
.map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(String(value))}`)
|
|
28
|
+
.join("&");
|
|
29
|
+
if (!search) {
|
|
30
|
+
return baseUrl;
|
|
31
|
+
}
|
|
32
|
+
const separator = baseUrl.includes("?") ? "&" : "?";
|
|
33
|
+
return `${baseUrl}${separator}${search}`;
|
|
34
|
+
};
|
|
35
|
+
exports.buildUrlWithParams = buildUrlWithParams;
|
|
@@ -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;
|
|
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,cAAc,GAAI,QAAQ,MAAM,EAAE,QAAQ,MAAM,iBAI5D,CAAA;AAGD;;;;;;GAMG;AACH,eAAO,MAAM,eAAe,GAAI,QAAQ,MAAM,EAAE,QAAQ,MAAM,EAAE,MAAM,OAAO,iBAK5E,CAAA;AAGD;;;;;GAKG;AACH,eAAO,MAAM,iBAAiB,GAAI,QAAQ,MAAM,EAAE,QAAQ,MAAM,iBAI/D,CAAA;AAED;;;;;;GAMG;AACH,eAAO,MAAM,iBAAiB,GAAI,QAAQ,MAAM,EAAE,QAAQ,MAAM,EAAE,MAAM,IAAI,iBAQ3E,CAAA"}
|
package/dist/lib/httpRequest.js
CHANGED
|
@@ -12,14 +12,7 @@ const constant_1 = require("../constant");
|
|
|
12
12
|
*/
|
|
13
13
|
const httpRequest = async (apiUrl, apiKey, options) => {
|
|
14
14
|
const { headers, params, data, ...rest } = options;
|
|
15
|
-
const url =
|
|
16
|
-
if (params) {
|
|
17
|
-
for (const [key, value] of Object.entries(params)) {
|
|
18
|
-
if (value !== undefined && value !== null) {
|
|
19
|
-
url.searchParams.set(key, String(value));
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
}
|
|
15
|
+
const url = (0, helper_1.buildUrlWithParams)(apiUrl, params);
|
|
23
16
|
const config = {
|
|
24
17
|
mode: "cors",
|
|
25
18
|
method: "GET",
|
|
@@ -42,7 +35,7 @@ const httpRequest = async (apiUrl, apiKey, options) => {
|
|
|
42
35
|
}
|
|
43
36
|
}
|
|
44
37
|
config.headers = finalHeaders;
|
|
45
|
-
const response = await fetch(url
|
|
38
|
+
const response = await fetch(url, config);
|
|
46
39
|
if (!response.ok) {
|
|
47
40
|
const errorData = await response.json().catch(() => ({ message: response.statusText }));
|
|
48
41
|
throw new Error(errorData || "HTTP request failed");
|
package/package.json
CHANGED
package/src/helper.ts
CHANGED
|
@@ -8,3 +8,29 @@
|
|
|
8
8
|
export const setLimit = (limit: number, min: number, max: number) => {
|
|
9
9
|
return Math.min(Math.max(limit, min), max);
|
|
10
10
|
}
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Builds URL with query string without relying on URL/URLSearchParams (React Native compatible)
|
|
15
|
+
* @param baseUrl - The base URL
|
|
16
|
+
* @param params - The parameters to add to the URL
|
|
17
|
+
* @returns The URL with the query string
|
|
18
|
+
*/
|
|
19
|
+
export const buildUrlWithParams = (baseUrl: string, params?: Record<string, unknown>): string => {
|
|
20
|
+
if (!params || Object.keys(params).length === 0) {
|
|
21
|
+
return baseUrl;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const search = Object.entries(params)
|
|
25
|
+
.filter(([, value]) => value !== undefined && value !== null)
|
|
26
|
+
.map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(String(value))}`)
|
|
27
|
+
.join("&");
|
|
28
|
+
|
|
29
|
+
if (!search) {
|
|
30
|
+
return baseUrl;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const separator = baseUrl.includes("?") ? "&" : "?";
|
|
34
|
+
|
|
35
|
+
return `${baseUrl}${separator}${search}`;
|
|
36
|
+
}
|
package/src/lib/httpRequest.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ListOptions } from "./types";
|
|
2
|
-
import { setLimit } from "../helper";
|
|
2
|
+
import { buildUrlWithParams, setLimit } from "../helper";
|
|
3
3
|
import { MAX_LIMIT } from "../constant";
|
|
4
4
|
|
|
5
5
|
type RequestOptions = Omit<RequestInit, "body"> & {
|
|
@@ -7,6 +7,7 @@ type RequestOptions = Omit<RequestInit, "body"> & {
|
|
|
7
7
|
data?: unknown;
|
|
8
8
|
};
|
|
9
9
|
|
|
10
|
+
|
|
10
11
|
/**
|
|
11
12
|
* Base function to make an HTTP request
|
|
12
13
|
* @param apiUrl - The API URL
|
|
@@ -17,14 +18,7 @@ type RequestOptions = Omit<RequestInit, "body"> & {
|
|
|
17
18
|
export const httpRequest = async (apiUrl: string, apiKey: string, options: RequestOptions) => {
|
|
18
19
|
const { headers, params, data, ...rest } = options;
|
|
19
20
|
|
|
20
|
-
const url =
|
|
21
|
-
if (params) {
|
|
22
|
-
for (const [key, value] of Object.entries(params)) {
|
|
23
|
-
if (value !== undefined && value !== null) {
|
|
24
|
-
url.searchParams.set(key, String(value));
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
}
|
|
21
|
+
const url = buildUrlWithParams(apiUrl, params);
|
|
28
22
|
|
|
29
23
|
const config: RequestInit = {
|
|
30
24
|
mode: "cors",
|
|
@@ -51,7 +45,7 @@ export const httpRequest = async (apiUrl: string, apiKey: string, options: Reque
|
|
|
51
45
|
|
|
52
46
|
config.headers = finalHeaders;
|
|
53
47
|
|
|
54
|
-
const response = await fetch(url
|
|
48
|
+
const response = await fetch(url, config);
|
|
55
49
|
|
|
56
50
|
if (!response.ok) {
|
|
57
51
|
const errorData = await response.json().catch(() => ({ message: response.statusText }));
|