@dexyn/common-library 1.0.13 → 1.1.2
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.js +20 -7
- package/package.json +1 -1
- package/result/resultUtils.d.ts +11 -1
- package/result/resultUtils.js +10 -0
package/api/apiClientUtils.js
CHANGED
@@ -26,17 +26,25 @@ const createApiClient = (basePath, debugMode = false) => {
|
|
26
26
|
}, {})).toString();
|
27
27
|
// Append query string to endpoint if queryParameters exist
|
28
28
|
const url = queryString ? `${sanitizedBasePath}/${resolvedEndpoint}?${queryString}` : `${sanitizedBasePath}/${resolvedEndpoint}`;
|
29
|
-
logger_1.logger.log(`Sending ${method} to ${url}`, debugMode);
|
29
|
+
logger_1.logger.log(`Sending ${method} request to ${url}`, debugMode);
|
30
30
|
if (method === 'GET' && body) {
|
31
31
|
throw new Error('GET requests cannot have a body');
|
32
32
|
}
|
33
|
-
|
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
|
+
}
|
34
45
|
const response = await fetch(url, {
|
35
46
|
method,
|
36
|
-
headers:
|
37
|
-
'Content-Type': 'application/json',
|
38
|
-
...headers,
|
39
|
-
},
|
47
|
+
headers: finalHeaders,
|
40
48
|
body: requestBody,
|
41
49
|
});
|
42
50
|
if (!response.ok) {
|
@@ -45,7 +53,12 @@ const createApiClient = (basePath, debugMode = false) => {
|
|
45
53
|
}
|
46
54
|
// Handle no content for 202 or other no-body status codes
|
47
55
|
if (response.status === 202 || response.status === 204 || !response.headers.get('Content-Type')) {
|
48
|
-
|
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
|
49
62
|
}
|
50
63
|
// Return parsed JSON for non-void responses
|
51
64
|
return await response.json();
|
package/package.json
CHANGED
package/result/resultUtils.d.ts
CHANGED
@@ -1,6 +1,9 @@
|
|
1
1
|
type SuccessResult<T> = {
|
2
2
|
success: true;
|
3
3
|
data: T;
|
4
|
+
page?: number;
|
5
|
+
pageSize?: number;
|
6
|
+
totalCount?: number;
|
4
7
|
};
|
5
8
|
export type ErrorResult = {
|
6
9
|
success: false;
|
@@ -23,9 +26,16 @@ export type Result<T> = SuccessResult<T> | ErrorResult;
|
|
23
26
|
* @returns A ResultUtils<T, E> representing the outcome of the promise.
|
24
27
|
*/
|
25
28
|
export declare function toResult<T>(data: T): Result<T>;
|
29
|
+
/**
|
30
|
+
* Converts an error into a ResultUtils type, wrapping its error outcome.
|
31
|
+
*
|
32
|
+
* @param error - Error to be wrapped in ResultUtils type
|
33
|
+
* @returns A ResultUtils<T, E> representing the error outcome.
|
34
|
+
*/
|
26
35
|
export declare function errorToErrorResult(error: Error): ErrorResult;
|
27
|
-
export type ErrorName = "UnhandledError" | "
|
36
|
+
export type ErrorName = "UnhandledError" | "BadRequest" | "NotFound" | "ValidationError" | "NotAuthorized";
|
28
37
|
export declare function toErrorResult(name: ErrorName, message: string, details?: Record<string, unknown>, code?: string, statusCode?: number): ErrorResult;
|
38
|
+
export declare function toPagedResult<T>(data: T, page: number, pageSize: number, totalCount: number): Result<T>;
|
29
39
|
declare const ResultUtils: {
|
30
40
|
toResult: typeof toResult;
|
31
41
|
errorToErrorResult: typeof errorToErrorResult;
|
package/result/resultUtils.js
CHANGED
@@ -5,6 +5,7 @@ exports.ResultUtils = void 0;
|
|
5
5
|
exports.toResult = toResult;
|
6
6
|
exports.errorToErrorResult = errorToErrorResult;
|
7
7
|
exports.toErrorResult = toErrorResult;
|
8
|
+
exports.toPagedResult = toPagedResult;
|
8
9
|
const dateUtils_1 = require("../date/dateUtils");
|
9
10
|
/**
|
10
11
|
* Converts a promise into a ResultUtils type, wrapping its success and error outcomes.
|
@@ -15,6 +16,12 @@ const dateUtils_1 = require("../date/dateUtils");
|
|
15
16
|
function toResult(data) {
|
16
17
|
return { success: true, data: data };
|
17
18
|
}
|
19
|
+
/**
|
20
|
+
* Converts an error into a ResultUtils type, wrapping its error outcome.
|
21
|
+
*
|
22
|
+
* @param error - Error to be wrapped in ResultUtils type
|
23
|
+
* @returns A ResultUtils<T, E> representing the error outcome.
|
24
|
+
*/
|
18
25
|
function errorToErrorResult(error) {
|
19
26
|
return { success: false, error: error };
|
20
27
|
}
|
@@ -28,6 +35,9 @@ function toErrorResult(name, message, details, code, statusCode) {
|
|
28
35
|
timestamp: dateUtils_1.DateUtils.formatDateTime(new Date())
|
29
36
|
} };
|
30
37
|
}
|
38
|
+
function toPagedResult(data, page, pageSize, totalCount) {
|
39
|
+
return { success: true, data: data, page: page, pageSize: pageSize, totalCount: totalCount };
|
40
|
+
}
|
31
41
|
// Export everything as a single constant object
|
32
42
|
const ResultUtils = {
|
33
43
|
toResult,
|