import { APIError, createClient, throwAPIError, type ArgosAPIClient, } from "@argos-ci/api-client"; import { getUserAgent } from "./user-agent"; /** Optional override for the API base URL (used in tests and dev). */ export function getApiBaseUrl(): string | undefined { return process.env["ARGOS_API_BASE_URL"]; } /** Create an Argos API client authenticated with the given token. */ export function createApiClient(authToken?: string): ArgosAPIClient { return createClient({ authToken, baseUrl: getApiBaseUrl(), // Read per request rather than captured here: agent detection is async and // resolves at startup, while clients are built synchronously all over. userAgent: getUserAgent, }); } /** * Unwrap an `openapi-fetch` result, returning its data or throwing a formatted * {@link APIError} when the request failed or returned an empty body. */ export function unwrap(result: { data?: T | undefined; error?: unknown; response: Response; }): T { // Discriminate on the response status, not on `error`: a failed request with // an empty body (e.g. a 403/404 from a proxy or CDN) leaves `error` as // `undefined` or `""`, and `formatAPIError` falls back to the HTTP status — // a far more useful message than "Unexpected empty response". if (!result.response.ok) { throwAPIError(result.error, result.response); } if (result.data === undefined || result.data === null) { throw new APIError("Unexpected empty response from API.", { status: result.response.status, }); } return result.data; } /** * Like {@link unwrap} for endpoints that answer `204 No Content`: it only * checks the request succeeded, since there is no body to return. */ export function unwrapEmpty(result: { error?: unknown; response: Response; }): void { if (!result.response.ok) { throwAPIError(result.error, result.response); } }