1- import { ITool } from ".." ;
1+ import { ITool , type Serializable } from ".." ;
22
33/**
44 * Built-in tool for persistent key-value storage.
@@ -14,9 +14,20 @@ import { ITool } from "..";
1414 * **Storage Characteristics:**
1515 * - Persistent across worker restarts
1616 * - Isolated per twist/tool instance
17- * - Supports any JSON -serializable data
17+ * - Supports SuperJSON -serializable data (see below)
1818 * - Async operations for scalability
1919 *
20+ * **Supported Data Types (via SuperJSON):**
21+ * - Primitives: string, number, boolean, null, undefined
22+ * - Complex types: Date, RegExp, Map, Set, Error, URL, BigInt
23+ * - Collections: Arrays and objects (recursively)
24+ *
25+ * **NOT Supported (will throw validation errors):**
26+ * - Functions (use callback tokens instead - see Callbacks tool)
27+ * - Symbols
28+ * - Circular references
29+ * - Custom class instances
30+ *
2031 * **Use Cases:**
2132 * - Storing authentication tokens
2233 * - Caching configuration data
@@ -51,42 +62,56 @@ export abstract class Store extends ITool {
5162 * Returns the stored value deserialized to the specified type,
5263 * or null if the key doesn't exist or the value is null.
5364 *
54- * @template T - The expected type of the stored value
65+ * Values are automatically deserialized using SuperJSON, which
66+ * properly restores Date objects, Maps, Sets, and other complex types.
67+ *
68+ * @template T - The expected type of the stored value (must be Serializable)
5569 * @param key - The storage key to retrieve
5670 * @returns Promise resolving to the stored value or null
5771 */
5872 // eslint-disable-next-line @typescript-eslint/no-unused-vars
59- abstract get < T > ( key : string ) : Promise < T | null > ;
73+ abstract get < T extends Serializable > ( key : string ) : Promise < T | null > ;
6074
6175 /**
6276 * Stores a value in persistent storage.
6377 *
64- * The value will be JSON- serialized and stored persistently.
78+ * The value will be serialized using SuperJSON and stored persistently.
6579 * Any existing value at the same key will be overwritten.
6680 *
67- * **Handling undefined values:**
68- * - Object keys with undefined values are automatically removed
69- * - Arrays with undefined elements will throw a validation error
70- * - Use null instead of undefined for array elements
81+ * SuperJSON automatically handles Date objects, Maps, Sets, undefined values,
82+ * and other complex types that standard JSON doesn't support.
7183 *
72- * @template T - The type of value being stored
84+ * @template T - The type of value being stored (must be Serializable)
7385 * @param key - The storage key to use
74- * @param value - The value to store (must be JSON -serializable)
86+ * @param value - The value to store (must be SuperJSON -serializable)
7587 * @returns Promise that resolves when the value is stored
7688 *
7789 * @example
7890 * ```typescript
79- * // Object keys with undefined are removed
80- * await this.set('data', { name: 'test', optional: undefined });
81- * // Stores: { name: 'test' }
91+ * // Date objects are preserved
92+ * await this.set('sync_state', {
93+ * lastSync: new Date(),
94+ * minDate: new Date(2024, 0, 1)
95+ * });
96+ *
97+ * // undefined is now supported
98+ * await this.set('data', { name: 'test', optional: undefined }); // ✅ Works
99+ *
100+ * // Arrays with undefined are supported
101+ * await this.set('items', [1, undefined, 3]); // ✅ Works
102+ * await this.set('items', [1, null, 3]); // ✅ Also works
103+ *
104+ * // Maps and Sets are supported
105+ * await this.set('mapping', new Map([['key', 'value']])); // ✅ Works
106+ * await this.set('tags', new Set(['tag1', 'tag2'])); // ✅ Works
82107 *
83- * // Arrays with undefined throw errors - use null instead
84- * await this.set('items', [1, null, 3]); // ✅ Works
85- * await this.set('items ', [1, undefined, 3] ); // ❌ Throws error
108+ * // Functions are NOT supported - use callback tokens instead
109+ * const token = await this.callback(this.myFunction);
110+ * await this.set('callback_ref ', token ); // ✅ Use callback token
86111 * ```
87112 */
88113 // eslint-disable-next-line @typescript-eslint/no-unused-vars
89- abstract set < T > ( key : string , value : T ) : Promise < void > ;
114+ abstract set < T extends Serializable > ( key : string , value : T ) : Promise < void > ;
90115
91116 /**
92117 * Removes a specific key from storage.
0 commit comments