1import {
2 Credentials,
3 fromApiToken,
4} from "@distilled.cloud/cloudflare/Credentials";
5import * as Effect from "effect/Effect";
6import * as Layer from "effect/Layer";
7import * as Redacted from "effect/Redacted";
8import * as FetchHttpClient from "effect/http/FetchHttpClient";
9import type { HttpClient } from "effect/http/HttpClient";
10import 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 */
16export 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 );