Skip to content

Commit

Permalink
fix(types): correct pagination return type for data which is an array (
Browse files Browse the repository at this point in the history
…#662)

* fix(types): correct pagination return type for data which is an array

Instead of returning `T` in `GetPaginationKeys<T>`, we return an empty object, because T would be an `OctokitResponse<T, S>`, and there is no response data in the `data` field

Fixes #661

* test: add test for array response pagination

* style: prettier
  • Loading branch information
wolfy1339 authored Feb 24, 2025
1 parent 8b8c500 commit 9a51aad
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
20 changes: 8 additions & 12 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ import type { PaginatingEndpoints } from "./generated/paginating-endpoints.js";
// : never
// : never;

type PaginationMetadataKeys =
| "repository_selection"
| "total_count"
| "incomplete_results";

// https://stackoverflow.com/a/58980331/206879
type KnownKeys<T> = Extract<
{
Expand All @@ -36,10 +41,7 @@ type KnownKeys<T> = Extract<
? U
: never,
// Exclude keys that are known to not contain the data
Exclude<
keyof T,
"repository_selection" | "total_count" | "incomplete_results"
>
Exclude<keyof T, PaginationMetadataKeys>
>;
type KeysMatching<T, V> = {
[K in keyof T]: T[K] extends V ? K : never;
Expand All @@ -56,15 +58,9 @@ type GetResultsType<T> = T extends { data: any[] }

// Extract the pagination keys from the response object in order to return them alongside the paginated results
type GetPaginationKeys<T> = T extends { data: any[] }
? T
? {}
: T extends { data: object }
? Pick<
T["data"],
Extract<
keyof T["data"],
"repository_selection" | "total_count" | "incomplete_results"
>
>
? Pick<T["data"], Extract<keyof T["data"], PaginationMetadataKeys>>
: never;

// Ensure that the type always returns the paginated results and not a mix of paginated results and the response object
Expand Down
14 changes: 14 additions & 0 deletions test/validate-typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,20 @@ export async function requestMethodWithNamespacedResponse() {
}
}

// https://github.com/octokit/plugin-paginate-rest.js/issues/661
// Ensure the endpoints that return an array don't have an extra `OctokitResponse` in the data
type Package =
PaginatingEndpoints["GET /orgs/{org}/packages/{package_type}/{package_name}/versions"]["response"]["data"][0];
export async function requestWithArrayResponse() {
const results = await octokit.paginate(
"GET /orgs/{org}/packages/{package_type}/{package_name}/versions",
);
for (const result of results) {
const pkg: Package = result;
console.log(pkg.name);
}
}

export async function paginatingEndpointKeyProvidedByUser<
R extends keyof PaginatingEndpoints,
>(route: R) {
Expand Down

0 comments on commit 9a51aad

Please sign in to comment.