Skip to content

Commit 77f8a61

Browse files
authored
feat: package is now ESM (#661)
BREAKING CHANGE: package is now ESM
1 parent 0cf80ff commit 77f8a61

11 files changed

+1257
-1893
lines changed

README.md

+19-12
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@ Node
4848
Install with <code>npm install @octokit/core</code>
4949

5050
```js
51-
const { Octokit } = require("@octokit/core");
52-
// or: import { Octokit } from "@octokit/core";
51+
import { Octokit } from "@octokit/core";
5352
```
5453

5554
</td></tr>
@@ -342,8 +341,9 @@ This is useful if you build reusable [plugins](#plugins).
342341
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
343342

344343
```js
344+
import consoleLogLevel from "console-log-level";
345345
const octokit = new Octokit({
346-
log: require("console-log-level")({ level: "info" }),
346+
log: consoleLogLevel({ level: "info" }),
347347
});
348348
```
349349

@@ -386,18 +386,20 @@ In order to extend `octokit`'s API, the plugin must return an object with the ne
386386

387387
```js
388388
// index.js
389-
const { Octokit } = require("@octokit/core")
389+
import { Octokit } from "@octokit/core";
390+
import myPlugin from "./lib/my-plugin.js";
391+
import octokitPluginExample from "octokit-plugin-example";
390392
const MyOctokit = Octokit.plugin(
391-
require("./lib/my-plugin"),
392-
require("octokit-plugin-example")
393+
myPlugin,
394+
octokitPluginExample
393395
);
394396

395397
const octokit = new MyOctokit({ greeting: "Moin moin" });
396398
octokit.helloWorld(); // logs "Moin moin, world!"
397399
octokit.request("GET /"); // logs "GET / - 200 in 123ms"
398400

399401
// lib/my-plugin.js
400-
module.exports = (octokit, options = { greeting: "Hello" }) => {
402+
const plugin = (octokit, options = { greeting: "Hello" }) => {
401403
// hook into the request lifecycle
402404
octokit.hook.wrap("request", async (request, options) => {
403405
const time = Date.now();
@@ -414,18 +416,23 @@ module.exports = (octokit, options = { greeting: "Hello" }) => {
414416
helloWorld: () => console.log(`${options.greeting}, world!`);
415417
}
416418
};
419+
export default plugin;
417420
```
418421

419422
## Build your own Octokit with Plugins and Defaults
420423

421424
You can build your own Octokit class with preset default options and plugins. In fact, this is mostly how the `@octokit/<context>` modules work, such as [`@octokit/action`](https://github.com/octokit/action.js):
422425

423426
```js
424-
const { Octokit } = require("@octokit/core");
427+
import { Octokit } from "@octokit/core";
428+
import { paginateRest } from "@octokit/plugin-paginate-rest";
429+
import { throttling } from "@octokit/plugin-throttling";
430+
import { retry } from "@octokit/plugin-retry";
431+
import { createActionAuth } from "@octokit/auth-action";
425432
const MyActionOctokit = Octokit.plugin(
426-
require("@octokit/plugin-paginate-rest").paginateRest,
427-
require("@octokit/plugin-throttling").throttling,
428-
require("@octokit/plugin-retry").retry,
433+
paginateRest,
434+
throttling,
435+
retry,
429436
).defaults({
430437
throttle: {
431438
onAbuseLimit: (retryAfter, options) => {
@@ -435,7 +442,7 @@ const MyActionOctokit = Octokit.plugin(
435442
/* ... */
436443
},
437444
},
438-
authStrategy: require("@octokit/auth-action").createActionAuth,
445+
authStrategy: createActionAuth,
439446
userAgent: `my-octokit-action/v1.2.3`,
440447
});
441448

0 commit comments

Comments
 (0)