Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 5c5be96

Browse files
committedApr 29, 2024
feat: add query key function
1 parent cf175e6 commit 5c5be96

File tree

8 files changed

+239
-61
lines changed

8 files changed

+239
-61
lines changed
 

‎README.md

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,75 @@ function App() {
155155
export default App;
156156
```
157157

158+
#### Using Mutation hooks
159+
160+
```tsx
161+
// App.tsx
162+
import { usePetServiceAddPet } from "../openapi/queries";
163+
164+
function App() {
165+
const { mutate } = usePetServiceAddPet();
166+
167+
const handleAddPet = () => {
168+
mutate({ name: "Fluffy", status: "available" });
169+
};
170+
171+
return (
172+
<div className="App">
173+
<h1>Add Pet</h1>
174+
<button onClick={handleAddPet}>Add Pet</button>
175+
</div>
176+
);
177+
}
178+
179+
export default App;
180+
```
181+
182+
#### Invalidating queries after mutation
183+
184+
Invalidating queries after a mutation is important to ensure the cache is updated with the new data. This is done by calling the `queryClient.invalidateQueries` function with the query key used by the query hook.
185+
186+
Learn more about invalidating queries [here](https://tanstack.com/query/latest/docs/framework/react/guides/query-invalidation).
187+
188+
To ensure the query key is created the same way as the query hook, you can use the query key function exported by the generated query hooks.
189+
190+
```tsx
191+
import {
192+
usePetServiceFindPetsByStatus,
193+
usePetServiceAddPet,
194+
UsePetServiceFindPetsByStatusKeyFn,
195+
} from "../openapi/queries";
196+
197+
// App.tsx
198+
function App() {
199+
const { data } = usePetServiceFindPetsByStatus({ status: ["available"] });
200+
const { mutate } = usePetServiceAddPet({
201+
onSuccess: () => {
202+
queryClient.invalidateQueries({
203+
// Call the query key function to get the query key, this is important to ensure the query key is created the same way as the query hook, this insures the cache is invalidated correctly and is typed correctly
204+
queryKey: [UsePetServiceFindPetsByStatusKeyFn()],
205+
});
206+
},
207+
});
208+
209+
return (
210+
<div className="App">
211+
<h1>Pet List</h1>
212+
<ul>{data?.map((pet) => <li key={pet.id}>{pet.name}</li>)}</ul>
213+
<button
214+
onClick={() => {
215+
mutate({ name: "Fluffy", status: "available" });
216+
}}
217+
>
218+
Add Pet
219+
</button>
220+
</div>
221+
);
222+
}
223+
224+
export default App;
225+
```
226+
158227
#### Runtime Configuration
159228

160229
You can modify the default values used by the generated service calls by modifying the OpenAPI configuration singleton object.

‎examples/react-app/src/App.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import "./App.css";
22
import {
33
useDefaultServiceAddPet,
44
useDefaultServiceFindPets,
5-
useDefaultServiceFindPetsKey,
5+
UseDefaultServiceFindPetsKeyFn,
66
useDefaultServiceGetNotDefined,
77
useDefaultServicePostNotDefined,
88
} from "../openapi/queries";
@@ -54,7 +54,7 @@ function App() {
5454
{
5555
onSuccess: () => {
5656
queryClient.invalidateQueries({
57-
queryKey: [useDefaultServiceFindPetsKey],
57+
queryKey: UseDefaultServiceFindPetsKeyFn(),
5858
});
5959
console.log("success");
6060
},

‎src/common.mts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ export const TData = ts.factory.createIdentifier("TData");
1515
export const TError = ts.factory.createIdentifier("TError");
1616
export const TContext = ts.factory.createIdentifier("TContext");
1717

18+
export const EqualsOrGreaterThanToken = ts.factory.createToken(
19+
ts.SyntaxKind.EqualsGreaterThanToken
20+
);
21+
22+
export const QuestionToken = ts.factory.createToken(
23+
ts.SyntaxKind.QuestionToken
24+
);
25+
1826
export const queryKeyGenericType =
1927
ts.factory.createTypeReferenceNode("TQueryKey");
2028
export const queryKeyConstraint = ts.factory.createTypeReferenceNode("Array", [

‎src/createExports.mts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,12 @@ export const createExports = (service: Service) => {
3838
];
3939

4040
const commonInQueries = allQueries
41-
.map(({ apiResponse, returnType, key }) => [apiResponse, returnType, key])
41+
.map(({ apiResponse, returnType, key, queryKeyFn }) => [
42+
apiResponse,
43+
returnType,
44+
key,
45+
queryKeyFn,
46+
])
4247
.flat();
4348
const commonInMutations = allMutations
4449
.map(({ mutationResult }) => [mutationResult])

‎src/createUseQuery.mts

Lines changed: 118 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ import { MethodDeclaration } from "ts-morph";
33
import {
44
BuildCommonTypeName,
55
capitalizeFirstLetter,
6+
EqualsOrGreaterThanToken,
67
extractPropertiesFromObjectParam,
78
getNameFromMethod,
89
getShortType,
910
queryKeyConstraint,
1011
queryKeyGenericType,
12+
QuestionToken,
1113
TData,
1214
TError,
1315
} from "./common.mjs";
@@ -61,12 +63,15 @@ export const createApiResponseType = ({
6163
);
6264

6365
return {
64-
/** DefaultResponseDataType
66+
/**
67+
* DefaultResponseDataType
68+
*
6569
* export type MyClassMethodDefaultResponse = Awaited<ReturnType<typeof myClass.myMethod>>
6670
*/
6771
apiResponse,
6872
/**
69-
* will be the name of the type of the response type of the method
73+
* This will be the name of the type of the response type of the method
74+
*
7075
* MyClassMethodDefaultResponse
7176
*/
7277
responseDataType,
@@ -128,6 +133,7 @@ export function getRequestParamFromMethod(method: MethodDeclaration) {
128133

129134
/**
130135
* Return Type
136+
*
131137
* export const classNameMethodNameQueryResult<TData = MyClassMethodDefaultResponse, TError = unknown> = UseQueryResult<TData, TError>;
132138
*/
133139
export function createReturnTypeExport({
@@ -311,7 +317,7 @@ function createQueryHook({
311317
),
312318
],
313319
undefined,
314-
ts.factory.createToken(ts.SyntaxKind.EqualsGreaterThanToken),
320+
EqualsOrGreaterThanToken,
315321
ts.factory.createCallExpression(
316322
ts.factory.createIdentifier(queryString),
317323
[
@@ -322,41 +328,28 @@ function createQueryHook({
322328
ts.factory.createObjectLiteralExpression([
323329
ts.factory.createPropertyAssignment(
324330
ts.factory.createIdentifier("queryKey"),
325-
ts.factory.createArrayLiteralExpression(
326-
[
327-
BuildCommonTypeName(queryKey),
328-
ts.factory.createSpreadElement(
329-
ts.factory.createParenthesizedExpression(
330-
ts.factory.createBinaryExpression(
331-
ts.factory.createIdentifier("queryKey"),
332-
ts.factory.createToken(
333-
ts.SyntaxKind.QuestionQuestionToken
334-
),
335-
method.getParameters().length
336-
? ts.factory.createArrayLiteralExpression([
337-
ts.factory.createObjectLiteralExpression(
338-
method
339-
.getParameters()
340-
.map((param) =>
341-
extractPropertiesFromObjectParam(
342-
param
343-
).map((p) =>
344-
ts.factory.createShorthandPropertyAssignment(
345-
ts.factory.createIdentifier(
346-
p.name
347-
)
348-
)
349-
)
350-
)
351-
.flat()
352-
),
353-
])
354-
: ts.factory.createArrayLiteralExpression([])
355-
)
356-
)
357-
),
358-
],
359-
false
331+
ts.factory.createCallExpression(
332+
BuildCommonTypeName(getQueryKeyFnName(queryKey)),
333+
undefined,
334+
335+
method.getParameters().length
336+
? [
337+
ts.factory.createObjectLiteralExpression(
338+
method
339+
.getParameters()
340+
.map((param) =>
341+
extractPropertiesFromObjectParam(param).map(
342+
(p) =>
343+
ts.factory.createShorthandPropertyAssignment(
344+
ts.factory.createIdentifier(p.name)
345+
)
346+
)
347+
)
348+
.flat()
349+
),
350+
ts.factory.createIdentifier("queryKey"),
351+
]
352+
: []
360353
)
361354
),
362355
ts.factory.createPropertyAssignment(
@@ -366,9 +359,7 @@ function createQueryHook({
366359
undefined,
367360
[],
368361
undefined,
369-
ts.factory.createToken(
370-
ts.SyntaxKind.EqualsGreaterThanToken
371-
),
362+
EqualsOrGreaterThanToken,
372363
ts.factory.createAsExpression(
373364
ts.factory.createCallExpression(
374365
ts.factory.createPropertyAccessExpression(
@@ -463,11 +454,97 @@ export const createUseQuery = ({
463454
queryKey,
464455
});
465456

457+
const queryKeyFn = createQueryKeyFnExport(queryKey, method);
458+
466459
return {
467460
apiResponse: defaultApiResponse,
468461
returnType: returnTypeExport,
469462
key: queryKeyExport,
470463
queryHook: hookWithJsDoc,
471464
suspenseQueryHook: suspenseHookWithJsDoc,
465+
queryKeyFn,
472466
};
473467
};
468+
469+
function getQueryKeyFnName(queryKey: string) {
470+
return `${capitalizeFirstLetter(queryKey)}Fn`;
471+
}
472+
473+
function createQueryKeyFnExport(queryKey: string, method: MethodDeclaration) {
474+
const params = getRequestParamFromMethod(method);
475+
476+
// override key is used to allow the user to override the the queryKey values
477+
const overrideKey = ts.factory.createParameterDeclaration(
478+
undefined,
479+
undefined,
480+
ts.factory.createIdentifier("queryKey"),
481+
QuestionToken,
482+
ts.factory.createTypeReferenceNode("Array<unknown>", [])
483+
);
484+
485+
return ts.factory.createVariableStatement(
486+
[ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)],
487+
ts.factory.createVariableDeclarationList(
488+
[
489+
ts.factory.createVariableDeclaration(
490+
ts.factory.createIdentifier(getQueryKeyFnName(queryKey)),
491+
undefined,
492+
undefined,
493+
ts.factory.createArrowFunction(
494+
undefined,
495+
undefined,
496+
params ? [params, overrideKey] : [],
497+
undefined,
498+
EqualsOrGreaterThanToken,
499+
queryKeyFn(queryKey, method)
500+
)
501+
),
502+
],
503+
ts.NodeFlags.Const
504+
)
505+
);
506+
}
507+
508+
function queryKeyFn(
509+
queryKey: string,
510+
method: MethodDeclaration
511+
): ts.Expression {
512+
const params = getRequestParamFromMethod(method);
513+
514+
if (!params) {
515+
return ts.factory.createArrayLiteralExpression([
516+
ts.factory.createIdentifier(queryKey),
517+
]);
518+
}
519+
520+
return ts.factory.createArrayLiteralExpression(
521+
[
522+
ts.factory.createIdentifier(queryKey),
523+
ts.factory.createSpreadElement(
524+
ts.factory.createParenthesizedExpression(
525+
ts.factory.createBinaryExpression(
526+
ts.factory.createIdentifier("queryKey"),
527+
ts.factory.createToken(ts.SyntaxKind.QuestionQuestionToken),
528+
method.getParameters().length
529+
? ts.factory.createArrayLiteralExpression([
530+
ts.factory.createObjectLiteralExpression(
531+
method
532+
.getParameters()
533+
.map((param) =>
534+
extractPropertiesFromObjectParam(param).map((p) =>
535+
ts.factory.createShorthandPropertyAssignment(
536+
ts.factory.createIdentifier(p.name)
537+
)
538+
)
539+
)
540+
.flat()
541+
),
542+
])
543+
: ts.factory.createArrayLiteralExpression([])
544+
)
545+
)
546+
),
547+
],
548+
false
549+
);
550+
}

‎tests/__snapshots__/createSource.test.ts.snap

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,20 @@ import { Pet, NewPet, Error, $OpenApiTs } from "../requests/types.gen";
1717
export type DefaultServiceFindPetsDefaultResponse = Awaited<ReturnType<typeof DefaultService.findPets>>;
1818
export type DefaultServiceFindPetsQueryResult<TData = DefaultServiceFindPetsDefaultResponse, TError = unknown> = UseQueryResult<TData, TError>;
1919
export const useDefaultServiceFindPetsKey = "DefaultServiceFindPets";
20+
export const UseDefaultServiceFindPetsKeyFn = ({ limit, tags }: {
21+
limit?: number;
22+
tags?: string[];
23+
} = {}, queryKey?: Array<unknown>) => [useDefaultServiceFindPetsKey, ...(queryKey ?? [{ limit, tags }])];
2024
export type DefaultServiceGetNotDefinedDefaultResponse = Awaited<ReturnType<typeof DefaultService.getNotDefined>>;
2125
export type DefaultServiceGetNotDefinedQueryResult<TData = DefaultServiceGetNotDefinedDefaultResponse, TError = unknown> = UseQueryResult<TData, TError>;
2226
export const useDefaultServiceGetNotDefinedKey = "DefaultServiceGetNotDefined";
27+
export const UseDefaultServiceGetNotDefinedKeyFn = () => [useDefaultServiceGetNotDefinedKey];
2328
export type DefaultServiceFindPetByIdDefaultResponse = Awaited<ReturnType<typeof DefaultService.findPetById>>;
2429
export type DefaultServiceFindPetByIdQueryResult<TData = DefaultServiceFindPetByIdDefaultResponse, TError = unknown> = UseQueryResult<TData, TError>;
2530
export const useDefaultServiceFindPetByIdKey = "DefaultServiceFindPetById";
31+
export const UseDefaultServiceFindPetByIdKeyFn = ({ id }: {
32+
id: number;
33+
}, queryKey?: Array<unknown>) => [useDefaultServiceFindPetByIdKey, ...(queryKey ?? [{ id }])];
2634
export type DefaultServiceAddPetMutationResult = Awaited<ReturnType<typeof DefaultService.addPet>>;
2735
export type DefaultServicePostNotDefinedMutationResult = Awaited<ReturnType<typeof DefaultService.postNotDefined>>;
2836
export type DefaultServiceDeletePetMutationResult = Awaited<ReturnType<typeof DefaultService.deletePet>>;
@@ -52,14 +60,14 @@ import { Pet, NewPet, Error, $OpenApiTs } from "../requests/types.gen";
5260
export const useDefaultServiceFindPets = <TData = Common.DefaultServiceFindPetsDefaultResponse, TError = unknown, TQueryKey extends Array<unknown> = unknown[]>({ limit, tags }: {
5361
limit?: number;
5462
tags?: string[];
55-
} = {}, queryKey?: TQueryKey, options?: Omit<UseQueryOptions<TData, TError>, "queryKey" | "queryFn">) => useQuery<TData, TError>({ queryKey: [Common.useDefaultServiceFindPetsKey, ...(queryKey ?? [{ limit, tags }])], queryFn: () => DefaultService.findPets({ limit, tags }) as TData, ...options });
63+
} = {}, queryKey?: TQueryKey, options?: Omit<UseQueryOptions<TData, TError>, "queryKey" | "queryFn">) => useQuery<TData, TError>({ queryKey: Common.UseDefaultServiceFindPetsKeyFn({ limit, tags }, queryKey), queryFn: () => DefaultService.findPets({ limit, tags }) as TData, ...options });
5664
/**
5765
* @deprecated
5866
* This path is not fully defined.
5967
* @returns unknown unexpected error
6068
* @throws ApiError
6169
*/
62-
export const useDefaultServiceGetNotDefined = <TData = Common.DefaultServiceGetNotDefinedDefaultResponse, TError = unknown, TQueryKey extends Array<unknown> = unknown[]>(queryKey?: TQueryKey, options?: Omit<UseQueryOptions<TData, TError>, "queryKey" | "queryFn">) => useQuery<TData, TError>({ queryKey: [Common.useDefaultServiceGetNotDefinedKey, ...(queryKey ?? [])], queryFn: () => DefaultService.getNotDefined() as TData, ...options });
70+
export const useDefaultServiceGetNotDefined = <TData = Common.DefaultServiceGetNotDefinedDefaultResponse, TError = unknown, TQueryKey extends Array<unknown> = unknown[]>(queryKey?: TQueryKey, options?: Omit<UseQueryOptions<TData, TError>, "queryKey" | "queryFn">) => useQuery<TData, TError>({ queryKey: Common.UseDefaultServiceGetNotDefinedKeyFn(), queryFn: () => DefaultService.getNotDefined() as TData, ...options });
6371
/**
6472
* Returns a user based on a single ID, if the user does not have access to the pet
6573
* @param data The data for the request.
@@ -70,7 +78,7 @@ export const useDefaultServiceGetNotDefined = <TData = Common.DefaultServiceGetN
7078
*/
7179
export const useDefaultServiceFindPetById = <TData = Common.DefaultServiceFindPetByIdDefaultResponse, TError = unknown, TQueryKey extends Array<unknown> = unknown[]>({ id }: {
7280
id: number;
73-
}, queryKey?: TQueryKey, options?: Omit<UseQueryOptions<TData, TError>, "queryKey" | "queryFn">) => useQuery<TData, TError>({ queryKey: [Common.useDefaultServiceFindPetByIdKey, ...(queryKey ?? [{ id }])], queryFn: () => DefaultService.findPetById({ id }) as TData, ...options });
81+
}, queryKey?: TQueryKey, options?: Omit<UseQueryOptions<TData, TError>, "queryKey" | "queryFn">) => useQuery<TData, TError>({ queryKey: Common.UseDefaultServiceFindPetByIdKeyFn({ id }, queryKey), queryFn: () => DefaultService.findPetById({ id }) as TData, ...options });
7482
/**
7583
* Creates a new pet in the store. Duplicates are allowed
7684
* @param data The data for the request.
@@ -130,14 +138,14 @@ import { Pet, NewPet, Error, $OpenApiTs } from "../requests/types.gen";
130138
export const useDefaultServiceFindPetsSuspense = <TData = Common.DefaultServiceFindPetsDefaultResponse, TError = unknown, TQueryKey extends Array<unknown> = unknown[]>({ limit, tags }: {
131139
limit?: number;
132140
tags?: string[];
133-
} = {}, queryKey?: TQueryKey, options?: Omit<UseQueryOptions<TData, TError>, "queryKey" | "queryFn">) => useSuspenseQuery<TData, TError>({ queryKey: [Common.useDefaultServiceFindPetsKey, ...(queryKey ?? [{ limit, tags }])], queryFn: () => DefaultService.findPets({ limit, tags }) as TData, ...options });
141+
} = {}, queryKey?: TQueryKey, options?: Omit<UseQueryOptions<TData, TError>, "queryKey" | "queryFn">) => useSuspenseQuery<TData, TError>({ queryKey: Common.UseDefaultServiceFindPetsKeyFn({ limit, tags }, queryKey), queryFn: () => DefaultService.findPets({ limit, tags }) as TData, ...options });
134142
/**
135143
* @deprecated
136144
* This path is not fully defined.
137145
* @returns unknown unexpected error
138146
* @throws ApiError
139147
*/
140-
export const useDefaultServiceGetNotDefinedSuspense = <TData = Common.DefaultServiceGetNotDefinedDefaultResponse, TError = unknown, TQueryKey extends Array<unknown> = unknown[]>(queryKey?: TQueryKey, options?: Omit<UseQueryOptions<TData, TError>, "queryKey" | "queryFn">) => useSuspenseQuery<TData, TError>({ queryKey: [Common.useDefaultServiceGetNotDefinedKey, ...(queryKey ?? [])], queryFn: () => DefaultService.getNotDefined() as TData, ...options });
148+
export const useDefaultServiceGetNotDefinedSuspense = <TData = Common.DefaultServiceGetNotDefinedDefaultResponse, TError = unknown, TQueryKey extends Array<unknown> = unknown[]>(queryKey?: TQueryKey, options?: Omit<UseQueryOptions<TData, TError>, "queryKey" | "queryFn">) => useSuspenseQuery<TData, TError>({ queryKey: Common.UseDefaultServiceGetNotDefinedKeyFn(), queryFn: () => DefaultService.getNotDefined() as TData, ...options });
141149
/**
142150
* Returns a user based on a single ID, if the user does not have access to the pet
143151
* @param data The data for the request.
@@ -148,6 +156,6 @@ export const useDefaultServiceGetNotDefinedSuspense = <TData = Common.DefaultSer
148156
*/
149157
export const useDefaultServiceFindPetByIdSuspense = <TData = Common.DefaultServiceFindPetByIdDefaultResponse, TError = unknown, TQueryKey extends Array<unknown> = unknown[]>({ id }: {
150158
id: number;
151-
}, queryKey?: TQueryKey, options?: Omit<UseQueryOptions<TData, TError>, "queryKey" | "queryFn">) => useSuspenseQuery<TData, TError>({ queryKey: [Common.useDefaultServiceFindPetByIdKey, ...(queryKey ?? [{ id }])], queryFn: () => DefaultService.findPetById({ id }) as TData, ...options });
159+
}, queryKey?: TQueryKey, options?: Omit<UseQueryOptions<TData, TError>, "queryKey" | "queryFn">) => useSuspenseQuery<TData, TError>({ queryKey: Common.UseDefaultServiceFindPetByIdKeyFn({ id }, queryKey), queryFn: () => DefaultService.findPetById({ id }) as TData, ...options });
152160
"
153161
`;

‎tests/__snapshots__/generate.test.ts.snap

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,20 @@ import { DefaultService } from "../requests/services.gen";
88
export type DefaultServiceFindPetsDefaultResponse = Awaited<ReturnType<typeof DefaultService.findPets>>;
99
export type DefaultServiceFindPetsQueryResult<TData = DefaultServiceFindPetsDefaultResponse, TError = unknown> = UseQueryResult<TData, TError>;
1010
export const useDefaultServiceFindPetsKey = "DefaultServiceFindPets";
11+
export const UseDefaultServiceFindPetsKeyFn = ({ limit, tags }: {
12+
limit?: number;
13+
tags?: string[];
14+
} = {}, queryKey?: Array<unknown>) => [useDefaultServiceFindPetsKey, ...(queryKey ?? [{ limit, tags }])];
1115
export type DefaultServiceGetNotDefinedDefaultResponse = Awaited<ReturnType<typeof DefaultService.getNotDefined>>;
1216
export type DefaultServiceGetNotDefinedQueryResult<TData = DefaultServiceGetNotDefinedDefaultResponse, TError = unknown> = UseQueryResult<TData, TError>;
1317
export const useDefaultServiceGetNotDefinedKey = "DefaultServiceGetNotDefined";
18+
export const UseDefaultServiceGetNotDefinedKeyFn = () => [useDefaultServiceGetNotDefinedKey];
1419
export type DefaultServiceFindPetByIdDefaultResponse = Awaited<ReturnType<typeof DefaultService.findPetById>>;
1520
export type DefaultServiceFindPetByIdQueryResult<TData = DefaultServiceFindPetByIdDefaultResponse, TError = unknown> = UseQueryResult<TData, TError>;
1621
export const useDefaultServiceFindPetByIdKey = "DefaultServiceFindPetById";
22+
export const UseDefaultServiceFindPetByIdKeyFn = ({ id }: {
23+
id: number;
24+
}, queryKey?: Array<unknown>) => [useDefaultServiceFindPetByIdKey, ...(queryKey ?? [{ id }])];
1725
export type DefaultServiceAddPetMutationResult = Awaited<ReturnType<typeof DefaultService.addPet>>;
1826
export type DefaultServicePostNotDefinedMutationResult = Awaited<ReturnType<typeof DefaultService.postNotDefined>>;
1927
export type DefaultServiceDeletePetMutationResult = Awaited<ReturnType<typeof DefaultService.deletePet>>;
@@ -51,14 +59,14 @@ import * as Common from "./common";
5159
export const useDefaultServiceFindPets = <TData = Common.DefaultServiceFindPetsDefaultResponse, TError = unknown, TQueryKey extends Array<unknown> = unknown[]>({ limit, tags }: {
5260
limit?: number;
5361
tags?: string[];
54-
} = {}, queryKey?: TQueryKey, options?: Omit<UseQueryOptions<TData, TError>, "queryKey" | "queryFn">) => useQuery<TData, TError>({ queryKey: [Common.useDefaultServiceFindPetsKey, ...(queryKey ?? [{ limit, tags }])], queryFn: () => DefaultService.findPets({ limit, tags }) as TData, ...options });
62+
} = {}, queryKey?: TQueryKey, options?: Omit<UseQueryOptions<TData, TError>, "queryKey" | "queryFn">) => useQuery<TData, TError>({ queryKey: Common.UseDefaultServiceFindPetsKeyFn({ limit, tags }, queryKey), queryFn: () => DefaultService.findPets({ limit, tags }) as TData, ...options });
5563
/**
5664
* @deprecated
5765
* This path is not fully defined.
5866
* @returns unknown unexpected error
5967
* @throws ApiError
6068
*/
61-
export const useDefaultServiceGetNotDefined = <TData = Common.DefaultServiceGetNotDefinedDefaultResponse, TError = unknown, TQueryKey extends Array<unknown> = unknown[]>(queryKey?: TQueryKey, options?: Omit<UseQueryOptions<TData, TError>, "queryKey" | "queryFn">) => useQuery<TData, TError>({ queryKey: [Common.useDefaultServiceGetNotDefinedKey, ...(queryKey ?? [])], queryFn: () => DefaultService.getNotDefined() as TData, ...options });
69+
export const useDefaultServiceGetNotDefined = <TData = Common.DefaultServiceGetNotDefinedDefaultResponse, TError = unknown, TQueryKey extends Array<unknown> = unknown[]>(queryKey?: TQueryKey, options?: Omit<UseQueryOptions<TData, TError>, "queryKey" | "queryFn">) => useQuery<TData, TError>({ queryKey: Common.UseDefaultServiceGetNotDefinedKeyFn(), queryFn: () => DefaultService.getNotDefined() as TData, ...options });
6270
/**
6371
* Returns a user based on a single ID, if the user does not have access to the pet
6472
* @param data The data for the request.
@@ -69,7 +77,7 @@ export const useDefaultServiceGetNotDefined = <TData = Common.DefaultServiceGetN
6977
*/
7078
export const useDefaultServiceFindPetById = <TData = Common.DefaultServiceFindPetByIdDefaultResponse, TError = unknown, TQueryKey extends Array<unknown> = unknown[]>({ id }: {
7179
id: number;
72-
}, queryKey?: TQueryKey, options?: Omit<UseQueryOptions<TData, TError>, "queryKey" | "queryFn">) => useQuery<TData, TError>({ queryKey: [Common.useDefaultServiceFindPetByIdKey, ...(queryKey ?? [{ id }])], queryFn: () => DefaultService.findPetById({ id }) as TData, ...options });
80+
}, queryKey?: TQueryKey, options?: Omit<UseQueryOptions<TData, TError>, "queryKey" | "queryFn">) => useQuery<TData, TError>({ queryKey: Common.UseDefaultServiceFindPetByIdKeyFn({ id }, queryKey), queryFn: () => DefaultService.findPetById({ id }) as TData, ...options });
7381
/**
7482
* Creates a new pet in the store. Duplicates are allowed
7583
* @param data The data for the request.
@@ -128,14 +136,14 @@ import * as Common from "./common";
128136
export const useDefaultServiceFindPetsSuspense = <TData = Common.DefaultServiceFindPetsDefaultResponse, TError = unknown, TQueryKey extends Array<unknown> = unknown[]>({ limit, tags }: {
129137
limit?: number;
130138
tags?: string[];
131-
} = {}, queryKey?: TQueryKey, options?: Omit<UseQueryOptions<TData, TError>, "queryKey" | "queryFn">) => useSuspenseQuery<TData, TError>({ queryKey: [Common.useDefaultServiceFindPetsKey, ...(queryKey ?? [{ limit, tags }])], queryFn: () => DefaultService.findPets({ limit, tags }) as TData, ...options });
139+
} = {}, queryKey?: TQueryKey, options?: Omit<UseQueryOptions<TData, TError>, "queryKey" | "queryFn">) => useSuspenseQuery<TData, TError>({ queryKey: Common.UseDefaultServiceFindPetsKeyFn({ limit, tags }, queryKey), queryFn: () => DefaultService.findPets({ limit, tags }) as TData, ...options });
132140
/**
133141
* @deprecated
134142
* This path is not fully defined.
135143
* @returns unknown unexpected error
136144
* @throws ApiError
137145
*/
138-
export const useDefaultServiceGetNotDefinedSuspense = <TData = Common.DefaultServiceGetNotDefinedDefaultResponse, TError = unknown, TQueryKey extends Array<unknown> = unknown[]>(queryKey?: TQueryKey, options?: Omit<UseQueryOptions<TData, TError>, "queryKey" | "queryFn">) => useSuspenseQuery<TData, TError>({ queryKey: [Common.useDefaultServiceGetNotDefinedKey, ...(queryKey ?? [])], queryFn: () => DefaultService.getNotDefined() as TData, ...options });
146+
export const useDefaultServiceGetNotDefinedSuspense = <TData = Common.DefaultServiceGetNotDefinedDefaultResponse, TError = unknown, TQueryKey extends Array<unknown> = unknown[]>(queryKey?: TQueryKey, options?: Omit<UseQueryOptions<TData, TError>, "queryKey" | "queryFn">) => useSuspenseQuery<TData, TError>({ queryKey: Common.UseDefaultServiceGetNotDefinedKeyFn(), queryFn: () => DefaultService.getNotDefined() as TData, ...options });
139147
/**
140148
* Returns a user based on a single ID, if the user does not have access to the pet
141149
* @param data The data for the request.
@@ -146,6 +154,6 @@ export const useDefaultServiceGetNotDefinedSuspense = <TData = Common.DefaultSer
146154
*/
147155
export const useDefaultServiceFindPetByIdSuspense = <TData = Common.DefaultServiceFindPetByIdDefaultResponse, TError = unknown, TQueryKey extends Array<unknown> = unknown[]>({ id }: {
148156
id: number;
149-
}, queryKey?: TQueryKey, options?: Omit<UseQueryOptions<TData, TError>, "queryKey" | "queryFn">) => useSuspenseQuery<TData, TError>({ queryKey: [Common.useDefaultServiceFindPetByIdKey, ...(queryKey ?? [{ id }])], queryFn: () => DefaultService.findPetById({ id }) as TData, ...options });
157+
}, queryKey?: TQueryKey, options?: Omit<UseQueryOptions<TData, TError>, "queryKey" | "queryFn">) => useSuspenseQuery<TData, TError>({ queryKey: Common.UseDefaultServiceFindPetByIdKeyFn({ id }, queryKey), queryFn: () => DefaultService.findPetById({ id }) as TData, ...options });
150158
"
151159
`;

‎tests/createExports.test.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { createExports } from "../src/createExports.mts";
55
import { getServices } from "../src/service.mts";
66
import path from "path";
77

8-
const fileName = "createExports"
8+
const fileName = "createExports";
99

1010
describe(fileName, () => {
1111
beforeAll(async () => await generateTSClients(fileName));
@@ -15,12 +15,12 @@ describe(fileName, () => {
1515
const project = new Project({
1616
skipAddingFilesFromTsConfig: true,
1717
});
18-
project.addSourceFilesAtPaths(path.join(outputPath(fileName), '**', '*'));
18+
project.addSourceFilesAtPaths(path.join(outputPath(fileName), "**", "*"));
1919
const service = await getServices(project);
2020
const exports = createExports(service);
2121

2222
const commonTypes = exports.allCommon
23-
.filter((c) => c.kind === SyntaxKind.TypeAliasDeclaration)
23+
.filter((c) => c.kind === SyntaxKind.TypeAliasDeclaration)
2424
// @ts-ignore
2525
.map((e) => e.name.escapedText);
2626
expect(commonTypes).toStrictEqual([
@@ -36,13 +36,16 @@ describe(fileName, () => {
3636
]);
3737

3838
const constants = exports.allCommon
39-
.filter((c) => c.kind === SyntaxKind.VariableStatement)
40-
// @ts-ignore
39+
.filter((c) => c.kind === SyntaxKind.VariableStatement)
40+
// @ts-ignore
4141
.map((c) => c.declarationList.declarations[0].name.escapedText);
4242
expect(constants).toStrictEqual([
4343
"useDefaultServiceFindPetsKey",
44+
"UseDefaultServiceFindPetsKeyFn",
4445
"useDefaultServiceGetNotDefinedKey",
46+
"UseDefaultServiceGetNotDefinedKeyFn",
4547
"useDefaultServiceFindPetByIdKey",
48+
"UseDefaultServiceFindPetByIdKeyFn",
4649
]);
4750

4851
const mainExports = exports.mainExports.map(

0 commit comments

Comments
 (0)
Please sign in to comment.