(options: XaiAuthPluginOptions = {})
| 196 | } |
| 197 | |
| 198 | export async function requestDeviceCode(options: XaiAuthPluginOptions = {}): Promise<DeviceCodeResponse> { |
| 199 | const response = await fetch(options.deviceAuthorizationUrl ?? DEVICE_AUTHORIZATION_URL, { |
| 200 | method: "POST", |
| 201 | headers: authHeaders(), |
| 202 | body: new URLSearchParams({ |
| 203 | client_id: CLIENT_ID, |
| 204 | scope: SCOPE, |
| 205 | }).toString(), |
| 206 | }) |
| 207 | if (!response.ok) { |
| 208 | const detail = await response.text().catch(() => "") |
| 209 | throw new Error(`xAI device code request failed (${response.status})${detail ? `: ${detail}` : ""}`) |
| 210 | } |
| 211 | const json = (await response.json()) as DeviceCodeResponse |
| 212 | if (!json.device_code || !json.user_code || !json.verification_uri) { |
| 213 | throw new Error("xAI device code response is missing device_code / user_code / verification_uri") |
| 214 | } |
| 215 | return json |
| 216 | } |
| 217 | |
| 218 | // Default sleep used between device-code polls. Test-injectable so we can |
| 219 | // exercise authorization_pending / slow_down branches without real waits. |
no test coverage detected