1.1.0 • Published 6 years ago
fetch-github-api v1.1.0
fetch-github-api
Fetch json of all pages for GitHub API. Just passing an API endpoint makes it easy to retrieve all the data.
Requirements
- Node.js version 8.x or later.
- Support Promiss/fetch/URL browser.
Installation
Install from npm.npm i fetch-github-api
How to use
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();
Options
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 100default: 0 |
Authenticate
This module supported Basic authenticate & OAuth2 token.
Use OAuth2 token
Sent as a parameter
// 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};
Sent in a header
let fetchGitHubApi = new FetchGitHubApi('/path/to/endpoint');
// Set accessToken property.
fetchGitHubApi.accessToken = OAUTH-TOKEN;
Use Basic authenticate
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;
Check must OTP code
// 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 result
+ , inputValue = prompt("Please enter OTP code:", "")
+ , value = Number(inputValue);
+ while (Number.isNaN(value)) {
+ inputValue = prompt("Please enter OTP code:", "");
+ value = Number(inputValue);
+ if (!Number.isNaN(value)) {
+ result = value;
+ break;
+ }
+ }
+ fetchGitHubApi.otpToken = result;
+
+ return fetchGitHubApi.fetchJson().catch(checkMustOtpCode);
+ }
+
fetchGitHubApi.fetchJson()
+ .catch(checkMustOtpCode)
.then(json => {...})
License
This module is released under the MIT License. See the LICENSE file for more information.
Include packages
See the INCLUDE-LICENSE file for information.