Fetch json of all pages for GitHub API. Just passing an API endpoint makes it easy to retrieve all the data.
- Node.js version 8.x or later.
- Support Promiss/fetch/URL browser.
Install from npm.
npm i fetch-github-api
Use default export. Default is class. Pass endpoint to import class.
// Use require, import not test.
const FetchGitHubApi = require('fetch-github-api');
// Initialize class.
let fetchGitHubApi = new FetchGitHubApi('/path/to/endpoint');
// Support url query.
// let fetchGitHubApi = new FetchGitHubApi('/path/to/endpoint', {'per_page': 50});
// let fetchGitHubApi = new FetchGitHubApi('/path/to/endpoint', {}, 0, 50); // this is equivalent to the above code.
// Fetch json with then chain.
fetchGitHubApi.fetchJson().then(json => {...});
// or async/await result.
let json = await fetchGitHubApi.fetchJson();
constructor arguments.
name | type | desc |
---|---|---|
endpoint | String | API endpoint. |
params | Object (associative array) |
GET query parameter. For example, when /users/:username/repos, {'sort':'updated'} etc. default: {} |
max | Number (Integer) |
Max page number. 0 is all page. default: 0 |
per | Number (Integer) |
Per page number. 0 is API default (30). max 100 default: 0 |
This module supported Basic authenticate & OAuth2 token.
// Add params option when initialize.
let fetchGitHubApi = new FetchGitHubApi('/path/to/endpoint', {'access_token': OAUTH-TOKEN});
// or set params property.
fetchGitHubApi.params = {'access_token': OAUTH-TOKEN};
let fetchGitHubApi = new FetchGitHubApi('/path/to/endpoint');
// Set accessToken property.
fetchGitHubApi.accessToken = OAUTH-TOKEN;
let fetchGitHubApi = new FetchGitHubApi('/path/to/endpoint');
// Set basicAuth property.
fetchGitHubApi.basicAuth = "username:password";
// or set username/password property.
fetchGitHubApi.username = "username";
fetchGitHubApi.password = "password";
// If two-factor authentication enabled, set otpToken property.
fetchGitHubApi.otpToken = 123456;
// If must OTP code, catch http error
+let checkMustOtpCode = err => {
+ if (err.name !== 'HTTPStatusError') {
+ throw err;
+ }
+
+ let errMsg = JSON.parse(err.message)
+ , resMsg = errMsg['data']['message'];
+
+ if (resMsg !== 'Must specify two-factor authentication OTP code.') {
+ throw err;
+ }
+
+ // Perform set otpToken property here.
+
+ // if browser, use prompt
+ let inputValue = prompt("Please enter OTP code:", "")
+ , result = Number(inputValue);
+ while (Number.isNaN(result)) {
+ inputValue = prompt("Please enter OTP code:", "");
+ result = Number(inputValue);
+ }
+ fetchGitHubApi.otpToken = result;
+
+ return fetchGitHubApi.fetchJson().catch(checkMustOtpCode);
+ }
+
fetchGitHubApi.fetchJson()
+ .catch(checkMustOtpCode)
.then(json => {...})
This module is released under the MIT License. See the LICENSE file for more information.
See the INCLUDE-LICENSE file for information.