octokit.js

简介: octokit.js


暴食杀人多于利剑杀人。——伯里兹

分享一个框架封装了GitHubAPI调用

GitHub - octokit/octokit.js: The all-batteries-included GitHub SDK for Browsers, Node.js, and Deno.

等于是一个SDK

目前,GitHub 维护以下语言/框架/平台的 SDK:

还有 2 个 SDK,它们是根据 GitHub 的 OpenAPI 描述生成的!

浏览器里安装:

<script type="module">
import { Octokit, App } from "https://esm.sh/octokit";
</script>

示例:获取经过身份验证的用户的用户名。

// Create a personal access token at https://github.com/settings/tokens/new?scopes=repo
const octokit = new Octokit({ auth: `personal-access-token123` });
// Compare: https://docs.github.com/en/rest/reference/users#get-the-authenticated-user
const {
  data: { login },
} = await octokit.rest.users.getAuthenticated();
console.log("Hello, %s", login);

这里支持两种方式,就拿创建issues举例

await octokit.rest.issues.create({
  owner: "octocat",
  repo: "hello-world",
  title: "Hello, world!",
  body: "I created this issue using Octokit!",
});

和下面的方式相同

await octokit.request("POST /repos/{owner}/{repo}/issues", {
  owner: "octocat",
  repo: "hello-world",
  title: "Hello, world!",
  body: "I created this issue using Octokit!",
});

还有分页查询仓库里的issues

const iterator = octokit.paginate.iterator(octokit.rest.issues.listForRepo, {
  owner: "octocat",
  repo: "hello-world",
  per_page: 100,
});
// iterate through each response
for await (const { data: issues } of iterator) {
  for (const issue of issues) {
    console.log("Issue #%d: %s", issue.number, issue.title);
  }
}

还可以使用异步的方式去做

const issues = await octokit.paginate(octokit.rest.issues.listForRepo, {
  owner: "octocat",
  repo: "hello-world",
  per_page: 100,
});

设置媒体格式:

const { data } = await octokit.rest.repos.getContent({
  mediaType: {
    format: "raw",
  },
  owner: "octocat",
  repo: "hello-world",
  path: "package.json",
});
console.log("package name: %s", JSON.parse(data).name);

错误处理

import { RequestError } from "octokit";


try {
  // your code here that sends at least one Octokit request
  await octokit.request("GET /");
} catch (error) {
  // Octokit errors are instances of RequestError, so they always have an `error.status` property containing the HTTP response code.
  if (error instanceof RequestError) {
    // handle Octokit error
    // error.message; // Oops
    // error.status; // 500
    // error.request; // { method, url, headers, body }
    // error.response; // { url, status, headers, data }
  } else {
    // handle all other errors
    throw error;
  }
}

也有GraphQL的支持,例如获取经过身份验证的用户的登录信息

const {
  viewer: { login },
} = await octokit.graphql(`{
  viewer {
    login
  }
}`);

可以把变量放到第二个参数传进去

const { lastIssues } = await octokit.graphql(
  `
    query lastIssues($owner: String!, $repo: String!, $num: Int = 3) {
      repository(owner: $owner, name: $repo) {
        issues(last: $num) {
          edges {
            node {
              title
            }
          }
        }
      }
    }
  `,
  {
    owner: "octokit",
    repo: "graphql.js",
  },
);

关于GraphQL的分页查询仓库的issues

const { allIssues } = await octokit.graphql.paginate(
  `
    query allIssues($owner: String!, $repo: String!, $num: Int = 10, $cursor: String) {
      repository(owner: $owner, name: $repo) {
        issues(first: $num, after: $cursor) {
          edges {
            node {
              title
            }
          }
          pageInfo {
            hasNextPage
            endCursor
          }
        }
      }
    }
  `,
  {
    owner: "octokit",
    repo: "graphql.js",
  },
);

创建Label

await octokit.graphql(
  `mutation createLabel($repositoryId:ID!,name:String!,color:String!) {
  createLabel(input:{repositoryId:$repositoryId,name:$name}) {
    label: {
      id
    }
  }
}`,
  {
    repositoryId: 1,
    name: "important",
    color: "cc0000",
    mediaType: {
      previews: ["bane"],
    },
  },
);


相关文章
|
6月前
|
JavaScript 前端开发
js详解1
js详解1
40 2
|
3月前
|
资源调度 JavaScript 前端开发
如何开始使用 Next.js?
【8月更文挑战第4天】如何开始使用 Next.js?
48 3
|
3月前
|
JavaScript 前端开发
浅谈js。。
浅谈js。。
17 0
|
3月前
|
JavaScript
什么是js?
什么是js?
146 0
|
5月前
|
XML JavaScript 前端开发
JS是什么
JS是什么
116 0
|
6月前
|
编解码 JavaScript 前端开发
js一些理解
js一些理解
31 1
|
6月前
|
JavaScript 前端开发
JS实现日历表
JS实现日历表
32 0
|
编解码 JavaScript 前端开发
什么是js?(初步了解)
什么是js?(初步了解)
|
JSON JavaScript 前端开发
js常见题
js常见题
61 0
|
JavaScript
js超实用的小技巧(1)
js超实用的小技巧(1)