github-base中文文档|github-base js中文教程|解析

npm npmdoc 2年前 (2022-01-01) 524次浏览

github-base中文文档|github-base js中文教程|解析

安装命令:npm i github-base

github-base NPM 版本 NPM 每月下载量 NPM 总下载量 Linux 构建状态 Windows 构建状态

在 node.js/JavaScript 中使用 GitHub API 的低级方法。

请考虑关注该项目的作者Jon Schlinkert,并考虑为该项目加星以表达您的 ❤️ 和支持。

目录

细节

动词使用markdown-toc生成的目录

安装

使用npm安装

$ npm install --save github-base

当心!

从 v1.0 开始,API 是 100% 基于承诺的,不再支持回调。有关更多详细信息,请参阅API 文档发布历史记录

为什么是 github-base,而不是…?

我们发现的每个其他 GitHub API 库要么有一个巨大的依赖树,试图成为每个人的一切过于臃肿的样板代码,过于自以为是,或者没有维护。

我们创建了 github-base 来为少数 HTTP 动词提供低级支持,以创建更高级别的库:

  • .request:所有 GitHub HTTP 动词的基本处理程序:GET, PUT, POST, DELETE,PATCH
  • .get : 代理.request('GET', path, options, cb)
  • .delete : 代理.request('DELETE', path, options, cb)
  • .patch : 代理.request('PATCH', path, options, cb)
  • .post : 代理.request('POST', path, options, cb)
  • .put : 代理.request('PUT', path, options, cb)
  • .paged:递归地发出GET请求,直到检索到所有页面。

跳转到API 部分以获取更多详细信息。

用法

使用以下代码行将 github-base 添加到您的 node.js/JavaScript 项目:

const GitHub = require('github-base');

示例用法

递归地GET为用户所有页面的要点:

const github = new GitHub({ /* options */ });
const owner = 'jonschlinkert';
 
github.paged(`/users/${owner}/gists`)
  .then(res => console.log(res))
  .catch(console.error);

应用程序接口

(所有请求方法都接受回调,如果回调没有作为最后一个参数传递,则返回一个承诺)

GitHub

GitHub使用给定的选项创建一个实例

参数

  • options {目的}

例子

const GitHub = require('github-base');
const github = new GitHub([options]);

。要求

使用Needle向 GitHub API 发出请求。支持下列动词:GETPUTPOSTPATCH,和DELETE接受回调或返回承诺。

参数

  • method {String}:要使用的 http 动词
  • path {String}:附加到基本 GitHub API URL 的路径。
  • options {选项}:请求选项

例子

// list all orgs for the authenticated user
const auth = require('./local-private-auth-info');
const github = new GitHub(auth);
github.request('GET', '/user/orgs')
  .then(res => console.log(res.body));

。得到

做一个GET对GitHub的API请求。

参数

  • path {String}:附加到基本 GitHub API URL 的路径。
  • options {选项}:请求选项

例子

// get a list of orgs for the authenticated user
github.get('/user/orgs')
  .then(res => console.log(res.body));
 
// get gists for the authenticated user
github.get('/gists')
  .then(res => console.log(res.body));

。删除

做一个DELETE对GitHub的API请求。

参数

  • path {String}:附加到基本 GitHub API URL 的路径。
  • options {选项}:请求选项

例子

// un-follow someone
github.delete('/user/following/:some_username', { some_username: 'whatever' })
  .then(res => {
    console.log('RESPONSE:', res);
  });

。修补

做一个PATCH对GitHub的API请求。

参数

  • path {String}:附加到基本 GitHub API URL 的路径。
  • options {选项}:请求选项

例子

// update a gist
const fs = require('fs');
const options = { files: { 'readme.md': { content: fs.readFileSync('README.md', 'utf8') } } };
github.patch('/gists/bd139161a425896f35f8', options)
  .then(res => {
    console.log('RESPONSE:', res);
  });

。邮政

做一个POST对GitHub的API请求。

参数

  • path {String}:附加到基本 GitHub API URL 的路径。
  • options {选项}:请求选项

例子

// create a new repository
github.post('/user/repos', { name: 'new-repo-name' })
  .then(res => {
    console.log('RESPONSE:', res);
  });

。放

做一个PUT对GitHub的API请求。

参数

  • path {String}:附加到基本 GitHub API URL 的路径。
  • options {选项}:请求选项

例子

// follow someone
github.put('/user/following/jonschlinkert')
  .then(res => {
    console.log('RESPONSE:', res);
  });

.paged

递归地发出 GET 请求,直到返回所有记录页。

参数

  • path {String}:附加到基本 GitHub API URL 的路径。
  • options {选项}:请求选项

例子

// get all repos for the authenticated user
github.paged('/user/repos?type=all&per_page=1000&sort=updated')
  .then(res => console.log(res.pages))
  .catch(console.error)

。利用

使用 use注册插件

const github = new GitHub();
 
github.use(function() {
  // do stuff with the github-base instance
});

验证

有几种方法可以进行身份​​验证,所有这些方法都需要在options上传递信息

const github = new GitHub({
  username: YOUR_USERNAME,
  password: YOUR_PASSWORD,
});
 
// or 
const github = new GitHub({
  token: YOUR_TOKEN
});
 
// or 
const github = new GitHub({
  bearer: YOUR_JSON_WEB_TOKEN
});

路径和占位符

已弃用:由于 es2015 模板使此功能不太有用,我们计划在将来的版本中将其删除。

路径类似于路由器路径,其中给定字符串中的占位符被替换为选项中的值。例如,以下示例中的路径:

const github = new GitHub();
const options = { user: 'jonschlinkert', repo: 'github-base', subscribed: true };
 
github.put('/repos/:user/:repo/subscription', options);

扩展为:

'/repos/jonschlinkert/github-base/subscription'

占位符名称也是任意的,只要可以使用选项中定义的值解析所有占位符名称,您就可以随意使用它们。

选项

选项可以在实例化时传递给构造函数,和/或直接在实例上设置,和/或传递给任何方法。

例子

// pass to constructor
const github = new GitHub({ user: 'doowb' });
 
// and/or directly set on instance options
github.options.user = 'doowb';
 
// and/or pass to a method
github.get('/users/:user/gists', { user: 'doowb' })

选项查询

类型:object

默认值{ per_page: 100 }对于获取分页请求,undefined对于所有其他请求。

使用qs 中.stringify方法将对象传递给字符串化并附加到 URL

例子

github.paged('/users/:user/gists', { user: 'doowb', query: { per_page: 30 } })
  .then(res => {
    console.log(res.pages);
  });

您还可以手动附加查询字符串:

github.paged('/users/:user/gists?per_page=30', { user: 'doowb' })
  .then(res => {
    console.log(res.pages);
  });

关于

贡献

拉取请求和星星总是受欢迎的。对于错误和功能请求,请创建问题

运行测试

运行和审查单元测试是熟悉库及其 API 的好方法。您可以使用以下命令安装依赖项并运行测试:

$ npm install && npm test
构建文档

(本项目的 readme.md 是由verb生成的,请不要直接编辑 readme。对 readme 的任何更改都必须在.verb.md readme 模板中进行。)

要生成自述文件,请运行以下命令:

$ npm install -g verbose/verb#dev verb-generate-readme && verb

相关项目

您可能还对这些项目感兴趣:

贡献者

提交 贡献者
40 琼施林克特
10 豆豆
7 奥尔斯滕拉克

作者

乔恩·施林克特

执照

版权所有 © 2018,乔恩·施林克特MIT 许可发布


此文件由verb-generate-readme v0.6.0 于2018 年8 月14 日生成

项目贡献人员列表:


极客公园 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:github-base中文文档|github-base js中文教程|解析
喜欢 (0)
.excerpt .focus {display:none}