| 1 | import type { |
| 2 | AppSecret, |
| 3 | GetSecretError, |
| 4 | } from "@distilled.cloud/fly-io/machines"; |
| 5 | import type * as Effect from "effect/Effect"; |
| 6 | import * as Binding from "../Binding.ts"; |
| 7 | import type { RuntimeContext } from "../RuntimeContext.ts"; |
| 8 | import type { Secret } from "./Secret.ts"; |
| 9 | |
| 10 | /** |
| 11 | * Fetch one Fly.io App secret. The App and name are fixed by |
| 12 | * `GetSecret(secret)`. Calls take no `app_name`. |
| 13 | * |
| 14 | * |
| 15 | * ### Read a secret |
| 16 | * Bind the client in init. Call it from `fetch` or an Action body. |
| 17 | * Provide {@link GetSecretHttp}. Alchemy transports the bound App name, |
| 18 | * secret name, and deployment's org token automatically. There is no |
| 19 | * need to configure `FLY_SECRET_*` environment variables. |
| 20 | * |
| 21 | * Fly only returns plaintext from a Machine in the same App. From a |
| 22 | * deploy-time Action you get metadata (name, digest, timestamps). |
| 23 | * |
| 24 | * **Example:** GetSecret |
| 25 | * ```typescript |
| 26 | * export default class Api extends Fly.Service<Api>()( |
| 27 | * "Api", |
| 28 | * { app: Site, main: import.meta.url, port: 3000 }, |
| 29 | * Effect.gen(function* () { |
| 30 | * const get = yield* Fly.GetSecret(ApiToken); |
| 31 | * |
| 32 | * return { |
| 33 | * fetch: Effect.gen(function* () { |
| 34 | * const got = yield* get().pipe(Effect.orDie); |
| 35 | * return HttpServerResponse.json({ name: got.name }); |
| 36 | * }), |
| 37 | * }; |
| 38 | * }).pipe(Effect.provide(Fly.GetSecretHttp)), |
| 39 | * ) {} |
| 40 | * ``` |
| 41 | * |
| 42 | * @binding |
| 43 | * @product Secret |
| 44 | */ |
| 45 | export interface GetSecret extends Binding.Service< |
| 46 | GetSecret, |
| 47 | "Fly.GetSecret", |
| 48 | ( |
| 49 | secret: Secret, |
| 50 | ) => Effect.Effect< |
| 51 | () => Effect.Effect<AppSecret, GetSecretError, RuntimeContext> |
| 52 | > |
| 53 | > {} |
| 54 | |
| 55 | export const GetSecret = Binding.Service<GetSecret>("Fly.GetSecret"); |