@dexyn/common-library 1.0.13 → 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.
@@ -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
- const requestBody = body ? JSON.stringify(body) : undefined;
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
- return {};
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dexyn/common-library",
3
- "version": "1.0.13",
3
+ "version": "1.1.1",
4
4
  "main": "index.js",
5
5
  "repository": {
6
6
  "type": "git",
@@ -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" | "Duplicate" | "NotFound" | "ApiError" | "ValidationError";
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;