-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathendpoints.ts.template
119 lines (111 loc) · 3.98 KB
/
endpoints.ts.template
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// DO NOT EDIT THIS FILE
import type { paths } from "@octokit/openapi-types";
import type { OctokitResponse } from "../OctokitResponse.js";
import type { RequestHeaders } from "../RequestHeaders.js";
import type { RequestRequestOptions } from "../RequestRequestOptions.js";
/**
* @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
) => void
? I
: never;
type ExtractParameters<T> = "parameters" extends keyof T
? UnionToIntersection<
{
// query parameter might be optional
// https://github.com/octokit/types.ts/pull/555#issuecomment-1585105522
[K in keyof T["parameters"]]-?: T["parameters"][K];
}[keyof T["parameters"]]
>
: {};
type ExtractRequestBody<T> = "requestBody" extends keyof T
? "content" extends keyof T["requestBody"]
? "application/json" extends keyof T["requestBody"]["content"]
? T["requestBody"]["content"]["application/json"]
: {
data: {
[K in keyof T["requestBody"]["content"]]: T["requestBody"]["content"][K];
}[keyof T["requestBody"]["content"]];
}
: "application/json" extends keyof T["requestBody"]
? T["requestBody"]["application/json"]
: {
data: {
[K in keyof T["requestBody"]]: T["requestBody"][K];
}[keyof T["requestBody"]];
}
: {};
type ToOctokitParameters<T> = ExtractParameters<T> & ExtractRequestBody<Required<T>>;
type Operation<Url extends keyof paths, Method extends keyof paths[Url]> = {
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 ReadOnlyMethods = "get" | "head";
type SuccessStatuses = 200 | 201 | 202 | 204 | 205;
type RedirectStatuses = 301 | 302;
type EmptyResponseStatuses = 201 | 204 | 205;
type KnownJsonResponseTypes =
| "application/json"
| "application/octocat-stream" // GET /octocat
| "application/scim+json"
| "text/html"
| "text/plain"; // GET /zen
type SuccessResponseDataType<Responses> = {
[K in SuccessStatuses & keyof Responses]: GetContentKeyIfPresent<
Responses[K]
> extends never
? never
: OctokitResponse<GetContentKeyIfPresent<Responses[K]>, K>;
}[SuccessStatuses & keyof Responses];
type RedirectResponseDataType<Responses> = {
[K in RedirectStatuses & keyof Responses]: OctokitResponse<unknown, K>;
}[RedirectStatuses & keyof Responses];
type EmptyResponseDataType<Responses> = {
[K in EmptyResponseStatuses & keyof Responses]: OctokitResponse<never, K>;
}[EmptyResponseStatuses & keyof Responses];
type GetContentKeyIfPresent<T> = "content" extends keyof T
? DataType<T["content"]>
: DataType<T>;
type DataType<T> = {
[K in KnownJsonResponseTypes & keyof T]: T[K];
}[KnownJsonResponseTypes & keyof T];
type ExtractOctokitResponse<R> = "responses" extends keyof R
? SuccessResponseDataType<R["responses"]> extends never
? RedirectResponseDataType<R["responses"]> extends never
? EmptyResponseDataType<R["responses"]>
: RedirectResponseDataType<R["responses"]>
: SuccessResponseDataType<R["responses"]>
: unknown;
export interface Endpoints {
{{#each endpointsByRoute}}
/**
* @see {{documentationUrl}}
{{#deprecated}}
* @deprecated {{{.}}}
{{/deprecated}}
*/
"{{@key}}": Operation<
"{{url}}",
"{{method}}"
>,
{{/each}}
}