| 1 | import * as sprites from "@distilled.cloud/fly-io/sprites"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as Layer from "effect/Layer"; |
| 4 | import * as FetchHttpClient from "effect/http/FetchHttpClient"; |
| 5 | import { CredentialsFromAmbientOrEnv } from "./Credentials.ts"; |
| 6 | import { Checkpoint, type CheckpointClient } from "./Checkpoint.ts"; |
| 7 | import { makeHttpSpriteBinding } from "./SpriteHttp.ts"; |
| 8 | |
| 9 | /** |
| 10 | * HTTP implementation of {@link Checkpoint}. Provide it on the |
| 11 | * {@link Sprite}, {@link Service}, or Action Effect. |
| 12 | * |
| 13 | * |
| 14 | * ### Provide the layer |
| 15 | * **Example:** On a Sprite |
| 16 | * ```typescript |
| 17 | * Effect.gen(function* () { |
| 18 | * const checkpoint = yield* Fly.Checkpoint(Box); |
| 19 | * // ... |
| 20 | * }).pipe(Effect.provide(Fly.CheckpointHttp)) |
| 21 | * ``` |
| 22 | * |
| 23 | * @layer |
| 24 | * @product Sprite |
| 25 | * @provides Fly.Checkpoint |
| 26 | */ |
| 27 | export const CheckpointHttp = Layer.effect( |
| 28 | Checkpoint, |
| 29 | Effect.suspend(() => |
| 30 | makeHttpSpriteBinding({ |
| 31 | makeClient: (auth, spriteName): CheckpointClient => ({ |
| 32 | create: Effect.fn("Fly.Checkpoint.create")(function* (options) { |
| 33 | return yield* auth.authorize( |
| 34 | sprites.createCheckpoint({ |
| 35 | name: yield* spriteName, |
| 36 | comment: options?.comment, |
| 37 | }), |
| 38 | ); |
| 39 | }), |
| 40 | list: Effect.fn("Fly.Checkpoint.list")(function* () { |
| 41 | return yield* auth.authorize( |
| 42 | sprites.listCheckpoints({ |
| 43 | name: yield* spriteName, |
| 44 | }), |
| 45 | ); |
| 46 | }), |
| 47 | get: Effect.fn("Fly.Checkpoint.get")(function* (checkpointId) { |
| 48 | return yield* auth.authorize( |
| 49 | sprites.getCheckpoint({ |
| 50 | name: yield* spriteName, |
| 51 | checkpoint_id: checkpointId, |
| 52 | }), |
| 53 | ); |
| 54 | }), |
| 55 | restore: Effect.fn("Fly.Checkpoint.restore")(function* (checkpointId) { |
| 56 | return yield* auth.authorize( |
| 57 | sprites.restoreCheckpoint({ |
| 58 | name: yield* spriteName, |
| 59 | checkpoint_id: checkpointId, |
| 60 | }), |
| 61 | ); |
| 62 | }), |
| 63 | }), |
| 64 | }), |
| 65 | ), |
| 66 | ).pipe( |
| 67 | Layer.provide(FetchHttpClient.layer), |
| 68 | Layer.provide(CredentialsFromAmbientOrEnv), |
| 69 | ); |