diff --git a/packages/cli/src/heygen/avatars.ts b/packages/cli/src/heygen/avatars.ts index 8005e6f1378..d43bf3d8147 100644 --- a/packages/cli/src/heygen/avatars.ts +++ b/packages/cli/src/heygen/avatars.ts @@ -1,4 +1,5 @@ import { resolveCloudAuthHeaders, resolveCloudBaseUrl } from "../cloud/auth.js"; +import { HyperframesApiError, HyperframesCloudClient } from "../cloud/_gen/client.js"; import { Fix } from "../measure/media.js"; import { fetchExplained, NetworkFailure } from "../utils/networkFailure.js"; @@ -42,31 +43,29 @@ function pageOf(body: unknown): { items: unknown[]; next: string | undefined } | return { items: page.data, next: page.has_more === true ? text(page.next_token) : undefined }; } -async function fetchPage(auth: Record, limit: number, token?: string) { - const url = new URL("/v3/avatars/looks", resolveCloudBaseUrl()); - url.searchParams.set("ownership", "private"); - url.searchParams.set("limit", String(limit)); - if (token) url.searchParams.set("token", token); - const res = await fetchExplained(url.toString(), { - headers: { ...auth, accept: "application/json" }, - signal: AbortSignal.timeout(30_000), - }).catch((err: unknown) => { - if (err instanceof NetworkFailure) throw new Fix(err.message, err.hint); - throw new Fix( - `no answer from HeyGen for its avatars (${err instanceof Error ? err.message : String(err)})`, - "run the same command again", - ); - }); - if (!res.ok) - throw new Fix( - `HeyGen ${res.status} listing its avatars`, - res.status === 401 || res.status === 403 - ? "npx hyperframes auth login" - : "run the same command again", - ); - const page = pageOf(await res.json().catch(() => null)); - if (!page) - throw new Fix("HeyGen answered its avatars with no list", "run the same command again"); +const noList = () => + new Fix("HeyGen answered its avatars with no list", "run the same command again"); + +async function fetchPage(client: HyperframesCloudClient, limit: number, token?: string) { + const body = await client + .listAvatarLooks({ ownership: "private", limit, token, signal: AbortSignal.timeout(30_000) }) + .catch((err: unknown) => { + if (err instanceof NetworkFailure) throw new Fix(err.message, err.hint); + if (!(err instanceof HyperframesApiError)) + throw new Fix( + `no answer from HeyGen for its avatars (${err instanceof Error ? err.message : String(err)})`, + "run the same command again", + ); + if (err.status >= 200 && err.status < 300) throw noList(); + throw new Fix( + `HeyGen ${err.status} listing its avatars`, + err.status === 401 || err.status === 403 + ? "npx hyperframes auth login" + : "run the same command again", + ); + }); + const page = pageOf(body); + if (!page) throw noList(); return page; } @@ -76,10 +75,15 @@ export async function ownAvatarLooks(limit: number): Promise { if (err instanceof NetworkFailure) throw new Fix(err.message, err.hint); throw new Fix("no HeyGen sign-in on this machine", "npx hyperframes auth login"); }); + const client = new HyperframesCloudClient({ + baseUrl: resolveCloudBaseUrl(), + getAuthHeaders: () => ({ ...auth, accept: "application/json" }), + fetchImpl: (url, init) => fetchExplained(String(url), init), + }); const looks: AvatarLook[] = []; let token: string | undefined; do { - const page = await fetchPage(auth, Math.min(PAGE_LIMIT, limit - looks.length), token); + const page = await fetchPage(client, Math.min(PAGE_LIMIT, limit - looks.length), token); for (const item of page.items) { const look = toLook(item); if (look && looks.length < limit) looks.push(look);