@fajarmaulana/komerce-lp-helper 0.4.15 → 0.4.16

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/README.md CHANGED
@@ -306,24 +306,64 @@ const { mutate, isLoading, progress } = api.mutation<User, FormData>('/users/upl
306
306
  | Property | Type | Description |
307
307
  |---|---|---|
308
308
  | `data` | `T \| null` | Aggregated items. |
309
- | `fetchNextPage`| `() => Promise<void>` | Trigger fetch for next offset. |
309
+ | `error` | `unknown` | Any encountered error. |
310
+ | `isLoading` | `boolean` | Initial page request state. |
310
311
  | `hasNextPage`| `boolean` | More pages available. |
311
312
  | `isFetchingNextPage`| `boolean` | Fetching next page state. |
313
+ | `fetchNextPage`| `() => Promise<void>` | Trigger fetch for next offset. |
314
+ | `refetch` | `() => Promise<void>` | Reset state and trigger a fresh fetch from initial offset. |
315
+ | `setData` | `(updater: T \| null \| ((prev: T \| null) => T \| null)) => void` | Manually update local cache data. |
316
+ | `reset` | `() => void` | Reset all local states to initial values. |
312
317
 
313
318
  **Example:**
314
319
  ```tsx
315
- const { data, fetchNextPage, hasNextPage } = api.infinite<User[], number>(
316
- '/users',
317
- {
318
- initialOffset: 0,
319
- offsetKey: 'page',
320
- setOffset: (lastItems, allItems, lastOffset) => {
321
- return lastItems.length > 0 ? lastOffset + 1 : null
320
+ import { createApi } from '@fajarmaulana/komerce-lp-helper'
321
+
322
+ const api = createApi({ baseURL: 'https://api.example.com' })
323
+
324
+ const UserList = () => {
325
+ const {
326
+ data: users,
327
+ error,
328
+ isLoading,
329
+ hasNextPage,
330
+ isFetchingNextPage,
331
+ fetchNextPage,
332
+ refetch,
333
+ } = api.infinite<User[], number>(
334
+ '/users',
335
+ {
336
+ initialOffset: 1,
337
+ offsetKey: 'page',
338
+ setOffset: (lastItems, allItems, lastOffset) => {
339
+ // Return next offset (page) or null if there is no more data
340
+ return lastItems && lastItems.length >= 10 ? lastOffset + 1 : null
341
+ }
322
342
  }
323
- }
324
- )
343
+ )
344
+
345
+ if (isLoading) return <p>Loading...</p>
346
+ if (error) return <p>Error loading users</p>
347
+
348
+ return (
349
+ <div>
350
+ <button onClick={refetch}>Refetch</button>
351
+ <ul>
352
+ {users?.map(user => (
353
+ <li key={user.id}>{user.name}</li>
354
+ ))}
355
+ </ul>
356
+ {hasNextPage && (
357
+ <button onClick={fetchNextPage} disabled={isFetchingNextPage}>
358
+ {isFetchingNextPage ? 'Loading more...' : 'Load More'}
359
+ </button>
360
+ )}
361
+ </div>
362
+ )
363
+ }
325
364
  ```
326
365
 
366
+
327
367
  **4. `batch<T>(initialRequests?)`**
328
368
  Execute multiple API requests in parallel.
329
369
 
@@ -437,6 +477,20 @@ Type-safe wrappers for `localStorage`.
437
477
  | `removeLocals` | `keys: string[]` | Removes multiple items. |
438
478
  | `clearLocals` | | Clears local storage. |
439
479
 
480
+ #### Session Storage
481
+
482
+ Type-safe wrappers for `sessionStorage`.
483
+
484
+ | Function | Parameters | Description |
485
+ | ---------------- | ------------------------- | --------------------------------------- |
486
+ | `setSession` | `key: string, value: T` | Stores a value (JSON stringified). |
487
+ | `setSessions` | `items: { key, value }[]` | Stores multiple values. |
488
+ | `getSession` | `key: string` | Retrieves and parses a value. |
489
+ | `getSessions` | `keys: string[]` | Retrieves multiple values as an object. |
490
+ | `removeSession` | `key: string` | Removes an item. |
491
+ | `removeSessions` | `keys: string[]` | Removes multiple items. |
492
+ | `clearSessions` | | Clears session storage. |
493
+
440
494
  #### File
441
495
 
442
496
  Helpers for file and blob manipulation.
@@ -507,6 +561,10 @@ numbers, symbols, no spaces).
507
561
 
508
562
  - `params`: `{ field_id, field_value, field_error }`
509
563
 
510
- ## License
564
+ ## 👨‍💻 Author & Creator
565
+
566
+ * **Fajar Maulana** - *Initial Work & Creator* - [fajarmaulana-dev](https://github.com/fajarmaulana-dev)
567
+
568
+ ## 📄 License
511
569
 
512
- MIT
570
+ This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
package/dist/index.d.ts CHANGED
@@ -12,4 +12,5 @@ export * from './utils/error-provider';
12
12
  export * from './utils/file';
13
13
  export * from './utils/general';
14
14
  export * from './utils/local';
15
+ export * from './utils/session';
15
16
  export { default as createApi } from './utils/useApi';
package/dist/index.js CHANGED
@@ -12,4 +12,5 @@ export * from './utils/error-provider';
12
12
  export * from './utils/file';
13
13
  export * from './utils/general';
14
14
  export * from './utils/local';
15
+ export * from './utils/session';
15
16
  export { default as createApi } from './utils/useApi';
@@ -0,0 +1,46 @@
1
+ /**
2
+ * Clear sessionStorage.
3
+ */
4
+ export declare const clearSessions: () => void;
5
+ /**
6
+ * Removes an item from sessionStorage by key.
7
+ *
8
+ * @param key - The key of the item to remove.
9
+ */
10
+ export declare const removeSession: (key: string) => void;
11
+ /**
12
+ * Removes multiple sessionStorage items by keys.
13
+ *
14
+ * @param keys - Array of keys to remove.
15
+ */
16
+ export declare const removeSessions: (keys: string[]) => void;
17
+ /**
18
+ * Sets an item in sessionStorage after serializing it to JSON.
19
+ *
20
+ * @param key - The key under which to store the value.
21
+ * @param value - The value to store (will be JSON-stringified).
22
+ */
23
+ export declare const setSession: <T>(key: string, value: T) => void;
24
+ /**
25
+ * Sets multiple items in sessionStorage after serializing it to JSON.
26
+ *
27
+ * @param items - Array of session storage items to set.
28
+ */
29
+ export declare const setSessions: <T extends Record<string, unknown>>(items: { [K in keyof T]: {
30
+ key: K & string;
31
+ value: T[K];
32
+ }; }[keyof T][]) => void;
33
+ /**
34
+ * Retrieves and parses a JSON item from sessionStorage by key.
35
+ *
36
+ * @param key - The key of the item to retrieve.
37
+ * @returns The parsed value from sessionStorage, or null if not found.
38
+ */
39
+ export declare const getSession: <T>(key: string) => T | null;
40
+ /**
41
+ * Retrieves and parses multiple JSON items from sessionStorage by key.
42
+ *
43
+ * @param keys - The keys of the items to retrieve.
44
+ * @returns The parsed values from sessionStorage.
45
+ */
46
+ export declare const getSessions: <T extends Record<string, unknown>, K extends keyof T = keyof T>(keys: readonly K[]) => { [P in K]: T[P] | null; };
@@ -0,0 +1,60 @@
1
+ /**
2
+ * Clear sessionStorage.
3
+ */
4
+ export const clearSessions = () => sessionStorage.clear();
5
+ /**
6
+ * Removes an item from sessionStorage by key.
7
+ *
8
+ * @param key - The key of the item to remove.
9
+ */
10
+ export const removeSession = (key) => sessionStorage.removeItem(key);
11
+ /**
12
+ * Removes multiple sessionStorage items by keys.
13
+ *
14
+ * @param keys - Array of keys to remove.
15
+ */
16
+ export const removeSessions = (keys) => keys.forEach(key => removeSession(key));
17
+ /**
18
+ * Sets an item in sessionStorage after serializing it to JSON.
19
+ *
20
+ * @param key - The key under which to store the value.
21
+ * @param value - The value to store (will be JSON-stringified).
22
+ */
23
+ export const setSession = (key, value) => sessionStorage.setItem(key, JSON.stringify(value));
24
+ /**
25
+ * Sets multiple items in sessionStorage after serializing it to JSON.
26
+ *
27
+ * @param items - Array of session storage items to set.
28
+ */
29
+ export const setSessions = (items) => {
30
+ items.forEach(item => setSession(item.key, item.value));
31
+ };
32
+ /**
33
+ * Retrieves and parses a JSON item from sessionStorage by key.
34
+ *
35
+ * @param key - The key of the item to retrieve.
36
+ * @returns The parsed value from sessionStorage, or null if not found.
37
+ */
38
+ export const getSession = (key) => {
39
+ const item = sessionStorage.getItem(key);
40
+ try {
41
+ if (item)
42
+ return JSON.parse(item);
43
+ return null;
44
+ }
45
+ catch {
46
+ return item;
47
+ }
48
+ };
49
+ /**
50
+ * Retrieves and parses multiple JSON items from sessionStorage by key.
51
+ *
52
+ * @param keys - The keys of the items to retrieve.
53
+ * @returns The parsed values from sessionStorage.
54
+ */
55
+ export const getSessions = (keys) => {
56
+ return keys.reduce((acc, key) => {
57
+ acc[key] = getSession(key);
58
+ return acc;
59
+ }, {});
60
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fajarmaulana/komerce-lp-helper",
3
- "version": "0.4.15",
3
+ "version": "0.4.16",
4
4
  "description": "Helper functions, hooks, and utils for Komerce LP",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -49,7 +49,7 @@
49
49
  "tsc-alias": "^1.8.16",
50
50
  "typescript": "^5.2.0"
51
51
  },
52
- "author": "Fajar Maulana",
52
+ "author": "Fajar Maulana <fajarmaulana.dev@gmail.com> (https://github.com/fajarmaulana-dev)",
53
53
  "license": "MIT",
54
54
  "keywords": [
55
55
  "react",