iterator.js 1.2 KB

1234567891011121314151617181920212223242526
  1. import { normalizePaginatedListResponse } from "./normalize-paginated-list-response";
  2. export function iterator(octokit, route, parameters) {
  3. const options = octokit.request.endpoint(route, parameters);
  4. const method = options.method;
  5. const headers = options.headers;
  6. let url = options.url;
  7. return {
  8. [Symbol.asyncIterator]: () => ({
  9. next() {
  10. if (!url) {
  11. return Promise.resolve({ done: true });
  12. }
  13. return octokit
  14. .request({ method, url, headers })
  15. .then((response) => {
  16. normalizePaginatedListResponse(octokit, url, response);
  17. // `response.headers.link` format:
  18. // '<https://api.github.com/users/aseemk/followers?page=2>; rel="next", <https://api.github.com/users/aseemk/followers?page=2>; rel="last"'
  19. // sets `url` to undefined if "next" URL is not present or `link` header is not set
  20. url = ((response.headers.link || "").match(/<([^>]+)>;\s*rel="next"/) || [])[1];
  21. return { value: response };
  22. });
  23. }
  24. })
  25. };
  26. }