| 1 | import { |
| 2 | Credentials, |
| 3 | fromApiToken, |
| 4 | } from "@distilled.cloud/cloudflare/Credentials"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | import * as Layer from "effect/Layer"; |
| 7 | import * as Redacted from "effect/Redacted"; |
| 8 | import * as FetchHttpClient from "effect/http/FetchHttpClient"; |
| 9 | import type { HttpClient } from "effect/http/HttpClient"; |
| 10 | import type { RuntimeContext } from "../RuntimeContext.ts"; |
| 11 | |
| 12 | /** |
| 13 | * Resolve credentials from a bound token's value and provide them (plus the |
| 14 | * fetch-based HTTP client) to a raw SDK operation. |
| 15 | */ |
| 16 | export const authorizeWith = |
| 17 | (token: { value: Effect.Effect<Redacted.Redacted<string>> }) => |
| 18 | <A, E>( |
| 19 | eff: Effect.Effect<A, E, Credentials | HttpClient>, |
| 20 | ): Effect.Effect<A, E, RuntimeContext> => |
| 21 | token.value.pipe( |
| 22 | Effect.flatMap((value) => |
| 23 | eff.pipe( |
| 24 | Effect.provide( |
| 25 | fromApiToken({ apiToken: Redacted.value(value) }).pipe( |
| 26 | Layer.provideMerge(FetchHttpClient.layer), |
| 27 | ), |
| 28 | ), |
| 29 | ), |
| 30 | ), |
| 31 | ); |