UNPKG

@octokit/plugin-paginate-graphql

Version:

Octokit plugin to paginate GraphQL API endpoint responses

35 lines (34 loc) 1.16 kB
import { extractPageInfos } from "./extract-page-info.js"; import { getCursorFrom, hasAnotherPage } from "./page-info.js"; import { MissingCursorChange } from "./errors.js"; const createIterator = (octokit) => { return (query, initialParameters = {}) => { let nextPageExists = true; let parameters = { ...initialParameters }; return { [Symbol.asyncIterator]: () => ({ async next() { if (!nextPageExists) return { done: true, value: {} }; const response = await octokit.graphql( query, parameters ); const pageInfoContext = extractPageInfos(response); const nextCursorValue = getCursorFrom(pageInfoContext.pageInfo); nextPageExists = hasAnotherPage(pageInfoContext.pageInfo); if (nextPageExists && nextCursorValue === parameters.cursor) { throw new MissingCursorChange(pageInfoContext, nextCursorValue); } parameters = { ...parameters, cursor: nextCursorValue }; return { done: false, value: response }; } }) }; }; }; export { createIterator };