@dexyn/common-library 1.0.7 → 1.0.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.
@@ -0,0 +1,25 @@
1
+ export type HttpMethod = 'GET' | 'POST' | 'PUT' | 'DELETE';
2
+ export interface ApiOptions {
3
+ method?: HttpMethod;
4
+ headers?: Record<string, string>;
5
+ body?: unknown;
6
+ }
7
+ /**
8
+ * A functional API client that requires a base URL upon creation.
9
+ * Provides utility functions for making HTTP requests with query parameters,
10
+ * custom HTTP methods, headers, and request bodies.
11
+ */
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>;
17
+ };
18
+ export declare const ApiClientUtils: {
19
+ 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>;
24
+ };
25
+ };
@@ -0,0 +1,55 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ApiClientUtils = exports.createApiClient = void 0;
4
+ /**
5
+ * A functional API client that requires a base URL upon creation.
6
+ * Provides utility functions for making HTTP requests with query parameters,
7
+ * custom HTTP methods, headers, and request bodies.
8
+ */
9
+ const createApiClient = (basePath) => {
10
+ if (!basePath) {
11
+ throw new Error('Base path is required');
12
+ }
13
+ const sanitizedBasePath = basePath.replace(/\/$/, '');
14
+ const request = async (endpoint, options = {}, queryParameters = {}) => {
15
+ const { method = 'GET', headers, body } = options;
16
+ // Construct query string from queryParameters
17
+ const queryString = new URLSearchParams(Object.entries(queryParameters).reduce((acc, [key, value]) => {
18
+ if (value !== undefined) {
19
+ acc[key] = String(value);
20
+ }
21
+ return acc;
22
+ }, {})).toString();
23
+ // Append query string to endpoint if queryParameters exist
24
+ const url = queryString ? `${sanitizedBasePath}/${endpoint}?${queryString}` : `${sanitizedBasePath}/${endpoint}`;
25
+ if (method === 'GET' && body) {
26
+ throw new Error('GET requests cannot have a body');
27
+ }
28
+ const requestBody = body ? JSON.stringify(body) : undefined;
29
+ const response = await fetch(url, {
30
+ method,
31
+ headers: {
32
+ 'Content-Type': 'application/json',
33
+ ...headers,
34
+ },
35
+ body: requestBody,
36
+ });
37
+ if (!response.ok) {
38
+ const errorData = await response.json();
39
+ throw new Error(`Request failed with status ${response.status}: ${JSON.stringify(errorData)}`);
40
+ }
41
+ // Handle no content for 202 or other no-body status codes
42
+ if (response.status === 202 || response.status === 204 || !response.headers.get('Content-Type')) {
43
+ return {};
44
+ }
45
+ return await response.json();
46
+ };
47
+ 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' }),
52
+ };
53
+ };
54
+ exports.createApiClient = createApiClient;
55
+ exports.ApiClientUtils = { createApiClient: exports.createApiClient };
package/index.d.ts CHANGED
@@ -3,4 +3,5 @@ export { ResultUtils } from "./result/resultUtils";
3
3
  export { HttpResultUtils } from "./result/httpResultUtils";
4
4
  export { DateUtils } from "./date/dateUtils";
5
5
  export { ValidationUtils } from "./validation/validationUtils";
6
+ export { ApiClientUtils } from "./api/apiClientUtils";
6
7
  export type { ErrorResult, StandardError, Result, ErrorName };
package/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ValidationUtils = exports.DateUtils = exports.HttpResultUtils = exports.ResultUtils = void 0;
3
+ exports.ApiClientUtils = exports.ValidationUtils = exports.DateUtils = exports.HttpResultUtils = exports.ResultUtils = void 0;
4
4
  var resultUtils_1 = require("./result/resultUtils");
5
5
  Object.defineProperty(exports, "ResultUtils", { enumerable: true, get: function () { return resultUtils_1.ResultUtils; } });
6
6
  var httpResultUtils_1 = require("./result/httpResultUtils");
@@ -9,3 +9,5 @@ var dateUtils_1 = require("./date/dateUtils");
9
9
  Object.defineProperty(exports, "DateUtils", { enumerable: true, get: function () { return dateUtils_1.DateUtils; } });
10
10
  var validationUtils_1 = require("./validation/validationUtils");
11
11
  Object.defineProperty(exports, "ValidationUtils", { enumerable: true, get: function () { return validationUtils_1.ValidationUtils; } });
12
+ var apiClientUtils_1 = require("./api/apiClientUtils");
13
+ Object.defineProperty(exports, "ApiClientUtils", { enumerable: true, get: function () { return apiClientUtils_1.ApiClientUtils; } });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dexyn/common-library",
3
- "version": "1.0.7",
3
+ "version": "1.0.9",
4
4
  "main": "index.js",
5
5
  "repository": {
6
6
  "type": "git",