feat(github): add paginate-all utility

This commit is contained in:
Ali BARIN
2022-10-18 20:55:07 +02:00
parent cd795a55d3
commit 0b8b5aeebd

View File

@@ -0,0 +1,36 @@
import { IGlobalVariable, IJSONObject } from "@automatisch/types";
import type { AxiosResponse } from 'axios';
import parseLinkHeader from '../../../helpers/parse-header-link';
type TResponse = {
data: IJSONObject[],
error?: IJSONObject,
}
export default async function paginateAll($: IGlobalVariable, request: Promise<AxiosResponse>) {
const response = await request;
const aggregatedResponse: TResponse = {
data: [...response.data],
};
let links = parseLinkHeader(response.headers.link);
while (links.next) {
const nextPageResponse = await $.http.request({
...response.config,
url: links.next.uri,
});
if (nextPageResponse.integrationError) {
aggregatedResponse.error = nextPageResponse.integrationError;
links = null;
} else {
aggregatedResponse.data.push(...nextPageResponse.data);
links = parseLinkHeader(nextPageResponse.headers.link);
}
}
return aggregatedResponse;
}