get-page.js 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. module.exports = getPage
  2. const deprecate = require('./deprecate')
  3. const getPageLinks = require('./get-page-links')
  4. const HttpError = require('./http-error')
  5. function getPage (octokit, link, which, headers) {
  6. deprecate(`octokit.get${which.charAt(0).toUpperCase() + which.slice(1)}Page() – You can use octokit.paginate or async iterators instead: https://github.com/octokit/rest.js#pagination.`)
  7. const url = getPageLinks(link)[which]
  8. if (!url) {
  9. const urlError = new HttpError(`No ${which} page found`, 404)
  10. return Promise.reject(urlError)
  11. }
  12. const requestOptions = {
  13. url,
  14. headers: applyAcceptHeader(link, headers)
  15. }
  16. const promise = octokit.request(requestOptions)
  17. return promise
  18. }
  19. function applyAcceptHeader (res, headers) {
  20. const previous = res.headers && res.headers['x-github-media-type']
  21. if (!previous || (headers && headers.accept)) {
  22. return headers
  23. }
  24. headers = headers || {}
  25. headers.accept = 'application/vnd.' + previous
  26. .replace('; param=', '.')
  27. .replace('; format=', '+')
  28. return headers
  29. }