Security News
pnpm 9.5 Introduces Catalogs: Shareable Dependency Version Specifiers
pnpm 9.5 introduces a Catalogs feature, enabling shareable dependency version specifiers, reducing merge conflicts and improving support for monorepos.
@octokit/rest
Advanced tools
Package description
The @octokit/rest npm package is a client library for accessing the GitHub REST API. It provides a convenient way to interact with GitHub resources such as repositories, issues, pull requests, and more, directly from your JavaScript code. It abstracts away the direct HTTP requests to GitHub, offering an easier and more intuitive way to access GitHub data and perform operations.
Repository Management
This feature allows you to manage repositories. The code sample demonstrates how to create a new public repository for the authenticated user.
const { Octokit } = require('@octokit/rest');
const octokit = new Octokit({ auth: 'personal-access-token' });
octokit.repos.createForAuthenticatedUser({
name: 'new-repo',
private: false
});
Issues Management
This feature enables you to manage issues in a repository. The code sample shows how to create a new issue in a specified repository.
const { Octokit } = require('@octokit/rest');
const octokit = new Octokit({ auth: 'personal-access-token' });
octokit.issues.create({
owner: 'username',
repo: 'repository-name',
title: 'New Issue Title',
body: 'Description of the issue.'
});
Pull Requests
This feature allows for the creation and management of pull requests. The code sample illustrates how to create a pull request from one branch to another within the same repository.
const { Octokit } = require('@octokit/rest');
const octokit = new Octokit({ auth: 'personal-access-token' });
octokit.pulls.create({
owner: 'username',
repo: 'repository-name',
title: 'New Pull Request',
head: 'branch-name',
base: 'base-branch'
});
The 'github' npm package is another client library for the GitHub API. It offers similar functionalities to @octokit/rest but has been deprecated in favor of @octokit/rest, which is its direct successor. @octokit/rest provides a more modern and feature-rich interface.
The 'node-github' package is an older client for the GitHub API, offering basic functionalities to interact with GitHub resources. Compared to @octokit/rest, it might not be as up-to-date or feature-rich, as @octokit/rest is the official client library recommended by GitHub.
Readme
GitHub REST API client for Node.js
const octokit = require('@octokit/rest')()
// Compare: https://developer.github.com/v3/repos/#list-organization-repositories
octokit.repos.getForOrg({
org: 'octokit',
type: 'public'
}).then(({data}) => {
// handle data
})
All available client options with default values
const octokit = require('@octokit/rest')({
timeout: 0, // 0 means no request timeout
requestMedia: 'application/vnd.github.v3+json',
headers: {
'user-agent': 'octokit/rest.js v1.2.3' // v1.2.3 will be current version
},
// change for custom GitHub Enterprise URL
host: 'api.github.com',
pathPrefix: '',
protocol: 'https',
port: 433,
// advanced request options
// see https://nodejs.org/api/http.html
proxy: undefined,
ca: undefined,
rejectUnauthorized: undefined,
family: undefined
})
@octokit/rest
API docs: https://octokit.github.io/rest.js/
GitHub v3 REST API docs: https://developer.github.com/v3/
Most GitHub API calls don't require authentication. Rules of thumb:
// 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_secert'
})
// token (https://github.com/settings/tokens)
octokit.authenticate({
type: 'token',
token: 'secrettoken123'
})
// GitHub app
octokit.authenticate({
type: 'integration',
token: 'secrettoken123'
})
Note: authenticate
is synchronous because it only sets the credentials
for the following requests.
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 = 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
})
Set DEBUG=octokit:rest*
for additional debug logs.
Run all tests
$ npm test
Or run a specific test
$ ./node_modules/.bin/mocha test/test/integration/get-repository-test.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.
We would love you to contribute to @octokit/rest
, pull requests are very welcomed!
Please see CONTRIBUTING.md for more information.
@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
FAQs
Unknown package
We found that @octokit/rest demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
pnpm 9.5 introduces a Catalogs feature, enabling shareable dependency version specifiers, reducing merge conflicts and improving support for monorepos.
Security News
A threat actor on BreachForums is selling an unverified npm vulnerability for account takeover, but npm has not officially confirmed the existence of this security concern.
Security News
Cyber insurance rates are dropping as the market matures, according to a new report projecting global premiums to reach $43 billion by 2030, driven by international market uptake and growth in the SME sector.