Skip to content

dependabit / github-client/src / GitHubClient

Class: GitHubClient

Defined in: packages/github-client/src/client.ts:83

Thin wrapper around Octokit that adds proactive rate-limit checking.

Remarks

This class is intentionally minimal — it exposes the underlying Octokit instance via GitHubClient.getOctokit so callers can use the full Octokit API without re-implementing every method.

Rate-limit state is cached per-instance; create one GitHubClient per GitHub token and reuse it to benefit from the cache.

Use When

Making authenticated GitHub API calls from the monitor or action packages where you need integrated rate-limit protection.

Avoid When

You need a full Octokit feature set with plugins (e.g., pagination, throttling) — instantiate Octokit directly and pass it to GitHubClient via the constructor is not possible; use the separate RateLimitHandler for advanced budget management.

Pitfalls

  • Primary vs. secondary rate limits: checkRateLimit only checks the primary REST API rate limit. GitHub also enforces secondary (abuse) rate limits on burst patterns (many requests in a short window). A 403 with Retry-After header indicates a secondary limit — this class does not handle that automatically.
  • Rate limit cache: getLastRateLimitCheck returns a potentially stale value; it is updated only when getRateLimit or checkRateLimit is called. Do not use it as a real-time indicator.

Example

ts
import { GitHubClient } from '@dependabit/github-client';

const client = new GitHubClient({ auth: process.env.GITHUB_TOKEN });
const octokit = client.getOctokit();
const { data } = await octokit.rest.repos.get({ owner: 'my-org', repo: 'my-repo' });

Constructors

Constructor

ts
new GitHubClient(config?): GitHubClient;

Defined in: packages/github-client/src/client.ts:89

Parameters

ParameterType
configGitHubClientConfig

Returns

GitHubClient

Methods

checkRateLimit()

ts
checkRateLimit(): Promise<void>;

Defined in: packages/github-client/src/client.ts:118

Check rate limit and throw if exceeded; log a warning when remaining is low.

Returns

Promise<void>


getLastRateLimitCheck()

ts
getLastRateLimitCheck(): RateLimitInfo | undefined;

Defined in: packages/github-client/src/client.ts:157

Get last known rate limit info (cached)

Returns

RateLimitInfo | undefined


getOctokit()

ts
getOctokit(): Octokit & {
  paginate: PaginateInterface;
} & paginateGraphQLInterface & Api & {
  retry: {
     retryRequest: (error, retries, retryAfter) => RequestError;
  };
};

Defined in: packages/github-client/src/client.ts:150

Get the underlying Octokit instance

Returns

Octokit & { paginate: PaginateInterface; } & paginateGraphQLInterface & Api & { retry: { retryRequest: (error, retries, retryAfter) => RequestError; }; }


getRateLimit()

ts
getRateLimit(): Promise<RateLimitInfo>;

Defined in: packages/github-client/src/client.ts:100

Get current rate limit status

Returns

Promise<RateLimitInfo>


withRateLimit()

ts
withRateLimit<T>(fn): Promise<T>;

Defined in: packages/github-client/src/client.ts:142

Execute a request with rate limit checking

Type Parameters

Type Parameter
T

Parameters

ParameterType
fn() => Promise<T>

Returns

Promise<T>

Released under the MIT License.