@hashrytech/quick-components-kit 0.12.0 → 0.12.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/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # @hashrytech/quick-components-kit
2
2
 
3
+ ## 0.12.2
4
+
5
+ ### Patch Changes
6
+
7
+ - fix: Adding bindable to text input value
8
+
9
+ ## 0.12.1
10
+
11
+ ### Patch Changes
12
+
13
+ - refactor: Using a typed wrapper approach for API Client
14
+
3
15
  ## 0.12.0
4
16
 
5
17
  ### Minor Changes
@@ -52,7 +52,7 @@
52
52
  <script lang="ts">
53
53
  import { twMerge } from 'tailwind-merge';
54
54
 
55
- let { id, type="text", name="", value="", placeholder="", labelText, size="md", inputClasses="", disabled=false, required=false, error, onchange, onmouseup, label, icon}: TextInputProps = $props();
55
+ let { id, type="text", name="", value=$bindable(""), placeholder="", labelText, size="md", inputClasses="", disabled=false, required=false, error, onchange, onmouseup, label, icon}: TextInputProps = $props();
56
56
 
57
57
  /**
58
58
  * Predefined size classes for the TextBox input.
@@ -42,6 +42,6 @@ export type TextInputProps = {
42
42
  label?: Snippet;
43
43
  icon?: Snippet;
44
44
  };
45
- declare const TextInput: import("svelte").Component<TextInputProps, {}, "">;
45
+ declare const TextInput: import("svelte").Component<TextInputProps, {}, "value">;
46
46
  type TextInput = ReturnType<typeof TextInput>;
47
47
  export default TextInput;
@@ -8,6 +8,12 @@
8
8
  * and API routes) to automatically benefit from its enhancements (e.g., cookie forwarding,
9
9
  * built-in request/response interception via SvelteKit's `handleFetch` hook).
10
10
  */
11
+ export type ApiResponse<T> = {
12
+ ok: boolean;
13
+ status: number;
14
+ data?: T;
15
+ error?: string;
16
+ };
11
17
  export interface ApiClientConfig {
12
18
  /** The base URL for your API (e.g., 'https://api.yourapi.com/v1'). */
13
19
  baseURL: string;
@@ -139,7 +145,7 @@ export declare class ApiClient {
139
145
  * @returns A Promise resolving to the parsed response data or raw Response/Blob.
140
146
  * @template T - Expected type of the response data.
141
147
  */
142
- request<T>(endpoint: string, method: string, body: BodyInit | object | null | undefined, options?: RequestOptions): Promise<T | Response>;
148
+ request<T>(endpoint: string, method: string, body: BodyInit | object | null | undefined, options?: RequestOptions): Promise<ApiResponse<T>>;
143
149
  /**
144
150
  * Sends a GET request to the specified API endpoint.
145
151
  *
@@ -148,7 +154,7 @@ export declare class ApiClient {
148
154
  * @returns A Promise resolving to the parsed response or raw Response object.
149
155
  * @template T - Expected shape of the JSON response.
150
156
  */
151
- get<T>(endpoint: string, options?: RequestOptions): Promise<T | Response>;
157
+ get<T>(endpoint: string, options?: RequestOptions): Promise<ApiResponse<T>>;
152
158
  /**
153
159
  * Sends a POST request to the specified API endpoint.
154
160
  *
@@ -158,7 +164,7 @@ export declare class ApiClient {
158
164
  * @returns A Promise resolving to the parsed response or raw Response object.
159
165
  * @template T - Expected shape of the JSON response.
160
166
  */
161
- post<T>(endpoint: string, data?: BodyInit | object | null, options?: RequestOptions): Promise<T | Response>;
167
+ post<T>(endpoint: string, data?: BodyInit | object | null, options?: RequestOptions): Promise<ApiResponse<T>>;
162
168
  /**
163
169
  * Sends a PUT request to the specified API endpoint.
164
170
  * Typically used for full resource replacement.
@@ -169,7 +175,7 @@ export declare class ApiClient {
169
175
  * @returns A Promise resolving to the parsed response or raw Response object.
170
176
  * @template T - Expected shape of the JSON response.
171
177
  */
172
- put<T>(endpoint: string, data?: BodyInit | object | null, options?: RequestOptions): Promise<T | Response>;
178
+ put<T>(endpoint: string, data?: BodyInit | object | null, options?: RequestOptions): Promise<ApiResponse<T>>;
173
179
  /**
174
180
  * Sends a PATCH request to the specified API endpoint.
175
181
  * Typically used for partial updates.
@@ -180,7 +186,7 @@ export declare class ApiClient {
180
186
  * @returns A Promise resolving to the parsed response or raw Response object.
181
187
  * @template T - Expected shape of the JSON response.
182
188
  */
183
- patch<T>(endpoint: string, data?: BodyInit | object | null, options?: RequestOptions): Promise<T | Response>;
189
+ patch<T>(endpoint: string, data?: BodyInit | object | null, options?: RequestOptions): Promise<ApiResponse<T>>;
184
190
  /**
185
191
  * Sends a DELETE request to the specified API endpoint.
186
192
  *
@@ -189,7 +195,7 @@ export declare class ApiClient {
189
195
  * @returns A Promise resolving to the parsed response or raw Response object.
190
196
  * @template T - Expected shape of the JSON response (if any).
191
197
  */
192
- delete<T>(endpoint: string, options?: RequestOptions): Promise<T | Response>;
198
+ delete<T>(endpoint: string, options?: RequestOptions): Promise<ApiResponse<T>>;
193
199
  /**
194
200
  * Handles file uploads.
195
201
  * @param endpoint - The API endpoint for file upload.
@@ -199,7 +205,7 @@ export declare class ApiClient {
199
205
  * @returns A Promise resolving to the parsed response data.
200
206
  * @template T - Expected type of the response data after upload.
201
207
  */
202
- uploadFile<T>(endpoint: string, file: File | Blob | FormData, fieldName?: string, options?: RequestOptions): Promise<T | Response>;
208
+ uploadFile<T>(endpoint: string, file: File | Blob | FormData, fieldName?: string, options?: RequestOptions): Promise<ApiResponse<T>>;
203
209
  /**
204
210
  * Handles file downloads.
205
211
  * @param endpoint - The API endpoint for file download.
@@ -159,9 +159,7 @@ export class ApiClient {
159
159
  try {
160
160
  // Attempt to parse a more descriptive error message from JSON response
161
161
  errorJson = await errorResponseClone.json();
162
- errorMessage = errorJson.message
163
- || errorJson.error
164
- || JSON.stringify(errorJson);
162
+ errorMessage = errorJson.message || errorJson.error || JSON.stringify(errorJson);
165
163
  }
166
164
  catch (e) {
167
165
  // If response is not JSON, use default status text or log parsing error
@@ -176,8 +174,7 @@ export class ApiClient {
176
174
  return (await response.blob());
177
175
  case 'arrayBuffer':
178
176
  return (await response.arrayBuffer());
179
- case 'raw':
180
- return response; // Return the raw Response object
177
+ case 'raw': return response;
181
178
  case 'json':
182
179
  default:
183
180
  // Handle 204 No Content (or other non-body responses)
@@ -220,18 +217,26 @@ export class ApiClient {
220
217
  * @template T - Expected type of the response data.
221
218
  */
222
219
  async request(endpoint, method, body, options = {}) {
223
- // Use the provided fetchInstance or fallback to global fetch
224
220
  const currentFetch = options.fetchInstance || fetch;
225
221
  try {
226
222
  const request = await this.processRequest(endpoint, method, body, options);
227
223
  const response = await currentFetch(request);
228
- return await this.processResponse(response, options);
224
+ const parsed = await this.processResponse(response, options);
225
+ return {
226
+ ok: true,
227
+ status: response.status,
228
+ data: parsed
229
+ };
229
230
  }
230
- catch (error) { // Use unknown for safety
231
- // Process error with registered handlers
231
+ catch (error) {
232
232
  await this.handleError(error);
233
- // Re-throw the error so the consumer can handle it if needed
234
- throw error; // Re-throw the original unknown error
233
+ const status = error instanceof ApiError ? error.status : 500;
234
+ const message = error instanceof Error ? error.message : 'Unknown error';
235
+ return {
236
+ ok: false,
237
+ status,
238
+ error: message
239
+ };
235
240
  }
236
241
  }
237
242
  // --- HTTP Method Shorthands ---
@@ -421,13 +426,14 @@ export class ApiClient {
421
426
  * @returns A Promise that resolves to a Blob containing the downloaded file.
422
427
  */
423
428
  async downloadWithProgress(endpoint, onProgress, options) {
424
- const res = await this.request(endpoint, 'GET', undefined, {
425
- ...options,
426
- responseType: 'raw'
427
- });
428
- const contentLengthHeader = res.headers.get('Content-Length');
429
+ const res = await this.request(endpoint, 'GET', undefined, { ...options, responseType: 'raw' });
430
+ if (!res.ok || !res.data) {
431
+ throw new Error(`Download failed: ${res.error ?? 'Unknown error'}`);
432
+ }
433
+ const response = res.data; // This is the raw Response object
434
+ const contentLengthHeader = response.headers.get('Content-Length');
429
435
  const contentLength = contentLengthHeader ? parseInt(contentLengthHeader, 10) : 0;
430
- const reader = res.body?.getReader();
436
+ const reader = response.body?.getReader();
431
437
  if (!reader)
432
438
  throw new Error('Failed to get reader from response body.');
433
439
  const chunks = [];
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.0",
8
+ "version": "0.12.2",
9
9
  "license": "MIT",
10
10
  "author": "Hashry Tech",
11
11
  "files": [