| 1 | import * as Effect from "effect/Effect"; |
| 2 | import * as Layer from "effect/Layer"; |
| 3 | import { makeReadKVClient } from "./ReadNamespaceBinding.ts"; |
| 4 | import { makeLocalKVNamespaceBinding } from "./NamespaceLocal.ts"; |
| 5 | import { ReadNamespace } from "./ReadNamespace.ts"; |
| 6 | import { makeReadKVHttpClient } from "./ReadNamespaceHttp.ts"; |
| 7 | |
| 8 | /** |
| 9 | * Local implementation of the {@link ReadNamespace} binding — reads KV values |
| 10 | * over the Cloudflare HTTP API using the **current credentials** instead of a |
| 11 | * native Worker binding (`ReadNamespaceBinding`) or a scoped API token |
| 12 | * (`ReadNamespaceHttp`). |
| 13 | * |
| 14 | * Provide it on an {@link Action} (or any deploy-time Effect) so you can read |
| 15 | * from a namespace with the same `get`/`getWithMetadata`/`list` client you'd |
| 16 | * use inside a Worker: |
| 17 | * |
| 18 | * @example Reading a value from an Action |
| 19 | * ```typescript |
| 20 | * const Check = Alchemy.Action( |
| 21 | * "Check", |
| 22 | * Effect.gen(function* () { |
| 23 | * const kv = yield* Cloudflare.KV.ReadNamespace(namespace); |
| 24 | * return Effect.fn(function* () { |
| 25 | * return yield* kv.get("my-key"); |
| 26 | * }); |
| 27 | * }).pipe(Effect.provide(Cloudflare.KV.ReadNamespaceLocal)), |
| 28 | * ); |
| 29 | * ``` |
| 30 | */ |
| 31 | export const ReadNamespaceLocal = Layer.effect( |
| 32 | ReadNamespace, |
| 33 | Effect.suspend(() => |
| 34 | makeLocalKVNamespaceBinding({ |
| 35 | makeHttpClient: makeReadKVHttpClient, |
| 36 | makeNativeClient: makeReadKVClient, |
| 37 | }), |
| 38 | ), |
| 39 | ); |