| 1 | import * as Context from "effect/Context"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as Layer from "effect/Layer"; |
| 4 | import * as FetchHttpClient from "effect/http/FetchHttpClient"; |
| 5 | import * as HttpApiClient from "effect/http-api/HttpApiClient"; |
| 6 | import { BackendApi } from "./BackendApi.ts"; |
| 7 | import { env } from "./Env.ts"; |
| 8 | |
| 9 | const backendClient = HttpApiClient.make(BackendApi, { |
| 10 | baseUrl: "http://localhost", |
| 11 | }).pipe( |
| 12 | Effect.provide( |
| 13 | FetchHttpClient.layer.pipe( |
| 14 | Layer.provide( |
| 15 | Layer.succeed(FetchHttpClient.Fetch, ((input, init) => |
| 16 | env.BACKEND.fetch(input, init)) as typeof globalThis.fetch), |
| 17 | ), |
| 18 | ), |
| 19 | ), |
| 20 | ); |
| 21 | |
| 22 | export class BackendClient extends Context.Service< |
| 23 | BackendClient, |
| 24 | Effect.Success<typeof backendClient> |
| 25 | >()("BackendClient") { |
| 26 | static Default = Layer.effect(BackendClient, backendClient); |
| 27 | } |