Skip to content

Commit 5ff77ef

Browse files
el-jFabian Althausfabien0102
authored
feat: Create Query Functions (eg. for react-router v6.4+) (#122)
* export queryKeyFn from context * genrate Query Fn for React-Router v6 first working version * return operation queries as spreadable array for queryClient.fetchQuery * fix: better return parameters set * queryKeyFn return type: QueryKey * unkown[] return type changed to reactQuery.QueryKey * extract plugin to extra file * export generateReactQueryFunctions from plugin index * write output to {namespace}Functions.ts and cleanup * Update plugins/typescript/src/generators/generateReactQueryComponents.ts Co-authored-by: Fabien BERNARD <[email protected]> * Update plugins/typescript/src/generators/generateReactQueryComponents.ts Co-authored-by: Fabien BERNARD <[email protected]> * use other factory for get requests * inline switch fetch mode in generator * add tests for queryfunctions * Update plugins/typescript/src/templates/context.ts Co-authored-by: Fabien BERNARD <[email protected]> * Update plugins/typescript/src/templates/context.ts Co-authored-by: Fabien BERNARD <[email protected]> * generate all operation types. not just for useQuery/get requests * only generate useQuery functions + update tests Co-authored-by: Fabian Althaus <[email protected]> Co-authored-by: Fabien BERNARD <[email protected]>
1 parent 934ea0c commit 5ff77ef

File tree

5 files changed

+1711
-19
lines changed

5 files changed

+1711
-19
lines changed
Lines changed: 275 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,275 @@
1+
import { camelCase } from "lodash";
2+
import { OperationObject } from "openapi3-ts";
3+
import ts, { factory as f } from "typescript";
4+
5+
/**
6+
* Create the declaration of the react-router queries.
7+
*
8+
* @returns Array of nodes
9+
*/
10+
export const createOperationQueryFnNodes = ({
11+
operationFetcherFnName,
12+
dataType,
13+
errorType,
14+
requestBodyType,
15+
queryParamsType,
16+
pathParamsType,
17+
headersType,
18+
variablesType,
19+
fetcherFn,
20+
operation,
21+
url,
22+
verb,
23+
name,
24+
}: {
25+
operationFetcherFnName: string;
26+
dataType: ts.TypeNode;
27+
errorType: ts.TypeNode;
28+
requestBodyType: ts.TypeNode;
29+
headersType: ts.TypeNode;
30+
pathParamsType: ts.TypeNode;
31+
queryParamsType: ts.TypeNode;
32+
variablesType: ts.TypeNode;
33+
operation: OperationObject;
34+
fetcherFn: string;
35+
url: string;
36+
verb: string;
37+
name: string;
38+
}) => {
39+
const nodes: ts.Node[] = [];
40+
if (operation.description) {
41+
nodes.push(f.createJSDocComment(operation.description.trim(), []));
42+
}
43+
44+
nodes.push(
45+
f.createVariableStatement(
46+
[f.createModifier(ts.SyntaxKind.ExportKeyword)],
47+
f.createVariableDeclarationList(
48+
[
49+
f.createVariableDeclaration(
50+
f.createIdentifier(camelCase(name)),
51+
undefined,
52+
undefined,
53+
f.createArrowFunction(
54+
undefined,
55+
undefined,
56+
[
57+
f.createParameterDeclaration(
58+
undefined,
59+
undefined,
60+
f.createIdentifier("variables"),
61+
undefined,
62+
variablesType,
63+
undefined
64+
),
65+
],
66+
f.createTupleTypeNode([
67+
f.createTypeReferenceNode(
68+
f.createQualifiedName(
69+
f.createIdentifier("reactQuery"),
70+
f.createIdentifier("QueryKey")
71+
),
72+
undefined
73+
),
74+
f.createFunctionTypeNode(
75+
undefined,
76+
verb === "get"
77+
? [
78+
f.createParameterDeclaration(
79+
undefined,
80+
undefined,
81+
f.createObjectBindingPattern([
82+
f.createBindingElement(
83+
undefined,
84+
undefined,
85+
f.createIdentifier("signal"),
86+
undefined
87+
),
88+
]),
89+
undefined,
90+
f.createTypeLiteralNode([
91+
f.createPropertySignature(
92+
undefined,
93+
f.createIdentifier("signal"),
94+
f.createToken(ts.SyntaxKind.QuestionToken),
95+
f.createTypeReferenceNode(
96+
f.createIdentifier("AbortSignal"),
97+
undefined
98+
)
99+
),
100+
]),
101+
undefined
102+
),
103+
]
104+
: [
105+
f.createParameterDeclaration(
106+
undefined,
107+
undefined,
108+
f.createObjectBindingPattern([
109+
f.createBindingElement(
110+
undefined,
111+
undefined,
112+
f.createIdentifier("variables"),
113+
undefined
114+
),
115+
f.createBindingElement(
116+
undefined,
117+
undefined,
118+
f.createIdentifier("signal"),
119+
undefined
120+
),
121+
]),
122+
undefined,
123+
f.createTypeLiteralNode([
124+
f.createPropertySignature(
125+
undefined,
126+
f.createIdentifier("variables"),
127+
undefined,
128+
variablesType
129+
),
130+
f.createPropertySignature(
131+
undefined,
132+
f.createIdentifier("signal"),
133+
f.createToken(ts.SyntaxKind.QuestionToken),
134+
f.createTypeReferenceNode(
135+
f.createIdentifier("AbortSignal"),
136+
undefined
137+
)
138+
),
139+
]),
140+
undefined
141+
),
142+
],
143+
f.createTypeReferenceNode(f.createIdentifier("Promise"), [
144+
dataType,
145+
])
146+
),
147+
]),
148+
f.createToken(ts.SyntaxKind.EqualsGreaterThanToken),
149+
f.createArrayLiteralExpression(
150+
[
151+
f.createCallExpression(
152+
f.createIdentifier("queryKeyFn"),
153+
undefined,
154+
[
155+
f.createObjectLiteralExpression(
156+
[
157+
f.createPropertyAssignment(
158+
f.createIdentifier("path"),
159+
f.createStringLiteral(url)
160+
),
161+
f.createPropertyAssignment(
162+
f.createIdentifier("operationId"),
163+
f.createStringLiteral(
164+
operation.operationId as string
165+
)
166+
),
167+
f.createShorthandPropertyAssignment(
168+
f.createIdentifier("variables"),
169+
undefined
170+
),
171+
],
172+
true
173+
),
174+
]
175+
),
176+
f.createArrowFunction(
177+
[f.createModifier(ts.SyntaxKind.AsyncKeyword)],
178+
undefined,
179+
verb === "get"
180+
? [
181+
f.createParameterDeclaration(
182+
undefined,
183+
undefined,
184+
f.createObjectBindingPattern([
185+
f.createBindingElement(
186+
undefined,
187+
undefined,
188+
f.createIdentifier("signal"),
189+
undefined
190+
),
191+
]),
192+
undefined,
193+
f.createTypeLiteralNode([
194+
f.createPropertySignature(
195+
undefined,
196+
f.createIdentifier("signal"),
197+
f.createToken(ts.SyntaxKind.QuestionToken),
198+
f.createTypeReferenceNode(
199+
f.createIdentifier("AbortSignal"),
200+
undefined
201+
)
202+
),
203+
]),
204+
undefined
205+
),
206+
]
207+
: [
208+
f.createParameterDeclaration(
209+
undefined,
210+
undefined,
211+
f.createObjectBindingPattern([
212+
f.createBindingElement(
213+
undefined,
214+
undefined,
215+
f.createIdentifier("variables"),
216+
undefined
217+
),
218+
f.createBindingElement(
219+
undefined,
220+
undefined,
221+
f.createIdentifier("signal"),
222+
undefined
223+
),
224+
]),
225+
undefined,
226+
f.createTypeLiteralNode([
227+
f.createPropertySignature(
228+
undefined,
229+
f.createIdentifier("variables"),
230+
undefined,
231+
variablesType
232+
),
233+
f.createPropertySignature(
234+
undefined,
235+
f.createIdentifier("signal"),
236+
f.createToken(ts.SyntaxKind.QuestionToken),
237+
f.createTypeReferenceNode(
238+
f.createIdentifier("AbortSignal"),
239+
undefined
240+
)
241+
),
242+
]),
243+
undefined
244+
),
245+
],
246+
undefined,
247+
f.createToken(ts.SyntaxKind.EqualsGreaterThanToken),
248+
f.createCallExpression(
249+
f.createIdentifier(operationFetcherFnName),
250+
undefined,
251+
[
252+
f.createObjectLiteralExpression(
253+
[
254+
f.createSpreadAssignment(
255+
f.createIdentifier("variables")
256+
),
257+
],
258+
false
259+
),
260+
f.createIdentifier("signal"),
261+
]
262+
)
263+
),
264+
],
265+
true
266+
)
267+
)
268+
),
269+
],
270+
ts.NodeFlags.Const
271+
)
272+
)
273+
);
274+
return nodes;
275+
};

0 commit comments

Comments
 (0)