Skip to content

Commit 4738d99

Browse files
committed
openapi3 support
1 parent a384d9d commit 4738d99

File tree

6 files changed

+31553
-33
lines changed

6 files changed

+31553
-33
lines changed

src/fetcher.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,12 @@ function getHeaders(init?: HeadersInit) {
8282
return headers
8383
}
8484

85+
function getBody(method: Method, payload: Record<string, unknown>) {
86+
const body = sendBody(method) ? JSON.stringify(payload) : undefined
87+
// if delete don't send body if empty
88+
return method === 'delete' && body === '{}' ? undefined : body
89+
}
90+
8591
function mergeRequestInit(
8692
first?: RequestInit,
8793
second?: RequestInit,
@@ -110,7 +116,7 @@ function getFetchParams(request: Request) {
110116
...request.init,
111117
method: request.method.toUpperCase(),
112118
headers,
113-
body: sendBody(request.method) ? JSON.stringify(payload) : undefined,
119+
body: getBody(request.method, payload),
114120
}
115121

116122
return { url, init }

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@ import type {
66
FetchReturnType,
77
Middleware,
88
OpArgType,
9+
OpDefaultReturnType,
910
OpReturnType,
1011
} from './types'
1112

1213
import { ApiError } from './types'
1314

1415
export type {
1516
OpArgType,
17+
OpDefaultReturnType,
1618
OpReturnType,
1719
FetchArgType,
1820
FetchReturnType,

src/types.ts

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,34 +14,47 @@ export type OpenapiPaths<Paths> = {
1414
}
1515

1616
export type OpArgType<OP> = OP extends {
17-
parameters: {
17+
parameters?: {
1818
path?: infer P
1919
query?: infer Q
2020
body?: infer B
2121
}
22+
// openapi 3
23+
requestBody?: {
24+
content: {
25+
'application/json': infer RB
26+
}
27+
}
2228
}
23-
? P & Q & (B extends Record<string, unknown> ? B[keyof B] : unknown)
29+
? P & Q & (B extends Record<string, unknown> ? B[keyof B] : unknown) & RB
2430
: Record<string, never>
2531

26-
// openapi 2 response type
27-
export type OpReturnType<OP> = OP extends {
32+
export type OpDefaultReturnType<OP> = OP extends {
2833
responses: {
29-
200: {
30-
schema: infer R
31-
}
34+
default: infer D
3235
}
3336
}
34-
? R
35-
: OP extends {
36-
responses: {
37-
default: infer D
38-
}
39-
}
4037
? D extends { schema: infer S }
4138
? S
39+
: D extends { content: { 'application/json': infer C } } // openapi 3
40+
? C
4241
: D
4342
: unknown
4443

44+
export type OpReturnType<OP> = OP extends {
45+
responses: {
46+
200: {
47+
schema?: infer R
48+
// openapi 3
49+
content?: {
50+
'application/json': infer C
51+
}
52+
}
53+
}
54+
}
55+
? R & C
56+
: OpDefaultReturnType<OP>
57+
4558
export type CustomRequestInit = Omit<RequestInit, 'headers'> & {
4659
readonly headers: Headers
4760
}
@@ -64,7 +77,7 @@ type _CreateFetch<OP, Q = never> = [Q] extends [never]
6477
? () => TypedFetch<OpReturnType<OP>, OpArgType<OP>>
6578
: (query: Q) => TypedFetch<OpReturnType<OP>, OpArgType<OP>>
6679

67-
export type CreateFetch<M, OP> = M extends 'post' | 'put' | 'patch'
80+
export type CreateFetch<M, OP> = M extends 'post' | 'put' | 'patch' | 'delete'
6881
? OP extends { parameters: { query: infer Q } }
6982
? _CreateFetch<OP, { [K in keyof Q]: true | 1 }>
7083
: _CreateFetch<OP>

0 commit comments

Comments
 (0)