Skip to content

Commit 9862782

Browse files
committed
feat(swr-openapi): add custom error types to query builder
1 parent 82e98b4 commit 9862782

File tree

3 files changed

+60
-10
lines changed

3 files changed

+60
-10
lines changed

packages/swr-openapi/src/__test__/types.test-d.ts

+46
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,52 @@ describe("types", () => {
336336
});
337337
});
338338
});
339+
340+
describe("custom error types", () => {
341+
const uniqueKey = "<unique-key>";
342+
type Key = typeof uniqueKey;
343+
const useQuery = createQueryHook<paths, `${string}/${string}`, Key, Error>(client, uniqueKey);
344+
const useImmutable = createImmutableHook<paths, `${string}/${string}`, Key, Error>(client, uniqueKey);
345+
const useInfinite = createInfiniteHook<paths, `${string}/${string}`, Key, Error>(client, uniqueKey);
346+
347+
describe("useQuery", () => {
348+
it("returns correct error", () => {
349+
const { error } = useQuery("/pet/{petId}", {
350+
params: {
351+
path: {
352+
petId: 5,
353+
},
354+
},
355+
});
356+
357+
expectTypeOf(error).toEqualTypeOf<PetInvalid | Error | undefined>();
358+
});
359+
});
360+
361+
describe("useImmutable", () => {
362+
it("returns correct error", () => {
363+
const { error } = useImmutable("/pet/{petId}", {
364+
params: {
365+
path: {
366+
petId: 5,
367+
},
368+
},
369+
});
370+
371+
expectTypeOf(error).toEqualTypeOf<PetInvalid | Error | undefined>();
372+
});
373+
});
374+
375+
describe("useInfinite", () => {
376+
it("returns correct error", () => {
377+
const { error } = useInfinite("/pet/findByStatus", (_index, _prev) => ({
378+
params: { query: { status: "available" } },
379+
}));
380+
381+
expectTypeOf(error).toEqualTypeOf<PetStatusInvalid | Error | undefined>();
382+
});
383+
});
384+
});
339385
});
340386

341387
describe("TypesForRequest", () => {

packages/swr-openapi/src/infinite.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,18 @@ import { useCallback, useDebugValue } from "react";
3333
* });
3434
* ```
3535
*/
36-
export function createInfiniteHook<Paths extends {}, IMediaType extends MediaType, Prefix extends string>(
37-
client: Client<Paths, IMediaType>,
38-
prefix: Prefix,
39-
) {
36+
export function createInfiniteHook<
37+
Paths extends {},
38+
IMediaType extends MediaType,
39+
Prefix extends string,
40+
FetcherError = never,
41+
>(client: Client<Paths, IMediaType>, prefix: Prefix) {
4042
return function useInfinite<
4143
Path extends PathsWithMethod<Paths, "get">,
4244
R extends TypesForGetRequest<Paths, Path>,
4345
Init extends R["Init"],
4446
Data extends R["Data"],
45-
Error extends R["Error"],
47+
Error extends R["Error"] | FetcherError,
4648
Config extends SWRInfiniteConfiguration<Data, Error>,
4749
>(path: Path, getInit: SWRInfiniteKeyLoader<Data, Init | null>, config?: Config) {
4850
type Key = [Prefix, Path, Init | undefined] | null;

packages/swr-openapi/src/query-base.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,18 @@ import { useCallback, useDebugValue, useMemo } from "react";
88
* @private
99
*/
1010
export function configureBaseQueryHook(useHook: SWRHook) {
11-
return function createQueryBaseHook<Paths extends {}, IMediaType extends MediaType, Prefix extends string>(
12-
client: Client<Paths, IMediaType>,
13-
prefix: Prefix,
14-
) {
11+
return function createQueryBaseHook<
12+
Paths extends {},
13+
IMediaType extends MediaType,
14+
Prefix extends string,
15+
FetcherError = never,
16+
>(client: Client<Paths, IMediaType>, prefix: Prefix) {
1517
return function useQuery<
1618
Path extends PathsWithMethod<Paths, "get">,
1719
R extends TypesForGetRequest<Paths, Path>,
1820
Init extends R["Init"],
1921
Data extends R["Data"],
20-
Error extends R["Error"],
22+
Error extends R["Error"] | FetcherError,
2123
Config extends R["SWRConfig"],
2224
>(
2325
path: Path,

0 commit comments

Comments
 (0)