1import * as Effect from "effect/Effect";
2import * as Layer from "effect/Layer";
3import { makeReadKVClient } from "./ReadNamespaceBinding.ts";
4import { makeLocalKVNamespaceBinding } from "./NamespaceLocal.ts";
5import { ReadNamespace } from "./ReadNamespace.ts";
6import { 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 */
31export const ReadNamespaceLocal = Layer.effect(
32 ReadNamespace,
33 Effect.suspend(() =>
34 makeLocalKVNamespaceBinding({
35 makeHttpClient: makeReadKVHttpClient,
36 makeNativeClient: makeReadKVClient,
37 }),
38 ),
39);