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