The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import type * as runtime from "@cloudflare/workers-types"; |
| 2 | import * as Binding from "alchemy/Binding"; |
| 3 | import type { Input } from "alchemy/Input"; |
| 4 | import type { RuntimeContext } from "alchemy/RuntimeContext"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | import type { InstantNamespace } from "./InstantNamespace.ts"; |
| 7 | import type { ReadInstantNamespaceClient } from "./InstantNamespaceTypes.ts"; |
| 8 | import type { Namespace } from "./Namespace.ts"; |
| 9 | import type { NamespaceError } from "./NamespaceTypes.ts"; |
| 10 | |
| 11 | /** |
| 12 | * Bind a {@link Namespace} to a Worker with read access and obtain the |
| 13 | * Effect-native KV client (`get`, `getWithMetadata`, `list`). |
| 14 | * |
| 15 | * `ReadNamespace` is a single identifier that is simultaneously the binding's |
| 16 | * Context tag, its type, and the callable — |
| 17 | * `yield* Cloudflare.KV.ReadNamespace(ns)`. |
| 18 | * |
| 19 | * Also accepts InstantNamespace, returning a client without metadata or |
| 20 | * paginated listing options. |
| 21 | * |
| 22 | * @binding |
| 23 | * @product KV |
| 24 | * @category Storage & Databases |
| 25 | */ |
| 26 | export interface ReadNamespace extends Binding.Service< |
| 27 | ReadNamespace, |
| 28 | "Cloudflare.KV.ReadNamespace", |
| 29 | { |
| 30 | (namespace: InstantNamespace): Effect.Effect<ReadInstantNamespaceClient>; |
| 31 | (namespace: Namespace): Effect.Effect<ReadNamespaceClient>; |
| 32 | (namespace: Namespace | InstantNamespace): Effect.Effect<ReadInstantNamespaceClient>; |
| 33 | } |
| 34 | > { |
| 35 | <Req = never>( |
| 36 | namespace: Input<Namespace> | Effect.Effect<Namespace, never, Req>, |
| 37 | ): Effect.Effect<ReadNamespaceClient, never, ReadNamespace | Req>; |
| 38 | <Req = never>( |
| 39 | namespace: |
| 40 | | Input<Namespace | InstantNamespace> |
| 41 | | Effect.Effect<Namespace | InstantNamespace, never, Req>, |
| 42 | ): Effect.Effect<ReadInstantNamespaceClient, never, ReadNamespace | Req>; |
| 43 | } |
| 44 | |
| 45 | export const ReadNamespace = Binding.Service<ReadNamespace>("Cloudflare.KV.ReadNamespace"); |
| 46 | |
| 47 | export interface ReadNamespaceClient<Key extends string = string> { |
| 48 | raw: Effect.Effect<runtime.KVNamespace, never, RuntimeContext>; |
| 49 | get( |
| 50 | key: Key, |
| 51 | options?: Partial<KVNamespaceGetOptions<undefined>>, |
| 52 | ): Effect.Effect<string | null, NamespaceError, RuntimeContext>; |
| 53 | get(key: Key, type: "text"): Effect.Effect<string | null, NamespaceError, RuntimeContext>; |
| 54 | get<ExpectedValue = unknown>( |
| 55 | key: Key, |
| 56 | type: "json", |
| 57 | ): Effect.Effect<ExpectedValue | null, NamespaceError, RuntimeContext>; |
| 58 | get( |
| 59 | key: Key, |
| 60 | type: "arrayBuffer", |
| 61 | ): Effect.Effect<ArrayBuffer | null, NamespaceError, RuntimeContext>; |
| 62 | get( |
| 63 | key: Key, |
| 64 | type: "stream", |
| 65 | ): Effect.Effect<ReadableStream | null, NamespaceError, RuntimeContext>; |
| 66 | get( |
| 67 | key: Key, |
| 68 | options?: KVNamespaceGetOptions<"text">, |
| 69 | ): Effect.Effect<string | null, NamespaceError, RuntimeContext>; |
| 70 | get<ExpectedValue = unknown>( |
| 71 | key: Key, |
| 72 | options?: KVNamespaceGetOptions<"json">, |
| 73 | ): Effect.Effect<ExpectedValue | null, NamespaceError, RuntimeContext>; |
| 74 | get( |
| 75 | key: Key, |
| 76 | options?: KVNamespaceGetOptions<"arrayBuffer">, |
| 77 | ): Effect.Effect<ArrayBuffer | null, NamespaceError, RuntimeContext>; |
| 78 | get( |
| 79 | key: Key, |
| 80 | options?: KVNamespaceGetOptions<"stream">, |
| 81 | ): Effect.Effect<ReadableStream | null, NamespaceError, RuntimeContext>; |
| 82 | get( |
| 83 | key: Array<Key>, |
| 84 | type: "text", |
| 85 | ): Effect.Effect<Map<string, string | null>, NamespaceError, RuntimeContext>; |
| 86 | get<ExpectedValue = unknown>( |
| 87 | key: Array<Key>, |
| 88 | type: "json", |
| 89 | ): Effect.Effect<Map<string, ExpectedValue | null>, NamespaceError, RuntimeContext>; |
| 90 | get( |
| 91 | key: Array<Key>, |
| 92 | options?: Partial<KVNamespaceGetOptions<undefined>>, |
| 93 | ): Effect.Effect<Map<string, string | null>, NamespaceError, RuntimeContext>; |
| 94 | get( |
| 95 | key: Array<Key>, |
| 96 | options?: KVNamespaceGetOptions<"text">, |
| 97 | ): Effect.Effect<Map<string, string | null>, NamespaceError, RuntimeContext>; |
| 98 | get<ExpectedValue = unknown>( |
| 99 | key: Array<Key>, |
| 100 | options?: KVNamespaceGetOptions<"json">, |
| 101 | ): Effect.Effect<Map<string, ExpectedValue | null>, NamespaceError, RuntimeContext>; |
| 102 | list<Metadata = unknown>( |
| 103 | options?: KVNamespaceListOptions, |
| 104 | ): Effect.Effect<KVNamespaceListResult<Metadata, Key>, NamespaceError, RuntimeContext>; |
| 105 | getWithMetadata<Metadata = unknown>( |
| 106 | key: Key, |
| 107 | options?: Partial<KVNamespaceGetOptions<undefined>>, |
| 108 | ): Effect.Effect< |
| 109 | KVNamespaceGetWithMetadataResult<string, Metadata>, |
| 110 | NamespaceError, |
| 111 | RuntimeContext |
| 112 | >; |
| 113 | getWithMetadata<Metadata = unknown>( |
| 114 | key: Key, |
| 115 | type: "text", |
| 116 | ): Effect.Effect< |
| 117 | KVNamespaceGetWithMetadataResult<string, Metadata>, |
| 118 | NamespaceError, |
| 119 | RuntimeContext |
| 120 | >; |
| 121 | getWithMetadata<ExpectedValue = unknown, Metadata = unknown>( |
| 122 | key: Key, |
| 123 | type: "json", |
| 124 | ): Effect.Effect< |
| 125 | KVNamespaceGetWithMetadataResult<ExpectedValue, Metadata>, |
| 126 | NamespaceError, |
| 127 | RuntimeContext |
| 128 | >; |
| 129 | getWithMetadata<Metadata = unknown>( |
| 130 | key: Key, |
| 131 | type: "arrayBuffer", |
| 132 | ): Effect.Effect< |
| 133 | KVNamespaceGetWithMetadataResult<ArrayBuffer, Metadata>, |
| 134 | NamespaceError, |
| 135 | RuntimeContext |
| 136 | >; |
| 137 | getWithMetadata<Metadata = unknown>( |
| 138 | key: Key, |
| 139 | type: "stream", |
| 140 | ): Effect.Effect< |
| 141 | KVNamespaceGetWithMetadataResult<ReadableStream, Metadata>, |
| 142 | NamespaceError, |
| 143 | RuntimeContext |
| 144 | >; |
| 145 | getWithMetadata<Metadata = unknown>( |
| 146 | key: Key, |
| 147 | options: KVNamespaceGetOptions<"text">, |
| 148 | ): Effect.Effect< |
| 149 | KVNamespaceGetWithMetadataResult<string, Metadata>, |
| 150 | NamespaceError, |
| 151 | RuntimeContext |
| 152 | >; |
| 153 | getWithMetadata<ExpectedValue = unknown, Metadata = unknown>( |
| 154 | key: Key, |
| 155 | options: KVNamespaceGetOptions<"json">, |
| 156 | ): Effect.Effect< |
| 157 | KVNamespaceGetWithMetadataResult<ExpectedValue, Metadata>, |
| 158 | NamespaceError, |
| 159 | RuntimeContext |
| 160 | >; |
| 161 | getWithMetadata<Metadata = unknown>( |
| 162 | key: Key, |
| 163 | options: KVNamespaceGetOptions<"arrayBuffer">, |
| 164 | ): Effect.Effect< |
| 165 | KVNamespaceGetWithMetadataResult<ArrayBuffer, Metadata>, |
| 166 | NamespaceError, |
| 167 | RuntimeContext |
| 168 | >; |
| 169 | getWithMetadata<Metadata = unknown>( |
| 170 | key: Key, |
| 171 | options: KVNamespaceGetOptions<"stream">, |
| 172 | ): Effect.Effect< |
| 173 | KVNamespaceGetWithMetadataResult<ReadableStream, Metadata>, |
| 174 | NamespaceError, |
| 175 | RuntimeContext |
| 176 | >; |
| 177 | getWithMetadata<Metadata = unknown>( |
| 178 | key: Array<Key>, |
| 179 | type: "text", |
| 180 | ): Effect.Effect< |
| 181 | Map<string, KVNamespaceGetWithMetadataResult<string, Metadata>>, |
| 182 | NamespaceError, |
| 183 | RuntimeContext |
| 184 | >; |
| 185 | getWithMetadata<ExpectedValue = unknown, Metadata = unknown>( |
| 186 | key: Array<Key>, |
| 187 | type: "json", |
| 188 | ): Effect.Effect< |
| 189 | Map<string, KVNamespaceGetWithMetadataResult<ExpectedValue, Metadata>>, |
| 190 | NamespaceError, |
| 191 | RuntimeContext |
| 192 | >; |
| 193 | getWithMetadata<Metadata = unknown>( |
| 194 | key: Array<Key>, |
| 195 | options?: Partial<KVNamespaceGetOptions<undefined>>, |
| 196 | ): Effect.Effect< |
| 197 | Map<string, KVNamespaceGetWithMetadataResult<string, Metadata>>, |
| 198 | NamespaceError, |
| 199 | RuntimeContext |
| 200 | >; |
| 201 | getWithMetadata<Metadata = unknown>( |
| 202 | key: Array<Key>, |
| 203 | options?: KVNamespaceGetOptions<"text">, |
| 204 | ): Effect.Effect< |
| 205 | Map<string, KVNamespaceGetWithMetadataResult<string, Metadata>>, |
| 206 | NamespaceError, |
| 207 | RuntimeContext |
| 208 | >; |
| 209 | getWithMetadata<ExpectedValue = unknown, Metadata = unknown>( |
| 210 | key: Array<Key>, |
| 211 | options?: KVNamespaceGetOptions<"json">, |
| 212 | ): Effect.Effect< |
| 213 | Map<string, KVNamespaceGetWithMetadataResult<ExpectedValue, Metadata>>, |
| 214 | NamespaceError, |
| 215 | RuntimeContext |
| 216 | >; |
| 217 | } |