Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(swr-openapi): add custom error types to query builder #2147

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions packages/swr-openapi/src/__test__/types.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,52 @@ describe("types", () => {
});
});
});

describe("custom error types", () => {
const uniqueKey = "<unique-key>";
type Key = typeof uniqueKey;
const useQuery = createQueryHook<paths, `${string}/${string}`, Key, Error>(client, uniqueKey);
const useImmutable = createImmutableHook<paths, `${string}/${string}`, Key, Error>(client, uniqueKey);
const useInfinite = createInfiniteHook<paths, `${string}/${string}`, Key, Error>(client, uniqueKey);

describe("useQuery", () => {
it("returns correct error", () => {
const { error } = useQuery("/pet/{petId}", {
params: {
path: {
petId: 5,
},
},
});

expectTypeOf(error).toEqualTypeOf<PetInvalid | Error | undefined>();
});
});

describe("useImmutable", () => {
it("returns correct error", () => {
const { error } = useImmutable("/pet/{petId}", {
params: {
path: {
petId: 5,
},
},
});

expectTypeOf(error).toEqualTypeOf<PetInvalid | Error | undefined>();
});
});

describe("useInfinite", () => {
it("returns correct error", () => {
const { error } = useInfinite("/pet/findByStatus", (_index, _prev) => ({
params: { query: { status: "available" } },
}));

expectTypeOf(error).toEqualTypeOf<PetStatusInvalid | Error | undefined>();
});
});
});
});

describe("TypesForRequest", () => {
Expand Down
12 changes: 7 additions & 5 deletions packages/swr-openapi/src/infinite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,18 @@ import { useCallback, useDebugValue } from "react";
* });
* ```
*/
export function createInfiniteHook<Paths extends {}, IMediaType extends MediaType, Prefix extends string>(
client: Client<Paths, IMediaType>,
prefix: Prefix,
) {
export function createInfiniteHook<
Paths extends {},
IMediaType extends MediaType,
Prefix extends string,
FetcherError = never,
>(client: Client<Paths, IMediaType>, prefix: Prefix) {
return function useInfinite<
Path extends PathsWithMethod<Paths, "get">,
R extends TypesForGetRequest<Paths, Path>,
Init extends R["Init"],
Data extends R["Data"],
Error extends R["Error"],
Error extends R["Error"] | FetcherError,
Config extends SWRInfiniteConfiguration<Data, Error>,
>(path: Path, getInit: SWRInfiniteKeyLoader<Data, Init | null>, config?: Config) {
type Key = [Prefix, Path, Init | undefined] | null;
Expand Down
12 changes: 7 additions & 5 deletions packages/swr-openapi/src/query-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@ import { useCallback, useDebugValue, useMemo } from "react";
* @private
*/
export function configureBaseQueryHook(useHook: SWRHook) {
return function createQueryBaseHook<Paths extends {}, IMediaType extends MediaType, Prefix extends string>(
client: Client<Paths, IMediaType>,
prefix: Prefix,
) {
return function createQueryBaseHook<
Paths extends {},
IMediaType extends MediaType,
Prefix extends string,
FetcherError = never,
>(client: Client<Paths, IMediaType>, prefix: Prefix) {
return function useQuery<
Path extends PathsWithMethod<Paths, "get">,
R extends TypesForGetRequest<Paths, Path>,
Init extends R["Init"],
Data extends R["Data"],
Error extends R["Error"],
Error extends R["Error"] | FetcherError,
Config extends R["SWRConfig"],
>(
path: Path,
Expand Down