Skip to content

Commit 3e149be

Browse files
committed
assign symbol directly
1 parent 34c2645 commit 3e149be

File tree

1 file changed

+32
-57
lines changed
  • packages/openapi-react-query/src

1 file changed

+32
-57
lines changed

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

+32-57
Original file line numberDiff line numberDiff line change
@@ -7,61 +7,35 @@ import {
77
type UseSuspenseQueryResult,
88
type QueryClient,
99
type QueryFunctionContext,
10+
type DataTag,
1011
useMutation,
1112
useQuery,
12-
useSuspenseQuery,
13-
queryOptions,
13+
useSuspenseQuery,
14+
dataTagSymbol,
1415
} from "@tanstack/react-query";
1516
import type { ClientMethod, FetchResponse, MaybeOptionalInit, Client as FetchClient } from "openapi-fetch";
1617
import type { HttpMethod, MediaType, PathsWithMethod, RequiredKeysOf } from "openapi-typescript-helpers";
1718

1819
type InitWithUnknowns<Init> = Init & { [key: string]: unknown };
1920

20-
// biome-ignore lint/suspicious/noRedeclare: <https://stackoverflow.com/questions/52760509/typescript-returntype-of-overloaded-function>
21-
type OverloadedReturnType<T> = T extends { (...args: any[]): infer R; (...args: any[]): infer R }
22-
? R
23-
: T extends (...args: any[]) => infer R
24-
? R
25-
: any;
26-
27-
type OverloadedParameters<T> = T extends { (...args: infer P1): any; (...args: infer P2): any }
28-
? P1
29-
: T extends (...args: infer P) => any
30-
? P
31-
: never;
32-
3321
export type QueryKey<
3422
Paths extends Record<string, Record<HttpMethod, {}>>,
3523
Method extends HttpMethod,
3624
Path extends PathsWithMethod<Paths, Method>,
37-
> = readonly [Method, Path, MaybeOptionalInit<Paths[Path], Method>];
38-
39-
export type QueryOptionsFunction<Paths extends Record<string, Record<HttpMethod, {}>>, Media extends MediaType> = <
40-
Method extends HttpMethod,
41-
Path extends PathsWithMethod<Paths, Method>,
42-
Init extends MaybeOptionalInit<Paths[Path], Method>,
43-
Response extends Required<FetchResponse<Paths[Path][Method], Init, Media>>, // note: Required is used to avoid repeating NonNullable in UseQuery types
44-
Options extends Omit<
45-
UseQueryOptions<Response["data"], Response["error"], Response["data"], QueryKey<Paths, Method, Path>>,
46-
"queryKey" | "queryFn"
25+
Media extends MediaType,
26+
Init extends MaybeOptionalInit<Paths[Path], Method> = MaybeOptionalInit<Paths[Path], Method>,
27+
Response extends Required<FetchResponse<Paths[Path][Method], Init, Media>> = Required<
28+
FetchResponse<Paths[Path][Method], Init, Media>
4729
>,
48-
>(
49-
method: Method,
50-
path: Path,
51-
...[init, options]: RequiredKeysOf<Init> extends never
52-
? [InitWithUnknowns<Init>?, Options?]
53-
: [InitWithUnknowns<Init>, Options?]
54-
) => UseQueryOptions<Response["data"], Response["error"], Response["data"], QueryKey<Paths, Method, Path>>;
30+
> = DataTag<readonly [Method, Path, Init], Response["data"]>;
5531

56-
export type QueryOptionsObject<Paths extends Record<string, Record<HttpMethod, {}>>, Media extends MediaType> = <
32+
export type QueryOptionsFunction<Paths extends Record<string, Record<HttpMethod, {}>>, Media extends MediaType> = <
5733
Method extends HttpMethod,
5834
Path extends PathsWithMethod<Paths, Method>,
5935
Init extends MaybeOptionalInit<Paths[Path], Method>,
6036
Response extends Required<FetchResponse<Paths[Path][Method], Init, Media>>, // note: Required is used to avoid repeating NonNullable in UseQuery types
6137
Options extends Omit<
62-
OverloadedParameters<
63-
typeof queryOptions<Response["data"], Response["error"], Response["data"], QueryKey<Paths, Method, Path>>
64-
>[0],
38+
UseQueryOptions<Response["data"], Response["error"], Response["data"], QueryKey<Paths, Method, Path, Media>>,
6539
"queryKey" | "queryFn"
6640
>,
6741
>(
@@ -70,17 +44,15 @@ export type QueryOptionsObject<Paths extends Record<string, Record<HttpMethod, {
7044
...[init, options]: RequiredKeysOf<Init> extends never
7145
? [InitWithUnknowns<Init>?, Options?]
7246
: [InitWithUnknowns<Init>, Options?]
73-
) => OverloadedReturnType<
74-
typeof queryOptions<Response["data"], Response["error"], Response["data"], QueryKey<Paths, Method, Path>>
75-
>;
47+
) => UseQueryOptions<Response["data"], Response["error"], Response["data"], QueryKey<Paths, Method, Path, Media>>;
7648

7749
export type UseQueryMethod<Paths extends Record<string, Record<HttpMethod, {}>>, Media extends MediaType> = <
7850
Method extends HttpMethod,
7951
Path extends PathsWithMethod<Paths, Method>,
8052
Init extends MaybeOptionalInit<Paths[Path], Method>,
8153
Response extends Required<FetchResponse<Paths[Path][Method], Init, Media>>, // note: Required is used to avoid repeating NonNullable in UseQuery types
8254
Options extends Omit<
83-
UseQueryOptions<Response["data"], Response["error"], Response["data"], QueryKey<Paths, Method, Path>>,
55+
UseQueryOptions<Response["data"], Response["error"], Response["data"], QueryKey<Paths, Method, Path, Media>>,
8456
"queryKey" | "queryFn"
8557
>,
8658
>(
@@ -97,7 +69,12 @@ export type UseSuspenseQueryMethod<Paths extends Record<string, Record<HttpMetho
9769
Init extends MaybeOptionalInit<Paths[Path], Method>,
9870
Response extends Required<FetchResponse<Paths[Path][Method], Init, Media>>, // note: Required is used to avoid repeating NonNullable in UseQuery types
9971
Options extends Omit<
100-
UseSuspenseQueryOptions<Response["data"], Response["error"], Response["data"], QueryKey<Paths, Method, Path>>,
72+
UseSuspenseQueryOptions<
73+
Response["data"],
74+
Response["error"],
75+
Response["data"],
76+
QueryKey<Paths, Method, Path, Media>
77+
>,
10178
"queryKey" | "queryFn"
10279
>,
10380
>(
@@ -122,7 +99,7 @@ export type UseMutationMethod<Paths extends Record<string, Record<HttpMethod, {}
12299
) => UseMutationResult<Response["data"], Response["error"], Init>;
123100

124101
export interface OpenapiQueryClient<Paths extends {}, Media extends MediaType = MediaType> {
125-
queryOptions: QueryOptionsObject<Paths, Media>;
102+
queryOptions: QueryOptionsFunction<Paths, Media>;
126103
useQuery: UseQueryMethod<Paths, Media>;
127104
useSuspenseQuery: UseSuspenseQueryMethod<Paths, Media>;
128105
useMutation: UseMutationMethod<Paths, Media>;
@@ -135,7 +112,7 @@ export default function createClient<Paths extends {}, Media extends MediaType =
135112
const queryFn = async <Method extends HttpMethod, Path extends PathsWithMethod<Paths, Method>>({
136113
queryKey: [method, path, init],
137114
signal,
138-
}: QueryFunctionContext<QueryKey<Paths, Method, Path>>) => {
115+
}: QueryFunctionContext<QueryKey<Paths, Method, Path, Media>>) => {
139116
const mth = method.toUpperCase() as Uppercase<typeof method>;
140117
const fn = client[mth] as ClientMethod<Paths, typeof method, Media>;
141118
const { data, error } = await fn(path, { signal, ...(init as any) }); // TODO: find a way to avoid as any
@@ -145,24 +122,22 @@ export default function createClient<Paths extends {}, Media extends MediaType =
145122
return data;
146123
};
147124

148-
const queryOptionsParams: QueryOptionsFunction<Paths, Media> = (method, path, ...[init, options]) => ({
149-
queryKey: [method, path, init as InitWithUnknowns<typeof init>] as const,
150-
queryFn,
151-
...options,
152-
});
125+
const queryOptions: QueryOptionsFunction<Paths, Media> = (method, path, ...[init, options]) => {
126+
return {
127+
queryKey: Object.assign([method, path, init as InitWithUnknowns<typeof init>] as const, {
128+
[dataTagSymbol]: {} as any,
129+
}),
130+
queryFn,
131+
...options,
132+
};
133+
};
153134

154135
return {
155-
queryOptions: (method, path, ...[init, options]) =>
156-
queryOptions({
157-
queryKey: [method, path, init as InitWithUnknowns<typeof init>] as const,
158-
queryFn,
159-
// TODO: find a way to avoid as any
160-
...(options as any),
161-
}),
136+
queryOptions,
162137
useQuery: (method, path, ...[init, options, queryClient]) =>
163-
useQuery(queryOptionsParams(method, path, init as InitWithUnknowns<typeof init>, options), queryClient),
138+
useQuery(queryOptions(method, path, init as InitWithUnknowns<typeof init>, options), queryClient),
164139
useSuspenseQuery: (method, path, ...[init, options, queryClient]) =>
165-
useSuspenseQuery(queryOptionsParams(method, path, init as InitWithUnknowns<typeof init>, options), queryClient),
140+
useSuspenseQuery(queryOptions(method, path, init as InitWithUnknowns<typeof init>, options), queryClient),
166141
useMutation: (method, path, options, queryClient) =>
167142
useMutation(
168143
{

0 commit comments

Comments
 (0)