Skip to content

Commit 1948db9

Browse files
committed
Update KV value type
1 parent d35969e commit 1948db9

2 files changed

Lines changed: 33 additions & 11 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@openworkers/workers-types",
3-
"version": "0.1.4",
3+
"version": "0.1.5",
44
"description": "TypeScript types for the OpenWorkers runtime",
55
"license": "MIT",
66
"repository": {

types/bindings.d.ts

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -119,33 +119,55 @@ declare global {
119119
limit?: number;
120120
}
121121

122+
/**
123+
* JSON-serializable value type.
124+
* Supports strings, numbers, booleans, null, arrays, and objects.
125+
* Note: Binary data (Uint8Array, ArrayBuffer) is not supported.
126+
*/
127+
type JsonValue =
128+
| string
129+
| number
130+
| boolean
131+
| null
132+
| JsonValue[]
133+
| { [key: string]: JsonValue };
134+
122135
/**
123136
* Key-Value storage binding for fast, low-latency data access.
124-
* Ideal for configuration, sessions, and small data.
137+
* Stores values as JSON, supporting strings, numbers, booleans, objects, and arrays.
138+
* Ideal for configuration, sessions, and structured data.
125139
*
126140
* @example
127141
* ```ts
128-
* // Store with expiration
129-
* await env.KV.put('session:abc', userData, { expiresIn: 3600 });
142+
* // Store a string
143+
* await env.KV.put('greeting', 'Hello, World!');
144+
*
145+
* // Store a number
146+
* await env.KV.put('count', 42);
130147
*
131-
* // Retrieve
132-
* const session = await env.KV.get('session:abc');
148+
* // Store an object with expiration
149+
* await env.KV.put('session:abc', { userId: 123, role: 'admin' }, { expiresIn: 3600 });
150+
*
151+
* // Retrieve with type inference
152+
* const session = await env.KV.get<{ userId: number; role: string }>('session:abc');
133153
* ```
134154
*/
135155
interface BindingKV {
136156
/**
137157
* Retrieves a value by key.
138-
* @returns The value as a string, or null if not found.
158+
* @typeParam T The expected value type (defaults to unknown).
159+
* @returns The value, or null if not found or expired.
139160
*/
140-
get(key: string): Promise<string | null>;
161+
get<T = unknown>(key: string): Promise<T | null>;
141162

142163
/**
143-
* Stores a value with optional expiration.
164+
* Stores a JSON-serializable value with optional expiration.
165+
* Maximum value size: 100KB.
144166
* @param key The storage key.
145-
* @param value The value to store.
167+
* @param value The value to store (must be JSON-serializable).
146168
* @param options Optional settings like TTL.
147169
*/
148-
put(key: string, value: string, options?: KVPutOptions): Promise<void>;
170+
put(key: string, value: JsonValue, options?: KVPutOptions): Promise<void>;
149171

150172
/**
151173
* Deletes a key.

0 commit comments

Comments
 (0)