@ram_28/kf-ai-sdk 1.0.23 → 1.0.25

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.
@@ -230,6 +230,30 @@ function ProtectedRoute({ children }) {
230
230
 
231
231
  ---
232
232
 
233
+ ## Date Handling
234
+
235
+ ### Display Encoded Date
236
+ ```typescript
237
+ import { decodeDate } from "@ram_28/kf-ai-sdk/api";
238
+ import { formatDate } from "@ram_28/kf-ai-sdk/utils";
239
+
240
+ const readable = formatDate(decodeDate(record.OrderDate), 'medium');
241
+ // => "Mar 15, 2025"
242
+ ```
243
+
244
+ ### Display Timestamp
245
+ ```typescript
246
+ import { decodeDateTime } from "@ram_28/kf-ai-sdk/api";
247
+ import { formatDateTime } from "@ram_28/kf-ai-sdk/utils";
248
+
249
+ const readable = formatDateTime(decodeDateTime(record._created_at), 'medium');
250
+ // => "Mar 15, 2025, 10:30:45 AM"
251
+ ```
252
+
253
+ [Full Documentation](./datetime.md)
254
+
255
+ ---
256
+
233
257
  ## Type Imports
234
258
 
235
259
  ```typescript
package/docs/api.md CHANGED
@@ -5,10 +5,11 @@ Direct API methods for CRUD operations, drafts, metrics, and metadata.
5
5
  ## Setup
6
6
 
7
7
  ```typescript
8
- import { Product, ProductType } from "../sources";
8
+ import { Product } from "../sources";
9
+ import type { ProductForRole } from "../sources";
9
10
  import { Roles } from "../sources/roles";
10
11
 
11
- type BuyerProduct = ProductType<typeof Roles.Buyer>;
12
+ type BuyerProduct = ProductForRole<typeof Roles.Buyer>;
12
13
 
13
14
  const product = new Product(Roles.Buyer);
14
15
  ```
@@ -200,10 +201,11 @@ const response = await product.list(options?: ListOptionsType): Promise<ListResp
200
201
  **Example:** Fetch paginated products with filter
201
202
 
202
203
  ```typescript
203
- import { Product, ProductType } from "../sources";
204
+ import { Product } from "../sources";
205
+ import type { ProductForRole } from "../sources";
204
206
  import { Roles } from "../sources/roles";
205
207
 
206
- type BuyerProduct = ProductType<typeof Roles.Buyer>;
208
+ type BuyerProduct = ProductForRole<typeof Roles.Buyer>;
207
209
 
208
210
  const product = new Product(Roles.Buyer);
209
211
 
@@ -244,10 +246,11 @@ const record = await product.get(id: string): Promise<T>
244
246
  **Example:** Fetch single product
245
247
 
246
248
  ```typescript
247
- import { Product, ProductType } from "../sources";
249
+ import { Product } from "../sources";
250
+ import type { ProductForRole } from "../sources";
248
251
  import { Roles } from "../sources/roles";
249
252
 
250
- type BuyerProduct = ProductType<typeof Roles.Buyer>;
253
+ type BuyerProduct = ProductForRole<typeof Roles.Buyer>;
251
254
 
252
255
  const product = new Product(Roles.Buyer);
253
256
 
@@ -271,10 +274,11 @@ const response = await product.create(data: Partial<T>): Promise<CreateUpdateRes
271
274
  **Example:** Create new product
272
275
 
273
276
  ```typescript
274
- import { Product, ProductType } from "../sources";
277
+ import { Product } from "../sources";
278
+ import type { ProductForRole } from "../sources";
275
279
  import { Roles } from "../sources/roles";
276
280
 
277
- type BuyerProduct = ProductType<typeof Roles.Buyer>;
281
+ type BuyerProduct = ProductForRole<typeof Roles.Buyer>;
278
282
 
279
283
  const product = new Product(Roles.Buyer);
280
284
 
@@ -302,10 +306,11 @@ const response = await product.update(id: string, data: Partial<T>): Promise<Cre
302
306
  **Example:** Update product price
303
307
 
304
308
  ```typescript
305
- import { Product, ProductType } from "../sources";
309
+ import { Product } from "../sources";
310
+ import type { ProductForRole } from "../sources";
306
311
  import { Roles } from "../sources/roles";
307
312
 
308
- type BuyerProduct = ProductType<typeof Roles.Buyer>;
313
+ type BuyerProduct = ProductForRole<typeof Roles.Buyer>;
309
314
 
310
315
  const product = new Product(Roles.Buyer);
311
316
 
@@ -330,10 +335,11 @@ const response = await product.delete(id: string): Promise<DeleteResponseType>
330
335
  **Example:** Delete product
331
336
 
332
337
  ```typescript
333
- import { Product, ProductType } from "../sources";
338
+ import { Product } from "../sources";
339
+ import type { ProductForRole } from "../sources";
334
340
  import { Roles } from "../sources/roles";
335
341
 
336
- type BuyerProduct = ProductType<typeof Roles.Buyer>;
342
+ type BuyerProduct = ProductForRole<typeof Roles.Buyer>;
337
343
 
338
344
  const product = new Product(Roles.Buyer);
339
345
 
@@ -357,10 +363,11 @@ const response = await product.draft(data: Partial<T>): Promise<DraftResponseTyp
357
363
  **Example:** Preview computed discount
358
364
 
359
365
  ```typescript
360
- import { Product, ProductType } from "../sources";
366
+ import { Product } from "../sources";
367
+ import type { ProductForRole } from "../sources";
361
368
  import { Roles } from "../sources/roles";
362
369
 
363
- type BuyerProduct = ProductType<typeof Roles.Buyer>;
370
+ type BuyerProduct = ProductForRole<typeof Roles.Buyer>;
364
371
 
365
372
  const product = new Product(Roles.Buyer);
366
373
 
@@ -387,10 +394,11 @@ const response = await product.draftPatch(id: string, data: Partial<T>): Promise
387
394
  **Example:** Update draft during editing
388
395
 
389
396
  ```typescript
390
- import { Product, ProductType } from "../sources";
397
+ import { Product } from "../sources";
398
+ import type { ProductForRole } from "../sources";
391
399
  import { Roles } from "../sources/roles";
392
400
 
393
- type BuyerProduct = ProductType<typeof Roles.Buyer>;
401
+ type BuyerProduct = ProductForRole<typeof Roles.Buyer>;
394
402
 
395
403
  const product = new Product(Roles.Buyer);
396
404
 
@@ -417,10 +425,11 @@ const response = await product.metric(options: MetricOptionsType): Promise<Metri
417
425
  **Example 1:** Total count
418
426
 
419
427
  ```typescript
420
- import { Product, ProductType } from "../sources";
428
+ import { Product } from "../sources";
429
+ import type { ProductForRole } from "../sources";
421
430
  import { Roles } from "../sources/roles";
422
431
 
423
- type BuyerProduct = ProductType<typeof Roles.Buyer>;
432
+ type BuyerProduct = ProductForRole<typeof Roles.Buyer>;
424
433
 
425
434
  const product = new Product(Roles.Buyer);
426
435
 
@@ -436,10 +445,11 @@ console.log("Total products:", response.Data[0]["count__id"]);
436
445
  **Example 2:** Sum with filter (low stock count)
437
446
 
438
447
  ```typescript
439
- import { Product, ProductType } from "../sources";
448
+ import { Product } from "../sources";
449
+ import type { ProductForRole } from "../sources";
440
450
  import { Roles } from "../sources/roles";
441
451
 
442
- type BuyerProduct = ProductType<typeof Roles.Buyer>;
452
+ type BuyerProduct = ProductForRole<typeof Roles.Buyer>;
443
453
 
444
454
  const product = new Product(Roles.Buyer);
445
455
 
@@ -466,10 +476,11 @@ console.log("Low stock products:", response.Data[0]["count__id"]);
466
476
  **Example 3:** Group by field (products by category)
467
477
 
468
478
  ```typescript
469
- import { Product, ProductType } from "../sources";
479
+ import { Product } from "../sources";
480
+ import type { ProductForRole } from "../sources";
470
481
  import { Roles } from "../sources/roles";
471
482
 
472
- type BuyerProduct = ProductType<typeof Roles.Buyer>;
483
+ type BuyerProduct = ProductForRole<typeof Roles.Buyer>;
473
484
 
474
485
  const product = new Product(Roles.Buyer);
475
486
 
@@ -491,10 +502,11 @@ response.Data.forEach((row) => {
491
502
  **Example 4:** Multiple metrics (stock sum and average by category)
492
503
 
493
504
  ```typescript
494
- import { Product, ProductType } from "../sources";
505
+ import { Product } from "../sources";
506
+ import type { ProductForRole } from "../sources";
495
507
  import { Roles } from "../sources/roles";
496
508
 
497
- type BuyerProduct = ProductType<typeof Roles.Buyer>;
509
+ type BuyerProduct = ProductForRole<typeof Roles.Buyer>;
498
510
 
499
511
  const product = new Product(Roles.Buyer);
500
512
 
@@ -528,10 +540,11 @@ const response = await product.pivot(options: PivotOptionsType): Promise<PivotRe
528
540
  **Example:** Sales pivot by region and quarter
529
541
 
530
542
  ```typescript
531
- import { Order, OrderType } from "../sources";
543
+ import { Order } from "../sources";
544
+ import type { OrderForRole } from "../sources";
532
545
  import { Roles } from "../sources/roles";
533
546
 
534
- type AdminOrder = OrderType<typeof Roles.Admin>;
547
+ type AdminOrder = OrderForRole<typeof Roles.Admin>;
535
548
 
536
549
  const order = new Order(Roles.Admin);
537
550
 
@@ -587,10 +600,11 @@ const response = await product.fields(): Promise<FieldsResponseType>
587
600
  **Example:** Get field metadata
588
601
 
589
602
  ```typescript
590
- import { Product, ProductType } from "../sources";
603
+ import { Product } from "../sources";
604
+ import type { ProductForRole } from "../sources";
591
605
  import { Roles } from "../sources/roles";
592
606
 
593
- type BuyerProduct = ProductType<typeof Roles.Buyer>;
607
+ type BuyerProduct = ProductForRole<typeof Roles.Buyer>;
594
608
 
595
609
  const product = new Product(Roles.Buyer);
596
610
 
@@ -625,10 +639,11 @@ const options = await product.fetchField(instanceId: string, fieldId: keyof T):
625
639
  **Example:** Fetch supplier options for dropdown
626
640
 
627
641
  ```typescript
628
- import { Product, ProductType } from "../sources";
642
+ import { Product } from "../sources";
643
+ import type { ProductForRole } from "../sources";
629
644
  import { Roles } from "../sources/roles";
630
645
 
631
- type BuyerProduct = ProductType<typeof Roles.Buyer>;
646
+ type BuyerProduct = ProductForRole<typeof Roles.Buyer>;
632
647
 
633
648
  const product = new Product(Roles.Buyer);
634
649
 
@@ -0,0 +1,144 @@
1
+ # Date & DateTime Handling
2
+
3
+ Working with Date and DateTime fields in the SDK.
4
+
5
+ ## API Response Format
6
+
7
+ The backend returns dates in encoded format:
8
+ - **Date**: `{ "$__d__": "YYYY-MM-DD" }`
9
+ - **DateTime**: `{ "$__dt__": unix_timestamp_seconds }`
10
+
11
+ ## Imports
12
+
13
+ ```typescript
14
+ // Decoding & formatting for API
15
+ import {
16
+ decodeDate,
17
+ decodeDateTime,
18
+ formatDate,
19
+ formatDateTime,
20
+ parseDate,
21
+ parseDateTime,
22
+ } from "@ram_28/kf-ai-sdk/api";
23
+
24
+ // UI display formatting
25
+ import {
26
+ formatDate,
27
+ formatDateTime
28
+ } from "@ram_28/kf-ai-sdk/utils";
29
+ ```
30
+
31
+ ## Decoding API Responses
32
+
33
+ Convert encoded dates to JavaScript Date objects.
34
+
35
+ ### decodeDate
36
+ ```typescript
37
+ const apiResponse = { "$__d__": "2025-03-15" };
38
+ const date = decodeDate(apiResponse);
39
+ // => Date object for March 15, 2025
40
+ ```
41
+
42
+ ### decodeDateTime
43
+ ```typescript
44
+ const apiResponse = { "$__dt__": 1769110463 };
45
+ const date = decodeDateTime(apiResponse);
46
+ // => Date object with full timestamp
47
+ ```
48
+
49
+ ## Formatting for API Requests
50
+
51
+ Convert Date objects to strings the API expects.
52
+
53
+ ### formatDate (API)
54
+ ```typescript
55
+ import { formatDate } from "@ram_28/kf-ai-sdk/api";
56
+
57
+ const date = new Date(2025, 2, 15);
58
+ formatDate(date); // => "2025-03-15"
59
+ ```
60
+
61
+ ### formatDateTime (API)
62
+ ```typescript
63
+ import { formatDateTime } from "@ram_28/kf-ai-sdk/api";
64
+
65
+ const date = new Date(2025, 2, 15, 10, 30, 45);
66
+ formatDateTime(date); // => "2025-03-15 10:30:45"
67
+ ```
68
+
69
+ ## Formatting for UI Display
70
+
71
+ Convert Date objects to human-readable strings.
72
+
73
+ ### formatDate (UI)
74
+ ```typescript
75
+ import { formatDate } from "@ram_28/kf-ai-sdk/utils";
76
+
77
+ const date = new Date(2025, 2, 15);
78
+ formatDate(date, 'short'); // => "3/15/25"
79
+ formatDate(date, 'medium'); // => "Mar 15, 2025"
80
+ formatDate(date, 'long'); // => "March 15, 2025"
81
+ ```
82
+
83
+ ### formatDateTime (UI)
84
+ ```typescript
85
+ import { formatDateTime } from "@ram_28/kf-ai-sdk/utils";
86
+
87
+ const date = new Date(2025, 2, 15, 10, 30, 45);
88
+ formatDateTime(date, 'short'); // => "3/15/25, 10:30 AM"
89
+ formatDateTime(date, 'medium'); // => "Mar 15, 2025, 10:30:45 AM"
90
+ formatDateTime(date, 'long'); // => "March 15, 2025 at 10:30:45 AM"
91
+ ```
92
+
93
+ ## Common Patterns
94
+
95
+ ### Display a date from API response
96
+ ```typescript
97
+ import { decodeDate } from "@ram_28/kf-ai-sdk/api";
98
+ import { formatDate } from "@ram_28/kf-ai-sdk/utils";
99
+
100
+ function displayDate(encodedDate: { $__d__: string }) {
101
+ const date = decodeDate(encodedDate);
102
+ return formatDate(date, 'medium');
103
+ }
104
+
105
+ // Usage in component
106
+ <span>{displayDate(record.OrderDate)}</span>
107
+ ```
108
+
109
+ ### Display created/modified timestamps
110
+ ```typescript
111
+ import { decodeDateTime } from "@ram_28/kf-ai-sdk/api";
112
+ import { formatDateTime } from "@ram_28/kf-ai-sdk/utils";
113
+
114
+ function displayTimestamp(encodedDateTime: { $__dt__: number }) {
115
+ const date = decodeDateTime(encodedDateTime);
116
+ return formatDateTime(date, 'medium');
117
+ }
118
+
119
+ // Usage
120
+ <span>Created: {displayTimestamp(record._created_at)}</span>
121
+ <span>Modified: {displayTimestamp(record._modified_at)}</span>
122
+ ```
123
+
124
+ ### Submit a date to the API
125
+ ```typescript
126
+ import { formatDate } from "@ram_28/kf-ai-sdk/api";
127
+
128
+ // From a date picker value
129
+ const datePickerValue = "2025-03-15"; // HTML date input value
130
+ // Already in correct format, use directly
131
+
132
+ // From a Date object
133
+ const dateObj = new Date();
134
+ const apiValue = formatDate(dateObj); // => "2025-03-15"
135
+ ```
136
+
137
+ ## Type Definitions
138
+
139
+ ```typescript
140
+ import type {
141
+ DateEncodedType, // { $__d__: string }
142
+ DateTimeEncodedType, // { $__dt__: number }
143
+ } from "@ram_28/kf-ai-sdk/api";
144
+ ```
package/docs/useFilter.md CHANGED
@@ -173,10 +173,11 @@ Use the generic type parameter to get TypeScript validation on field names.
173
173
 
174
174
  ```tsx
175
175
  import { useFilter } from "@ram_28/kf-ai-sdk/filter";
176
- import { Product, ProductType } from "../sources";
176
+ import { Product } from "../sources";
177
+ import type { ProductForRole } from "../sources";
177
178
  import { Roles } from "../sources/roles";
178
179
 
179
- type BuyerProduct = ProductType<typeof Roles.Buyer>;
180
+ type BuyerProduct = ProductForRole<typeof Roles.Buyer>;
180
181
 
181
182
  function TypeSafeFilter() {
182
183
  // Pass the type parameter for type-safe LHSField
@@ -216,10 +217,11 @@ function TypeSafeFilter() {
216
217
  ```tsx
217
218
  import { useFilter } from "@ram_28/kf-ai-sdk/filter";
218
219
  import type { UseFilterOptionsType } from "@ram_28/kf-ai-sdk/filter/types";
219
- import { Product, ProductType } from "../sources";
220
+ import { Product } from "../sources";
221
+ import type { ProductForRole } from "../sources";
220
222
  import { Roles } from "../sources/roles";
221
223
 
222
- type BuyerProduct = ProductType<typeof Roles.Buyer>;
224
+ type BuyerProduct = ProductForRole<typeof Roles.Buyer>;
223
225
 
224
226
  // Type-safe filter options
225
227
  const initialFilter: UseFilterOptionsType<BuyerProduct> = {
package/docs/useForm.md CHANGED
@@ -227,10 +227,11 @@ interface FormFieldConfigType {
227
227
 
228
228
  ```tsx
229
229
  import { useForm } from "@ram_28/kf-ai-sdk/form";
230
- import { Product, ProductType } from "../sources";
230
+ import { Product } from "../sources";
231
+ import type { ProductForRole } from "../sources";
231
232
  import { Roles } from "../sources/roles";
232
233
 
233
- type BuyerProduct = ProductType<typeof Roles.Buyer>;
234
+ type BuyerProduct = ProductForRole<typeof Roles.Buyer>;
234
235
 
235
236
  function CreateProductForm() {
236
237
  const product = new Product(Roles.Buyer);
@@ -283,10 +284,11 @@ Create form with validation and success handling.
283
284
  import { useForm } from "@ram_28/kf-ai-sdk/form";
284
285
  import type { FieldErrors } from "@ram_28/kf-ai-sdk/form/types";
285
286
  import { useNavigate } from "react-router-dom";
286
- import { Product, ProductType } from "../sources";
287
+ import { Product } from "../sources";
288
+ import type { ProductForRole } from "../sources";
287
289
  import { Roles } from "../sources/roles";
288
290
 
289
- type BuyerProduct = ProductType<typeof Roles.Buyer>;
291
+ type BuyerProduct = ProductForRole<typeof Roles.Buyer>;
290
292
 
291
293
  function ProductListingForm() {
292
294
  const product = new Product(Roles.Buyer);
@@ -362,10 +364,11 @@ Update mode with record loading state.
362
364
 
363
365
  ```tsx
364
366
  import { useForm } from "@ram_28/kf-ai-sdk/form";
365
- import { Product, ProductType } from "../sources";
367
+ import { Product } from "../sources";
368
+ import type { ProductForRole } from "../sources";
366
369
  import { Roles } from "../sources/roles";
367
370
 
368
- type BuyerProduct = ProductType<typeof Roles.Buyer>;
371
+ type BuyerProduct = ProductForRole<typeof Roles.Buyer>;
369
372
 
370
373
  function EditProductForm({ productId }: { productId: string }) {
371
374
  const product = new Product(Roles.Buyer);
@@ -428,10 +431,11 @@ Working with computed fields and the watch function.
428
431
 
429
432
  ```tsx
430
433
  import { useForm } from "@ram_28/kf-ai-sdk/form";
431
- import { Product, ProductType } from "../sources";
434
+ import { Product } from "../sources";
435
+ import type { ProductForRole } from "../sources";
432
436
  import { Roles } from "../sources/roles";
433
437
 
434
- type BuyerProduct = ProductType<typeof Roles.Buyer>;
438
+ type BuyerProduct = ProductForRole<typeof Roles.Buyer>;
435
439
 
436
440
  function PricingForm() {
437
441
  const product = new Product(Roles.Buyer);
@@ -483,10 +487,11 @@ Static fields return options with `Value` and `Label`.
483
487
  ```tsx
484
488
  import { useState } from "react";
485
489
  import { useForm } from "@ram_28/kf-ai-sdk/form";
486
- import { Product, ProductType } from "../sources";
490
+ import { Product } from "../sources";
491
+ import type { ProductForRole } from "../sources";
487
492
  import { Roles } from "../sources/roles";
488
493
 
489
- type BuyerProduct = ProductType<typeof Roles.Buyer>;
494
+ type BuyerProduct = ProductForRole<typeof Roles.Buyer>;
490
495
 
491
496
  function ProductCategoryForm({ recordId }: { recordId: string }) {
492
497
  const product = new Product(Roles.Buyer);
@@ -558,10 +563,11 @@ Reference fields return the full object structure. Use a custom dropdown to disp
558
563
  ```tsx
559
564
  import { useState } from "react";
560
565
  import { useForm } from "@ram_28/kf-ai-sdk/form";
561
- import { Product, ProductType } from "../sources";
566
+ import { Product } from "../sources";
567
+ import type { ProductForRole } from "../sources";
562
568
  import { Roles } from "../sources/roles";
563
569
 
564
- type BuyerProduct = ProductType<typeof Roles.Buyer>;
570
+ type BuyerProduct = ProductForRole<typeof Roles.Buyer>;
565
571
 
566
572
  type SupplierOption = {
567
573
  _id: string;
package/docs/useTable.md CHANGED
@@ -221,10 +221,11 @@ A minimal table displaying data with loading and error states.
221
221
  ```tsx
222
222
  import { useTable } from "@ram_28/kf-ai-sdk/table";
223
223
  import type { ColumnDefinitionType } from "@ram_28/kf-ai-sdk/table/types";
224
- import { Product, ProductType } from "../sources";
224
+ import { Product } from "../sources";
225
+ import type { ProductForRole } from "../sources";
225
226
  import { Roles } from "../sources/roles";
226
227
 
227
- type BuyerProduct = ProductType<typeof Roles.Buyer>;
228
+ type BuyerProduct = ProductForRole<typeof Roles.Buyer>;
228
229
 
229
230
  function ProductsTable() {
230
231
  const product = new Product(Roles.Buyer);
@@ -282,10 +283,11 @@ import type {
282
283
  UseTableReturnType,
283
284
  ColumnDefinitionType,
284
285
  } from "@ram_28/kf-ai-sdk/table/types";
285
- import { Product, ProductType } from "../sources";
286
+ import { Product } from "../sources";
287
+ import type { ProductForRole } from "../sources";
286
288
  import { Roles } from "../sources/roles";
287
289
 
288
- type BuyerProduct = ProductType<typeof Roles.Buyer>;
290
+ type BuyerProduct = ProductForRole<typeof Roles.Buyer>;
289
291
 
290
292
  function MyItemsTable() {
291
293
  const product = new Product(Roles.Buyer);
@@ -858,10 +860,11 @@ import type {
858
860
  UseTableReturnType,
859
861
  ColumnDefinitionType,
860
862
  } from "@ram_28/kf-ai-sdk/table/types";
861
- import { Product, ProductType } from "../sources";
863
+ import { Product } from "../sources";
864
+ import type { ProductForRole } from "../sources";
862
865
  import { Roles } from "../sources/roles";
863
866
 
864
- type BuyerProduct = ProductType<typeof Roles.Buyer>;
867
+ type BuyerProduct = ProductForRole<typeof Roles.Buyer>;
865
868
 
866
869
  function ProductListPage() {
867
870
  const product = new Product(Roles.Buyer);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ram_28/kf-ai-sdk",
3
- "version": "1.0.23",
3
+ "version": "1.0.25",
4
4
  "description": "Type-safe, AI-driven SDK for building modern web applications with role-based access control",
5
5
  "author": "Ramprasad",
6
6
  "license": "MIT",