INNER CODE UNIT · TypeScript
runCavemanDeviceFlow
JuliusBrussee/caveman · packages/device-auth/src/index.ts:114
export async function runCavemanDeviceFlow(options: {
baseURL: string;
client: string;
fetch?: typeof globalThis.fetch;
signal?: AbortSignal;
sleep?: (ms: number) => Promise<void>;
onCode?: (code: DeviceCode) => void | Promise<void>;
}): Promise<DeviceGrant> {
const fetcher = options.fetch ?? globalThis.fetch;
const wait = options.sleep ?? ((ms: number) => defaultSleep(ms, options.signal));
const baseURL = options.baseURL.replace(/\/$/, "");
// Device codes never follow redirects: a redirected endpoint is not the
// control plane the user asked to sign in to.
const codeResponse = await fetcher(`${baseURL}/api/v1/auth/device/code`, {
method: "POST",
redirect: "error",
headers: { "content-type": "application/json", "x-cave-client": options.client },
body: "{}",