npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@piranna/github-basic

v7.0.0

Published

Basic https interface to GitHub

Downloads

9

Readme

github-basic

Forked from https://github.com/scriptit/github-basic

Basic https interface to GitHub. Intended so it's easy to code using the official documentation. It also includes helpers for streaming paged results, and doing all the necessary actions to automatically submit pull requests.

Build Status Dependency Status NPM version

Installation

npm install github-basic

API

github(options)

var github = require('github-basic');
var client = github({version: 3});

Create a new GitHub client with a set of options:

  • version: (required) you should set this to the version number of the API you want to make requests against (e.g. {version: 3})
  • auth: (default: null) '<my oauth token>' or {username: 'my user', password: 'my password', otp: 'my optional 2 factor authentication code'} to authenticate your requests
  • cache: (default: null) proivde a caching mechanism for "get" requests. Can be 'memory', 'file' or a custom cache (only "file" will work with the "sync" option)
  • sync: (default: false) set this to true and you will get a fully synchronous GitHub client, just use the results of functions directly, no need for promises or callbacks.

client.method(path, query[, callback])

  • client.get(path, query[, callback])
  • client.head(path, query[, callback])
  • client.delete(path, query[, callback])
  • client.post(path, query[, callback])
  • client.patch(path, query[, callback])
  • client.put(path, query[, callback])

Make an API request and parse the response as JSON. For get, head and delete requests, the query will be added as a query string to the end of the url. For other methods, it will be used as a body. You can optionally specify that parts of the query should instead be used as part of the url by adding :qs-name segments to the url. e.g.

client.get('/users/:user/gists', {user: 'ForbesLindesay'});

If the request is part of a paged request, you can use res.getNext(), res.getPrev(), res.getLast() and res.getFirst() to request other pages. Note that not all of these methods will always exist (e.g. there is no res.getNext() if you already have the last page).

client.methodBuffer(path, query[, callback])

  • client.getBuffer(path, query[, callback])
  • client.headBuffer(path, query[, callback])
  • client.deleteBuffer(path, query[, callback])
  • client.postBuffer(path, query[, callback])
  • client.patchBuffer(path, query[, callback])
  • client.putBuffer(path, query[, callback])

Make an API request and return the response as a Buffer. For get, head and delete requests, the query will be added as a query string to the end of the url. For other methods, it will be used as a body. You can optionally specify that parts of the query should instead be used as part of the url by adding :qs-name segments to the url. e.g.

client.getBuffer('/users/:user/gists', {user: 'ForbesLindesay'});

client.exists(user, repo[, callback])

Returns true if :user/:repo exists, and false if requesting the repo url returns an error.

client.fork(user, repo, options[, callback])

Forks the repo github.com/:user/:repo to the authenticated user and waits until the fork operation completes. To fork to an organization, just pass an organization string in the options object.

N.B. forking will currently appear successful even if the target repo already exists.

client.branch(user, repo, from, to[, callback])

Creates a new branch in github.com/:user/:repo using from as the source branch and to as the new branch name.

client.commit(user, repo, commit, options[, callback])

Commits a set of changes to github.com/:user/:repo. It only supports updating text files.

commit:

An object with:

property | type | default | description ---------|---------------------|--------------|---------------------------- branch | String | 'master' | The branch to commit to message | String | required | The commit message updates | Array<FileUpdate> | required | The actual changes to make

FileUpdate:

An object with:

property | type | default | description ---------|----------|--------------|---------------------------- path | String | required | The file path within the repo (e.g. test/index.js) content | String | required | The new content of the file mode | String | '100644' | The mode to commit the file with (you probably don't want to change this) type | String | 'blob' | The type of entry to create (you probably don't want to change this)

options:

An (optonal) object with:

property | type | default | description ---------|-----------|--------------|---------------------------- force | Boolean | false | Will force push the change if set to true. You almost certainly don't want to do this.

client.pull(from, to, message [, callback])

Creates a pull request from from to to.

from:

An object with:

property | type | default | description ---------|----------|--------------|---------------------------- user | String | required | The source user repo | String | required | The source repository branch | String | 'master' | The source branch

to:

An object with:

property | type | default | description ---------|----------|--------------|---------------------------- user | String | required | The destination user repo | String | required | The destination repository branch | String | 'master' | The destination branch

message:

Either:

property | type | default | description ---------|----------|--------------|---------------------------- title | String | required | The title of the pull request body | String | '' | The body of the pull request

or:

property | type | default | description ---------|----------|--------------|---------------------------- issue | Number | required | An issue number to convert into a pull request

client.getStream(path, query)

Only works in async mode (default)

Sometimes the easiest way to handle GitHub's paginated results is to treat them as a stream. This method isn't (currently) clever enough to do streaming JSON parsing of the response, but it will keep requesting more pages as needed and it works properly with back pressure so as to not request more pages than are needed:

var github = require('github-basic')
var Stringifier = require('newline-json').Stringifier

var client = github({version: 3})

//stream all of ForbesLindesay's repos
client.getStream('/users/:user/repos', {user: 'ForbesLindesay'})
  .pipe(new Stringifier())
  .pipe(process.stdout)

Usage

var github = require('github-basic')
var client = github({version: 3})

//get all 'ForbesLindesay's gists in the last year

var since = new Date()
since.setUTCFullYear(since.getUTCFullYear() - 1)

// using callbacks

client.get('/users/:user/gists', {user: 'ForbesLindesay', since: since}, function (err, res) {
  if (err) throw err;
  console.dir(res)
})

// or

client.get('/users/ForbesLindesay/gists', {since: since}, function (err, res) {
  if (err) throw err;
  console.dir(res)
})

// using promises

client.get('/users/:user/gists', {user: 'ForbesLindesay', since: since}).done(function (res) {
  console.dir(res)
})

//or

client.get( '/users/ForbesLindesay/gists', {since: since}).done(function (res) {
  console.dir(res)
})

// getting raw github data

client.getBuffer('https://raw.githubusercontent.com/:owner/:repo/master/README.md', {
  owner: 'ForbesLindesay',
  repo: 'github-basic'
}).done(function (res) {
  process.stdout.write(res)
})

License

MIT