NSA/Microsoft GitHub REST API client for JavaScript (evacuated from NSA/Microsoft Github)

greenkeeper[bot] 216713a057 chore(package): update dotenv to version 6.0.0 6 роки тому
.github 9491838d8c docs(PULL_REQUEST_TEMPLATE): initial version (#798) 6 роки тому
assets f7c9f8699f docs(HOW_IT_WORKS): initial version 7 роки тому
examples 44865e0565 style: standard 7 роки тому
lib 7dfe4d1b24 deprecate: `:name` property for getCommunityProfileMetrics is now `:repo` 6 роки тому
scripts 244c7bf07a build: adapt typescript definitions for response.headers & response.data 6 роки тому
test 244c7bf07a build: adapt typescript definitions for response.headers & response.data 6 роки тому
.gitignore 191c7e3ffc build(gitignore): cypress 7 роки тому
.travis.yml 65bc7f9cae ci(travis): add smoke test for Node 4 while we still support it 6 роки тому
CODE_OF_CONDUCT.md b9af034d88 chore(Code of Conduct): contributor covenant (#603) 7 роки тому
CONTRIBUTING.md aa816a6ea0 docs(CONTRIBUTING): testing pull request locally (#836) 6 роки тому
HOW_IT_WORKS.md f7c9f8699f docs(HOW_IT_WORKS): initial version 7 роки тому
LICENSE 2c35158f89 docs(LICENSE): 2018 7 роки тому
README.md 8c3837485b docs(README): show response status & headers in code examples 6 роки тому
cypress.json a5db496a0f ci(cypress): rename configuration for cypress v3 6 роки тому
index.js edda619965 fix: load all plugins again 7 роки тому
package.json 216713a057 chore(package): update dotenv to version 6.0.0 6 роки тому

README.md

rest.js

GitHub REST API client for Node.js

Build Status Coverage Status Greenkeeper

Usage

Node

Install with npm install @octokit/rest.

const octokit = require('@octokit/rest')()

// Compare: https://developer.github.com/v3/repos/#list-organization-repositories
octokit.repos.getForOrg({
  org: 'octokit',
  type: 'public'
}).then(({data, headers, status}) => {
  // handle data
})

Browser

  1. Download octokit-rest.min.js from the latest release: https://github.com/octokit/rest.js/releases

  2. Load it as script into your web application:

   <script src="octokit-rest.min.js"></script>
  1. Initialize octokit
   const octokit = new Octokit()

   // Compare: https://developer.github.com/v3/repos/#list-organization-repositories
   octokit.repos.getForOrg({
     org: 'octokit',
     type: 'public'
   }).then(({data, headers, status}) => {
     // handle data
   })

Client options

All available client options with default values

const octokit = require('@octokit/rest')({
  timeout: 0, // 0 means no request timeout
  headers: {
    accept: 'application/vnd.github.v3+json',
    'user-agent': 'octokit/rest.js v1.2.3' // v1.2.3 will be current version
  },

  // custom GitHub Enterprise URL
  baseUrl: 'https://api.github.com',

  // Node only: advanced request options can be passed as http(s) agent
  agent: undefined
})

@octokit/rest API docs: https://octokit.github.io/rest.js/
GitHub v3 REST API docs: https://developer.github.com/v3/

API Previews

To take advantage of GitHub’s API Previews, pass a custom accept header, which you can do with any endpoint method documented in the API docs, e.g.

const {data: {topics}} = octokit.repos.get({
  owner: 'octokit',
  repo: 'rest.js',
  headers: {
    accept: 'application/vnd.github.mercy-preview+json'
  }
})

Multiple preview headers can be combined by separating them with commas

const {data: {topics, code_of_conduct}} = octokit.repos.get({
  owner: 'octokit',
  repo: 'rest.js',
  headers: {
    accept: 'application/vnd.github.mercy-preview+json,application/vnd.github.scarlet-witch-preview+json'
  }
})

Authentication

Most GitHub API calls don't require authentication. Rules of thumb:

  1. If you can see the information by visiting the site without being logged in, you don't have to be authenticated to retrieve the same information through the API.
  2. If you want to change data, you have to be authenticated.
// basic
octokit.authenticate({
  type: 'basic',
  username: 'yourusername',
  password: 'password'
})

// oauth
octokit.authenticate({
  type: 'oauth',
  token: 'secrettoken123'
})

// oauth key/secret (to get a token)
octokit.authenticate({
  type: 'oauth',
  key: 'client_id',
  secret: 'client_secret'
})

// token (https://github.com/settings/tokens)
octokit.authenticate({
  type: 'token',
  token: 'secrettoken123'
})

// GitHub app
octokit.authenticate({
  type: 'app',
  token: 'secrettoken123'
})

Note: authenticate is synchronous because it only sets the credentials for the following requests.

Pagination

There are a few pagination-related methods:

  • hasNextPage(response)
  • hasPreviousPage(response)
  • hasFirstPage(response)
  • hasLastPage(response)
  • getNextPage(response)
  • getPreviousPage(response)
  • getFirstPage(response)
  • getLastPage(response)

Usage

async function paginate (method) {
  let response = await method({per_page: 100})
  let {data} = response
  while (octokit.hasNextPage(response)) {
    response = await octokit.getNextPage(response)
    data = data.concat(response.data)
  }
  return data
}

paginate(octokit.repos.getAll)
  .then(data => {
    // handle all results
  })

Debug

Set DEBUG=octokit:rest* for additional debug logs.

Tests

Before running any tests you have to start the fixtures server

$ npm run start-fixtures-server

In a second terminal, run the tests

$ npm test

Or run a specific test

$ ./node_modules/.bin/mocha test/scenarios/get-repository-test.js

Run browser tests

$ npm run test:browser

Note: In order to run the same scenario tests in both Node and browser, we simulate the Cypress environment in Node, see test/mocha-node-setup.js.

The examples are run as part of the tests. You can set an EXAMPLES_GITHUB_TOKEN environment variable (or set it in a .env file) to avoid running against GitHub's rate limit.

Contributing

We would love you to contribute to @octokit/rest, pull requests are very welcomed! Please see CONTRIBUTING.md for more information.

Credits

@octokit/rest was originally created as node-github in 2012 by Mike de Boer from Cloud9 IDE, Inc. It was adopted and renamed by GitHub in 2017

LICENSE

MIT