import { ConfigError } from "@distilled.cloud/core/errors"; /** * Remote credentials -- hand-written. * * The `Credentials` service holds an *effect* that resolves the current * credentials on every request (the protocol resolves it per request * on the calling fiber). Auth is a Remote API / OAuth bearer token sent as * `Authorization: Bearer `. */ import * as Context from "effect/Context"; import * as Effect from "effect/Effect"; import * as Layer from "effect/Layer"; import * as Redacted from "effect/Redacted"; export const DEFAULT_API_BASE_URL = "https://gateway.remote.com"; export interface Config { readonly apiKey: Redacted.Redacted; readonly apiBaseUrl: string; } export class Credentials extends /*@__PURE__*/ Context.Service>()( "RemoteCredentials", ) {} /** Layer from a redacted API token + optional base URL. */ export const fromApiKey = (config: { readonly apiKey: Redacted.Redacted; readonly apiBaseUrl?: string; }): Layer.Layer => Layer.succeed( Credentials, Effect.succeed({ apiKey: config.apiKey, apiBaseUrl: config.apiBaseUrl ?? DEFAULT_API_BASE_URL, }), ); /** * Reads REMOTE_API_KEY or REMOTE_ACCESS_TOKEN (required) and * REMOTE_API_BASE_URL (optional). Sandbox is * `https://gateway.remote-sandbox.com`. */ export const CredentialsFromEnv: Layer.Layer = /*@__PURE__*/ Layer.succeed( Credentials, /*@__PURE__*/ Effect.gen(function* () { const apiKey = process.env.REMOTE_API_KEY ?? process.env.REMOTE_ACCESS_TOKEN; if (!apiKey) { return yield* new ConfigError({ message: "REMOTE_API_KEY (or REMOTE_ACCESS_TOKEN) environment variable is required", }); } return { apiKey: Redacted.make(apiKey), apiBaseUrl: process.env.REMOTE_API_BASE_URL ?? DEFAULT_API_BASE_URL, }; }), );