@dexyn/common-library 1.0.11 → 1.0.13

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.
@@ -9,7 +9,7 @@ export interface ApiOptions {
9
9
  * Provides utility functions for making HTTP requests with query parameters,
10
10
  * custom HTTP methods, headers, and request bodies.
11
11
  */
12
- export declare const createApiClient: (basePath: string) => {
12
+ export declare const createApiClient: (basePath: string, debugMode?: boolean) => {
13
13
  get: <T>(endpoint: string, queryParameters?: Record<string, string | number | boolean | null | undefined>, pathParameters?: unknown) => Promise<T>;
14
14
  post: <T>(endpoint: string, body?: unknown, pathParameters?: unknown) => Promise<T>;
15
15
  put: <T>(endpoint: string, body?: unknown, pathParameters?: unknown) => Promise<T>;
@@ -18,7 +18,7 @@ export declare const createApiClient: (basePath: string) => {
18
18
  declare function resolveUrl(template: string, params: unknown): string;
19
19
  export declare const ApiClientUtils: {
20
20
  resolveUrl: typeof resolveUrl;
21
- createApiClient: (basePath: string) => {
21
+ createApiClient: (basePath: string, debugMode?: boolean) => {
22
22
  get: <T>(endpoint: string, queryParameters?: Record<string, string | number | boolean | null | undefined>, pathParameters?: unknown) => Promise<T>;
23
23
  post: <T>(endpoint: string, body?: unknown, pathParameters?: unknown) => Promise<T>;
24
24
  put: <T>(endpoint: string, body?: unknown, pathParameters?: unknown) => Promise<T>;
@@ -1,13 +1,14 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ApiClientUtils = exports.createApiClient = void 0;
4
+ const logger_1 = require("../logger/logger");
4
5
  /**
5
6
  * A functional API client that requires a base URL upon creation.
6
7
  * Provides utility functions for making HTTP requests with query parameters,
7
8
  * custom HTTP methods, headers, and request bodies.
8
9
  */
9
- const createApiClient = (basePath) => {
10
- if (!basePath) {
10
+ const createApiClient = (basePath, debugMode = false) => {
11
+ if (basePath == undefined) {
11
12
  throw new Error('Base path is required');
12
13
  }
13
14
  const sanitizedBasePath = basePath.replace(/\/$/, '');
@@ -25,6 +26,7 @@ const createApiClient = (basePath) => {
25
26
  }, {})).toString();
26
27
  // Append query string to endpoint if queryParameters exist
27
28
  const url = queryString ? `${sanitizedBasePath}/${resolvedEndpoint}?${queryString}` : `${sanitizedBasePath}/${resolvedEndpoint}`;
29
+ logger_1.logger.log(`Sending ${method} to ${url}`, debugMode);
28
30
  if (method === 'GET' && body) {
29
31
  throw new Error('GET requests cannot have a body');
30
32
  }
@@ -39,12 +41,13 @@ const createApiClient = (basePath) => {
39
41
  });
40
42
  if (!response.ok) {
41
43
  const errorData = await response.json();
42
- throw new Error(`Request failed with status ${response.status}: ${JSON.stringify(errorData)}`);
44
+ return errorData;
43
45
  }
44
46
  // Handle no content for 202 or other no-body status codes
45
47
  if (response.status === 202 || response.status === 204 || !response.headers.get('Content-Type')) {
46
48
  return {};
47
49
  }
50
+ // Return parsed JSON for non-void responses
48
51
  return await response.json();
49
52
  };
50
53
  return {
@@ -0,0 +1,3 @@
1
+ export declare const logger: {
2
+ log: (message: string, print: boolean) => void;
3
+ };
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.logger = void 0;
4
+ const createLogger = () => {
5
+ const log = (message, print) => {
6
+ if (!print)
7
+ return;
8
+ console.log(message);
9
+ };
10
+ return { log };
11
+ };
12
+ exports.logger = createLogger();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dexyn/common-library",
3
- "version": "1.0.11",
3
+ "version": "1.0.13",
4
4
  "main": "index.js",
5
5
  "repository": {
6
6
  "type": "git",
@@ -25,6 +25,7 @@
25
25
  "typescript": "^5.7.3"
26
26
  },
27
27
  "dependencies": {
28
+ "ts-node": "^10.9.2",
28
29
  "zod": "^3.24.1"
29
30
  }
30
31
  }