@dexyn/common-library 1.0.12 → 1.1.1
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 +23 -8
- package/logger/logger.d.ts +3 -0
- package/logger/logger.js +12 -0
- package/package.json +2 -1
- package/result/resultUtils.d.ts +1 -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,16 +26,25 @@ 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} request to ${url}`, debugMode);
|
28
30
|
if (method === 'GET' && body) {
|
29
31
|
throw new Error('GET requests cannot have a body');
|
30
32
|
}
|
31
|
-
|
33
|
+
// Prepare the request body and headers
|
34
|
+
let requestBody;
|
35
|
+
const finalHeaders = { ...headers };
|
36
|
+
if (body instanceof FormData) {
|
37
|
+
// If body is FormData, do not set Content-Type (browser will handle it)
|
38
|
+
requestBody = body;
|
39
|
+
}
|
40
|
+
else if (body) {
|
41
|
+
// Assume JSON body
|
42
|
+
requestBody = JSON.stringify(body);
|
43
|
+
finalHeaders['Content-Type'] = 'application/json';
|
44
|
+
}
|
32
45
|
const response = await fetch(url, {
|
33
46
|
method,
|
34
|
-
headers:
|
35
|
-
'Content-Type': 'application/json',
|
36
|
-
...headers,
|
37
|
-
},
|
47
|
+
headers: finalHeaders,
|
38
48
|
body: requestBody,
|
39
49
|
});
|
40
50
|
if (!response.ok) {
|
@@ -43,7 +53,12 @@ const createApiClient = (basePath) => {
|
|
43
53
|
}
|
44
54
|
// Handle no content for 202 or other no-body status codes
|
45
55
|
if (response.status === 202 || response.status === 204 || !response.headers.get('Content-Type')) {
|
46
|
-
|
56
|
+
const body = await response.json().catch(() => null); // Safely attempt to parse body
|
57
|
+
// Check if body is not an empty object
|
58
|
+
if (body && Object.keys(body).length > 0) {
|
59
|
+
return body;
|
60
|
+
}
|
61
|
+
return {}; // Return empty object if no content or empty object
|
47
62
|
}
|
48
63
|
// Return parsed JSON for non-void responses
|
49
64
|
return await response.json();
|
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.
|
3
|
+
"version": "1.1.1",
|
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
|
}
|
package/result/resultUtils.d.ts
CHANGED
@@ -24,7 +24,7 @@ export type Result<T> = SuccessResult<T> | ErrorResult;
|
|
24
24
|
*/
|
25
25
|
export declare function toResult<T>(data: T): Result<T>;
|
26
26
|
export declare function errorToErrorResult(error: Error): ErrorResult;
|
27
|
-
export type ErrorName = "UnhandledError" | "
|
27
|
+
export type ErrorName = "UnhandledError" | "BadRequest" | "NotFound" | "ValidationError" | "NotAuthorized";
|
28
28
|
export declare function toErrorResult(name: ErrorName, message: string, details?: Record<string, unknown>, code?: string, statusCode?: number): ErrorResult;
|
29
29
|
declare const ResultUtils: {
|
30
30
|
toResult: typeof toResult;
|