From 7b21ba90c746070fd3e2e0041cfe9c472cfd58a8 Mon Sep 17 00:00:00 2001 From: Luc van Kampen Date: Sat, 26 Jul 2025 10:24:31 +0000 Subject: [PATCH] fix: relax path type constraints --- packages/openapi-hooks/src/index.ts | 41 ++++++++++++++++++----------- 1 file changed, 26 insertions(+), 15 deletions(-) diff --git a/packages/openapi-hooks/src/index.ts b/packages/openapi-hooks/src/index.ts index 4f394ba..85d0e76 100644 --- a/packages/openapi-hooks/src/index.ts +++ b/packages/openapi-hooks/src/index.ts @@ -1,8 +1,8 @@ -type Paths = { - [key: string]: { - [key: string]: any; - }; -} +// A lightweight description of the `paths` object shape emitted by +// `openapi-typescript`. We intentionally do not require an index signature so +// that the generated interface can be used directly. +type PathsRecord = Record>; + type HTTPMethod = | 'get' @@ -14,11 +14,15 @@ type HTTPMethod = | 'patch' | 'trace'; -export type PathMethods = { - [TMethod in HTTPMethod]: paths[TPath][TMethod] extends undefined - ? never - : TMethod; -}[HTTPMethod]; +export type PathMethods = paths[TPath] extends Record + ? { + [TMethod in HTTPMethod]: TMethod extends keyof paths[TPath] + ? paths[TPath][TMethod] extends undefined + ? never + : TMethod + : never; + }[HTTPMethod] + : never; type AnyRequestBody = { content: Record; @@ -134,7 +138,10 @@ const convertBody = ( } }; -const decodeResponse = async (response, responseContentType) => { +const decodeResponse = async ( + response: Response, + responseContentType: string | null +) => { switch (responseContentType) { // eslint-disable-next-line unicorn/no-null case null: @@ -162,7 +169,7 @@ const decodeResponse = async (response, responseContentType) => { } }; -export const createFetch = (options?: OpenApiHookOptions) => { +export const createFetch = (options?: OpenApiHookOptions) => { const { baseUrl = window.location.toString(), headers: defaultHeaders, onError } = options ?? {}; /** @@ -254,9 +261,13 @@ export const createFetch = (options?: OpenApiHookOptions) = TOptions extends TRoute['parameters'] & ApiRequestBody & { fetchOptions?: RequestInit; }, - TRoute extends AnyRoute = paths[TPath][TMethod] extends AnyRoute - ? paths[TPath][TMethod] - : never, + TRoute extends AnyRoute = paths[TPath] extends Record + ? TMethod extends keyof paths[TPath] + ? paths[TPath][TMethod] extends AnyRoute + ? paths[TPath][TMethod] + : never + : never + : never, >( path: TPath, method: TMethod,