Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | 3x 3x 3x 3x 3x 7x 6x 6x 6x 6x 2x 2x | import { EndsWithList, ObjectWithListValue } from "utils/hasAtLeastOnePage";
interface GetAllPagesParams<TData extends ObjectWithListValue<TData>, TParams> {
serviceFunction: (params: TParams) => Promise<TData>;
params: (previousData: TData | undefined) => TParams;
listKey: keyof TData & EndsWithList;
hasMore: (previousData: TData) => boolean;
maxPages?: number;
}
export default async function getAllPages<
TData extends ObjectWithListValue<TData>,
TParams,
>({
serviceFunction,
params,
listKey,
hasMore,
maxPages = 10,
}: GetAllPagesParams<TData, TParams>) {
let page = 0;
let previousData = undefined;
const nestedResults = [];
while (true) {
const data: TData = await serviceFunction(params(previousData));
nestedResults.push(data[listKey]);
previousData = data;
page++;
if (!hasMore(data) || page >= maxPages) {
break;
}
}
return nestedResults.flat(1);
}
|