Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: package is now ESM #661

Merged
merged 10 commits into from
Feb 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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