(
bookId: string,
options: WaitForStudioBookReadyOptions = {},
)
| 21 | } |
| 22 | |
| 23 | export async function waitForStudioBookReady( |
| 24 | bookId: string, |
| 25 | options: WaitForStudioBookReadyOptions = {}, |
| 26 | ): Promise<StudioBookDetail> { |
| 27 | const fetchImpl = options.fetchImpl ?? fetch; |
| 28 | const wait = options.wait ?? defaultWait; |
| 29 | const maxAttempts = options.maxAttempts ?? 900; |
| 30 | const retryDelayMs = options.retryDelayMs ?? 1000; |
| 31 | |
| 32 | for (let attempt = 1; attempt <= maxAttempts; attempt += 1) { |
| 33 | const encodedBookId = encodeURIComponent(bookId); |
| 34 | const statusResponse = await fetchImpl(`/api/v1/books/${encodedBookId}/create-status`); |
| 35 | if (statusResponse.ok) { |
| 36 | const status = await statusResponse.json() as StudioBookCreateStatus; |
| 37 | if (status.status === "error") { |
| 38 | throw new Error(status.error || `Book "${bookId}" creation failed.`); |
| 39 | } |
| 40 | if (status.status === "creating") { |
| 41 | if (attempt < maxAttempts) { |
| 42 | await wait(retryDelayMs); |
| 43 | continue; |
| 44 | } |
| 45 | break; |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | const response = await fetchImpl(`/api/v1/books/${encodedBookId}`); |
| 50 | if (response.ok) { |
| 51 | return await response.json() as StudioBookDetail; |
| 52 | } |
| 53 | |
| 54 | if (attempt < maxAttempts && response.status === 404) { |
| 55 | await wait(retryDelayMs); |
| 56 | continue; |
| 57 | } |
| 58 | |
| 59 | break; |
| 60 | } |
| 61 | |
| 62 | throw new Error(`Book "${bookId}" was not ready after ${maxAttempts} attempts.`); |
| 63 | } |
no outgoing calls
no test coverage detected