2
2
3
3
// HTTP types
4
4
5
- export type HttpMethod = "get" | "put" | "post" | "delete" | "options" | "head" | "patch" | "trace" ;
5
+ export type HttpMethod =
6
+ | "get"
7
+ | "put"
8
+ | "post"
9
+ | "delete"
10
+ | "options"
11
+ | "head"
12
+ | "patch"
13
+ | "trace" ;
6
14
/** 2XX statuses */
7
15
export type OkStatus = 200 | 201 | 202 | 203 | 204 | 206 | 207 | "2XX" ;
8
16
// prettier-ignore
@@ -12,8 +20,15 @@ export type ErrorStatus = 500 | 501 | 502 | 503 | 504 | 505 | 506 | 507 | 508 |
12
20
// OpenAPI type helpers
13
21
14
22
/** Given an OpenAPI **Paths Object**, find all paths that have the given method */
15
- export type PathsWithMethod < Paths extends Record < string , PathItemObject > , PathnameMethod extends HttpMethod > = {
16
- [ Pathname in keyof Paths ] : Paths [ Pathname ] extends { [ K in PathnameMethod ] : any } ? Pathname : never ;
23
+ export type PathsWithMethod <
24
+ Paths extends Record < string , PathItemObject > ,
25
+ PathnameMethod extends HttpMethod ,
26
+ > = {
27
+ [ Pathname in keyof Paths ] : Paths [ Pathname ] extends {
28
+ [ K in PathnameMethod ] : any ;
29
+ }
30
+ ? Pathname
31
+ : never ;
17
32
} [ keyof Paths ] ;
18
33
/** DO NOT USE! Only used only for OperationObject type inference */
19
34
export interface OperationObject {
@@ -23,28 +38,49 @@ export interface OperationObject {
23
38
responses : any ;
24
39
}
25
40
/** Internal helper used in PathsWithMethod */
26
- export type PathItemObject = { [ M in HttpMethod ] : OperationObject } & { parameters ?: any } ;
41
+ export type PathItemObject = {
42
+ [ M in HttpMethod ] : OperationObject ;
43
+ } & { parameters ?: any } ;
27
44
/** Return `responses` for an Operation Object */
28
- export type ResponseObjectMap < T > = T extends { responses : any } ? T [ "responses" ] : unknown ;
45
+ export type ResponseObjectMap < T > = T extends { responses : any }
46
+ ? T [ "responses" ]
47
+ : unknown ;
29
48
/** Return `content` for a Response Object */
30
- export type ResponseContent < T > = T extends { content : any } ? T [ "content" ] : unknown ;
49
+ export type ResponseContent < T > = T extends { content : any }
50
+ ? T [ "content" ]
51
+ : unknown ;
31
52
/** Return `requestBody` for an Operation Object */
32
- export type OperationRequestBody < T > = T extends { requestBody ?: any } ? T [ "requestBody" ] : never ;
53
+ export type OperationRequestBody < T > = T extends { requestBody ?: any }
54
+ ? T [ "requestBody" ]
55
+ : never ;
33
56
/** Internal helper used in OperationRequestBodyContent */
34
- export type OperationRequestBodyMediaContent < T > = undefined extends OperationRequestBody < T > ? FilterKeys < NonNullable < OperationRequestBody < T > > , "content" > | undefined : FilterKeys < OperationRequestBody < T > , "content" > ;
57
+ export type OperationRequestBodyMediaContent < T > =
58
+ undefined extends OperationRequestBody < T >
59
+ ? FilterKeys < NonNullable < OperationRequestBody < T > > , "content" > | undefined
60
+ : FilterKeys < OperationRequestBody < T > , "content" > ;
35
61
/** Return first `content` from a Request Object Mapping, allowing any media type */
36
- export type OperationRequestBodyContent < T > = FilterKeys < OperationRequestBodyMediaContent < T > , MediaType > extends never
37
- ? FilterKeys < NonNullable < OperationRequestBodyMediaContent < T > > , MediaType > | undefined
62
+ export type OperationRequestBodyContent < T > = FilterKeys <
63
+ OperationRequestBodyMediaContent < T > ,
64
+ MediaType
65
+ > extends never
66
+ ?
67
+ | FilterKeys < NonNullable < OperationRequestBodyMediaContent < T > > , MediaType >
68
+ | undefined
38
69
: FilterKeys < OperationRequestBodyMediaContent < T > , MediaType > ;
39
70
/** Return first 2XX response from a Response Object Map */
40
71
export type SuccessResponse < T > = FilterKeys < FilterKeys < T , OkStatus > , "content" > ;
41
72
/** Return first 5XX or 4XX response (in that order) from a Response Object Map */
42
- export type ErrorResponse < T > = FilterKeys < FilterKeys < T , ErrorStatus > , "content" > ;
73
+ export type ErrorResponse < T > = FilterKeys <
74
+ FilterKeys < T , ErrorStatus > ,
75
+ "content"
76
+ > ;
43
77
44
78
// Generic TS utils
45
79
46
80
/** Find first match of multiple keys */
47
- export type FilterKeys < Obj , Matchers > = { [ K in keyof Obj ] : K extends Matchers ? Obj [ K ] : never } [ keyof Obj ] ;
81
+ export type FilterKeys < Obj , Matchers > = {
82
+ [ K in keyof Obj ] : K extends Matchers ? Obj [ K ] : never ;
83
+ } [ keyof Obj ] ;
48
84
/** Return any `[string]/[string]` media type (important because openapi-fetch allows any content response, not just JSON-like) */
49
85
export type MediaType = `${string } /${string } `;
50
86
/** Filter objects that have required keys */
0 commit comments