Skip to content
This repository has been archived by the owner on Oct 16, 2021. It is now read-only.

gr2m/octokit-rest-plugin-example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

@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')()
octokit.plugin(require('octokit-rest-plugin-example'))

octokit.root().then({data} => {
  // data from response
})

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.

License

MIT

About

@octokit/rest plugin example

Resources

License

Code of conduct

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published