35 lines
1.1 KiB
JavaScript
35 lines
1.1 KiB
JavaScript
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
|
|
};
|