@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.
- package/api/apiClientUtils.d.ts +2 -2
- package/api/apiClientUtils.js +6 -3
- package/logger/logger.d.ts +3 -0
- package/logger/logger.js +12 -0
- package/package.json +2 -1
package/api/apiClientUtils.d.ts
CHANGED
@@ -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>;
|
package/api/apiClientUtils.js
CHANGED
@@ -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 (
|
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
|
-
|
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 {
|
package/logger/logger.js
ADDED
@@ -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.
|
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
|
}
|