Files
Superalgos/node_modules/@octokit/rest/lib/plugins/pagination/get-page.js
T

47 lines
1.0 KiB
JavaScript

module.exports = getPage
const HttpError = require('../../request/http-error')
const getPageLinks = require('./get-page-links')
function getPage (octokit, link, which, headers, callback) {
if (typeof headers === 'function') {
callback = headers
headers = null
}
const url = getPageLinks(link)[which]
if (!url) {
const urlError = new HttpError(`No ${which} page found`, 404)
if (callback) {
return callback(urlError)
}
return Promise.reject(urlError)
}
const requestOptions = {
url,
headers: applyAcceptHeader(link, headers)
}
const promise = octokit.request(requestOptions)
if (callback) {
promise.then(callback.bind(null, null), callback)
return
}
return promise
}
function applyAcceptHeader (res, headers) {
const previous = res.meta && res.meta['x-github-media-type']
if (!previous || (headers && headers.accept)) {
return headers
}
headers = headers || {}
headers.accept = `application/vnd.${previous.replace('; format=', '+')}`
return headers
}