2021-11-26 20:57:51 +08:00
|
|
|
/**
|
|
|
|
* Some “list” response that can be paginated have a different response structure
|
|
|
|
*
|
|
|
|
* They have a `total_count` key in the response (search also has `incomplete_results`,
|
|
|
|
* /installation/repositories also has `repository_selection`), as well as a key with
|
|
|
|
* the list of the items which name varies from endpoint to endpoint.
|
|
|
|
*
|
|
|
|
* Octokit normalizes these responses so that paginated results are always returned following
|
|
|
|
* the same structure. One challenge is that if the list response has only one page, no Link
|
|
|
|
* header is provided, so this header alone is not sufficient to check wether a response is
|
|
|
|
* paginated or not.
|
|
|
|
*
|
|
|
|
* We check if a "total_count" key is present in the response data, but also make sure that
|
|
|
|
* a "url" property is not, as the "Get the combined status for a specific ref" endpoint would
|
|
|
|
* otherwise match: https://developer.github.com/v3/repos/statuses/#get-the-combined-status-for-a-specific-ref
|
|
|
|
*/
|
|
|
|
export function normalizePaginatedListResponse(response) {
|
2022-04-04 21:27:01 +08:00
|
|
|
// endpoints can respond with 204 if repository is empty
|
|
|
|
if (!response.data) {
|
|
|
|
return {
|
|
|
|
...response,
|
|
|
|
data: [],
|
|
|
|
};
|
|
|
|
}
|
2021-11-26 20:57:51 +08:00
|
|
|
const responseNeedsNormalization = "total_count" in response.data && !("url" in response.data);
|
|
|
|
if (!responseNeedsNormalization)
|
|
|
|
return response;
|
|
|
|
// keep the additional properties intact as there is currently no other way
|
|
|
|
// to retrieve the same information.
|
|
|
|
const incompleteResults = response.data.incomplete_results;
|
|
|
|
const repositorySelection = response.data.repository_selection;
|
|
|
|
const totalCount = response.data.total_count;
|
|
|
|
delete response.data.incomplete_results;
|
|
|
|
delete response.data.repository_selection;
|
|
|
|
delete response.data.total_count;
|
|
|
|
const namespaceKey = Object.keys(response.data)[0];
|
|
|
|
const data = response.data[namespaceKey];
|
|
|
|
response.data = data;
|
|
|
|
if (typeof incompleteResults !== "undefined") {
|
|
|
|
response.data.incomplete_results = incompleteResults;
|
|
|
|
}
|
|
|
|
if (typeof repositorySelection !== "undefined") {
|
|
|
|
response.data.repository_selection = repositorySelection;
|
|
|
|
}
|
|
|
|
response.data.total_count = totalCount;
|
|
|
|
return response;
|
|
|
|
}
|