From bbca1d26f3c7c63dd01f74bfdb9f47b898cf72a4 Mon Sep 17 00:00:00 2001 From: wolfy1339 Date: Sun, 25 Feb 2024 13:40:18 -0500 Subject: [PATCH] docs(README): update for ESM --- README.md | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 985841bf0..622429c03 100644 --- a/README.md +++ b/README.md @@ -48,8 +48,7 @@ Node Install with npm install @octokit/core ```js -const { Octokit } = require("@octokit/core"); -// or: import { Octokit } from "@octokit/core"; +import { Octokit } from "@octokit/core"; ``` @@ -342,8 +341,9 @@ This is useful if you build reusable [plugins](#plugins). If you would like to make the log level configurable using an environment variable or external option, we recommend the [console-log-level](https://github.com/watson/console-log-level) package. Example ```js +import consoleLogLevel from "console-log-level"; const octokit = new Octokit({ - log: require("console-log-level")({ level: "info" }), + log: consoleLogLevel({ level: "info" }), }); ``` @@ -386,10 +386,12 @@ In order to extend `octokit`'s API, the plugin must return an object with the ne ```js // index.js -const { Octokit } = require("@octokit/core") +import { Octokit } from "@octokit/core"; +import myPlugin from "./lib/my-plugin.js"; +import octokitPluginExample from "octokit-plugin-example"; const MyOctokit = Octokit.plugin( - require("./lib/my-plugin"), - require("octokit-plugin-example") + myPlugin, + octokitPluginExample ); const octokit = new MyOctokit({ greeting: "Moin moin" }); @@ -397,7 +399,7 @@ octokit.helloWorld(); // logs "Moin moin, world!" octokit.request("GET /"); // logs "GET / - 200 in 123ms" // lib/my-plugin.js -module.exports = (octokit, options = { greeting: "Hello" }) => { +const plugin = (octokit, options = { greeting: "Hello" }) => { // hook into the request lifecycle octokit.hook.wrap("request", async (request, options) => { const time = Date.now(); @@ -414,6 +416,7 @@ module.exports = (octokit, options = { greeting: "Hello" }) => { helloWorld: () => console.log(`${options.greeting}, world!`); } }; +export default plugin; ``` ## Build your own Octokit with Plugins and Defaults @@ -421,11 +424,15 @@ module.exports = (octokit, options = { greeting: "Hello" }) => { You can build your own Octokit class with preset default options and plugins. In fact, this is mostly how the `@octokit/` modules work, such as [`@octokit/action`](https://github.com/octokit/action.js): ```js -const { Octokit } = require("@octokit/core"); +import { Octokit } from "@octokit/core"; +import { paginateRest } from "@octokit/plugin-paginate-rest"; +import { throttling } from "@octokit/plugin-throttling"; +import { retry } from "@octokit/plugin-retry"; +import { createActionAuth } from "@octokit/auth-action"; const MyActionOctokit = Octokit.plugin( - require("@octokit/plugin-paginate-rest").paginateRest, - require("@octokit/plugin-throttling").throttling, - require("@octokit/plugin-retry").retry, + paginateRest, + throttling, + retry, ).defaults({ throttle: { onAbuseLimit: (retryAfter, options) => { @@ -435,7 +442,7 @@ const MyActionOctokit = Octokit.plugin( /* ... */ }, }, - authStrategy: require("@octokit/auth-action").createActionAuth, + authStrategy: createActionAuth, userAgent: `my-octokit-action/v1.2.3`, });