From 069718f00267e432483f25ef17af52d396085d17 Mon Sep 17 00:00:00 2001 From: mpppk Date: Mon, 16 Sep 2024 16:29:34 +0900 Subject: [PATCH] =?UTF-8?q?headers=E3=82=84body=E3=81=8C=E5=85=A8=E3=81=A6?= =?UTF-8?q?optional=E3=81=AE=E5=A0=B4=E5=90=88=E3=81=AF=E7=9C=81=E7=95=A5?= =?UTF-8?q?=E3=81=A7=E3=81=8D=E3=82=8B=E3=82=88=E3=81=86=E3=81=AB=E3=81=99?= =?UTF-8?q?=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/fetch/index.t-test.ts | 15 ++++++++++++++- src/fetch/index.ts | 27 +++++++++++++-------------- 2 files changed, 27 insertions(+), 15 deletions(-) diff --git a/src/fetch/index.t-test.ts b/src/fetch/index.t-test.ts index ec028ab..99936b5 100644 --- a/src/fetch/index.t-test.ts +++ b/src/fetch/index.t-test.ts @@ -143,6 +143,19 @@ const JSONT = JSON as JSONT; })(); } +{ + type Spec = DefineApiEndpoints<{ + "/users": { + post: { responses: { 200: { body: { prop: string } } } }; + }; + }>; + (async () => { + const f = fetch as FetchT<"", Spec>; + // TODO: getが定義されていない場合、methodを省略したらエラーになってほしいが今はならない + await f(`/users`, {}); + })(); +} + { type Spec = DefineApiEndpoints<{ "/users": { @@ -156,7 +169,7 @@ const JSONT = JSON as JSONT; (async () => { const f = fetch as FetchT<"", Spec>; // headers and body can be omitted because they are optional - await f(`/users`, {}); + await f(`/users`, { method: "post" }); })(); } diff --git a/src/fetch/index.ts b/src/fetch/index.ts index 0f57e75..c54e2b5 100644 --- a/src/fetch/index.ts +++ b/src/fetch/index.ts @@ -4,7 +4,6 @@ import { ApiP, AnyApiResponses, CaseInsensitiveMethod, - FilterNever, MatchedPatterns, MergeApiResponseBodies, Method, @@ -25,19 +24,19 @@ export type RequestInitT< HeadersObj extends Record | undefined, > = Omit & { method?: InputMethod; -} & FilterNever<{ - // eslint-disable-next-line @typescript-eslint/no-explicit-any - body: Body extends Record - ? IsAllOptional extends true - ? Body | TypedString | undefined - : TypedString - : never; - headers: HeadersObj extends Record - ? IsAllOptional extends true - ? HeadersObj | Headers | undefined - : HeadersObj | Headers - : never; - }>; + // eslint-disable-next-line @typescript-eslint/no-explicit-any +} & (Body extends Record + ? IsAllOptional extends true + ? { body?: Body | TypedString } + : { body: TypedString } + : // eslint-disable-next-line @typescript-eslint/ban-types + {}) & + (HeadersObj extends Record + ? IsAllOptional extends true + ? { headers?: HeadersObj | Headers } + : { headers: HeadersObj | Headers } + : // eslint-disable-next-line @typescript-eslint/ban-types + {}); /** * FetchT is a type for window.fetch like function but more strict type information