| 1 | import * as Effect from "effect/Effect"; |
| 2 | import * as Layer from "effect/Layer"; |
| 3 | import { |
| 4 | makeHttpKVNamespaceBinding, |
| 5 | makeKVAuth, |
| 6 | type KVAuth, |
| 7 | } from "./NamespaceHttp.ts"; |
| 8 | import { makeReadKVHttpClient } from "./ReadNamespaceHttp.ts"; |
| 9 | import { |
| 10 | ReadWriteNamespace, |
| 11 | type ReadWriteNamespaceClient, |
| 12 | } from "./ReadWriteNamespace.ts"; |
| 13 | import { makeWriteKVHttpClient } from "./WriteNamespaceHttp.ts"; |
| 14 | |
| 15 | /** |
| 16 | * HTTP-backed implementation of the {@link ReadWriteNamespace} binding. |
| 17 | * |
| 18 | * It creates a scoped {@link AccountApiToken} with the `Workers KV Storage |
| 19 | * Read` and `Workers KV Storage Write` permissions. |
| 20 | */ |
| 21 | export const ReadWriteNamespaceHttp = Layer.effect( |
| 22 | ReadWriteNamespace, |
| 23 | Effect.suspend(() => |
| 24 | makeHttpKVNamespaceBinding({ |
| 25 | permissionGroups: ["Workers KV Storage Read", "Workers KV Storage Write"], |
| 26 | makeClient: (token, namespaceId) => |
| 27 | makeReadWriteKVHttpClient(makeKVAuth(token), namespaceId), |
| 28 | }), |
| 29 | ), |
| 30 | ); |
| 31 | |
| 32 | /** Build the HTTP-backed read-write client over an auth + namespace. */ |
| 33 | export const makeReadWriteKVHttpClient = ( |
| 34 | auth: KVAuth, |
| 35 | namespaceId: Effect.Effect<string>, |
| 36 | ): ReadWriteNamespaceClient => |
| 37 | ({ |
| 38 | ...makeReadKVHttpClient(auth, namespaceId), |
| 39 | ...makeWriteKVHttpClient(auth, namespaceId), |
| 40 | }) as ReadWriteNamespaceClient; |