Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/shinnn/gh-get

Create a request to the Github API
https://github.com/shinnn/gh-get

Last synced: 27 days ago
JSON representation

Create a request to the Github API

Awesome Lists containing this project

README

        

# gh-get

[![NPM version](https://img.shields.io/npm/v/gh-get.svg)](https://www.npmjs.com/package/gh-get)
[![Build Status](https://travis-ci.org/shinnn/gh-get.svg?branch=master)](https://travis-ci.org/shinnn/gh-get)
[![Coverage Status](https://img.shields.io/coveralls/shinnn/gh-get.svg)](https://coveralls.io/github/shinnn/gh-get?branch=master)
[![Dependency Status](https://david-dm.org/shinnn/gh-get.svg)](https://david-dm.org/shinnn/gh-get)
[![devDependency Status](https://david-dm.org/shinnn/gh-get/dev-status.svg)](https://david-dm.org/shinnn/gh-get#info=devDependencies)

A [Node.js](https://nodejs.org/) module to create a request to the [Github API](https://developer.github.com/v3/)

```javascript
const ghGet = require('gh-get');

ghGet('users/isaacs', {userAgent: 'your application name'}).then(response => {
response.body.login; //=> 'isaacs'
});
```

## Installation

[Use npm.](https://docs.npmjs.com/cli/install)

```
npm install gh-get
```

## API

```javascript
const ghGet = require('gh-get');
```

### ghGet(*url* [, *options*])

*url*: `String` ("path" part of a Github API URL)
*options*: `Object`
Return: [`Promise`](https://promisesaplus.com/)

It makes a `GET` request to the [Github API](https://developer.github.com/v3/#overview) and returns a promise. Request method is overridable with the `method` [option](https://github.com/shinnn/gh-get#options).

When the API request finishes successfully, the promise will be [*fulfilled*](https://promisesaplus.com/#point-26) with the [`http.IncomingMessage`](https://nodejs.org/api/http.html#http_http_incomingmessage) object with the additional `body` property that contains a JSON object of the API response.

#### Options

You can use [`Request` options](https://github.com/request/request#requestoptions-callback) and the following.

##### options.userAgent

Type: `String` (GitHub username or the name of your application)

[Required](https://developer.github.com/v3/#user-agent-required). Add `user-agent` to the request header.

##### options.token

Type: `String`
Default: `process.env.GITHUB_TOKEN`

Use specific [GitHub access token](https://github.com/blog/1509-personal-api-tokens).

```javascript
ghGet('user', {
token: 'xxxxx' //=> for example @shinnn's access token
userAgent: 'Shinnosuke Watanabe https://github.com/shinnn/'
}).then(response => {
response.body.login; //=> 'shinnn'
});
```

##### options.verbose

Type: `Boolean`
Default: `false`

`true` adds an [`http.IncomingMessage`](https://nodejs.org/api/http.html#http_http_incomingmessage) object to the error message as `response` property.

```javascript
ghGet('user/repos', {token: 'invalid_token'}).then(err => {
err.message; //=> '401 Unauthorized (Bad credentials)'
'response' in error; //=> false
});

ghGet('user/repos', {
token: 'invalid_token',
verbose: true
}).then(err => {
err.message; //=> '401 Unauthorized (Bad credentials)'
err.response; //=> {statusCode: 401, body: { ... }, headers: { ... }, ...}
});
```

##### options.baseUrl

Type: `String`
Default: `process.env.GITHUB_ENDPOINT` if available, otherwise `'https://api.github.com'`

Use the different root [endpoint](https://developer.github.com/v3/#root-endpoint) to support [Github enterprise](https://enterprise.github.com/).

## License

Copyright (c) 2015 - 2017 [Shinnosuke Watanabe](https://github.com/shinnn)

Licensed under [the MIT License](./LICENSE).