Skip to content

feat: expose rate limit headers via onRateLimitInfo callback#86

Merged
mikeh-lago merged 2 commits intomainfrom
feat/rate-limit-observability
May 5, 2026
Merged

feat: expose rate limit headers via onRateLimitInfo callback#86
mikeh-lago merged 2 commits intomainfrom
feat/rate-limit-observability

Conversation

@mikeh-lago
Copy link
Copy Markdown
Contributor

Adds an optional onRateLimitInfo callback on RateLimitFetchConfig so callers can read x-ratelimit-* headers from successful responses. Today the SDK drops them on 2xx, which makes 80/90/95% threshold observability impossible without inspecting internals. The 429 path (via LagoRateLimitError) is unchanged. Mirrors PRs getlago/lago-python-client#393, getlago/lago-go-client#336, and getlago/lago-ruby-client#347.

Notes for review

  • New option is optional and defaults to undefined. Backward-compatible.
  • Callback fires only on successful responses with rate limit headers present.
  • Callback errors are caught and surfaced via console.warn so a buggy observer can't break a request.
  • Sync, runs inline before resolving the response promise.

Tests

12 new tests in tests/rate_limit.test.ts. deno task test passes (98 tests). deno task build produces a clean npm package.

Surface x-ratelimit-* headers from successful responses so callers can
build observability around the rate limit (warn at thresholds, emit
metrics) without inspecting internals.

- Add RateLimitInfo type with parseRateLimitInfo + rateLimitUsagePct helpers
- Add onRateLimitInfo option on RateLimitFetchConfig
- Parse and emit headers on every non-429 response in createRateLimitFetch
- Ship loggingRateLimitObserver as a zero-config observer (defaults: 80/90/95%)
- Callback errors are caught and logged so they cannot break requests
- Backward-compatible: callback is optional, default behavior unchanged
Comment thread rate_limit_fetch.ts Outdated
Comment thread rate_limit_fetch.ts Outdated
Address review feedback from @vincent-pochet on PR #86:

- Honor Request.method when input is a Request and init.method is
  undefined (a valid fetch signature). Previously this fell through to
  'GET' regardless of the actual method on the Request.
- Only emit on_rate_limit_info on 2xx responses. Non-429 errors no
  longer trigger the callback. This matches the comment on the call
  site, the docstring on the option, and the behavior of the Python,
  Go, Ruby, and Rust SDKs.
- Add tests for the Request input case and for the 5xx no-emit case.
@mikeh-lago mikeh-lago merged commit d46ebcf into main May 5, 2026
6 checks passed
@mikeh-lago mikeh-lago deleted the feat/rate-limit-observability branch May 5, 2026 16:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants