You can customize and extend Octokit’s functionality using plugins
// index.jsconst Octokit = require("@octokit/rest");const MyOctokit = Octokit.plugin(require("./lib/my-plugin"),require("octokit-plugin-example"));// lib/my-plugin.jsmodule.exports = (octokit, options = { greeting: "Hello" }) => {// hook into the request lifecycleoctokit.hook.wrap("request", async (request, options) => {const time = Date.now();const response = await request(options);octokit.log.info(`${options.method} ${options.url} – ${response.status} in ${Date.now() - time}ms`);return response;});// add a custom method: octokit.helloWorld()return {helloWorld: () => console.log(`${options.greeting}, world!`),};};
.plugin
accepts a function or an array of functions.
We recommend using Octokit’s log methods to help users of your plugin with debugging.
You can add new methods to the octokit
instance passed as the first argument to the plugin function. The 2nd argument is the options object passed to the constructor when instantiating the octokit
client.
const octokit = new MyOctokit({ greeting: "Hola" });octokit.helloWorld();// Hola, world!