From 98627826c0f1cb00b706fdb8e294a151696b0c52 Mon Sep 17 00:00:00 2001 From: SSlime-s <62363188+SSlime-s@users.noreply.github.com> Date: Sun, 9 Feb 2025 18:29:02 +0900 Subject: [PATCH] feat(swr-openapi): add custom error types to query builder --- .../swr-openapi/src/__test__/types.test-d.ts | 46 +++++++++++++++++++ packages/swr-openapi/src/infinite.ts | 12 +++-- packages/swr-openapi/src/query-base.ts | 12 +++-- 3 files changed, 60 insertions(+), 10 deletions(-) diff --git a/packages/swr-openapi/src/__test__/types.test-d.ts b/packages/swr-openapi/src/__test__/types.test-d.ts index 1ea997c36..b3a16420b 100644 --- a/packages/swr-openapi/src/__test__/types.test-d.ts +++ b/packages/swr-openapi/src/__test__/types.test-d.ts @@ -336,6 +336,52 @@ describe("types", () => { }); }); }); + + describe("custom error types", () => { + const uniqueKey = ""; + type Key = typeof uniqueKey; + const useQuery = createQueryHook(client, uniqueKey); + const useImmutable = createImmutableHook(client, uniqueKey); + const useInfinite = createInfiniteHook(client, uniqueKey); + + describe("useQuery", () => { + it("returns correct error", () => { + const { error } = useQuery("/pet/{petId}", { + params: { + path: { + petId: 5, + }, + }, + }); + + expectTypeOf(error).toEqualTypeOf(); + }); + }); + + describe("useImmutable", () => { + it("returns correct error", () => { + const { error } = useImmutable("/pet/{petId}", { + params: { + path: { + petId: 5, + }, + }, + }); + + expectTypeOf(error).toEqualTypeOf(); + }); + }); + + describe("useInfinite", () => { + it("returns correct error", () => { + const { error } = useInfinite("/pet/findByStatus", (_index, _prev) => ({ + params: { query: { status: "available" } }, + })); + + expectTypeOf(error).toEqualTypeOf(); + }); + }); + }); }); describe("TypesForRequest", () => { diff --git a/packages/swr-openapi/src/infinite.ts b/packages/swr-openapi/src/infinite.ts index 4ad7fdb88..c17584c30 100644 --- a/packages/swr-openapi/src/infinite.ts +++ b/packages/swr-openapi/src/infinite.ts @@ -33,16 +33,18 @@ import { useCallback, useDebugValue } from "react"; * }); * ``` */ -export function createInfiniteHook( - client: Client, - prefix: Prefix, -) { +export function createInfiniteHook< + Paths extends {}, + IMediaType extends MediaType, + Prefix extends string, + FetcherError = never, +>(client: Client, prefix: Prefix) { return function useInfinite< Path extends PathsWithMethod, R extends TypesForGetRequest, Init extends R["Init"], Data extends R["Data"], - Error extends R["Error"], + Error extends R["Error"] | FetcherError, Config extends SWRInfiniteConfiguration, >(path: Path, getInit: SWRInfiniteKeyLoader, config?: Config) { type Key = [Prefix, Path, Init | undefined] | null; diff --git a/packages/swr-openapi/src/query-base.ts b/packages/swr-openapi/src/query-base.ts index 234101842..bf6ff3d88 100644 --- a/packages/swr-openapi/src/query-base.ts +++ b/packages/swr-openapi/src/query-base.ts @@ -8,16 +8,18 @@ import { useCallback, useDebugValue, useMemo } from "react"; * @private */ export function configureBaseQueryHook(useHook: SWRHook) { - return function createQueryBaseHook( - client: Client, - prefix: Prefix, - ) { + return function createQueryBaseHook< + Paths extends {}, + IMediaType extends MediaType, + Prefix extends string, + FetcherError = never, + >(client: Client, prefix: Prefix) { return function useQuery< Path extends PathsWithMethod, R extends TypesForGetRequest, Init extends R["Init"], Data extends R["Data"], - Error extends R["Error"], + Error extends R["Error"] | FetcherError, Config extends R["SWRConfig"], >( path: Path,