Customized Octokit for Octoherd
npm install @octoherd/octokit@octoherd/octokit> Customized Octokit for Octoherd
Browsers | Load `` |
|---|---|
Node (12+) | Install with ` |
Deno | Load ` |
`js
import { Octokit } from "@octoherd/octokit";
const octokit = new Octokit({
auth: / token here, create one at https://github.com/settings/tokens/new /,
});
const { data: me } = await octokit.request("GET /user")
console.log(me)
`
By default you authenticate using a token, but you can use any authentication strategy.
@octokit/octokit is built on @octokit/core. You can send requests to GitHub's REST API using octokit.request and GraphQL queries octokit.graphql.
By default, messages are logged with meta data using console.info, console.warn, and console.error. octokit.log.debug is a no-op, unless options.octoherd.debug is set to true.
Important: options.log is ignored. Setting it has no effect.
You can log simple messages, interpolation is supported.
`js`
octokit.log.info("Checking repository %s", repository.full_name);
You can pass extra meta information as the first argument
`js`
octokit.log.info(
{ id: repository.id },
"Checking repository %s",
repository.full_name
);
You can also just log meta information for reporting later
`js`
octokit.log.info({
id: repository.id,
owner: repositor.owner.login,
repo: repository.name,
private: repository.private,
});
The way data is logged can be customized using options.octoherd.onLogMessage and options.octoherd.onLogData.
`js.level
const octokit = new Octokit({
octoherd: {
onLogData(data) {
// e.g. write data as JSON line to debug log file
// data always has , and .time properties. .msg is set from the log message if set.[%s]
},
onLogMessage(level, message, additionalData) {
// level is one of: debug, info, warn, error.
// message is the log message
// additionalData is any data that was passed as first argument to the log methods. It defaults to {}
console.log(
,${message} ${chalk.gray(JSON.stringify(additionalData))}
level.toUpperCase(),
Object.keys(additionalData).length
? `
: message
);
},
},
});
Additional context can be changed at runtime using octokit.log.setContext(context). The additional context is only passed to options.octoherd.onLogData
`jsonLogData
octokit.log.setContext({ repo_id: 123 });
octokit.log.info("test");
// data passed to will be { repo_id: 123, msg: "test", level: "info", time: 0 }onLogMessage
// additionalData passed to will not have the .repo_id property`
@octoherd/octokit comes with a few plugins out of the box:
- @octokit/plugin-paginate-rest
- @octokit/plugin-retry
- @octokit/plugin-throttling
You can use octokit.paginate() or octokit.paginate.iterator() for paginating REST API requests.
The retry & throttling plugins hook into the request lifecycle, retries requests in case of unrelated server errors, and throttles requests to avoid hitting rate or abuse limits.
You can extend @octoherd/octokit` with hooks and plugins