@dexyn/common-library 1.0.9 → 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.
@@ -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 {};
@@ -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}/${endpoint}?${queryString}` : `${sanitizedBasePath}/${endpoint}`;
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
  }
@@ -45,11 +48,19 @@ const createApiClient = (basePath) => {
45
48
  return await response.json();
46
49
  };
47
50
  return {
48
- get: (endpoint, queryParameters = {}) => request(endpoint, { method: 'GET' }, queryParameters),
49
- post: (endpoint, body) => request(endpoint, { method: 'POST', body }),
50
- put: (endpoint, body) => request(endpoint, { method: 'PUT', body }),
51
- 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' }),
52
55
  };
53
56
  };
54
57
  exports.createApiClient = createApiClient;
55
- exports.ApiClientUtils = { createApiClient: exports.createApiClient };
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 };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dexyn/common-library",
3
- "version": "1.0.9",
3
+ "version": "1.0.10",
4
4
  "main": "index.js",
5
5
  "repository": {
6
6
  "type": "git",