1import * as Effect from "effect/Effect";
2import * as Binding from "../../Binding.ts";
3import type { RuntimeContext } from "../../RuntimeContext.ts";
4import type { Namespace } from "./Namespace.ts";
5import type { NamespaceError } from "./NamespaceTypes.ts";
6
7/**
8 * Bind a {@link Namespace} to a Worker with write access and obtain the
9 * Effect-native KV client (`put`, `delete`).
10 *
11 * `WriteNamespace` is a single identifier that is simultaneously the binding's
12 * Context tag, its type, and the callable —
13 * `yield* Cloudflare.KV.WriteNamespace(ns)`.
14 *
15 * @binding
16 * @product KV
17 * @category Storage & Databases
18 */
19export interface WriteNamespace extends Binding.Service<
20 WriteNamespace,
21 "Cloudflare.KV.WriteNamespace",
22 (namespace: Namespace) => Effect.Effect<WriteNamespaceClient>
23> {}
24
25export const WriteNamespace = Binding.Service<WriteNamespace>(
26 "Cloudflare.KV.WriteNamespace",
27);
28
29export interface WriteNamespaceClient<Key extends string = string> {
30 put(
31 key: Key,
32 value: string | ArrayBuffer | ArrayBufferView | ReadableStream,
33 options?: KVNamespacePutOptions,
34 ): Effect.Effect<void, NamespaceError, RuntimeContext>;
35 delete(key: Key): Effect.Effect<void, NamespaceError, RuntimeContext>;
36}