Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add return types for request.parseSuccessResponseBody #608

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
41 changes: 40 additions & 1 deletion src/RequestInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,15 @@ import type { OctokitResponse } from "./OctokitResponse";
import type { RequestParameters } from "./RequestParameters";
import type { Route } from "./Route";

import type { Endpoints } from "./generated/Endpoints";
import type { Endpoints, Simplify } from "./generated/Endpoints";

/**
* Parameters that can be passed into `request(route, parameters)` or `endpoint(route, parameters)` methods.
* This type represents the options when `request.parseSuccessResponseBody` is set to `false`.
*/
type StreamBodyOption = Simplify<
RequestParameters & { request: { parseSuccessResponseBody: false } }
>;
export interface RequestInterface<D extends object = object> {
/**
* Sends a request based on endpoint options
Expand All @@ -17,6 +24,38 @@ export interface RequestInterface<D extends object = object> {
: { url: string }),
): Promise<OctokitResponse<T>>;

/**
* Sends a request based on endpoint options
*
* @param {object} endpoint Must set `method` and `url`. Plus URL, query or body parameters, as well as `headers`, `mediaType.{format|previews}`, `request`, or `baseUrl`.
*/
<T = any, O extends StreamBodyOption = StreamBodyOption>(
options: O & { method?: string } & ("url" extends keyof D
? { url?: string }
: { url: string }),
): Promise<OctokitResponse<ReadableStream<T>>>;

/**
* Sends a request based on endpoint options
*
* @param {string} route Request method + URL. Example: `'GET /orgs/{org}'`
* @param {object} [parameters] URL, query or body parameters, as well as `headers`, `mediaType.{format|previews}`, `request`, or `baseUrl`.
*/
<R extends Route, O extends StreamBodyOption>(
route: keyof Endpoints | R,
options?: R extends keyof Endpoints
? Endpoints[R]["parameters"] & O
: StreamBodyOption,
): R extends keyof Endpoints
? Promise<
OctokitResponse<
ReadableStream<
Exclude<Endpoints[R]["response"], keyof OctokitResponse<any>>
>
>
>
: Promise<OctokitResponse<ReadableStream<any>>>;

/**
* Sends a request based on endpoint options
*
Expand Down
3 changes: 0 additions & 3 deletions test.ts
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tests fail for the assertPaginate test

Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,7 @@
const listForRef = {} as {
(
params?: RequestParameters &
Omit<
Endpoints["GET /repos/{owner}/{repo}/commits/{ref}/check-runs"]["parameters"],
"baseUrl" | "headers" | "mediaType"
>
): Promise<Endpoints[typeof checkRunsRoute]["response"]>;
defaults: RequestInterface["defaults"];
endpoint: EndpointInterface<{
Expand All @@ -66,4 +63,4 @@
};

// octokit.paginate can take `request` method. ref: https://github.com/octokit/plugin-paginate-rest.js/blob/b3fb11e301f9658554e110aeebbd7cbb89b8aad4/README.md?plain=1#L117-L126
assertPaginate(listForRef);

Check failure on line 66 in test.ts

View workflow job for this annotation

GitHub Actions / test

No overload matches this call.
Loading