| 1 | import type * as runtime from "@cloudflare/workers-types"; |
| 2 | import * as Data from "effect/Data"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import * as Redacted from "effect/Redacted"; |
| 5 | import * as Binding from "../../Binding.ts"; |
| 6 | import type { RuntimeContext } from "../../RuntimeContext.ts"; |
| 7 | import type { Secret } from "./Secret.ts"; |
| 8 | |
| 9 | /** |
| 10 | * Bind a {@link Secret} to a Worker and obtain the Effect-native secret |
| 11 | * client. The client is itself an `Effect` that resolves to the secret's |
| 12 | * current value. |
| 13 | * |
| 14 | * `ReadSecret` is a single identifier that is simultaneously the binding's |
| 15 | * Context tag, its type, and the callable — |
| 16 | * `yield* Cloudflare.SecretsStore.ReadSecret(ApiKey)`. |
| 17 | * |
| 18 | * **Example:** Reading a secret at runtime |
| 19 | * ```typescript |
| 20 | * const apiKey = yield* Cloudflare.SecretsStore.ReadSecret(ApiKey); |
| 21 | * const value = yield* apiKey; |
| 22 | * ``` |
| 23 | * |
| 24 | * @binding |
| 25 | * @product Secrets Store |
| 26 | * @category Storage & Databases |
| 27 | */ |
| 28 | export interface ReadSecret extends Binding.Service< |
| 29 | ReadSecret, |
| 30 | "Cloudflare.SecretsStore.ReadSecret", |
| 31 | (secret: Secret) => Effect.Effect<ReadSecretClient> |
| 32 | > {} |
| 33 | |
| 34 | export const ReadSecret = Binding.Service<ReadSecret>( |
| 35 | "Cloudflare.SecretsStore.ReadSecret", |
| 36 | ); |
| 37 | |
| 38 | export class SecretError extends Data.TaggedError("SecretError")<{ |
| 39 | message: string; |
| 40 | cause: Error; |
| 41 | }> {} |
| 42 | |
| 43 | /** |
| 44 | * A bound secret. The client itself is an `Effect` that resolves to the |
| 45 | * secret's current value, so you can `yield* apiKey` directly. Use `.get()` |
| 46 | * for the same thing as a callable, or `.raw` for the underlying |
| 47 | * `SecretsStoreSecret` binding. |
| 48 | */ |
| 49 | export interface ReadSecretClient extends Effect.Effect< |
| 50 | Redacted.Redacted<string>, |
| 51 | SecretError, |
| 52 | RuntimeContext |
| 53 | > { |
| 54 | /** |
| 55 | * Effect that resolves to the raw Cloudflare `SecretsStoreSecret` binding. |
| 56 | */ |
| 57 | raw: Effect.Effect<runtime.SecretsStoreSecret, never, RuntimeContext>; |
| 58 | /** |
| 59 | * Read the current value of the secret. |
| 60 | */ |
| 61 | get(): Effect.Effect<Redacted.Redacted<string>, SecretError, RuntimeContext>; |
| 62 | } |