@dexyn/common-library 1.0.10 → 1.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/api/apiClientUtils.d.ts +9 -9
- package/api/apiClientUtils.js +14 -6
- package/package.json +1 -1
package/api/apiClientUtils.d.ts
CHANGED
@@ -10,19 +10,19 @@ export interface ApiOptions {
|
|
10
10
|
* custom HTTP methods, headers, and request bodies.
|
11
11
|
*/
|
12
12
|
export declare const createApiClient: (basePath: string) => {
|
13
|
-
get: <T>(endpoint: string, queryParameters?: Record<string, string | number | boolean | null | undefined>, pathParameters?:
|
14
|
-
post: <T>(endpoint: string, body?: unknown, pathParameters?:
|
15
|
-
put: <T>(endpoint: string, body?: unknown, pathParameters?:
|
16
|
-
delete: <T>(endpoint: string, pathParameters?:
|
13
|
+
get: <T>(endpoint: string, queryParameters?: Record<string, string | number | boolean | null | undefined>, pathParameters?: unknown) => Promise<T>;
|
14
|
+
post: <T>(endpoint: string, body?: unknown, pathParameters?: unknown) => Promise<T>;
|
15
|
+
put: <T>(endpoint: string, body?: unknown, pathParameters?: unknown) => Promise<T>;
|
16
|
+
delete: <T>(endpoint: string, pathParameters?: unknown) => Promise<T>;
|
17
17
|
};
|
18
|
-
declare function resolveUrl(template: string, params:
|
18
|
+
declare function resolveUrl(template: string, params: unknown): string;
|
19
19
|
export declare const ApiClientUtils: {
|
20
20
|
resolveUrl: typeof resolveUrl;
|
21
21
|
createApiClient: (basePath: string) => {
|
22
|
-
get: <T>(endpoint: string, queryParameters?: Record<string, string | number | boolean | null | undefined>, pathParameters?:
|
23
|
-
post: <T>(endpoint: string, body?: unknown, pathParameters?:
|
24
|
-
put: <T>(endpoint: string, body?: unknown, pathParameters?:
|
25
|
-
delete: <T>(endpoint: string, pathParameters?:
|
22
|
+
get: <T>(endpoint: string, queryParameters?: Record<string, string | number | boolean | null | undefined>, pathParameters?: unknown) => Promise<T>;
|
23
|
+
post: <T>(endpoint: string, body?: unknown, pathParameters?: unknown) => Promise<T>;
|
24
|
+
put: <T>(endpoint: string, body?: unknown, pathParameters?: unknown) => Promise<T>;
|
25
|
+
delete: <T>(endpoint: string, pathParameters?: unknown) => Promise<T>;
|
26
26
|
};
|
27
27
|
};
|
28
28
|
export {};
|
package/api/apiClientUtils.js
CHANGED
@@ -15,7 +15,7 @@ const createApiClient = (basePath) => {
|
|
15
15
|
) => {
|
16
16
|
const { method = 'GET', headers, body } = options;
|
17
17
|
// Resolve the URL template with path parameters
|
18
|
-
const resolvedEndpoint = resolveUrl(endpoint, pathParameters);
|
18
|
+
const resolvedEndpoint = pathParameters ? resolveUrl(endpoint, pathParameters) : endpoint;
|
19
19
|
// Construct query string from queryParameters
|
20
20
|
const queryString = new URLSearchParams(Object.entries(queryParameters).reduce((acc, [key, value]) => {
|
21
21
|
if (value !== undefined) {
|
@@ -49,18 +49,26 @@ const createApiClient = (basePath) => {
|
|
49
49
|
};
|
50
50
|
return {
|
51
51
|
get: (endpoint, queryParameters = {}, pathParameters = {}) => request(endpoint, { method: 'GET' }, queryParameters, pathParameters),
|
52
|
-
post: (endpoint, body, pathParameters = {}) => request(endpoint, { method: 'POST', body }, pathParameters),
|
53
|
-
put: (endpoint, body, pathParameters = {}) => request(endpoint, { method: 'PUT', body }, pathParameters),
|
54
|
-
delete: (endpoint, pathParameters = {}) => request(endpoint, { method: 'DELETE' }),
|
52
|
+
post: (endpoint, body, pathParameters = {}) => request(endpoint, { method: 'POST', body }, {}, pathParameters),
|
53
|
+
put: (endpoint, body, pathParameters = {}) => request(endpoint, { method: 'PUT', body }, {}, pathParameters),
|
54
|
+
delete: (endpoint, pathParameters = {}) => request(endpoint, { method: 'DELETE' }, {}, pathParameters),
|
55
55
|
};
|
56
56
|
};
|
57
57
|
exports.createApiClient = createApiClient;
|
58
58
|
function resolveUrl(template, params) {
|
59
|
+
if (typeof params !== 'object' || params === null) {
|
60
|
+
throw new Error("The 'params' argument must be a non-null object.");
|
61
|
+
}
|
59
62
|
return template.replace(/:([a-zA-Z_]+)/g, (_, key) => {
|
60
|
-
|
63
|
+
const paramsObj = params;
|
64
|
+
if (!(key in paramsObj)) {
|
61
65
|
throw new Error(`Missing path parameter: ${key}`);
|
62
66
|
}
|
63
|
-
|
67
|
+
const value = paramsObj[key];
|
68
|
+
if (typeof value !== 'string' && typeof value !== 'number') {
|
69
|
+
throw new Error(`Invalid type for path parameter: ${key}. Expected a string or number.`);
|
70
|
+
}
|
71
|
+
return String(value);
|
64
72
|
});
|
65
73
|
}
|
66
74
|
exports.ApiClientUtils = { resolveUrl, createApiClient: exports.createApiClient };
|