Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 8 additions & 12 deletions 20 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 14 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
Morty Proxy This is a proxified and sanitized view of the page, visit original site.