* Log GitHub API rate-limit information extracted from a REST response's * headers. Call this immediately after any `github.rest.*.*()` call to * record the current rate-limit state without an extra API round-trip. * * @param {{ headers?: Record }} response - The git
(response, operation)
| 88 | * @param {string} operation - Human-readable description of the operation (e.g. "issues.listComments") |
| 89 | */ |
| 90 | function logRateLimitFromResponse(response, operation) { |
| 91 | const headers = response?.headers; |
| 92 | if (!headers) return; |
| 93 | |
| 94 | const limit = headers["x-ratelimit-limit"]; |
| 95 | const remaining = headers["x-ratelimit-remaining"]; |
| 96 | const reset = headers["x-ratelimit-reset"]; |
| 97 | const used = headers["x-ratelimit-used"]; |
| 98 | const resource = headers["x-ratelimit-resource"]; |
| 99 | |
| 100 | // Skip if no rate-limit headers are present (e.g. GraphQL responses) |
| 101 | if (!limit && !remaining && !reset) return; |
| 102 | |
| 103 | /** @type {Record<string, unknown>} */ |
| 104 | const entry = { |
| 105 | timestamp: new Date().toISOString(), |
| 106 | source: "response_headers", |
| 107 | operation, |
| 108 | }; |
| 109 | |
| 110 | if (resource) entry.resource = resource; |
| 111 | if (limit !== undefined) entry.limit = parseInt(limit, 10); |
| 112 | if (remaining !== undefined) entry.remaining = parseInt(remaining, 10); |
| 113 | if (used !== undefined) entry.used = parseInt(used, 10); |
| 114 | if (reset) entry.reset = parseResetTimestamp(reset); |
| 115 | |
| 116 | appendEntry(entry); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Fetch the current GitHub API rate-limit information via the rate-limit API |
no test coverage detected