| 1 | import type * as runtime from "@cloudflare/workers-types"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as Layer from "effect/Layer"; |
| 4 | import { |
| 5 | makeKVNamespaceBinding, |
| 6 | makeKVNamespaceHelpers, |
| 7 | } from "./NamespaceBinding.ts"; |
| 8 | import { ReadNamespace, type ReadNamespaceClient } from "./ReadNamespace.ts"; |
| 9 | |
| 10 | /** |
| 11 | * Implementation of the {@link ReadNamespace} binding that uses a Worker |
| 12 | * binding. |
| 13 | */ |
| 14 | export const ReadNamespaceBinding = Layer.effect( |
| 15 | ReadNamespace, |
| 16 | Effect.suspend(() => |
| 17 | makeKVNamespaceBinding({ makeClient: makeReadKVClient }), |
| 18 | ), |
| 19 | ); |
| 20 | |
| 21 | /** Build the read half of the binding client. */ |
| 22 | export const makeReadKVClient = ({ |
| 23 | raw, |
| 24 | use, |
| 25 | }: ReturnType<typeof makeKVNamespaceHelpers>): ReadNamespaceClient => { |
| 26 | return { |
| 27 | raw, |
| 28 | get: ((...args: Parameters<runtime.KVNamespace["get"]>) => |
| 29 | use((raw) => raw.get(...(args as [any, any])))) as any, |
| 30 | getWithMetadata: (( |
| 31 | ...args: Parameters<runtime.KVNamespace["getWithMetadata"]> |
| 32 | ) => use((raw) => raw.getWithMetadata(...(args as [any, any])))) as any, |
| 33 | list: ((...args: Parameters<runtime.KVNamespace["list"]>) => |
| 34 | use((raw) => raw.list(...args))) as any, |
| 35 | }; |
| 36 | }; |