octokit-rest-plugin-example
v1.0.0
Published
[`@octokit/rest`](https://github.com/octokit/rest.js) has an [experimental plugin API](https://github.com/octokit/rest.js/tree/master/lib/plugins). A plugin can be shared as npm module.
Downloads
2
Readme
@octokit/rest plugin example
@octokit/rest
has an experimental plugin API. A plugin can
be shared as npm module.
The example plugin from this repository adds a new octokit.root()
method which sends a get request to the root endpoint
const octokit = require('@octokit/rest')()
// load plugin
octokit.plugin(require('octokit-rest-plugin-example'))
How to add new methods to octokit using plugins
In order to add a new method to octokit
, a plugin can look like this
module.exports = function helloWorldPlugin(octokit) {
// add .helloWorld method to `octokit` instance
octokit.helloWorld = function () {
console.log('Hello, world!')
}
}
To add a method for a new GitHub API endpoint, a plugin can look like this
module.exports = function newEndpointsPlugin(octokit) {
// add .helloWorld method to `octokit` instance
octokit.repos.myNewEndpoint = function (options) {
return octokit.request({
method: 'POST',
url: '/repos/:owner/:repo/something-new',
headers: {
accept: 'application/vnd.github.black-panther-preview+json'
},
...options
})
}
}
The method can then be used like this
const octokit = require('@octokit/rest')()
octokit.plugin(require('octokit-rest-new-endpoints-plugin'))
octokit.repos.myNewEndpoint({
owner: 'octokit',
repo: 'welcome',
foo: 'bar'
})
If there are many new endpoints it could be worth to create a routes.json file like @octokit/rest/lib/routes.json
and then build the endpoints based on that file like the core endpoint-methods plugin.