@dexyn/common-library 1.0.8 → 1.0.10
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 +11 -8
- package/api/apiClientUtils.js +22 -7
- package/package.json +1 -1
package/api/apiClientUtils.d.ts
CHANGED
@@ -10,16 +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>) => Promise<T>;
|
14
|
-
post: <T>(endpoint: string, body?: unknown) => Promise<T>;
|
15
|
-
put: <T>(endpoint: string, body?: unknown) => Promise<T>;
|
16
|
-
delete: <T>(endpoint: string) => Promise<T>;
|
13
|
+
get: <T>(endpoint: string, queryParameters?: Record<string, string | number | boolean | null | undefined>, pathParameters?: Record<string, string>) => Promise<T>;
|
14
|
+
post: <T>(endpoint: string, body?: unknown, pathParameters?: Record<string, string>) => Promise<T>;
|
15
|
+
put: <T>(endpoint: string, body?: unknown, pathParameters?: Record<string, string>) => Promise<T>;
|
16
|
+
delete: <T>(endpoint: string, pathParameters?: Record<string, string>) => Promise<T>;
|
17
17
|
};
|
18
|
+
declare function resolveUrl(template: string, params: Record<string, string>): string;
|
18
19
|
export declare const ApiClientUtils: {
|
20
|
+
resolveUrl: typeof resolveUrl;
|
19
21
|
createApiClient: (basePath: string) => {
|
20
|
-
get: <T>(endpoint: string, queryParameters?: Record<string, string | number | boolean | null | undefined>) => Promise<T>;
|
21
|
-
post: <T>(endpoint: string, body?: unknown) => Promise<T>;
|
22
|
-
put: <T>(endpoint: string, body?: unknown) => Promise<T>;
|
23
|
-
delete: <T>(endpoint: string) => Promise<T>;
|
22
|
+
get: <T>(endpoint: string, queryParameters?: Record<string, string | number | boolean | null | undefined>, pathParameters?: Record<string, string>) => Promise<T>;
|
23
|
+
post: <T>(endpoint: string, body?: unknown, pathParameters?: Record<string, string>) => Promise<T>;
|
24
|
+
put: <T>(endpoint: string, body?: unknown, pathParameters?: Record<string, string>) => Promise<T>;
|
25
|
+
delete: <T>(endpoint: string, pathParameters?: Record<string, string>) => Promise<T>;
|
24
26
|
};
|
25
27
|
};
|
28
|
+
export {};
|
package/api/apiClientUtils.js
CHANGED
@@ -11,8 +11,11 @@ const createApiClient = (basePath) => {
|
|
11
11
|
throw new Error('Base path is required');
|
12
12
|
}
|
13
13
|
const sanitizedBasePath = basePath.replace(/\/$/, '');
|
14
|
-
const request = async (endpoint, options = {}, queryParameters = {}
|
14
|
+
const request = async (endpoint, options = {}, queryParameters = {}, pathParameters = {} // New parameter for path placeholders
|
15
|
+
) => {
|
15
16
|
const { method = 'GET', headers, body } = options;
|
17
|
+
// Resolve the URL template with path parameters
|
18
|
+
const resolvedEndpoint = resolveUrl(endpoint, pathParameters);
|
16
19
|
// Construct query string from queryParameters
|
17
20
|
const queryString = new URLSearchParams(Object.entries(queryParameters).reduce((acc, [key, value]) => {
|
18
21
|
if (value !== undefined) {
|
@@ -21,7 +24,7 @@ const createApiClient = (basePath) => {
|
|
21
24
|
return acc;
|
22
25
|
}, {})).toString();
|
23
26
|
// Append query string to endpoint if queryParameters exist
|
24
|
-
const url = queryString ? `${sanitizedBasePath}/${
|
27
|
+
const url = queryString ? `${sanitizedBasePath}/${resolvedEndpoint}?${queryString}` : `${sanitizedBasePath}/${resolvedEndpoint}`;
|
25
28
|
if (method === 'GET' && body) {
|
26
29
|
throw new Error('GET requests cannot have a body');
|
27
30
|
}
|
@@ -38,14 +41,26 @@ const createApiClient = (basePath) => {
|
|
38
41
|
const errorData = await response.json();
|
39
42
|
throw new Error(`Request failed with status ${response.status}: ${JSON.stringify(errorData)}`);
|
40
43
|
}
|
44
|
+
// Handle no content for 202 or other no-body status codes
|
45
|
+
if (response.status === 202 || response.status === 204 || !response.headers.get('Content-Type')) {
|
46
|
+
return {};
|
47
|
+
}
|
41
48
|
return await response.json();
|
42
49
|
};
|
43
50
|
return {
|
44
|
-
get: (endpoint, queryParameters = {}) => request(endpoint, { method: 'GET' }, queryParameters),
|
45
|
-
post: (endpoint, body) => request(endpoint, { method: 'POST', body }),
|
46
|
-
put: (endpoint, body) => request(endpoint, { method: 'PUT', body }),
|
47
|
-
delete: (endpoint) => request(endpoint, { method: 'DELETE' }),
|
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' }),
|
48
55
|
};
|
49
56
|
};
|
50
57
|
exports.createApiClient = createApiClient;
|
51
|
-
|
58
|
+
function resolveUrl(template, params) {
|
59
|
+
return template.replace(/:([a-zA-Z_]+)/g, (_, key) => {
|
60
|
+
if (!params[key]) {
|
61
|
+
throw new Error(`Missing path parameter: ${key}`);
|
62
|
+
}
|
63
|
+
return params[key];
|
64
|
+
});
|
65
|
+
}
|
66
|
+
exports.ApiClientUtils = { resolveUrl, createApiClient: exports.createApiClient };
|