在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):octokit/core.js开源软件地址(OpenSource Url):https://github.com/octokit/core.js开源编程语言(OpenSource Language):TypeScript 100.0%开源软件介绍(OpenSource Introduction):core.js
If you need a minimalistic library to utilize GitHub's REST API and GraphQL API which you can extend with plugins as needed, then If you don't need the Plugin API then using Usage
REST API example// Create a personal access token at https://github.com/settings/tokens/new?scopes=repo
const octokit = new Octokit({ auth: `personal-access-token123` });
const response = await octokit.request("GET /orgs/{org}/repos", {
org: "octokit",
type: "private",
}); See GraphQL exampleconst octokit = new Octokit({ auth: `secret123` });
const response = await octokit.graphql(
`query ($login: String!) {
organization(login: $login) {
repositories(privacy: PRIVATE) {
totalCount
}
}
}`,
{ login: "octokit" }
); See Options
DefaultsYou can create a new Octokit class with customized default options. const MyOctokit = Octokit.defaults({
auth: "personal-access-token123",
baseUrl: "https://github.acme-inc.com/api/v3",
userAgent: "my-app/v1.2.3",
});
const octokit1 = new MyOctokit();
const octokit2 = new MyOctokit(); If you pass additional options to your new constructor, the options will be merged shallowly. const MyOctokit = Octokit.defaults({
foo: {
opt1: 1,
},
});
const octokit = new MyOctokit({
foo: {
opt2: 1,
},
});
// options will be { foo: { opt2: 1 }} If you need a deep or conditional merge, you can pass a function instead. const MyOctokit = Octokit.defaults((options) => {
return {
foo: Object.assign({}, options.foo, { opt2: 1 }),
};
});
const octokit = new MyOctokit({
foo: { opt2: 1 },
});
// options will be { foo: { opt1: 1, opt2: 1 }} Be careful about mutating the AuthenticationAuthentication is optional for some REST API endpoints accessing public data, but is required for GraphQL queries. Using authentication also increases your API rate limit. By default, Octokit authenticates using the token authentication strategy. Pass in a token using import { Octokit } from "@octokit/core";
const octokit = new Octokit({
auth: "mypersonalaccesstoken123",
});
const { data } = await octokit.request("/user"); To use a different authentication strategy, set Example import { Octokit } from "@octokit/core";
import { createAppAuth } from "@octokit/auth-app";
const appOctokit = new Octokit({
authStrategy: createAppAuth,
auth: {
appId: 123,
privateKey: process.env.PRIVATE_KEY,
},
});
const { data } = await appOctokit.request("/app"); The const { token } = await appOctokit.auth({
type: "installation",
installationId: 123,
}); LoggingThere are four built-in log methods
They can be configured using the This is useful if you build reusable plugins. If you would like to make the log level configurable using an environment variable or external option, we recommend the console-log-level package. Example const octokit = new Octokit({
log: require("console-log-level")({ level: "info" }),
}); HooksYou can customize Octokit's request lifecycle with hooks. octokit.hook.before("request", async (options) => {
validate(options);
});
octokit.hook.after("request", async (response, options) => {
console.log(`${options.method} ${options.url}: ${response.status}`);
});
octokit.hook.error("request", async (error, options) => {
if (error.status === 304) {
return findInCache(error.response.headers.etag);
}
throw error;
});
octokit.hook.wrap("request", async (request, options) => {
// add logic before, after, catch errors or replace the request altogether
return request(options);
}); See before-after-hook for more documentation on hooks. PluginsOctokit’s functionality can be extended using plugins. The A plugin is a function which gets two arguments:
In order to extend // index.js
const { Octokit } = require("@octokit/core")
const MyOctokit = Octokit.plugin(
require("./lib/my-plugin"),
require("octokit-plugin-example")
);
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" }) => {
// hook into the request lifecycle
octokit.hook.wrap("request", async (request, options) => {
const time = Date.now();
const response = await request(options);
console.log(
`${options.method} ${options.url} – ${response.status} in ${Date.now() -
time}ms`
);
return response;
});
// add a custom method
return {
helloWorld: () => console.log(`${options.greeting}, world!`);
}
}; Build your own Octokit with Plugins and DefaultsYou can build your own Octokit class with preset default options and plugins. In fact, this is mostly how the const { Octokit } = require("@octokit/core");
const MyActionOctokit = Octokit.plugin(
require("@octokit/plugin-paginate-rest").paginateRest,
require("@octokit/plugin-throttling").throttling,
require("@octokit/plugin-retry").retry
).defaults({
throttle: {
onAbuseLimit: (retryAfter, options) => {
/* ... */
},
onRateLimit: (retryAfter, options) => {
/* ... */
},
},
authStrategy: require("@octokit/auth-action").createActionAuth,
userAgent: `my-octokit-action/v1.2.3`,
});
const octokit = new MyActionOctokit();
const installations = await octokit.paginate("GET /app/installations"); LICENSE |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论