|
1 | 1 | // OpenWorkers Bindings types |
2 | 2 |
|
| 3 | +/** |
| 4 | + * Static asset binding for serving files from the worker bundle. |
| 5 | + * |
| 6 | + * @example |
| 7 | + * ```ts |
| 8 | + * export default { |
| 9 | + * async fetch(request, env) { |
| 10 | + * return env.ASSETS.fetch('/index.html'); |
| 11 | + * } |
| 12 | + * } |
| 13 | + * ``` |
| 14 | + */ |
3 | 15 | interface BindingAssets { |
| 16 | + /** |
| 17 | + * Fetches a static asset from the bundle. |
| 18 | + * @param path The asset path (e.g., "/index.html"). |
| 19 | + * @param options Optional request options. |
| 20 | + */ |
4 | 21 | fetch(path: string, options?: RequestInit): Promise<Response>; |
5 | 22 | } |
6 | 23 |
|
| 24 | +/** |
| 25 | + * Result of a storage head operation. |
| 26 | + */ |
7 | 27 | interface StorageHeadResult { |
| 28 | + /** The size of the object in bytes. */ |
8 | 29 | size: number; |
| 30 | + |
| 31 | + /** The ETag of the object, if available. */ |
9 | 32 | etag?: string; |
10 | 33 | } |
11 | 34 |
|
| 35 | +/** |
| 36 | + * Options for listing storage objects. |
| 37 | + */ |
12 | 38 | interface StorageListOptions { |
| 39 | + /** Only return keys starting with this prefix. */ |
13 | 40 | prefix?: string; |
| 41 | + |
| 42 | + /** Maximum number of keys to return. */ |
14 | 43 | limit?: number; |
15 | 44 | } |
16 | 45 |
|
| 46 | +/** |
| 47 | + * Result of a storage list operation. |
| 48 | + */ |
17 | 49 | interface StorageListResult { |
| 50 | + /** The matching keys. */ |
18 | 51 | keys: string[]; |
| 52 | + |
| 53 | + /** Whether there are more keys beyond the limit. */ |
19 | 54 | truncated: boolean; |
20 | 55 | } |
21 | 56 |
|
| 57 | +/** |
| 58 | + * Object storage binding for storing binary data. |
| 59 | + * Suitable for files, images, and large data. |
| 60 | + * |
| 61 | + * @example |
| 62 | + * ```ts |
| 63 | + * // Store data |
| 64 | + * await env.STORAGE.put('file.txt', 'Hello, World!'); |
| 65 | + * |
| 66 | + * // Retrieve data |
| 67 | + * const data = await env.STORAGE.get('file.txt'); |
| 68 | + * ``` |
| 69 | + */ |
22 | 70 | interface BindingStorage { |
| 71 | + /** |
| 72 | + * Retrieves a value by key. |
| 73 | + * @returns The value as a string, or null if not found. |
| 74 | + */ |
23 | 75 | get(key: string): Promise<string | null>; |
| 76 | + |
| 77 | + /** |
| 78 | + * Stores a value. |
| 79 | + * @param key The storage key. |
| 80 | + * @param value The value to store. |
| 81 | + */ |
24 | 82 | put(key: string, value: string | Uint8Array): Promise<void>; |
| 83 | + |
| 84 | + /** |
| 85 | + * Gets metadata about an object without retrieving its contents. |
| 86 | + */ |
25 | 87 | head(key: string): Promise<StorageHeadResult>; |
| 88 | + |
| 89 | + /** |
| 90 | + * Lists keys in storage. |
| 91 | + */ |
26 | 92 | list(options?: StorageListOptions): Promise<StorageListResult>; |
| 93 | + |
| 94 | + /** |
| 95 | + * Deletes a key. |
| 96 | + */ |
27 | 97 | delete(key: string): Promise<void>; |
28 | 98 | } |
29 | 99 |
|
| 100 | +/** |
| 101 | + * Options for KV put operations. |
| 102 | + */ |
30 | 103 | interface KVPutOptions { |
| 104 | + /** Time-to-live in seconds. After this time, the key expires. */ |
31 | 105 | expiresIn?: number; |
32 | 106 | } |
33 | 107 |
|
| 108 | +/** |
| 109 | + * Options for KV list operations. |
| 110 | + */ |
34 | 111 | interface KVListOptions { |
| 112 | + /** Only return keys starting with this prefix. */ |
35 | 113 | prefix?: string; |
| 114 | + |
| 115 | + /** Maximum number of keys to return. */ |
36 | 116 | limit?: number; |
37 | 117 | } |
38 | 118 |
|
| 119 | +/** |
| 120 | + * Key-Value storage binding for fast, low-latency data access. |
| 121 | + * Ideal for configuration, sessions, and small data. |
| 122 | + * |
| 123 | + * @example |
| 124 | + * ```ts |
| 125 | + * // Store with expiration |
| 126 | + * await env.KV.put('session:abc', userData, { expiresIn: 3600 }); |
| 127 | + * |
| 128 | + * // Retrieve |
| 129 | + * const session = await env.KV.get('session:abc'); |
| 130 | + * ``` |
| 131 | + */ |
39 | 132 | interface BindingKV { |
| 133 | + /** |
| 134 | + * Retrieves a value by key. |
| 135 | + * @returns The value as a string, or null if not found. |
| 136 | + */ |
40 | 137 | get(key: string): Promise<string | null>; |
| 138 | + |
| 139 | + /** |
| 140 | + * Stores a value with optional expiration. |
| 141 | + * @param key The storage key. |
| 142 | + * @param value The value to store. |
| 143 | + * @param options Optional settings like TTL. |
| 144 | + */ |
41 | 145 | put(key: string, value: string, options?: KVPutOptions): Promise<void>; |
| 146 | + |
| 147 | + /** |
| 148 | + * Deletes a key. |
| 149 | + */ |
42 | 150 | delete(key: string): Promise<void>; |
| 151 | + |
| 152 | + /** |
| 153 | + * Lists keys with optional filtering. |
| 154 | + */ |
43 | 155 | list(options?: KVListOptions): Promise<string[]>; |
44 | 156 | } |
45 | 157 |
|
| 158 | +/** |
| 159 | + * SQL database binding for relational data. |
| 160 | + * |
| 161 | + * @example |
| 162 | + * ```ts |
| 163 | + * const users = await env.DB.query<User>( |
| 164 | + * 'SELECT * FROM users WHERE active = ?', |
| 165 | + * [true] |
| 166 | + * ); |
| 167 | + * ``` |
| 168 | + */ |
46 | 169 | interface BindingDatabase { |
| 170 | + /** |
| 171 | + * Executes a SQL query. |
| 172 | + * @param sql The SQL query with ? placeholders. |
| 173 | + * @param params Parameter values to bind. |
| 174 | + * @returns An array of result rows. |
| 175 | + */ |
47 | 176 | query<T = Record<string, unknown>>(sql: string, params?: unknown[]): Promise<T[]>; |
48 | 177 | } |
49 | 178 |
|
| 179 | +/** |
| 180 | + * Service binding for calling other workers. |
| 181 | + * |
| 182 | + * @example |
| 183 | + * ```ts |
| 184 | + * const response = await env.AUTH_SERVICE.fetch( |
| 185 | + * new Request('https://internal/validate', { |
| 186 | + * method: 'POST', |
| 187 | + * body: JSON.stringify({ token }) |
| 188 | + * }) |
| 189 | + * ); |
| 190 | + * ``` |
| 191 | + */ |
50 | 192 | interface BindingWorker { |
| 193 | + /** |
| 194 | + * Calls another worker with a request. |
| 195 | + * @param request The request to send. |
| 196 | + * @param init Optional request options (when using string URL). |
| 197 | + */ |
51 | 198 | fetch(request: Request | string, init?: RequestInit): Promise<Response>; |
52 | 199 | } |
0 commit comments