Sends a request like `api` and also returns the relative path of the next page from the Link header.
(method: string, path: string, body?: unknown)
| 54 | |
| 55 | /** Sends a request like `api` and also returns the relative path of the next page from the Link header. */ |
| 56 | protected async page<T>(method: string, path: string, body?: unknown): Promise<{ body: T; next?: string }> { |
| 57 | if (!path.startsWith("/") || path.startsWith("//")) throw new Error("Expected a relative GitHub API path"); |
| 58 | const idempotent = ["GET", "PUT", "PATCH", "DELETE"].includes(method); |
| 59 | for (let attempt = 0; ; attempt++) { |
| 60 | try { |
| 61 | return await this.once<T>(method, path, body); |
| 62 | } catch (error) { |
| 63 | const code = transportCode(error); |
| 64 | const reset = method === "GET" && code === "ECONNRESET"; |
| 65 | // The REST API intermittently answers with 5xx or 429. Repeating a GET, PUT, PATCH or DELETE leaves the |
| 66 | // same end state, so retrying keeps a transient outage from erroring a whole trial. POSTs (reviews, repos, |
| 67 | // pull requests) are not retried: a 5xx may follow a completed write and a retry could duplicate it. |
| 68 | const retryableStatus = error instanceof ApiError && ( |
| 69 | (idempotent && [429, 500, 502, 503, 504].includes(error.status)) |
| 70 | || this.transient(method, path, error.status)); |
| 71 | const attempts = reset ? Math.min(1, this.retryDelaysMs.length) : this.retryDelaysMs.length; |
| 72 | if ((!reset && !retryableStatus) || attempt >= attempts) { |
| 73 | if (error instanceof ApiError) throw error; |
| 74 | const detail = error instanceof Error ? error.message : String(error); |
| 75 | const suffix = code && !detail.includes(code) ? ` (${code})` : ""; |
| 76 | throw new Error(`GitHub ${method} ${path}: ${detail}${suffix}`, { cause: error }); |
| 77 | } |
| 78 | if (reset) console.error(`GitHub ${method} ${path}: connection reset; retrying GET once.`); |
| 79 | const delay = Math.min((error instanceof ApiError ? error.retryAfterMs : undefined) ?? this.retryDelaysMs[attempt]!, 30_000); |
| 80 | await new Promise(resolve => setTimeout(resolve, delay)); |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | private async once<T>(method: string, path: string, body?: unknown): Promise<{ body: T; next?: string }> { |
| 86 | const response = await this.request(`https://api.github.com${path}`, { |
nothing calls this directly
no test coverage detected