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