feat(github): add paginate-all utility
This commit is contained in:
36
packages/backend/src/apps/github/common/paginate-all.ts
Normal file
36
packages/backend/src/apps/github/common/paginate-all.ts
Normal 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;
|
||||||
|
}
|
Reference in New Issue
Block a user