gh-api
v1.0.0
Published
simple GitHub API requests in Node / browser
Downloads
10
Maintainers
Readme
gh-api
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
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 authenticationquery
(String|Object) optional query parameters for the URLmethod
(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/undefineddata
the result of the request, either a JSON object, string, orArrayBuffer
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
- gh-api-stream - for paginated / streaming results
- gh-got - a related library
License
MIT, see LICENSE.md for details.