https://github.com/gr2m/octokit-rest-plugin-example
@octokit/rest plugin example
https://github.com/gr2m/octokit-rest-plugin-example
Last synced: 12 months ago
JSON representation
@octokit/rest plugin example
- Host: GitHub
- URL: https://github.com/gr2m/octokit-rest-plugin-example
- Owner: gr2m
- License: mit
- Archived: true
- Created: 2018-02-08T21:59:22.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2021-03-05T06:48:28.000Z (over 5 years ago)
- Last Synced: 2024-04-13T22:24:01.600Z (about 2 years ago)
- Language: JavaScript
- Size: 6.84 KB
- Stars: 3
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# @octokit/rest plugin example
[`@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.
The [example plugin](index.js) from this repository adds a new `octokit.root()`
method which sends a get request to the [root endpoint](https://developer.github.com/v3/#root-endpoint)
```js
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
```js
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
```js
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
```js
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`](https://github.com/octokit/rest.js/blob/master/lib/routes.json)
and then build the endpoints based on that file like the core [endpoint-methods plugin](https://github.com/octokit/rest.js/tree/master/lib/plugins/endpoint-methods).
## License
[MIT](LICENSE)