From e18363f0812b61cbbb6bc0e022daba4cd997dea0 Mon Sep 17 00:00:00 2001 From: Jake Bolam Date: Sun, 23 Dec 2018 11:24:29 +0800 Subject: [PATCH] feat: way to avoid github API limit (#122) **What**: Add a way to avoid GITHUB API LIMIT by supplying an optional GITHUB_AUTH token to increase github rate limiting from 50 requests to 5000 (and limited by user, not IP) **Why**: To address: #121 **How**: By Me **Checklist**: - [ ] Documentation - [ ] Tests - [ ] Ready to be merged - [ ] Added myself to contributors table --- src/repo/github.js | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/repo/github.js b/src/repo/github.js index 3a51788..0c49f7e 100644 --- a/src/repo/github.js +++ b/src/repo/github.js @@ -1,6 +1,19 @@ const pify = require('pify') const request = pify(require('request')) +function getRequestHeaders() { + const requestHeaders = { + 'User-Agent': 'request', + } + + const optionalAuthToken = process.env.GITHUB_TOKEN + if (optionalAuthToken) { + requestHeaders.Authorization = `token ${optionalAuthToken}` + } + + return requestHeaders +} + function getNextLink(link) { if (!link) { return null @@ -19,9 +32,7 @@ function getContributorsPage(url) { return request .get({ url, - headers: { - 'User-Agent': 'request', - }, + headers: getRequestHeaders(), }) .then(res => { const body = JSON.parse(res.body) @@ -54,9 +65,7 @@ const getUserInfo = function(username, hostname) { return request .get({ url: `${root}/users/${username}`, - headers: { - 'User-Agent': 'request', - }, + headers: getRequestHeaders(), }) .then(res => { const body = JSON.parse(res.body)