From 09c2ee5d42b1ddcce8160228274f2ce18d979f88 Mon Sep 17 00:00:00 2001 From: "Gianmarco Rengucci (freshgiammi)" Date: Thu, 10 Oct 2024 17:04:42 +0200 Subject: [PATCH 1/2] add MethodResponse for openapi-react-query --- packages/openapi-react-query/src/index.ts | 13 ++++++++++++- packages/openapi-react-query/test/index.test.tsx | 3 ++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/packages/openapi-react-query/src/index.ts b/packages/openapi-react-query/src/index.ts index 95b965472..a897ec4ba 100644 --- a/packages/openapi-react-query/src/index.ts +++ b/packages/openapi-react-query/src/index.ts @@ -11,7 +11,7 @@ import { useQuery, useSuspenseQuery, } from "@tanstack/react-query"; -import type { ClientMethod, FetchResponse, MaybeOptionalInit, Client as FetchClient } from "openapi-fetch"; +import type { ClientMethod, FetchResponse, MaybeOptionalInit, Client as FetchClient, ClientPathsWithMethod } from "openapi-fetch"; import type { HttpMethod, MediaType, PathsWithMethod, RequiredKeysOf } from "openapi-typescript-helpers"; type InitWithUnknowns = Init & { [key: string]: unknown }; @@ -93,6 +93,16 @@ export interface OpenapiQueryClient; } +export type MethodResponse< + CreatedClient extends OpenapiQueryClient, + Method extends HttpMethod, + Path extends ClientPathsWithMethod, Method>, + Options = {} +> = + CreatedClient extends OpenapiQueryClient + ? NonNullable["data"]> + : never + // TODO: Add the ability to bring queryClient as argument export default function createClient( client: FetchClient, @@ -141,3 +151,4 @@ export default function createClient { const { data, error } = result.current; + expectTypeOf(data).toEqualTypeOf | undefined>(); expectTypeOf(data).toEqualTypeOf(); expectTypeOf(error).toEqualTypeOf<{ code: number; message: string } | null>(); }); From 624a335f3e7ad788ba2f9c88dccce2573a97e7f9 Mon Sep 17 00:00:00 2001 From: "Gianmarco Rengucci (freshgiammi)" Date: Mon, 14 Oct 2024 11:02:22 +0200 Subject: [PATCH 2/2] improve Path param construction --- packages/openapi-react-query/src/index.ts | 16 ++++++++-------- packages/openapi-react-query/test/index.test.tsx | 2 +- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/packages/openapi-react-query/src/index.ts b/packages/openapi-react-query/src/index.ts index a897ec4ba..d89d8f6c0 100644 --- a/packages/openapi-react-query/src/index.ts +++ b/packages/openapi-react-query/src/index.ts @@ -11,7 +11,7 @@ import { useQuery, useSuspenseQuery, } from "@tanstack/react-query"; -import type { ClientMethod, FetchResponse, MaybeOptionalInit, Client as FetchClient, ClientPathsWithMethod } from "openapi-fetch"; +import type { ClientMethod, FetchResponse, MaybeOptionalInit, Client as FetchClient } from "openapi-fetch"; import type { HttpMethod, MediaType, PathsWithMethod, RequiredKeysOf } from "openapi-typescript-helpers"; type InitWithUnknowns = Init & { [key: string]: unknown }; @@ -96,12 +96,13 @@ export interface OpenapiQueryClient, Method extends HttpMethod, - Path extends ClientPathsWithMethod, Method>, - Options = {} -> = - CreatedClient extends OpenapiQueryClient - ? NonNullable["data"]> - : never + Path extends CreatedClient extends OpenapiQueryClient + ? PathsWithMethod + : never, + Options = object, +> = CreatedClient extends OpenapiQueryClient + ? NonNullable["data"]> + : never; // TODO: Add the ability to bring queryClient as argument export default function createClient( @@ -151,4 +152,3 @@ export default function createClient { const { data, error } = result.current; - expectTypeOf(data).toEqualTypeOf | undefined>(); + expectTypeOf(data).toEqualTypeOf | undefined>(); expectTypeOf(data).toEqualTypeOf(); expectTypeOf(error).toEqualTypeOf<{ code: number; message: string } | null>(); });