Skip to content

Commit

Permalink
feat: package is now ESM (#661)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: package is now ESM
  • Loading branch information
wolfy1339 committed Feb 25, 2024
1 parent 0cf80ff commit 77f8a61
Show file tree
Hide file tree
Showing 11 changed files with 1,257 additions and 1,893 deletions.
31 changes: 19 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ Node
Install with <code>npm install @octokit/core</code>

```js
const { Octokit } = require("@octokit/core");
// or: import { Octokit } from "@octokit/core";
import { Octokit } from "@octokit/core";
```

</td></tr>
Expand Down Expand Up @@ -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" }),
});
```

Expand Down Expand Up @@ -386,18 +386,20 @@ 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" });
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();
Expand All @@ -414,18 +416,23 @@ module.exports = (octokit, options = { greeting: "Hello" }) => {
helloWorld: () => console.log(`${options.greeting}, world!`);
}
};
export default plugin;
```

## Build your own Octokit with Plugins and Defaults

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):

```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) => {
Expand All @@ -435,7 +442,7 @@ const MyActionOctokit = Octokit.plugin(
/* ... */
},
},
authStrategy: require("@octokit/auth-action").createActionAuth,
authStrategy: createActionAuth,
userAgent: `my-octokit-action/v1.2.3`,
});

Expand Down

0 comments on commit 77f8a61

Please sign in to comment.