diff options
author | Nick Coutsos <[email protected]> | 2021-10-30 11:13:24 -0400 |
---|---|---|
committer | Nick Coutsos <[email protected]> | 2021-10-30 11:13:24 -0400 |
commit | 8fd26192689c06c75e4f6909fb2eb485fa377e56 (patch) | |
tree | 18fd9616b5f6e42edf7ee7e107d02f2d7caedfe3 | |
parent | 8dc597870006d177c40dac96f4dcd7e1cd7b5d50 (diff) | |
download | keymap-editor-8fd26192689c06c75e4f6909fb2eb485fa377e56.tar.gz keymap-editor-8fd26192689c06c75e4f6909fb2eb485fa377e56.zip |
Page through list of repositories
-rw-r--r-- | api/routes/github.js | 2 | ||||
-rw-r--r-- | api/services/github/api.js | 9 | ||||
-rw-r--r-- | api/services/github/installations.js | 21 | ||||
-rw-r--r-- | package-lock.json | 14 | ||||
-rw-r--r-- | package.json | 1 |
5 files changed, 39 insertions, 8 deletions
diff --git a/api/routes/github.js b/api/routes/github.js index 19d87ad..5c4456e 100644 --- a/api/routes/github.js +++ b/api/routes/github.js @@ -72,7 +72,7 @@ const getInstallation = async (req, res, next) => { return res.json({ installation: null }) } - const { data: { repositories } } = await fetchInstallationRepos(user.oauth_access_token, installation.id) + const repositories = await fetchInstallationRepos(user.oauth_access_token, installation.id) res.json({ installation, repositories }) } catch (err) { diff --git a/api/services/github/api.js b/api/services/github/api.js index b9bf600..bfaad61 100644 --- a/api/services/github/api.js +++ b/api/services/github/api.js @@ -1,7 +1,8 @@ const axios = require('axios') const baseUrl = 'https://api.github.com' -function request (options={}) { + +async function request (options={}) { if (typeof options === 'string') { options = { url: options @@ -20,7 +21,11 @@ function request (options={}) { options.headers.Authorization = `Bearer ${options.token}` } - return axios(options) + const response = await axios(options) + + console.log(response.headers['x-ratelimit-remaining']) + + return response } module.exports = { diff --git a/api/services/github/installations.js b/api/services/github/installations.js index 73b1764..346dc45 100644 --- a/api/services/github/installations.js +++ b/api/services/github/installations.js @@ -1,3 +1,5 @@ +const linkHeader = require('http-link-header') + const api = require('./api') const { createAppToken } = require('./auth') @@ -12,11 +14,20 @@ function fetchInstallation (user) { }) } -function fetchInstallationRepos (installationToken, installationId) { - return api.request({ - url: `/user/installations/${installationId}/repositories`, - token: installationToken - }) +async function fetchInstallationRepos (installationToken, installationId) { + const initialPage = `/user/installations/${installationId}/repositories` + const repositories = [] + + let url = initialPage + while (url) { + console.log('fetching page', url) + const { headers, data } = await api.request({ url, token: installationToken }) + const paging = linkHeader.parse(headers.link || '') + repositories.push(...data.repositories) + url = paging.get('rel', 'next')?.[0]?.uri + } + + return repositories } module.exports = { diff --git a/package-lock.json b/package-lock.json index c0a8b7f..1aa391f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -16,6 +16,7 @@ "dotenv": "^10.0.0", "express": "^4.17.1", "express-ws": "^4.0.0", + "http-link-header": "^1.0.3", "jsonwebtoken": "^8.5.1", "lodash": "^4.17.21", "morgan": "^1.10.0" @@ -323,6 +324,14 @@ "node": ">= 0.6" } }, + "node_modules/http-link-header": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/http-link-header/-/http-link-header-1.0.3.tgz", + "integrity": "sha512-nARK1wSKoBBrtcoESlHBx36c1Ln/gnbNQi1eB6MeTUefJIT3NvUOsV15bClga0k38f0q/kN5xxrGSDS3EFnm9w==", + "engines": { + "node": ">=4.0.0" + } + }, "node_modules/iconv-lite": { "version": "0.4.24", "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.24.tgz", @@ -959,6 +968,11 @@ "toidentifier": "1.0.0" } }, + "http-link-header": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/http-link-header/-/http-link-header-1.0.3.tgz", + "integrity": "sha512-nARK1wSKoBBrtcoESlHBx36c1Ln/gnbNQi1eB6MeTUefJIT3NvUOsV15bClga0k38f0q/kN5xxrGSDS3EFnm9w==" + }, "iconv-lite": { "version": "0.4.24", "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.24.tgz", diff --git a/package.json b/package.json index dbf6627..a68c1ff 100644 --- a/package.json +++ b/package.json @@ -19,6 +19,7 @@ "dotenv": "^10.0.0", "express": "^4.17.1", "express-ws": "^4.0.0", + "http-link-header": "^1.0.3", "jsonwebtoken": "^8.5.1", "lodash": "^4.17.21", "morgan": "^1.10.0" |