universal-github-client
Version:
A Github API Client for the browser and Node
95 lines (59 loc) • 2.79 kB
Markdown
> A Github API Client for the browser and Node JS
First install the package
```sh
npm i universal-github-client
yarn add universal-github-client
```
Then you need to generate a new token from your [Github profile](https://github.com/settings/tokens) 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:
You need to install a fetch polyfill because Node does not include one.
```sh
npm i node-fetch
yarn add node-fetch
```
```javascript
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
});
```
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.
```javascript
import { GithubClient } = from 'universal-github-client';
const client = new GitHubClient({
base: 'https://api.github.com',
token: 'YOUR_GITHUB_TOKEN_HERE',
fetch
});
```
When you have installed and configured the `client`, you can make calls to the [Github API](https://developer.github.com/v3/):
You can use the paths defined in the [Github API](https://developer.github.com/v3/) documentation.
For example, if you want to [get the repositories](https://developer.github.com/v3/repos/#list-repositories-for-a-user) for a user you need to do the following:
```javascript
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.
```javascript
client.get({ path });
client.post({ path, data });
client.delete({ path });
client.put({ path, data });
client.patch({ path, data });
```
[](https://twitter.com/intent/tweet?text=Checkout%20this%20awesome%20software%20project%3A&url=https%3A%2F%2Fgithub.com%2Fscriptex%2Funiversal-github-client&via=scriptexbg&hashtags=software%2Cgithub%2Ccode%2Cawesome)
[](https://www.paypal.me/scriptex)
[](https://www.patreon.com/atanas)
MIT