Skip to content
This repository has been archived by the owner on Jan 13, 2022. It is now read-only.

kpherox/fetch-github-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

67 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

fetch-github-api

License npm version code size Dependencies Status DevDependencies Status
Maintainability Status Test Coverage Status Master Build Status Develop Build Status

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 100
default: 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 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 => {...})

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.