Class GitHubClient

Main entry point for the GitHub REST API client.

const gh = new GitHubClient({ token: 'ghp_myPersonalAccessToken' });

const me = await gh.currentUser();
const user = await gh.user('octocat');
const repos = await gh.user('octocat').repos({ sort: 'updated' });
const org = await gh.org('github');
const repo = await gh.repo('octocat', 'Hello-World');
const prs = await gh.repo('octocat', 'Hello-World').pullRequests({ state: 'open' });
const commits = await gh.repo('octocat', 'Hello-World').commits({ per_page: 10 });
const results = await gh.searchRepos({ q: 'language:typescript stars:>1000' });

Constructors

Methods

  • Fetches a single global security advisory by its GHSA ID.

    GET /advisories/{ghsa_id}

    Parameters

    • ghsaId: string

      The GHSA identifier (e.g., 'GHSA-xxxx-xxxx-xxxx')

    • Optionalsignal: AbortSignal

    Returns Promise<GitHubAdvisory>

    The advisory object

    const advisory = await gh.advisory('GHSA-1234-5678-9abc');
    
  • Fetches a global security advisory by its CVE ID.

    GET /advisories?cve_id={cveId}

    Parameters

    • cveId: string

      The CVE identifier (e.g., 'CVE-2021-44228')

    • Optionalsignal: AbortSignal

    Returns Promise<null | GitHubAdvisory>

    The advisory object, or null if no advisory is found for the given CVE ID

    const advisory = await gh.advisoryByCve('CVE-2021-44228');
    if (advisory) console.log(advisory.summary);
  • Fetches the authenticated user's profile.

    GET /user

    Parameters

    • Optionalsignal: AbortSignal

    Returns Promise<GitHubUser>

    The authenticated user object

    const me = await gh.currentUser();
    console.log(me.login); // 'octocat'
  • Returns a GistResource for a given gist ID.

    The returned resource can be awaited directly to fetch the gist, or chained to access nested resources (comments, forks, star).

    Parameters

    • gistId: string

      The gist ID

    Returns GistResource

    A chainable gist resource

    const gist     = await gh.gist('abc123');
    const comments = await gh.gist('abc123').comments();
    await gh.gist('abc123').star();
  • Executes an arbitrary GitHub GraphQL query.

    POST https://api.github.com/graphql

    Type Parameters

    • T

    Parameters

    • query: string

      The GraphQL query string

    • Optionalvariables: Record<string, unknown>

      Optional query variables

    • Optionalsignal: AbortSignal

      Optional AbortSignal

    Returns Promise<T>

    The data field of the GraphQL response

    If the response contains GraphQL errors

    If the HTTP request fails

    const result = await gh.graphql<{ viewer: { login: string } }>(`
    query { viewer { login } }
    `);
    console.log(result.viewer.login);
  • Lists issues assigned to the authenticated user across all repositories.

    GET /issues

    Note: GitHub returns pull requests as issues in this endpoint. Filter them out by checking for the absence of pull_request on each item.

    Parameters

    • Optionalparams: IssuesParams

      Optional filters: filter, state, labels, sort, direction, since, per_page, page

    • Optionalsignal: AbortSignal

    Returns Promise<GitHubPagedResponse<GitHubIssue>>

    A paged response of issues

    // All open issues across all repos the user has access to
    const { values } = await gh.issues({ filter: 'all', state: 'open' });
    const realIssues = values.filter(i => !i.pull_request);
  • Marks all notifications as read.

    PUT /notifications

    Returns void — GitHub responds with 205 No Content on success.

    Parameters

    • Optionalsignal: AbortSignal

    Returns Promise<void>

    await gh.markAllNotificationsRead();
    
  • Marks a single notification thread as read.

    PATCH /notifications/threads/{thread_id}

    Returns void — GitHub responds with 205 No Content on success.

    Parameters

    • threadId: string

      The numeric thread ID (from GitHubNotification.id)

    • Optionalsignal: AbortSignal

    Returns Promise<void>

    await gh.markNotificationRead('123456789');
    
  • Subscribes to a client event.

    Type Parameters

    • K extends "request"

    Parameters

    Returns this

    gh.on('request', (event) => {
    console.log(`${event.method} ${event.url}${event.durationMs}ms`);
    if (event.error) console.error('Request failed:', event.error);
    });
  • Returns an OrganizationResource for a given GitHub organization, providing access to organization data and its repositories.

    The returned resource can be awaited directly to fetch organization info, or chained to access nested resources.

    Parameters

    • name: string

      The organization's login name (e.g., 'github')

    Returns OrganizationResource

    A chainable organization resource

    const org   = await gh.org('github');
    const repos = await gh.org('github').repos({ type: 'public' });
    const prs = await gh.org('github').repo('linguist').pullRequests({ state: 'open' });
  • Returns a RepositoryResource for a given owner and repository name.

    Shortcut that works for both user repositories and organization repositories.

    Parameters

    • owner: string

      The owner login (user or organization)

    • name: string

      The repository name

    Returns RepositoryResource

    A chainable repository resource

    const repo  = await gh.repo('octocat', 'Hello-World');
    const prs = await gh.repo('octocat', 'Hello-World').pullRequests();
  • Searches for issues and pull requests using GitHub's search syntax.

    GET /search/issues

    Parameters

    • params: SearchIssuesParams

      Search query and optional sort/order. q is required.

    • Optionalsignal: AbortSignal

    Returns Promise<GitHubPagedResponse<GitHubIssue>>

    A paged response of issues/PRs with totalCount

    // Open PRs authored by a user
    const results = await gh.searchIssues({ q: 'is:pr is:open author:octocat' });
    console.log(`Found ${results.totalCount} pull requests`);

    // Stale issues not updated in 30+ days
    const stale = await gh.searchIssues({ q: 'is:issue is:open updated:<2024-01-01', sort: 'updated' });
  • Searches for users using GitHub's search syntax.

    GET /search/users

    Parameters

    • params: SearchUsersParams

      Search query and optional sort/order. q is required.

    • Optionalsignal: AbortSignal

    Returns Promise<GitHubPagedResponse<GitHubUser>>

    A paged response of users with totalCount

    const results = await gh.searchUsers({ q: 'location:Berlin language:typescript', sort: 'followers' });
    console.log(`Found ${results.totalCount} users`);
  • Returns a UserResource for a given GitHub login, providing access to user data and their repositories.

    The returned resource can be awaited directly to fetch user info, or chained to access nested resources.

    Parameters

    • login: string

      The user's login name (e.g., 'octocat')

    Returns UserResource

    A chainable user resource

    const user  = await gh.user('octocat');
    const repos = await gh.user('octocat').repos({ sort: 'updated' });
    const pr = await gh.user('octocat').repo('Hello-World').pullRequest(1).files();