gh-api

1.0.0 • Public • Published

gh-api

stable

A thin utility for GitHub API requests in Node and the browser. After browserify and uglify, it bundles to 9kb.

For a streaming alternative that supports pagination, see gh-api-stream.

Install

npm install gh-api --save

Example

For example, getting a package.json from a GitHub repository:

var github = require('gh-api')
 
var api = '/repos/mattdesl/gh-api/contents/package.json'
github(api, function (err, data, res) {
  if (err) throw err
  
  console.log(res.statusCode)
  //> 200
  
  var buf = new Buffer(data.content, data.encoding)
  var pkg = JSON.parse(buf.toString())
  
  console.log(pkg.description)
  //> 'simple GitHub API requests in Node / browser'
})

For simple pagination, you can use parse-link-header on res.headers.link.

Usage

NPM

req = ghApi(path, [opt], [cb])

Sends a request to the GitHub API at the specified path, such as '/repos/:owner/:repo/readme'.

Options can be:

  • token an optional GitHub API token for authentication
  • query (String|Object) optional query parameters for the URL
  • method (String) the method to use, default 'GET'
  • timeout (Number) milliseconds before timeout, default 0 (no timeout)
  • headers optional headers object to override the defaults

Other options, such as json, are passed to xhr-request.

The callback is called with the arguments (error, data, response)

  • error on success will be null/undefined
  • data the result of the request, either a JSON object, string, or ArrayBuffer
  • response the request response, see below

The response is normalized by xhr-request in Node and the browser.

req = ghApi.url(url, [opt], [cb])

Same as above, but expects a full url to the GitHub endpoint.

req.abort()

Cancels a pending request and sends an error to the callback.

See Also

License

MIT, see LICENSE.md for details.

Package Sidebar

Install

npm i gh-api

Weekly Downloads

1

Version

1.0.0

License

MIT

Last publish

Collaborators

  • mattdesl