2022-01-21 08:28:41 +00:00
|
|
|
import { iterator } from "./iterator";
|
|
|
|
export function paginate(octokit, route, parameters, mapFn) {
|
|
|
|
if (typeof parameters === "function") {
|
|
|
|
mapFn = parameters;
|
|
|
|
parameters = undefined;
|
|
|
|
}
|
|
|
|
return gather(octokit, [], iterator(octokit, route, parameters)[Symbol.asyncIterator](), mapFn);
|
|
|
|
}
|
|
|
|
function gather(octokit, results, iterator, mapFn) {
|
2023-12-19 14:36:33 +00:00
|
|
|
return iterator.next().then((result) => {
|
2022-01-21 08:28:41 +00:00
|
|
|
if (result.done) {
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
let earlyExit = false;
|
|
|
|
function done() {
|
|
|
|
earlyExit = true;
|
|
|
|
}
|
|
|
|
results = results.concat(mapFn ? mapFn(result.value, done) : result.value.data);
|
|
|
|
if (earlyExit) {
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
return gather(octokit, results, iterator, mapFn);
|
|
|
|
});
|
|
|
|
}
|