Skip to content

Commit

Permalink
feat: backport improvements from @octokit-next (#595)
Browse files Browse the repository at this point in the history
Backports some improvements from `@octokit-next`:

- Simplify (flatten) the parameters
- Add type support for 205 HTTP status code
- Add types for the `data` parameter
  • Loading branch information
wolfy1339 authored Nov 12, 2023
1 parent 7269dcc commit 0eebf54
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 34 deletions.
44 changes: 27 additions & 17 deletions scripts/update-endpoints/templates/endpoints.ts.template
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ import type { OctokitResponse } from "../OctokitResponse";
import type { RequestHeaders } from "../RequestHeaders";
import type { RequestRequestOptions } from "../RequestRequestOptions";

/**
* @license (MIT OR CC0-1.0)
* @source https://github.com/sindresorhus/type-fest/blob/570e27f8fdaee37ef5d5e0fbf241e0212ff8fc1a/source/simplify.d.ts
*/
export type Simplify<T> = {[KeyType in keyof T]: T[KeyType]} & {};

// https://stackoverflow.com/a/50375286/206879
type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (
k: infer I
Expand Down Expand Up @@ -40,30 +46,34 @@ type ExtractRequestBody<T> = "requestBody" extends keyof T
type ToOctokitParameters<T> = ExtractParameters<T> & ExtractRequestBody<Required<T>>;

type Operation<Url extends keyof paths, Method extends keyof paths[Url]> = {
parameters: ToOctokitParameters<paths[Url][Method]>;
request: {
method: Method extends keyof MethodsMap ? MethodsMap[Method] : never;
url: Url;
headers: RequestHeaders;
request: RequestRequestOptions;
};
parameters: Simplify<ToOctokitParameters<paths[Url][Method]>>;
request: Method extends ReadOnlyMethods
? {
method: Method extends string ? Uppercase<Method> : never;
url: Url;
headers: RequestHeaders;
request: RequestRequestOptions;
}
: {
method: Method extends string ? Uppercase<Method> : never;
url: Url;
headers: RequestHeaders;
request: RequestRequestOptions;
data: ExtractRequestBody<paths[Url][Method]>;
};
response: ExtractOctokitResponse<paths[Url][Method]>;
};

type MethodsMap = {
delete: "DELETE";
get: "GET";
patch: "PATCH";
post: "POST";
put: "PUT";
};
type SuccessStatuses = 200 | 201 | 202 | 204;
type ReadOnlyMethods = "get" | "head";
type SuccessStatuses = 200 | 201 | 202 | 204 | 205;
type RedirectStatuses = 301 | 302;
type EmptyResponseStatuses = 201 | 204;
type EmptyResponseStatuses = 201 | 204 | 205;
type KnownJsonResponseTypes =
| "application/json"
| "application/octocat-stream" // GET /octocat
| "application/scim+json"
| "text/html";
| "text/html"
| "text/plain"; // GET /zen

type SuccessResponseDataType<Responses> = {
[K in SuccessStatuses & keyof Responses]: GetContentKeyIfPresent<
Expand Down
44 changes: 27 additions & 17 deletions src/generated/Endpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ import type { OctokitResponse } from "../OctokitResponse";
import type { RequestHeaders } from "../RequestHeaders";
import type { RequestRequestOptions } from "../RequestRequestOptions";

/**
* @license (MIT OR CC0-1.0)
* @source https://github.com/sindresorhus/type-fest/blob/570e27f8fdaee37ef5d5e0fbf241e0212ff8fc1a/source/simplify.d.ts
*/
export type Simplify<T> = { [KeyType in keyof T]: T[KeyType] } & {};

// https://stackoverflow.com/a/50375286/206879
type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (
k: infer I,
Expand Down Expand Up @@ -41,30 +47,34 @@ type ToOctokitParameters<T> = ExtractParameters<T> &
ExtractRequestBody<Required<T>>;

type Operation<Url extends keyof paths, Method extends keyof paths[Url]> = {
parameters: ToOctokitParameters<paths[Url][Method]>;
request: {
method: Method extends keyof MethodsMap ? MethodsMap[Method] : never;
url: Url;
headers: RequestHeaders;
request: RequestRequestOptions;
};
parameters: Simplify<ToOctokitParameters<paths[Url][Method]>>;
request: Method extends ReadOnlyMethods
? {
method: Method extends string ? Uppercase<Method> : never;
url: Url;
headers: RequestHeaders;
request: RequestRequestOptions;
}
: {
method: Method extends string ? Uppercase<Method> : never;
url: Url;
headers: RequestHeaders;
request: RequestRequestOptions;
data: ExtractRequestBody<paths[Url][Method]>;
};
response: ExtractOctokitResponse<paths[Url][Method]>;
};

type MethodsMap = {
delete: "DELETE";
get: "GET";
patch: "PATCH";
post: "POST";
put: "PUT";
};
type SuccessStatuses = 200 | 201 | 202 | 204;
type ReadOnlyMethods = "get" | "head";
type SuccessStatuses = 200 | 201 | 202 | 204 | 205;
type RedirectStatuses = 301 | 302;
type EmptyResponseStatuses = 201 | 204;
type EmptyResponseStatuses = 201 | 204 | 205;
type KnownJsonResponseTypes =
| "application/json"
| "application/octocat-stream" // GET /octocat
| "application/scim+json"
| "text/html";
| "text/html"
| "text/plain"; // GET /zen

type SuccessResponseDataType<Responses> = {
[K in SuccessStatuses & keyof Responses]: GetContentKeyIfPresent<
Expand Down

0 comments on commit 0eebf54

Please sign in to comment.