Skip to content

Add rate limiting headers to error response #309

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 11 additions & 1 deletion src/clients/baseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import type { RequestConfig } from '../requestConfig';
const STRICT_GDPR_FLAG = 'x-atlassian-force-account-id';
const ATLASSIAN_TOKEN_CHECK_FLAG = 'X-Atlassian-Token';
const ATLASSIAN_TOKEN_CHECK_NOCHECK_VALUE = 'no-check';
const RETRY_AFTER = 'Retry-After';
const RATE_LIMIT_RESET = 'X-RateLimit-Reset';

export class BaseClient implements Client {
private instance: AxiosInstance;
Expand Down Expand Up @@ -133,11 +135,19 @@ export class BaseClient implements Client {
}

private buildErrorHandlingResponse(error: AxiosError<any>) {
const headers = error.response?.headers ?? {};
const responseData = error.response?.data ?? {};
const data = typeof responseData === 'object' ? responseData : { data: responseData };

return {
code: error.code,
headers: this.removeUndefinedProperties({
[RETRY_AFTER]: headers[RETRY_AFTER],
[RATE_LIMIT_RESET]: headers[RATE_LIMIT_RESET],
}),
status: error.response?.status,
statusText: error.response?.statusText,
...(error.response?.data ?? {}),
...data,
};
}
}