Skip to content

Commit 2be6e6f

Browse files
javierturyRendez
authored andcommitted
infer json definition for all parameters
Infer definition of application/json content-type for all parameters, for instance application/json; charset=utf-8
1 parent 2e0d66e commit 2be6e6f

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

src/types.ts

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,18 @@ export type OpenapiPaths<Paths> = {
1313
}
1414
}
1515

16+
type JSONBody <T> =
17+
| {
18+
content: {
19+
'application/json': T
20+
}
21+
}
22+
| {
23+
content: {
24+
[K in `application/json;${string}`]: T
25+
}
26+
}
27+
1628
export type OpArgType<OP> = OP extends {
1729
parameters?: {
1830
path?: infer P
@@ -23,9 +35,14 @@ export type OpArgType<OP> = OP extends {
2335
}
2436
// openapi 3
2537
requestBody?: {
26-
content: {
27-
'application/json': infer RB
28-
}
38+
content:
39+
| {
40+
'application/json': infer RB
41+
}
42+
| {
43+
[K in `application/json;${string}`]: infer RB
44+
}
45+
| { 'multipart/form-data': infer FD }
2946
}
3047
}
3148
? P & Q & (B extends Record<string, unknown> ? B[keyof B] : unknown) & RB
@@ -37,7 +54,7 @@ type OpResponseTypes<OP> = OP extends {
3754
? {
3855
[S in keyof R]: R[S] extends { schema?: infer S } // openapi 2
3956
? S
40-
: R[S] extends { content: { 'application/json': infer C } } // openapi 3
57+
: R[S] extends JSONBody<infer C> // openapi 3
4158
? C
4259
: S extends 'default'
4360
? R[S]

0 commit comments

Comments
 (0)