GitHub API Utilities

5.0.2 · active · verified Sun Apr 19

ghutils is a concise collection of utility functions designed to simplify interactions with the GitHub API. Currently stable at version 5.0.2, it has recently undergone a major overhaul, modernizing its codebase. The package now exclusively uses ES Modules (ESM), returns Promises for all asynchronous operations, and leverages the native `fetch` API for HTTP requests. It targets Node.js environments version 20 or higher. ghutils differentiates itself by providing a streamlined, promise-based interface over the GitHub API's various endpoints (GET, POST, PATCH, DELETE) and offers a powerful `lister` function for handling paginated results. It serves as a foundational library for other specialized GitHub interaction packages like `ghissues` and `ghpulls`.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates authenticating with a GitHub token, making a single GET request for user data, and listing paginated issues from a repository using `ghget` and `lister`.

import { ghget, lister } from 'ghutils';

const auth = { token: process.env.GITHUB_TOKEN ?? '' };

if (!auth.token) {
  console.error('Please set the GITHUB_TOKEN environment variable.');
  process.exit(1);
}

async function main() {
  try {
    // Make a single GET request to fetch the authenticated user's profile
    const { data: user } = await ghget(auth, 'https://api.github.com/user');
    console.log('Authenticated User:', user.login);

    // List all open issues from a specific repository
    const issues = await lister(auth, 'https://api.github.com/repos/rvagg/ghutils/issues', {
      state: 'open',
      per_page: 100 // Example: fetch 100 items per page
    });
    console.log(`Found ${issues.length} open issues in rvagg/ghutils.`);
    if (issues.length > 0) {
      console.log('First issue title:', issues[0].title);
    }
  } catch (error) {
    console.error('Error interacting with GitHub API:', error.message);
  }
}

main();

view raw JSON →