( region: Region, apiKey: string, timeoutMs: number, )
| 8 | } |
| 9 | |
| 10 | async function probeRegion( |
| 11 | region: Region, |
| 12 | apiKey: string, |
| 13 | timeoutMs: number, |
| 14 | ): Promise<boolean> { |
| 15 | // MiniMax endpoints accept either Bearer or x-api-key auth — try both. |
| 16 | // Some API key types only work with one style; trying both prevents false |
| 17 | // negatives that would cause the wrong region to be selected, leading to |
| 18 | // every subsequent request timing out or returning 401. |
| 19 | const authHeaders: Record<string, string>[] = [ |
| 20 | { Authorization: `Bearer ${apiKey}` }, |
| 21 | { "x-api-key": apiKey }, |
| 22 | ]; |
| 23 | |
| 24 | for (const authHeader of authHeaders) { |
| 25 | try { |
| 26 | const res = await fetch(quotaUrl(region), { |
| 27 | headers: { ...authHeader, "Content-Type": "application/json" }, |
| 28 | signal: AbortSignal.timeout(timeoutMs), |
| 29 | }); |
| 30 | if (!res.ok) continue; |
| 31 | const data = (await res.json()) as { |
| 32 | base_resp?: { status_code?: number }; |
| 33 | }; |
| 34 | if (data.base_resp?.status_code === 0) return true; |
| 35 | } catch { |
| 36 | // Try next auth style before giving up on this region |
| 37 | } |
| 38 | } |
| 39 | return false; |
| 40 | } |
| 41 | |
| 42 | export async function detectRegion(apiKey: string): Promise<Region> { |
| 43 | process.stderr.write("Detecting region..."); |
no test coverage detected