From 6d286227f76f0be3b647fdd663b85aca5cf34a04 Mon Sep 17 00:00:00 2001 From: mpppk Date: Wed, 12 Jun 2024 23:30:28 +0900 Subject: [PATCH] =?UTF-8?q?fetch=E3=81=AEmethod=E3=81=AB=E5=A4=A7=E6=96=87?= =?UTF-8?q?=E5=AD=97=E3=81=A7=E3=82=82=E6=B8=A1=E3=81=9B=E3=82=8B=E3=82=88?= =?UTF-8?q?=E3=81=86=E3=81=AB=E3=81=97=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/express/fetch.ts | 5 ++++- src/common/spec.ts | 1 + src/fetch/index.ts | 10 ++++++---- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/examples/express/fetch.ts b/examples/express/fetch.ts index feae716..006c42e 100644 --- a/examples/express/fetch.ts +++ b/examples/express/fetch.ts @@ -31,7 +31,10 @@ const main = async () => { unreachable(res); } } - + { + // case-insensitive method example + await fetchT(`${origin}/users`, { method: "GET" }); + } { // query parameter example // TODO: Add common information for query parameter diff --git a/src/common/spec.ts b/src/common/spec.ts index 3ce074e..7c512b7 100644 --- a/src/common/spec.ts +++ b/src/common/spec.ts @@ -11,6 +11,7 @@ export const Method = [ "head", ] as const; export type Method = (typeof Method)[number]; +export type CaseInsensitiveMethod = Method | Uppercase; export type ApiEndpoint = Partial< Record>> diff --git a/src/fetch/index.ts b/src/fetch/index.ts index 711ffd1..5d1f5ed 100644 --- a/src/fetch/index.ts +++ b/src/fetch/index.ts @@ -1,6 +1,7 @@ import { ApiBodySchema, ApiEndpoints, + CaseInsensitiveMethod, MergeApiResponses, Method, NormalizePath, @@ -15,11 +16,11 @@ import { import { TypedString } from "../json"; export interface RequestInitT< - M extends Method, + InputMethod extends CaseInsensitiveMethod, // eslint-disable-next-line @typescript-eslint/no-explicit-any Body extends Record | undefined, > extends RequestInit { - method?: M; + method?: InputMethod; body?: TypedString; } @@ -36,10 +37,11 @@ type FetchT = < "" >, CandidatePaths extends MatchedPatterns, - M extends Method = "get", + InputMethod extends CaseInsensitiveMethod = "get", + M extends Method = Lowercase, >( input: Input, - init?: RequestInitT>, + init?: RequestInitT>, // FIXME: NonNullable ) => Promise["res"]>>;