Skip to content

scriptex/universal-github-client

Repository files navigation

Travis CI Github Build Codacy Badge Codebeat Badge CodeFactor Badge DeepScan grade Analytics

Universal Github Client

A Github API Client for the browser and Node JS

Visitor stats

GitHub stars GitHub forks GitHub watchers GitHub followers

Code stats

GitHub code size in bytes GitHub repo size GitHub language count GitHub top language GitHub last commit

Installation and getting started

First install the package

npm i universal-github-client

# or

yarn add universal-github-client

Then you need to generate a new token from your Github profile and make sure that this token has access to the relevant scopes that you plan to query the API for.

Then you need to configure and setup your Github Client instance:

In Node

Node prior to v17.5.0 and v16.5.0 does not include the fetch API so you might need to install a polyfill in order to use this module in Node.

npm i node-fetch

# or

yarn add node-fetch

Node v16.5.0 and v17.5.0 include the fetch API but behind the --experimental-fetch CLI flag.

Node v18.0.0 no longer requires the --experimental-fetch CLI flag but the fetch API is still marked as "experimental".

Node v21.0.0 comes with a stable version of the fetch API.

More about the history of the fetch API and its usage in Node can be found in the official documentation.

const fetch = require('node-fetch');
const { GithubClient } = require('universal-github-client');

const client = new GitHubClient({
	base: 'https://api.github.com',
	token: 'YOUR_GITHUB_TOKEN_HERE',
	fetch
});

In browser

Please note that this package is supported by browsers which implement natively the Fetch API and have support for Promises. If you are using an outdated browser, you need to install a polyfill for Fetch and Promises.

import { GithubClient } = from 'universal-github-client';

const client = new GitHubClient({
	base: 'https://api.github.com',
	token: 'YOUR_GITHUB_TOKEN_HERE',
	fetch
});

Usage

When you have installed and configured the client, you can make calls to the Github API:

You can use the paths defined in the Github API documentation.

For example, if you want to get the repositories for a user you need to do the following:

const repos = client.get({ path: '/users/scriptex/repo' }); // scriptex is the Github user name

There are five different instance methods based on the HTTP method required by a particular endpoint in the Github API.

client.get({ path });

client.post({ path, data });

client.delete({ path });

client.put({ path, data });

client.patch({ path, data });

LICENSE

MIT


Connect with me:

                     

Support and sponsor my work: