@hashrytech/quick-components-kit 0.12.5 → 0.12.7

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/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # @hashrytech/quick-components-kit
2
2
 
3
+ ## 0.12.7
4
+
5
+ ### Patch Changes
6
+
7
+ - fix: Fixing api client and problem details
8
+
9
+ ## 0.12.6
10
+
11
+ ### Patch Changes
12
+
13
+ - fix: api-client and prodlem-details
14
+
3
15
  ## 0.12.5
4
16
 
5
17
  ### Patch Changes
@@ -1,3 +1,4 @@
1
+ import { type ProblemDetail } from "./problem-details.js";
1
2
  /**
2
3
  * @file This module defines a generic REST API client for SvelteKit applications.
3
4
  * It provides methods for standard HTTP operations (GET, POST, PUT, PATCH, DELETE),
@@ -12,7 +13,7 @@ export type ApiResponse<T> = {
12
13
  ok: boolean;
13
14
  status: number;
14
15
  data?: T;
15
- error?: string;
16
+ error?: ProblemDetail;
16
17
  };
17
18
  export interface ApiClientConfig {
18
19
  /** The base URL for your API (e.g., 'https://api.yourapi.com/v1'). */
@@ -1,4 +1,5 @@
1
1
  // src/lib/api/client.ts
2
+ import { getProblemDetail } from "./problem-details.js";
2
3
  /**
3
4
  * Custom error class for API responses.
4
5
  * Provides access to the HTTP status code.
@@ -233,10 +234,11 @@ export class ApiClient {
233
234
  const isApiError = error instanceof ApiError;
234
235
  const status = isApiError ? error.status : 503; // // Service Unavailable fallback
235
236
  const message = error instanceof Error ? error.message : 'Unexpected error occurred';
237
+ const errorObj = getProblemDetail({ status, title: "Server fetch error", type: "/exceptions/fetch-error/", detail: "Error fetching data from API", server: [message] });
236
238
  return {
237
239
  ok: false,
238
240
  status,
239
- error: message
241
+ error: errorObj
240
242
  };
241
243
  }
242
244
  }
@@ -28,4 +28,35 @@ export declare class ProblemDetailError extends Error {
28
28
  * @param type - A URI identifier for the problem type (default: "/exceptions/bad_request/").
29
29
  */
30
30
  constructor(field: string, field_error: string, status?: number, title?: string, type?: string);
31
+ /**
32
+ * Converts the ProblemDetailError instance into a plain dictionary object,
33
+ * suitable for serializing to JSON in HTTP responses (e.g., conforming to RFC 7807).
34
+ *
35
+ * @returns A plain object containing the structured error details.
36
+ */
37
+ toDict(): Record<string, string | string[] | number | Record<string, string[]>>;
31
38
  }
39
+ export interface ProblemDetail {
40
+ status: number;
41
+ title: string;
42
+ detail: string;
43
+ type: string;
44
+ error?: Record<string, string | string[] | number | boolean>;
45
+ [key: string]: unknown | unknown[];
46
+ }
47
+ /**
48
+ * Constructs a structured problem detail object (RFC 7807-style),
49
+ * with support for arbitrary key-value pairs (e.g., field-level error messages).
50
+ *
51
+ * @param input - An object with optional status, title, detail, and type,
52
+ * plus any additional field-specific errors as key-value strings.
53
+ *
54
+ * @returns A `ProblemDetail` object suitable for use in APIs.
55
+ */
56
+ export declare function getProblemDetail(input: {
57
+ status?: number;
58
+ title?: string;
59
+ detail?: string;
60
+ type?: string;
61
+ [key: string]: string | number | undefined;
62
+ }): ProblemDetail;
@@ -37,4 +37,44 @@ export class ProblemDetailError extends Error {
37
37
  [field]: [field_error]
38
38
  };
39
39
  }
40
+ /**
41
+ * Converts the ProblemDetailError instance into a plain dictionary object,
42
+ * suitable for serializing to JSON in HTTP responses (e.g., conforming to RFC 7807).
43
+ *
44
+ * @returns A plain object containing the structured error details.
45
+ */
46
+ toDict() {
47
+ return {
48
+ // The HTTP status code associated with the error (e.g., 400, 404)
49
+ status: this.status,
50
+ // A short, human-readable summary of the problem
51
+ title: this.message,
52
+ // A URI identifier for the type of error
53
+ type: this.type,
54
+ // A detailed human-readable explanation of the error
55
+ detail: this.detail,
56
+ // Field-specific validation errors (uncomment if needed)
57
+ errors: this.errors
58
+ };
59
+ }
60
+ }
61
+ /**
62
+ * Constructs a structured problem detail object (RFC 7807-style),
63
+ * with support for arbitrary key-value pairs (e.g., field-level error messages).
64
+ *
65
+ * @param input - An object with optional status, title, detail, and type,
66
+ * plus any additional field-specific errors as key-value strings.
67
+ *
68
+ * @returns A `ProblemDetail` object suitable for use in APIs.
69
+ */
70
+ export function getProblemDetail(input) {
71
+ const { status = 400, title = "Bad Request", detail = "Your request contained invalid input.", type = "/exceptions/bad-request/", ...extraFields } = input;
72
+ const problem = { status, title, detail, type };
73
+ // Attach arbitrary string fields (e.g., email, password)
74
+ for (const [key, value] of Object.entries(extraFields)) {
75
+ if (typeof value === 'string' || typeof value === 'number') {
76
+ problem[key] = [value];
77
+ }
78
+ }
79
+ return problem;
40
80
  }
package/package.json CHANGED
@@ -5,7 +5,7 @@
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/hashrytech/quick-components-kit.git"
7
7
  },
8
- "version": "0.12.5",
8
+ "version": "0.12.7",
9
9
  "license": "MIT",
10
10
  "author": "Hashry Tech",
11
11
  "files": [