Skip to content

Commit 19e9cc0

Browse files
committed
Migrate current react-query impl to typescript
1 parent 94a03e9 commit 19e9cc0

File tree

2 files changed

+60
-104
lines changed

2 files changed

+60
-104
lines changed

packages/openapi-react-query/src/index.ts

Lines changed: 55 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,104 +1,65 @@
11
import {
2-
UseMutationOptions,
3-
UseMutationResult,
4-
UseQueryOptions,
5-
UseQueryResult,
6-
UseSuspenseQueryOptions,
7-
UseSuspenseQueryResult,
2+
type UseMutationOptions,
3+
type UseMutationResult,
4+
type UseQueryOptions,
5+
type UseQueryResult,
6+
type UseSuspenseQueryOptions,
7+
type UseSuspenseQueryResult,
88
useMutation,
99
useQuery,
1010
} from "@tanstack/react-query";
11-
import {
12-
ClientMethod,
13-
FetchResponse,
14-
MaybeOptionalInit,
15-
OpenapiClient,
16-
} from "openapi-fetch";
17-
import {
18-
HasRequiredKeys,
19-
HttpMethod,
20-
MediaType,
21-
PathsWithMethod,
22-
} from "openapi-typescript-helpers";
11+
import type { ClientMethod, FetchResponse, MaybeOptionalInit, OpenapiClient } from "openapi-fetch";
12+
import type { HasRequiredKeys, HttpMethod, MediaType, PathsWithMethod } from "openapi-typescript-helpers";
2313

24-
export type UseQueryMethod<
25-
Paths extends Record<string, Record<HttpMethod, {}>>,
26-
Media extends MediaType,
27-
> = {
28-
<
29-
Method extends HttpMethod,
30-
Path extends PathsWithMethod<Paths, Method>,
31-
Init extends MaybeOptionalInit<Paths[Path], Method>,
32-
Response extends Required<FetchResponse<Paths[Path][Method], Init, Media>>, // note: Required is used to avoid repeating NonNullable in UseQuery types
33-
Options extends Omit<
34-
UseQueryOptions<Response["data"], Response["error"]>,
35-
"queryKey" | "queryFn"
36-
>,
37-
>(
38-
method: Method,
39-
url: Path,
40-
...[init, options]: HasRequiredKeys<Init> extends never
41-
? [(Init & { [key: string]: unknown })?, Options?]
42-
: [Init & { [key: string]: unknown }, Options?]
43-
): UseQueryResult<Response["data"], Response["error"]>;
44-
};
14+
export type UseQueryMethod<Paths extends Record<string, Record<HttpMethod, {}>>, Media extends MediaType> = <
15+
Method extends HttpMethod,
16+
Path extends PathsWithMethod<Paths, Method>,
17+
Init extends MaybeOptionalInit<Paths[Path], Method>,
18+
Response extends Required<FetchResponse<Paths[Path][Method], Init, Media>>, // note: Required is used to avoid repeating NonNullable in UseQuery types
19+
Options extends Omit<UseQueryOptions<Response["data"], Response["error"]>, "queryKey" | "queryFn">,
20+
>(
21+
method: Method,
22+
url: Path,
23+
...[init, options]: HasRequiredKeys<Init> extends never
24+
? [(Init & { [key: string]: unknown })?, Options?]
25+
: [Init & { [key: string]: unknown }, Options?]
26+
) => UseQueryResult<Response["data"], Response["error"]>;
4527

46-
export type UseSuspenseQueryMethod<
47-
Paths extends Record<string, Record<HttpMethod, {}>>,
48-
Media extends MediaType,
49-
> = {
50-
<
51-
Method extends HttpMethod,
52-
Path extends PathsWithMethod<Paths, Method>,
53-
Init extends MaybeOptionalInit<Paths[Path], Method>,
54-
Response extends Required<FetchResponse<Paths[Path][Method], Init, Media>>, // note: Required is used to avoid repeating NonNullable in UseQuery types
55-
Options extends Omit<
56-
UseSuspenseQueryOptions<Response["data"], Response["error"]>,
57-
"queryKey" | "queryFn"
58-
>,
59-
>(
60-
method: Method,
61-
url: Path,
62-
...[init, options]: HasRequiredKeys<Init> extends never
63-
? [(Init & { [key: string]: unknown })?, Options?]
64-
: [Init & { [key: string]: unknown }, Options?]
65-
): UseSuspenseQueryResult<Response["data"], Response["error"]>;
66-
};
28+
export type UseSuspenseQueryMethod<Paths extends Record<string, Record<HttpMethod, {}>>, Media extends MediaType> = <
29+
Method extends HttpMethod,
30+
Path extends PathsWithMethod<Paths, Method>,
31+
Init extends MaybeOptionalInit<Paths[Path], Method>,
32+
Response extends Required<FetchResponse<Paths[Path][Method], Init, Media>>, // note: Required is used to avoid repeating NonNullable in UseQuery types
33+
Options extends Omit<UseSuspenseQueryOptions<Response["data"], Response["error"]>, "queryKey" | "queryFn">,
34+
>(
35+
method: Method,
36+
url: Path,
37+
...[init, options]: HasRequiredKeys<Init> extends never
38+
? [(Init & { [key: string]: unknown })?, Options?]
39+
: [Init & { [key: string]: unknown }, Options?]
40+
) => UseSuspenseQueryResult<Response["data"], Response["error"]>;
6741

68-
export type UseMutationMethod<
69-
Paths extends Record<string, Record<HttpMethod, {}>>,
70-
Media extends MediaType,
71-
> = {
72-
<
73-
Method extends HttpMethod,
74-
Path extends PathsWithMethod<Paths, Method>,
75-
Init extends MaybeOptionalInit<Paths[Path], Method>,
76-
Response extends Required<FetchResponse<Paths[Path][Method], Init, Media>>, // note: Required is used to avoid repeating NonNullable in UseQuery types
77-
Options extends Omit<
78-
UseMutationOptions<Response["data"], Response["error"], Init>,
79-
"mutationKey" | "mutationFn"
80-
>,
81-
>(
82-
method: Method,
83-
url: Path,
84-
options?: Options,
85-
// TODO: Add support for Partial request options that become optional in UseMutationResult Context
86-
): UseMutationResult<Response["data"], Response["error"], Init>;
87-
};
42+
export type UseMutationMethod<Paths extends Record<string, Record<HttpMethod, {}>>, Media extends MediaType> = <
43+
Method extends HttpMethod,
44+
Path extends PathsWithMethod<Paths, Method>,
45+
Init extends MaybeOptionalInit<Paths[Path], Method>,
46+
Response extends Required<FetchResponse<Paths[Path][Method], Init, Media>>, // note: Required is used to avoid repeating NonNullable in UseQuery types
47+
Options extends Omit<UseMutationOptions<Response["data"], Response["error"], Init>, "mutationKey" | "mutationFn">,
48+
>(
49+
method: Method,
50+
url: Path,
51+
options?: Options,
52+
) => UseMutationResult<Response["data"], Response["error"], Init>;
8853

89-
export interface OpenapiQueryClient<
90-
Paths extends {},
91-
Media extends MediaType = MediaType,
92-
> {
54+
export interface OpenapiQueryClient<Paths extends {}, Media extends MediaType = MediaType> {
9355
useQuery: UseQueryMethod<Paths, Media>;
9456
useSuspenseQery: any;
9557
useMutation: UseMutationMethod<Paths, Media>;
9658
}
9759

98-
export default function createClient<
99-
Paths extends {},
100-
Media extends MediaType = MediaType,
101-
>(client: OpenapiClient<Paths, Media>): OpenapiQueryClient<Paths, Media> {
60+
export default function createClient<Paths extends {}, Media extends MediaType = MediaType>(
61+
client: OpenapiClient<Paths, Media>,
62+
): OpenapiQueryClient<Paths, Media> {
10263
return {
10364
useQuery: (method, path, ...[init, options]) => {
10465
return useQuery({
@@ -107,7 +68,9 @@ export default function createClient<
10768
const mth = method.toUpperCase() as keyof typeof client;
10869
const fn = client[mth] as ClientMethod<Paths, typeof method, Media>;
10970
const { data, error } = await fn(path, init as any); // TODO: find a way to avoid as any
110-
if (error || !data) throw error;
71+
if (error || !data) {
72+
throw error;
73+
}
11174
return data;
11275
},
11376
...options,
@@ -121,7 +84,9 @@ export default function createClient<
12184
const mth = method.toUpperCase() as keyof typeof client;
12285
const fn = client[mth] as ClientMethod<Paths, typeof method, Media>;
12386
const { data, error } = await fn(path, init as any); // TODO: find a way to avoid as any
124-
if (error || !data) throw error;
87+
if (error || !data) {
88+
throw error;
89+
}
12590
return data;
12691
},
12792
...options,

packages/openapi-react-query/test/index.test.tsx

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
import { afterAll, beforeAll, describe, expect, it } from "vitest";
2-
import {
3-
server,
4-
baseUrl,
5-
useMockRequestHandler,
6-
} from "./fixtures/mock-server.js";
2+
import { server, baseUrl, useMockRequestHandler } from "./fixtures/mock-server.js";
73
import type { paths } from "./fixtures/api.js";
84
import createClient from "../src/index.js";
95
import createFetchClient from "openapi-fetch";
@@ -14,9 +10,7 @@ import React, { type ReactNode } from "react";
1410
beforeAll(() => {
1511
server.listen({
1612
onUnhandledRequest: (request) => {
17-
throw new Error(
18-
`No request handler found for ${request.method} ${request.url}`,
19-
);
13+
throw new Error(`No request handler found for ${request.method} ${request.url}`);
2014
},
2115
});
2216
});
@@ -75,12 +69,9 @@ describe("client", () => {
7569
body: { message: "OK" },
7670
});
7771

78-
const { result } = renderHook(
79-
() => client.useMutation("get", "/tag/{name}"),
80-
{
81-
wrapper,
82-
},
83-
);
72+
const { result } = renderHook(() => client.useMutation("get", "/tag/{name}"), {
73+
wrapper,
74+
});
8475

8576
result.current.mutate({
8677
params: {

0 commit comments

Comments
 (0)