From b8c43465794e88d52d9650fe4251f138ca309544 Mon Sep 17 00:00:00 2001 From: javiertury Date: Mon, 18 Jul 2022 21:33:34 +0200 Subject: [PATCH] infer json definition for all parameters Infer definition of application/json content-type for all parameters, for instance application/json; charset=utf-8 --- src/types.ts | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/types.ts b/src/types.ts index 9d51fb00..dd57e2dd 100644 --- a/src/types.ts +++ b/src/types.ts @@ -13,6 +13,18 @@ export type OpenapiPaths = { } } +type JSONBody = + | { + content: { + 'application/json': T + } + } + | { + content: { + [K in `application/json;${string}`]: T + } + }; + export type OpArgType = OP extends { parameters?: { path?: infer P @@ -22,11 +34,7 @@ export type OpArgType = OP extends { cookie?: unknown // ignore } // openapi 3 - requestBody?: { - content: { - 'application/json': infer RB - } - } + requestBody?: JSONBody } ? P & Q & (B extends Record ? B[keyof B] : unknown) & RB : Record @@ -37,7 +45,7 @@ type OpResponseTypes = OP extends { ? { [S in keyof R]: R[S] extends { schema?: infer S } // openapi 2 ? S - : R[S] extends { content: { 'application/json': infer C } } // openapi 3 + : R[S] extends JSONBody // openapi 3 ? C : S extends 'default' ? R[S]