wasm-ast-types 0.12.0 → 0.14.0
Sign up to get free protection for your applications and to get access to all the features.
- package/main/context/context.js +2 -1
- package/main/react-query/react-query.js +140 -70
- package/main/react-query/react-query.spec.js +13 -0
- package/module/context/context.js +2 -1
- package/module/react-query/react-query.js +92 -22
- package/module/react-query/react-query.spec.js +13 -0
- package/package.json +2 -2
- package/src/client/client.ts +1 -1
- package/src/context/context.ts +4 -1
- package/src/react-query/__snapshots__/react-query.spec.ts.snap +396 -0
- package/src/react-query/react-query.spec.ts +17 -0
- package/src/react-query/react-query.ts +902 -750
- package/types/client/client.d.ts +1 -1
- package/types/context/context.d.ts +2 -0
@@ -2,283 +2,370 @@ import type { Expression } from '@babel/types';
|
|
2
2
|
import * as t from '@babel/types';
|
3
3
|
import { camel, pascal } from 'case';
|
4
4
|
import { ExecuteMsg, QueryMsg } from '../types';
|
5
|
-
import { callExpression, getMessageProperties, identifier, tsObjectPattern, tsPropertySignature } from '../utils';
|
6
5
|
import {
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
6
|
+
callExpression,
|
7
|
+
getMessageProperties,
|
8
|
+
identifier,
|
9
|
+
tsObjectPattern,
|
10
|
+
tsPropertySignature
|
11
|
+
} from '../utils';
|
12
|
+
import {
|
13
|
+
omitTypeReference,
|
14
|
+
optionalConditionalExpression,
|
15
|
+
propertySignature,
|
16
|
+
shorthandProperty
|
11
17
|
} from '../utils/babel';
|
12
|
-
import {
|
18
|
+
import {
|
19
|
+
getParamsTypeAnnotation,
|
20
|
+
getPropertyType,
|
21
|
+
getResponseType
|
22
|
+
} from '../utils/types';
|
13
23
|
import { ReactQueryOptions, RenderContext } from '../context';
|
14
24
|
import { JSONSchema } from '../types';
|
15
25
|
import { FIXED_EXECUTE_PARAMS } from '../client';
|
26
|
+
import { ArrowFunctionExpression, objectExpression } from '@babel/types';
|
16
27
|
|
17
28
|
interface ReactQueryHookQuery {
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
29
|
+
context: RenderContext;
|
30
|
+
hookName: string;
|
31
|
+
hookParamsTypeName: string;
|
32
|
+
hookKeyName: string;
|
33
|
+
queryKeysName: string;
|
34
|
+
responseType: string;
|
35
|
+
methodName: string;
|
36
|
+
jsonschema: any;
|
37
|
+
}
|
38
|
+
|
39
|
+
interface ParsedQueryMsg {
|
40
|
+
underscoreName: string;
|
41
|
+
methodName: string;
|
42
|
+
hookParamsTypeName: string;
|
43
|
+
hookName: string;
|
44
|
+
responseType: string;
|
45
|
+
getterKey: string;
|
46
|
+
jsonschema: any;
|
47
|
+
// jsonschema: JSONSchema;
|
26
48
|
}
|
27
49
|
|
28
50
|
interface ReactQueryHooks {
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
51
|
+
context: RenderContext;
|
52
|
+
queryMsg: QueryMsg;
|
53
|
+
contractName: string;
|
54
|
+
QueryClient: string;
|
33
55
|
}
|
34
56
|
|
35
57
|
export const createReactQueryHooks = ({
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
58
|
+
context,
|
59
|
+
queryMsg,
|
60
|
+
contractName,
|
61
|
+
QueryClient
|
40
62
|
}: ReactQueryHooks) => {
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
63
|
+
const options = context.options.reactQuery;
|
64
|
+
|
65
|
+
const genericQueryInterfaceName = `${pascal(contractName)}ReactQuery`;
|
66
|
+
const underscoreNames: string[] = getMessageProperties(queryMsg).map(
|
67
|
+
(schema) => Object.keys(schema.properties)[0]
|
68
|
+
);
|
69
|
+
|
70
|
+
const body = [];
|
71
|
+
|
72
|
+
const queryKeysName = `${camel(contractName)}QueryKeys`;
|
73
|
+
if (options.queryKeys) {
|
74
|
+
body.push(
|
75
|
+
createReactQueryKeys({
|
76
|
+
context,
|
77
|
+
queryKeysName,
|
78
|
+
camelContractName: camel(contractName),
|
79
|
+
underscoreNames
|
80
|
+
})
|
81
|
+
);
|
82
|
+
}
|
83
|
+
|
84
|
+
const queryMsgs: ParsedQueryMsg[] = getMessageProperties(queryMsg).map(
|
85
|
+
(schema) => {
|
86
|
+
// list_voters
|
87
|
+
const underscoreName = Object.keys(schema.properties)[0];
|
88
|
+
// listVoters
|
89
|
+
const methodName = camel(underscoreName);
|
90
|
+
// Cw3FlexMultisigListVotersQuery
|
91
|
+
const hookParamsTypeName = `${pascal(contractName)}${pascal(
|
92
|
+
methodName
|
93
|
+
)}Query`;
|
94
|
+
// useCw3FlexMultisigListVotersQuery
|
95
|
+
const hookName = `use${hookParamsTypeName}`;
|
96
|
+
// listVotersResponse
|
97
|
+
const responseType = getResponseType(context, underscoreName);
|
98
|
+
// cw3FlexMultisigListVoters
|
99
|
+
const getterKey = camel(`${contractName}${pascal(methodName)}`);
|
100
|
+
const jsonschema = schema.properties[underscoreName];
|
101
|
+
|
102
|
+
return {
|
103
|
+
underscoreName,
|
104
|
+
methodName,
|
105
|
+
hookParamsTypeName,
|
106
|
+
hookName,
|
107
|
+
responseType,
|
108
|
+
getterKey,
|
109
|
+
jsonschema
|
110
|
+
};
|
57
111
|
}
|
112
|
+
);
|
58
113
|
|
114
|
+
const queryFactoryName = `${camel(contractName)}Queries`;
|
115
|
+
if (options.queryFactory) {
|
59
116
|
body.push(
|
60
|
-
|
117
|
+
createReactQueryFactory({
|
118
|
+
context,
|
119
|
+
queryFactoryName,
|
120
|
+
queryKeysName,
|
121
|
+
queryMsgs,
|
122
|
+
})
|
123
|
+
);
|
124
|
+
}
|
125
|
+
|
126
|
+
body.push(
|
127
|
+
createReactQueryHookGenericInterface({
|
128
|
+
context,
|
129
|
+
QueryClient,
|
130
|
+
genericQueryInterfaceName
|
131
|
+
})
|
132
|
+
);
|
133
|
+
|
134
|
+
body.push(
|
135
|
+
...queryMsgs.reduce(
|
136
|
+
(
|
137
|
+
m,
|
138
|
+
{
|
139
|
+
methodName,
|
140
|
+
hookParamsTypeName,
|
141
|
+
hookName,
|
142
|
+
responseType,
|
143
|
+
getterKey,
|
144
|
+
jsonschema
|
145
|
+
}
|
146
|
+
) => {
|
147
|
+
return [
|
148
|
+
createReactQueryHookInterface({
|
61
149
|
context,
|
150
|
+
hookParamsTypeName,
|
151
|
+
responseType,
|
152
|
+
queryInterfaceName: genericQueryInterfaceName,
|
62
153
|
QueryClient,
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
return [
|
82
|
-
createReactQueryHookInterface({
|
83
|
-
context,
|
84
|
-
hookParamsTypeName,
|
85
|
-
responseType,
|
86
|
-
queryInterfaceName: genericQueryInterfaceName,
|
87
|
-
QueryClient,
|
88
|
-
jsonschema,
|
89
|
-
}),
|
90
|
-
createReactQueryHook({
|
91
|
-
context,
|
92
|
-
methodName,
|
93
|
-
hookName,
|
94
|
-
hookParamsTypeName,
|
95
|
-
queryKeysName,
|
96
|
-
responseType,
|
97
|
-
hookKeyName: getterKey,
|
98
|
-
jsonschema
|
99
|
-
}),
|
100
|
-
...m,
|
101
|
-
]
|
102
|
-
}, [])
|
103
|
-
);
|
154
|
+
jsonschema
|
155
|
+
}),
|
156
|
+
createReactQueryHook({
|
157
|
+
context,
|
158
|
+
methodName,
|
159
|
+
hookName,
|
160
|
+
hookParamsTypeName,
|
161
|
+
queryKeysName,
|
162
|
+
responseType,
|
163
|
+
hookKeyName: getterKey,
|
164
|
+
jsonschema
|
165
|
+
}),
|
166
|
+
...m
|
167
|
+
];
|
168
|
+
},
|
169
|
+
[]
|
170
|
+
)
|
171
|
+
);
|
104
172
|
|
105
|
-
|
173
|
+
return body;
|
106
174
|
};
|
107
175
|
|
176
|
+
function buildQueryFn(
|
177
|
+
methodName: string,
|
178
|
+
jsonschema: any,
|
179
|
+
options: ReactQueryOptions
|
180
|
+
): ArrowFunctionExpression {
|
181
|
+
const keys = Object.keys(jsonschema.properties ?? {});
|
182
|
+
let args = [];
|
183
|
+
if (keys.length) {
|
184
|
+
args = [
|
185
|
+
t.objectExpression([
|
186
|
+
...keys.map((prop) => {
|
187
|
+
return t.objectProperty(
|
188
|
+
t.identifier(camel(prop)),
|
189
|
+
t.memberExpression(t.identifier('args'), t.identifier(camel(prop)))
|
190
|
+
);
|
191
|
+
})
|
192
|
+
])
|
193
|
+
];
|
194
|
+
}
|
108
195
|
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
196
|
+
const rejectInvalidClient = t.callExpression(
|
197
|
+
t.memberExpression(t.identifier('Promise'), t.identifier('reject')),
|
198
|
+
[
|
199
|
+
t.newExpression(t.identifier('Error'), [
|
200
|
+
t.stringLiteral('Invalid client')
|
201
|
+
])
|
202
|
+
]
|
203
|
+
);
|
204
|
+
|
205
|
+
return t.arrowFunctionExpression(
|
206
|
+
[],
|
207
|
+
optionalConditionalExpression(
|
208
|
+
t.identifier('client'),
|
209
|
+
t.callExpression(
|
210
|
+
t.memberExpression(t.identifier('client'), t.identifier(methodName)),
|
211
|
+
args
|
212
|
+
),
|
213
|
+
rejectInvalidClient,
|
214
|
+
options.optionalClient
|
215
|
+
),
|
216
|
+
false
|
217
|
+
);
|
218
|
+
}
|
119
219
|
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
220
|
+
const ENABLED_QUERY_OPTION = t.objectProperty(
|
221
|
+
t.identifier('enabled'),
|
222
|
+
t.logicalExpression(
|
223
|
+
'&&',
|
224
|
+
t.unaryExpression('!', t.unaryExpression('!', t.identifier('client'))),
|
225
|
+
t.conditionalExpression(
|
226
|
+
// explicitly check for undefined
|
227
|
+
t.binaryExpression(
|
228
|
+
'!=',
|
229
|
+
t.optionalMemberExpression(
|
230
|
+
t.identifier('options'),
|
231
|
+
t.identifier('enabled'),
|
232
|
+
false,
|
233
|
+
true
|
234
|
+
),
|
235
|
+
t.identifier('undefined')
|
236
|
+
),
|
237
|
+
t.memberExpression(t.identifier('options'), t.identifier('enabled')),
|
238
|
+
t.booleanLiteral(true)
|
239
|
+
)
|
240
|
+
)
|
241
|
+
);
|
141
242
|
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
243
|
+
function buildQueryOptions(options: ReactQueryOptions) {
|
244
|
+
return options.optionalClient
|
245
|
+
? t.objectExpression([
|
246
|
+
t.spreadElement(t.identifier('options')),
|
247
|
+
t.objectProperty(
|
248
|
+
t.identifier('enabled'),
|
249
|
+
t.logicalExpression(
|
250
|
+
'&&',
|
251
|
+
t.unaryExpression(
|
252
|
+
'!',
|
253
|
+
t.unaryExpression('!', t.identifier('client'))
|
254
|
+
),
|
255
|
+
t.conditionalExpression(
|
256
|
+
// explicitly check for undefined
|
257
|
+
t.binaryExpression(
|
258
|
+
'!=',
|
259
|
+
t.optionalMemberExpression(
|
260
|
+
t.identifier('options'),
|
261
|
+
t.identifier('enabled'),
|
262
|
+
false,
|
263
|
+
true
|
264
|
+
),
|
265
|
+
t.identifier('undefined')
|
266
|
+
),
|
267
|
+
t.memberExpression(
|
268
|
+
t.identifier('options'),
|
269
|
+
t.identifier('enabled')
|
270
|
+
),
|
271
|
+
t.booleanLiteral(true)
|
272
|
+
)
|
273
|
+
)
|
274
|
+
)
|
275
|
+
])
|
276
|
+
: t.identifier('options');
|
277
|
+
}
|
146
278
|
|
147
|
-
|
279
|
+
export const createReactQueryHook = ({
|
280
|
+
context,
|
281
|
+
hookName,
|
282
|
+
hookParamsTypeName,
|
283
|
+
responseType,
|
284
|
+
hookKeyName,
|
285
|
+
queryKeysName,
|
286
|
+
methodName,
|
287
|
+
jsonschema
|
288
|
+
}: ReactQueryHookQuery) => {
|
289
|
+
context.addUtil('useQuery');
|
290
|
+
context.addUtil('UseQueryOptions');
|
148
291
|
|
149
|
-
|
150
|
-
|
151
|
-
t.identifier(hookName),
|
152
|
-
[
|
153
|
-
tsObjectPattern(
|
154
|
-
[
|
155
|
-
...props.map(prop => {
|
156
|
-
return t.objectProperty(
|
157
|
-
t.identifier(prop),
|
158
|
-
t.identifier(prop),
|
159
|
-
false,
|
160
|
-
true
|
161
|
-
)
|
162
|
-
})
|
163
|
-
],
|
164
|
-
t.tsTypeAnnotation(t.tsTypeReference(
|
165
|
-
t.identifier(hookParamsTypeName),
|
166
|
-
t.tsTypeParameterInstantiation([
|
167
|
-
t.tsTypeReference(t.identifier(selectResponseGenericTypeName))
|
168
|
-
])
|
169
|
-
))
|
170
|
-
)
|
171
|
-
],
|
172
|
-
t.blockStatement(
|
173
|
-
[
|
292
|
+
const options = context.options.reactQuery;
|
293
|
+
const keys = Object.keys(jsonschema.properties ?? {});
|
174
294
|
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
generateUseQueryQueryKey({ hookKeyName, queryKeysName, methodName, props, options }),
|
180
|
-
t.arrowFunctionExpression(
|
181
|
-
[],
|
182
|
-
optionalConditionalExpression(
|
183
|
-
t.identifier('client'),
|
184
|
-
t.callExpression(
|
185
|
-
t.memberExpression(
|
186
|
-
t.identifier('client'),
|
187
|
-
t.identifier(methodName)
|
188
|
-
),
|
189
|
-
args
|
190
|
-
),
|
191
|
-
t.callExpression(
|
192
|
-
t.memberExpression(
|
193
|
-
t.identifier('Promise'),
|
194
|
-
t.identifier('reject'),
|
195
|
-
),
|
196
|
-
[
|
197
|
-
t.newExpression(
|
198
|
-
t.identifier('Error'),
|
199
|
-
[
|
200
|
-
t.stringLiteral('Invalid client')
|
201
|
-
]
|
202
|
-
)
|
203
|
-
]
|
204
|
-
),
|
205
|
-
options.optionalClient
|
206
|
-
),
|
207
|
-
false
|
208
|
-
),
|
209
|
-
options.optionalClient
|
210
|
-
? t.objectExpression([
|
211
|
-
t.spreadElement(t.identifier('options')),
|
212
|
-
t.objectProperty(
|
213
|
-
t.identifier('enabled'),
|
214
|
-
t.logicalExpression(
|
215
|
-
'&&',
|
216
|
-
t.unaryExpression(
|
217
|
-
'!',
|
218
|
-
t.unaryExpression('!', t.identifier('client'))
|
219
|
-
),
|
220
|
-
t.conditionalExpression(
|
221
|
-
// explicitly check for undefined
|
222
|
-
t.binaryExpression(
|
223
|
-
'!=',
|
224
|
-
t.optionalMemberExpression(
|
225
|
-
t.identifier('options'),
|
226
|
-
t.identifier('enabled'),
|
227
|
-
false,
|
228
|
-
true
|
229
|
-
),
|
230
|
-
t.identifier('undefined')
|
231
|
-
),
|
232
|
-
t.memberExpression(
|
233
|
-
t.identifier('options'),
|
234
|
-
t.identifier('enabled')
|
235
|
-
),
|
236
|
-
t.booleanLiteral(true)
|
237
|
-
)
|
238
|
-
|
239
|
-
)),
|
240
|
-
])
|
241
|
-
: t.identifier('options'),
|
242
|
-
],
|
243
|
-
t.tsTypeParameterInstantiation(
|
244
|
-
[
|
245
|
-
t.tsTypeReference(
|
246
|
-
t.identifier(responseType)
|
247
|
-
),
|
248
|
-
t.tsTypeReference(
|
249
|
-
t.identifier('Error')
|
250
|
-
),
|
251
|
-
t.tsTypeReference(
|
252
|
-
t.identifier(selectResponseGenericTypeName)
|
253
|
-
)
|
254
|
-
]
|
255
|
-
)
|
256
|
-
)
|
257
|
-
)
|
295
|
+
let props = ['client', 'options'];
|
296
|
+
if (keys.length) {
|
297
|
+
props = ['client', 'args', 'options'];
|
298
|
+
}
|
258
299
|
|
259
|
-
|
260
|
-
|
300
|
+
const selectResponseGenericTypeName = GENERIC_SELECT_RESPONSE_NAME;
|
301
|
+
|
302
|
+
const queryFunctionDeclaration = t.functionDeclaration(
|
303
|
+
t.identifier(hookName),
|
304
|
+
[
|
305
|
+
tsObjectPattern(
|
306
|
+
[
|
307
|
+
...props.map((prop) => {
|
308
|
+
return t.objectProperty(
|
309
|
+
t.identifier(prop),
|
310
|
+
t.identifier(prop),
|
311
|
+
false,
|
312
|
+
true
|
313
|
+
);
|
314
|
+
})
|
315
|
+
],
|
316
|
+
t.tsTypeAnnotation(
|
317
|
+
t.tsTypeReference(
|
318
|
+
t.identifier(hookParamsTypeName),
|
319
|
+
t.tsTypeParameterInstantiation([
|
320
|
+
t.tsTypeReference(t.identifier(selectResponseGenericTypeName))
|
321
|
+
])
|
322
|
+
)
|
261
323
|
)
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
324
|
+
)
|
325
|
+
],
|
326
|
+
t.blockStatement([
|
327
|
+
t.returnStatement(
|
328
|
+
callExpression(
|
329
|
+
t.identifier('useQuery'),
|
330
|
+
[
|
331
|
+
generateUseQueryQueryKey({
|
332
|
+
hookKeyName,
|
333
|
+
queryKeysName,
|
334
|
+
methodName,
|
335
|
+
props,
|
336
|
+
options
|
337
|
+
}),
|
338
|
+
buildQueryFn(methodName, jsonschema, options),
|
339
|
+
buildQueryOptions(options)
|
340
|
+
],
|
341
|
+
t.tsTypeParameterInstantiation([
|
342
|
+
t.tsTypeReference(t.identifier(responseType)),
|
343
|
+
t.tsTypeReference(t.identifier('Error')),
|
344
|
+
t.tsTypeReference(t.identifier(selectResponseGenericTypeName))
|
345
|
+
])
|
269
346
|
)
|
347
|
+
)
|
270
348
|
])
|
349
|
+
);
|
350
|
+
|
351
|
+
// Add the TData type parameters
|
352
|
+
queryFunctionDeclaration.typeParameters = t.tsTypeParameterDeclaration([
|
353
|
+
t.tsTypeParameter(
|
354
|
+
undefined,
|
355
|
+
t.tSTypeReference(t.identifier(responseType)),
|
356
|
+
selectResponseGenericTypeName
|
357
|
+
)
|
358
|
+
]);
|
271
359
|
|
272
|
-
|
273
|
-
|
360
|
+
return t.exportNamedDeclaration(queryFunctionDeclaration);
|
274
361
|
};
|
275
362
|
|
276
363
|
interface ReactQueryMutationHookInterface {
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
364
|
+
context: RenderContext;
|
365
|
+
ExecuteClient: string;
|
366
|
+
mutationHookParamsTypeName: string;
|
367
|
+
jsonschema: JSONSchema;
|
368
|
+
useMutationTypeParameter: t.TSTypeParameterInstantiation;
|
282
369
|
}
|
283
370
|
|
284
371
|
/**
|
@@ -298,167 +385,155 @@ export interface Cw4UpdateMembersMutation {
|
|
298
385
|
```
|
299
386
|
*/
|
300
387
|
export const createReactQueryMutationArgsInterface = ({
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
388
|
+
context,
|
389
|
+
ExecuteClient,
|
390
|
+
mutationHookParamsTypeName,
|
391
|
+
useMutationTypeParameter,
|
392
|
+
jsonschema
|
306
393
|
}: ReactQueryMutationHookInterface) => {
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
394
|
+
const typedUseMutationOptions = t.tsTypeReference(
|
395
|
+
t.identifier('UseMutationOptions'),
|
396
|
+
useMutationTypeParameter
|
397
|
+
);
|
398
|
+
|
399
|
+
const body = [
|
400
|
+
tsPropertySignature(
|
401
|
+
t.identifier('client'),
|
402
|
+
t.tsTypeAnnotation(t.tsTypeReference(t.identifier(ExecuteClient))),
|
403
|
+
false
|
311
404
|
)
|
405
|
+
];
|
312
406
|
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
t.tsTypeReference(
|
318
|
-
t.identifier(ExecuteClient)
|
319
|
-
)
|
320
|
-
),
|
321
|
-
false
|
322
|
-
),
|
323
|
-
]
|
324
|
-
|
325
|
-
const msgType: t.TSTypeAnnotation = getParamsTypeAnnotation(context, jsonschema)
|
326
|
-
|
327
|
-
if (msgType) {
|
328
|
-
body.push(
|
329
|
-
t.tsPropertySignature(
|
330
|
-
t.identifier('msg'),
|
331
|
-
msgType
|
332
|
-
))
|
333
|
-
}
|
407
|
+
const msgType: t.TSTypeAnnotation = getParamsTypeAnnotation(
|
408
|
+
context,
|
409
|
+
jsonschema
|
410
|
+
);
|
334
411
|
|
335
|
-
|
336
|
-
|
337
|
-
|
412
|
+
if (msgType) {
|
413
|
+
body.push(t.tsPropertySignature(t.identifier('msg'), msgType));
|
414
|
+
}
|
338
415
|
|
339
|
-
|
340
|
-
|
341
|
-
|
416
|
+
context.addUtil('StdFee');
|
417
|
+
context.addUtil('Coin');
|
418
|
+
// fee: number | StdFee | "auto" = "auto", memo?: string, funds?: Coin[]
|
419
|
+
|
420
|
+
const optionalArgs = t.tsPropertySignature(
|
421
|
+
t.identifier('args'),
|
422
|
+
t.tsTypeAnnotation(
|
423
|
+
// @ts-ignore:next-line
|
424
|
+
t.tsTypeLiteral(
|
425
|
+
FIXED_EXECUTE_PARAMS.map((param) =>
|
426
|
+
propertySignature(
|
427
|
+
param.name,
|
342
428
|
// @ts-ignore:next-line
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
param.typeAnnotation,
|
347
|
-
param.optional
|
348
|
-
)))
|
429
|
+
param.typeAnnotation,
|
430
|
+
param.optional
|
431
|
+
)
|
349
432
|
)
|
433
|
+
)
|
350
434
|
)
|
435
|
+
);
|
351
436
|
|
352
|
-
|
353
|
-
|
354
|
-
body.push(optionalArgs)
|
437
|
+
optionalArgs.optional = true;
|
355
438
|
|
439
|
+
body.push(optionalArgs);
|
356
440
|
|
357
|
-
|
358
|
-
|
359
|
-
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
|
364
|
-
|
441
|
+
return t.exportNamedDeclaration(
|
442
|
+
t.tsInterfaceDeclaration(
|
443
|
+
t.identifier(mutationHookParamsTypeName),
|
444
|
+
null,
|
445
|
+
[],
|
446
|
+
t.tsInterfaceBody(body)
|
447
|
+
)
|
448
|
+
);
|
365
449
|
};
|
366
450
|
|
367
|
-
|
368
451
|
interface ReactQueryMutationHooks {
|
369
|
-
|
370
|
-
|
371
|
-
|
372
|
-
|
452
|
+
context: RenderContext;
|
453
|
+
execMsg: ExecuteMsg;
|
454
|
+
contractName: string;
|
455
|
+
ExecuteClient: string;
|
373
456
|
}
|
374
457
|
|
375
458
|
export const createReactQueryMutationHooks = ({
|
376
|
-
|
377
|
-
|
378
|
-
|
379
|
-
|
459
|
+
context,
|
460
|
+
execMsg,
|
461
|
+
contractName,
|
462
|
+
ExecuteClient
|
380
463
|
}: ReactQueryMutationHooks) => {
|
381
|
-
|
382
|
-
|
383
|
-
|
384
|
-
|
385
|
-
|
386
|
-
|
387
|
-
|
388
|
-
|
389
|
-
|
390
|
-
|
391
|
-
|
392
|
-
|
393
|
-
|
394
|
-
|
395
|
-
|
396
|
-
|
397
|
-
|
398
|
-
|
399
|
-
|
400
|
-
|
401
|
-
|
402
|
-
|
403
|
-
|
404
|
-
|
405
|
-
|
464
|
+
// merge the user options with the defaults
|
465
|
+
return getMessageProperties(execMsg).reduce((m, schema) => {
|
466
|
+
// update_members
|
467
|
+
const execMethodUnderscoreName = Object.keys(schema.properties)[0];
|
468
|
+
// updateMembers
|
469
|
+
const execMethodName = camel(execMethodUnderscoreName);
|
470
|
+
// Cw20UpdateMembersMutation
|
471
|
+
const mutationHookParamsTypeName = `${pascal(contractName)}${pascal(
|
472
|
+
execMethodName
|
473
|
+
)}Mutation`;
|
474
|
+
// useCw20UpdateMembersMutation
|
475
|
+
const mutationHookName = `use${mutationHookParamsTypeName}`;
|
476
|
+
|
477
|
+
const jsonschema = schema.properties[execMethodUnderscoreName];
|
478
|
+
|
479
|
+
const properties = jsonschema.properties ?? {};
|
480
|
+
|
481
|
+
// TODO: there should be a better way to do this
|
482
|
+
const hasMsg = !!(Object.keys(properties)?.length || jsonschema?.$ref);
|
483
|
+
|
484
|
+
// <ExecuteResult, Error, Cw4UpdateMembersMutation>
|
485
|
+
const useMutationTypeParameter = generateMutationTypeParameter(
|
486
|
+
context,
|
487
|
+
mutationHookParamsTypeName
|
488
|
+
);
|
406
489
|
|
407
|
-
|
408
|
-
|
409
|
-
|
410
|
-
|
411
|
-
|
412
|
-
|
413
|
-
|
414
|
-
|
415
|
-
|
416
|
-
|
417
|
-
|
418
|
-
|
419
|
-
|
420
|
-
|
421
|
-
|
422
|
-
|
423
|
-
|
424
|
-
|
425
|
-
|
490
|
+
return [
|
491
|
+
createReactQueryMutationArgsInterface({
|
492
|
+
context,
|
493
|
+
mutationHookParamsTypeName,
|
494
|
+
ExecuteClient,
|
495
|
+
jsonschema,
|
496
|
+
useMutationTypeParameter
|
497
|
+
}),
|
498
|
+
createReactQueryMutationHook({
|
499
|
+
context,
|
500
|
+
execMethodName,
|
501
|
+
mutationHookName,
|
502
|
+
mutationHookParamsTypeName,
|
503
|
+
hasMsg,
|
504
|
+
useMutationTypeParameter
|
505
|
+
}),
|
506
|
+
...m
|
507
|
+
];
|
508
|
+
}, []);
|
426
509
|
};
|
427
510
|
|
428
511
|
/**
|
429
512
|
* Generates the mutation type parameter. If args exist, we use a pick. If not, we just return the params type.
|
430
513
|
*/
|
431
514
|
const generateMutationTypeParameter = (
|
432
|
-
|
433
|
-
|
515
|
+
context: RenderContext,
|
516
|
+
mutationHookParamsTypeName: string
|
434
517
|
) => {
|
435
|
-
|
436
|
-
|
437
|
-
|
438
|
-
|
439
|
-
|
440
|
-
|
441
|
-
|
442
|
-
|
443
|
-
|
444
|
-
|
445
|
-
|
446
|
-
),
|
447
|
-
// Variables
|
448
|
-
t.tsTypeReference(
|
449
|
-
t.identifier(mutationHookParamsTypeName)
|
450
|
-
)
|
451
|
-
]);
|
452
|
-
}
|
453
|
-
|
518
|
+
context.addUtil('ExecuteResult');
|
519
|
+
|
520
|
+
return t.tsTypeParameterInstantiation([
|
521
|
+
// Data
|
522
|
+
t.tSTypeReference(t.identifier('ExecuteResult')),
|
523
|
+
// Error
|
524
|
+
t.tsTypeReference(t.identifier('Error')),
|
525
|
+
// Variables
|
526
|
+
t.tsTypeReference(t.identifier(mutationHookParamsTypeName))
|
527
|
+
]);
|
528
|
+
};
|
454
529
|
|
455
530
|
interface ReactQueryMutationHook {
|
456
|
-
|
457
|
-
|
458
|
-
|
459
|
-
|
460
|
-
|
461
|
-
|
531
|
+
context: RenderContext;
|
532
|
+
mutationHookName: string;
|
533
|
+
mutationHookParamsTypeName: string;
|
534
|
+
execMethodName: string;
|
535
|
+
useMutationTypeParameter: t.TSTypeParameterInstantiation;
|
536
|
+
hasMsg: boolean;
|
462
537
|
}
|
463
538
|
|
464
539
|
/**
|
@@ -473,441 +548,518 @@ export const useCw4UpdateMembersMutation = ({ client, options }: Omit<Cw4UpdateM
|
|
473
548
|
```
|
474
549
|
*/
|
475
550
|
export const createReactQueryMutationHook = ({
|
476
|
-
|
477
|
-
|
478
|
-
|
479
|
-
|
480
|
-
|
481
|
-
|
551
|
+
context,
|
552
|
+
mutationHookName,
|
553
|
+
mutationHookParamsTypeName,
|
554
|
+
execMethodName,
|
555
|
+
useMutationTypeParameter,
|
556
|
+
hasMsg
|
482
557
|
}: ReactQueryMutationHook) => {
|
483
|
-
|
484
|
-
|
485
|
-
|
486
|
-
|
487
|
-
|
488
|
-
|
489
|
-
|
490
|
-
|
491
|
-
|
492
|
-
|
493
|
-
|
494
|
-
|
558
|
+
context.addUtil('useMutation');
|
559
|
+
context.addUtil('UseMutationOptions');
|
560
|
+
|
561
|
+
const useMutationFunctionArgs = [shorthandProperty('client')];
|
562
|
+
if (hasMsg) useMutationFunctionArgs.push(shorthandProperty('msg'));
|
563
|
+
useMutationFunctionArgs.push(
|
564
|
+
t.objectProperty(
|
565
|
+
t.identifier('args'),
|
566
|
+
t.assignmentPattern(
|
567
|
+
t.objectPattern(
|
568
|
+
FIXED_EXECUTE_PARAMS.map((param) => shorthandProperty(param.name))
|
569
|
+
),
|
570
|
+
t.objectExpression([])
|
571
|
+
)
|
572
|
+
)
|
573
|
+
);
|
574
|
+
|
575
|
+
return t.exportNamedDeclaration(
|
576
|
+
t.functionDeclaration(
|
577
|
+
t.identifier(mutationHookName),
|
578
|
+
[
|
579
|
+
identifier(
|
580
|
+
'options',
|
581
|
+
t.tsTypeAnnotation(
|
582
|
+
omitTypeReference(
|
583
|
+
t.tsTypeReference(
|
584
|
+
t.identifier('UseMutationOptions'),
|
585
|
+
useMutationTypeParameter
|
586
|
+
),
|
587
|
+
'mutationFn'
|
495
588
|
)
|
589
|
+
),
|
590
|
+
true
|
496
591
|
)
|
497
|
-
|
498
|
-
|
499
|
-
|
500
|
-
|
501
|
-
t.identifier(
|
592
|
+
],
|
593
|
+
t.blockStatement([
|
594
|
+
t.returnStatement(
|
595
|
+
callExpression(
|
596
|
+
t.identifier('useMutation'),
|
502
597
|
[
|
503
|
-
|
504
|
-
|
505
|
-
|
506
|
-
|
507
|
-
|
508
|
-
|
509
|
-
|
598
|
+
t.arrowFunctionExpression(
|
599
|
+
[t.objectPattern(useMutationFunctionArgs)],
|
600
|
+
t.callExpression(
|
601
|
+
t.memberExpression(
|
602
|
+
t.identifier('client'),
|
603
|
+
t.identifier(execMethodName)
|
604
|
+
),
|
605
|
+
(hasMsg ? [t.identifier('msg')] : []).concat(
|
606
|
+
FIXED_EXECUTE_PARAMS.map((param) =>
|
607
|
+
t.identifier(param.name)
|
510
608
|
)
|
511
|
-
|
609
|
+
)
|
610
|
+
),
|
611
|
+
false // not async
|
612
|
+
),
|
613
|
+
t.identifier('options')
|
512
614
|
],
|
513
|
-
|
514
|
-
|
515
|
-
t.returnStatement(
|
516
|
-
callExpression(
|
517
|
-
t.identifier('useMutation'),
|
518
|
-
[
|
519
|
-
t.arrowFunctionExpression(
|
520
|
-
[t.objectPattern(useMutationFunctionArgs)],
|
521
|
-
t.callExpression(
|
522
|
-
t.memberExpression(
|
523
|
-
t.identifier('client'),
|
524
|
-
t.identifier(execMethodName)
|
525
|
-
),
|
526
|
-
(hasMsg
|
527
|
-
? [t.identifier('msg')]
|
528
|
-
: []
|
529
|
-
)
|
530
|
-
.concat(FIXED_EXECUTE_PARAMS.map(param => t.identifier(param.name)))
|
531
|
-
),
|
532
|
-
false // not async
|
533
|
-
),
|
534
|
-
t.identifier('options'),
|
535
|
-
],
|
536
|
-
useMutationTypeParameter
|
537
|
-
)
|
538
|
-
)
|
539
|
-
|
540
|
-
]
|
541
|
-
),
|
615
|
+
useMutationTypeParameter
|
616
|
+
)
|
542
617
|
)
|
618
|
+
])
|
543
619
|
)
|
544
|
-
|
620
|
+
);
|
545
621
|
};
|
546
622
|
|
547
623
|
function createReactQueryKeys({
|
548
|
-
|
549
|
-
|
550
|
-
|
551
|
-
|
624
|
+
context,
|
625
|
+
queryKeysName,
|
626
|
+
camelContractName,
|
627
|
+
underscoreNames
|
552
628
|
}: {
|
553
|
-
|
554
|
-
|
555
|
-
|
556
|
-
|
629
|
+
context: RenderContext;
|
630
|
+
queryKeysName: string;
|
631
|
+
camelContractName: string;
|
632
|
+
underscoreNames: string[];
|
557
633
|
}) {
|
558
|
-
|
559
|
-
|
560
|
-
|
561
|
-
|
562
|
-
|
563
|
-
|
564
|
-
|
565
|
-
|
566
|
-
|
567
|
-
|
568
|
-
|
569
|
-
|
570
|
-
t.
|
571
|
-
|
572
|
-
|
634
|
+
const options = context.options.reactQuery;
|
635
|
+
|
636
|
+
const contractAddressTypeAnnotation = t.tsTypeAnnotation(
|
637
|
+
options.optionalClient
|
638
|
+
? t.tsUnionType([t.tsStringKeyword(), t.tsUndefinedKeyword()])
|
639
|
+
: t.tSStringKeyword()
|
640
|
+
);
|
641
|
+
|
642
|
+
return t.exportNamedDeclaration(
|
643
|
+
t.variableDeclaration('const', [
|
644
|
+
t.variableDeclarator(
|
645
|
+
t.identifier(queryKeysName),
|
646
|
+
t.objectExpression([
|
647
|
+
// 1: contract
|
648
|
+
t.objectProperty(
|
649
|
+
t.identifier('contract'),
|
650
|
+
t.tSAsExpression(
|
651
|
+
t.arrayExpression([
|
573
652
|
t.objectExpression([
|
574
|
-
|
575
|
-
t.
|
576
|
-
|
577
|
-
|
578
|
-
|
579
|
-
|
580
|
-
|
581
|
-
|
582
|
-
|
583
|
-
|
584
|
-
|
585
|
-
|
586
|
-
|
587
|
-
|
653
|
+
t.objectProperty(
|
654
|
+
t.identifier('contract'),
|
655
|
+
t.stringLiteral(camelContractName)
|
656
|
+
)
|
657
|
+
])
|
658
|
+
]),
|
659
|
+
t.tSTypeReference(t.identifier('const'))
|
660
|
+
)
|
661
|
+
),
|
662
|
+
// 2: address
|
663
|
+
t.objectProperty(
|
664
|
+
t.identifier('address'),
|
665
|
+
t.arrowFunctionExpression(
|
666
|
+
[identifier('contractAddress', contractAddressTypeAnnotation)],
|
667
|
+
t.tSAsExpression(
|
668
|
+
t.arrayExpression([
|
669
|
+
t.objectExpression([
|
670
|
+
// 1
|
671
|
+
t.spreadElement(
|
672
|
+
t.memberExpression(
|
673
|
+
t.memberExpression(
|
674
|
+
t.identifier(queryKeysName),
|
675
|
+
t.identifier('contract')
|
676
|
+
),
|
677
|
+
t.numericLiteral(0),
|
678
|
+
true // computed
|
679
|
+
)
|
588
680
|
),
|
589
|
-
// 2: address
|
590
681
|
t.objectProperty(
|
591
|
-
|
592
|
-
|
593
|
-
|
594
|
-
|
595
|
-
|
596
|
-
|
597
|
-
|
598
|
-
|
599
|
-
|
600
|
-
|
601
|
-
|
602
|
-
|
603
|
-
|
604
|
-
|
605
|
-
|
606
|
-
|
607
|
-
|
608
|
-
|
609
|
-
|
610
|
-
|
611
|
-
|
612
|
-
|
613
|
-
|
614
|
-
|
615
|
-
|
616
|
-
|
617
|
-
|
618
|
-
|
619
|
-
t.tSTypeReference(t.identifier('const'))
|
620
|
-
)
|
621
|
-
)
|
682
|
+
t.identifier('address'),
|
683
|
+
t.identifier('contractAddress')
|
684
|
+
)
|
685
|
+
])
|
686
|
+
]),
|
687
|
+
t.tSTypeReference(t.identifier('const'))
|
688
|
+
)
|
689
|
+
)
|
690
|
+
),
|
691
|
+
// 3: methods
|
692
|
+
...underscoreNames.map((underscoreMethodName) =>
|
693
|
+
t.objectProperty(
|
694
|
+
// key id is the camel method name
|
695
|
+
t.identifier(camel(underscoreMethodName)),
|
696
|
+
t.arrowFunctionExpression(
|
697
|
+
[
|
698
|
+
identifier('contractAddress', contractAddressTypeAnnotation),
|
699
|
+
identifier(
|
700
|
+
'args',
|
701
|
+
// Record<string, unknown>
|
702
|
+
t.tSTypeAnnotation(
|
703
|
+
t.tsTypeReference(
|
704
|
+
t.identifier('Record'),
|
705
|
+
t.tsTypeParameterInstantiation([
|
706
|
+
t.tsStringKeyword(),
|
707
|
+
t.tsUnknownKeyword()
|
708
|
+
])
|
709
|
+
)
|
622
710
|
),
|
623
|
-
//
|
624
|
-
|
625
|
-
|
626
|
-
|
627
|
-
|
628
|
-
|
629
|
-
|
630
|
-
|
631
|
-
|
632
|
-
|
633
|
-
|
634
|
-
|
635
|
-
|
636
|
-
|
637
|
-
|
638
|
-
|
639
|
-
|
640
|
-
|
641
|
-
t.tsStringKeyword(),
|
642
|
-
t.tsUnknownKeyword()
|
643
|
-
])
|
644
|
-
)
|
645
|
-
),
|
646
|
-
true // optional
|
647
|
-
)
|
648
|
-
],
|
649
|
-
t.tSAsExpression(
|
650
|
-
t.arrayExpression([
|
651
|
-
t.objectExpression([
|
652
|
-
//...cw3FlexMultisigQueryKeys.address(contractAddress)[0]
|
653
|
-
t.spreadElement(
|
654
|
-
t.memberExpression(
|
655
|
-
t.callExpression(
|
656
|
-
t.memberExpression(
|
657
|
-
t.identifier(queryKeysName),
|
658
|
-
t.identifier('address')
|
659
|
-
),
|
660
|
-
[t.identifier('contractAddress')]
|
661
|
-
),
|
662
|
-
t.numericLiteral(0),
|
663
|
-
true // computed
|
664
|
-
)
|
665
|
-
),
|
666
|
-
// method: list_voters
|
667
|
-
t.objectProperty(
|
668
|
-
t.identifier('method'),
|
669
|
-
t.stringLiteral(underscoreMethodName)
|
670
|
-
),
|
671
|
-
// args
|
672
|
-
shorthandProperty('args')
|
673
|
-
])
|
674
|
-
]),
|
675
|
-
t.tSTypeReference(t.identifier('const'))
|
676
|
-
)
|
677
|
-
)
|
711
|
+
true // optional
|
712
|
+
)
|
713
|
+
],
|
714
|
+
t.tSAsExpression(
|
715
|
+
t.arrayExpression([
|
716
|
+
t.objectExpression([
|
717
|
+
//...cw3FlexMultisigQueryKeys.address(contractAddress)[0]
|
718
|
+
t.spreadElement(
|
719
|
+
t.memberExpression(
|
720
|
+
t.callExpression(
|
721
|
+
t.memberExpression(
|
722
|
+
t.identifier(queryKeysName),
|
723
|
+
t.identifier('address')
|
724
|
+
),
|
725
|
+
[t.identifier('contractAddress')]
|
726
|
+
),
|
727
|
+
t.numericLiteral(0),
|
728
|
+
true // computed
|
678
729
|
)
|
730
|
+
),
|
731
|
+
// method: list_voters
|
732
|
+
t.objectProperty(
|
733
|
+
t.identifier('method'),
|
734
|
+
t.stringLiteral(underscoreMethodName)
|
735
|
+
),
|
736
|
+
// args
|
737
|
+
shorthandProperty('args')
|
738
|
+
])
|
739
|
+
]),
|
740
|
+
t.tSTypeReference(t.identifier('const'))
|
741
|
+
)
|
742
|
+
)
|
743
|
+
)
|
744
|
+
)
|
745
|
+
])
|
746
|
+
)
|
747
|
+
])
|
748
|
+
);
|
749
|
+
}
|
750
|
+
|
751
|
+
function createReactQueryFactory({
|
752
|
+
context,
|
753
|
+
queryFactoryName,
|
754
|
+
queryKeysName,
|
755
|
+
queryMsgs
|
756
|
+
}: {
|
757
|
+
context: RenderContext;
|
758
|
+
queryFactoryName: string;
|
759
|
+
queryKeysName: string;
|
760
|
+
queryMsgs: ParsedQueryMsg[];
|
761
|
+
}) {
|
762
|
+
const options = context.options.reactQuery;
|
763
|
+
|
764
|
+
return t.exportNamedDeclaration(
|
765
|
+
t.variableDeclaration('const', [
|
766
|
+
t.variableDeclarator(
|
767
|
+
t.identifier(queryFactoryName),
|
768
|
+
t.objectExpression([
|
769
|
+
...queryMsgs.map(
|
770
|
+
({ methodName, hookParamsTypeName, responseType, jsonschema }) => {
|
771
|
+
const hasArgs =
|
772
|
+
Object.keys(jsonschema.properties ?? {}).length > 0;
|
773
|
+
|
774
|
+
const methodQueryOptionsFn = t.arrowFunctionExpression(
|
775
|
+
[
|
776
|
+
tsObjectPattern(
|
777
|
+
[
|
778
|
+
shorthandProperty('client'),
|
779
|
+
...(hasArgs ? [shorthandProperty('args')] : []),
|
780
|
+
shorthandProperty('options')
|
781
|
+
],
|
782
|
+
t.tsTypeAnnotation(
|
783
|
+
t.tsTypeReference(
|
784
|
+
t.identifier(hookParamsTypeName),
|
785
|
+
t.tsTypeParameterInstantiation([
|
786
|
+
t.tsTypeReference(t.identifier(GENERIC_SELECT_RESPONSE_NAME))
|
787
|
+
])
|
788
|
+
)
|
789
|
+
)
|
790
|
+
)
|
791
|
+
],
|
792
|
+
t.objectExpression([
|
793
|
+
// 1: queryKey
|
794
|
+
t.objectProperty(
|
795
|
+
t.identifier('queryKey'),
|
796
|
+
t.callExpression(
|
797
|
+
t.memberExpression(
|
798
|
+
t.identifier(queryKeysName),
|
799
|
+
t.identifier(methodName)
|
800
|
+
),
|
801
|
+
[
|
802
|
+
t.optionalMemberExpression(
|
803
|
+
t.identifier('client'),
|
804
|
+
t.identifier('contractAddress'),
|
805
|
+
false,
|
806
|
+
true
|
807
|
+
),
|
808
|
+
...(hasArgs ? [t.identifier('args')] : [])
|
809
|
+
]
|
679
810
|
)
|
811
|
+
),
|
812
|
+
// 2: queryFn
|
813
|
+
t.objectProperty(
|
814
|
+
t.identifier('queryFn'),
|
815
|
+
buildQueryFn(methodName, jsonschema, options)
|
816
|
+
),
|
817
|
+
// 3: spread options
|
818
|
+
t.spreadElement(t.identifier('options')),
|
819
|
+
// 4. enabled
|
820
|
+
ENABLED_QUERY_OPTION
|
680
821
|
])
|
681
|
-
|
822
|
+
);
|
823
|
+
|
824
|
+
methodQueryOptionsFn.typeParameters =
|
825
|
+
t.tsTypeParameterDeclaration([
|
826
|
+
t.tsTypeParameter(
|
827
|
+
undefined,
|
828
|
+
t.tsTypeReference(t.identifier(responseType)),
|
829
|
+
GENERIC_SELECT_RESPONSE_NAME
|
830
|
+
)
|
831
|
+
]);
|
832
|
+
|
833
|
+
methodQueryOptionsFn.returnType = t.tsTypeAnnotation(
|
834
|
+
t.tsTypeReference(
|
835
|
+
t.identifier('UseQueryOptions'),
|
836
|
+
t.tsTypeParameterInstantiation([
|
837
|
+
t.tsTypeReference(t.identifier(responseType)),
|
838
|
+
t.tsTypeReference(t.identifier('Error')),
|
839
|
+
t.tsTypeReference(t.identifier(GENERIC_SELECT_RESPONSE_NAME))
|
840
|
+
])
|
841
|
+
)
|
842
|
+
);
|
843
|
+
|
844
|
+
return t.objectProperty(
|
845
|
+
// key id is the camel method name
|
846
|
+
t.identifier(camel(methodName)),
|
847
|
+
methodQueryOptionsFn
|
848
|
+
);
|
849
|
+
}
|
850
|
+
)
|
682
851
|
])
|
683
|
-
|
852
|
+
)
|
853
|
+
])
|
854
|
+
);
|
684
855
|
}
|
685
856
|
|
686
857
|
interface ReactQueryHookGenericInterface {
|
687
|
-
|
688
|
-
|
689
|
-
|
858
|
+
context: RenderContext;
|
859
|
+
QueryClient: string;
|
860
|
+
genericQueryInterfaceName: string;
|
690
861
|
}
|
691
862
|
|
863
|
+
const GENERIC_SELECT_RESPONSE_NAME = 'TData';
|
864
|
+
|
692
865
|
function createReactQueryHookGenericInterface({
|
693
|
-
|
694
|
-
|
695
|
-
|
866
|
+
context,
|
867
|
+
QueryClient,
|
868
|
+
genericQueryInterfaceName
|
696
869
|
}: ReactQueryHookGenericInterface) {
|
697
870
|
|
698
|
-
|
871
|
+
const options = context.options.reactQuery;
|
872
|
+
const genericResponseTypeName = 'TResponse';
|
699
873
|
|
700
|
-
|
701
|
-
const genericSelectResponseTypeName = 'TData'
|
874
|
+
context.addUtil('UseQueryOptions');
|
702
875
|
|
703
|
-
|
876
|
+
// UseQueryOptions<TResponse, Error, TData>,
|
877
|
+
const typedUseQueryOptions = t.tsTypeReference(
|
878
|
+
t.identifier('UseQueryOptions'),
|
879
|
+
t.tsTypeParameterInstantiation([
|
880
|
+
t.tsTypeReference(t.identifier(genericResponseTypeName)),
|
881
|
+
t.tsTypeReference(t.identifier('Error')),
|
882
|
+
t.tsTypeReference(t.identifier(GENERIC_SELECT_RESPONSE_NAME))
|
883
|
+
])
|
884
|
+
);
|
704
885
|
|
705
|
-
|
706
|
-
|
707
|
-
|
708
|
-
|
709
|
-
|
710
|
-
|
711
|
-
|
712
|
-
|
713
|
-
|
714
|
-
|
715
|
-
|
886
|
+
const body = [
|
887
|
+
tsPropertySignature(
|
888
|
+
t.identifier('client'),
|
889
|
+
t.tsTypeAnnotation(
|
890
|
+
options.optionalClient
|
891
|
+
? t.tsUnionType([
|
892
|
+
t.tsTypeReference(t.identifier(QueryClient)),
|
893
|
+
t.tsUndefinedKeyword()
|
894
|
+
])
|
895
|
+
: t.tsTypeReference(t.identifier(QueryClient))
|
896
|
+
),
|
897
|
+
false
|
898
|
+
),
|
899
|
+
tsPropertySignature(
|
900
|
+
t.identifier('options'),
|
901
|
+
t.tsTypeAnnotation(
|
902
|
+
options.version === 'v4'
|
903
|
+
? t.tSIntersectionType([
|
904
|
+
omitTypeReference(
|
905
|
+
typedUseQueryOptions,
|
906
|
+
"'queryKey' | 'queryFn' | 'initialData'"
|
907
|
+
),
|
908
|
+
t.tSTypeLiteral([
|
909
|
+
t.tsPropertySignature(
|
910
|
+
t.identifier('initialData?'),
|
911
|
+
t.tsTypeAnnotation(t.tsUndefinedKeyword())
|
716
912
|
)
|
913
|
+
])
|
717
914
|
])
|
915
|
+
: typedUseQueryOptions
|
916
|
+
),
|
917
|
+
true
|
718
918
|
)
|
719
|
-
|
720
|
-
|
721
|
-
|
722
|
-
|
723
|
-
|
724
|
-
|
725
|
-
|
726
|
-
|
727
|
-
|
728
|
-
|
729
|
-
|
730
|
-
|
731
|
-
|
732
|
-
t.identifier(QueryClient)
|
733
|
-
)
|
734
|
-
),
|
735
|
-
false
|
736
|
-
),
|
737
|
-
tsPropertySignature(
|
738
|
-
t.identifier('options'),
|
739
|
-
t.tsTypeAnnotation(
|
740
|
-
options.version === 'v4'
|
741
|
-
? t.tSIntersectionType([
|
742
|
-
omitTypeReference(typedUseQueryOptions, "'queryKey' | 'queryFn' | 'initialData'"),
|
743
|
-
t.tSTypeLiteral([
|
744
|
-
t.tsPropertySignature(
|
745
|
-
t.identifier('initialData?'),
|
746
|
-
t.tsTypeAnnotation(
|
747
|
-
t.tsUndefinedKeyword()
|
748
|
-
)
|
749
|
-
)
|
750
|
-
])
|
751
|
-
])
|
752
|
-
: typedUseQueryOptions
|
753
|
-
),
|
754
|
-
true
|
755
|
-
)
|
756
|
-
];
|
757
|
-
|
758
|
-
return t.exportNamedDeclaration(
|
759
|
-
t.tsInterfaceDeclaration(
|
760
|
-
t.identifier(genericQueryInterfaceName),
|
761
|
-
t.tsTypeParameterDeclaration([
|
762
|
-
// 1: TResponse
|
763
|
-
t.tsTypeParameter(undefined, undefined, genericResponseTypeName),
|
764
|
-
// 2: TData
|
765
|
-
t.tsTypeParameter(
|
766
|
-
undefined,
|
767
|
-
t.tSTypeReference(t.identifier(genericResponseTypeName)),
|
768
|
-
genericSelectResponseTypeName
|
769
|
-
)
|
770
|
-
]),
|
771
|
-
[],
|
772
|
-
t.tSInterfaceBody(body)
|
919
|
+
];
|
920
|
+
|
921
|
+
return t.exportNamedDeclaration(
|
922
|
+
t.tsInterfaceDeclaration(
|
923
|
+
t.identifier(genericQueryInterfaceName),
|
924
|
+
t.tsTypeParameterDeclaration([
|
925
|
+
// 1: TResponse
|
926
|
+
t.tsTypeParameter(undefined, undefined, genericResponseTypeName),
|
927
|
+
// 2: TData
|
928
|
+
t.tsTypeParameter(
|
929
|
+
undefined,
|
930
|
+
t.tSTypeReference(t.identifier(genericResponseTypeName)),
|
931
|
+
GENERIC_SELECT_RESPONSE_NAME
|
773
932
|
)
|
933
|
+
]),
|
934
|
+
[],
|
935
|
+
t.tSInterfaceBody(body)
|
774
936
|
)
|
937
|
+
);
|
775
938
|
}
|
776
939
|
|
777
940
|
interface ReactQueryHookQueryInterface {
|
778
|
-
|
779
|
-
|
780
|
-
|
781
|
-
|
782
|
-
|
783
|
-
|
941
|
+
context: RenderContext;
|
942
|
+
QueryClient: string;
|
943
|
+
hookParamsTypeName: string;
|
944
|
+
queryInterfaceName: string;
|
945
|
+
responseType: string;
|
946
|
+
jsonschema: any;
|
784
947
|
}
|
785
948
|
|
786
949
|
export const createReactQueryHookInterface = ({
|
787
|
-
|
788
|
-
|
789
|
-
|
790
|
-
|
791
|
-
|
792
|
-
|
950
|
+
context,
|
951
|
+
QueryClient,
|
952
|
+
hookParamsTypeName,
|
953
|
+
queryInterfaceName,
|
954
|
+
responseType,
|
955
|
+
jsonschema
|
793
956
|
}: ReactQueryHookQueryInterface) => {
|
794
|
-
|
795
|
-
|
796
|
-
|
797
|
-
const body = []
|
798
|
-
|
799
|
-
const props = getProps(context, jsonschema);
|
800
|
-
if (props.length) {
|
801
|
-
body.push(t.tsPropertySignature(
|
802
|
-
t.identifier('args'),
|
803
|
-
t.tsTypeAnnotation(
|
804
|
-
// @ts-ignore:next-line
|
805
|
-
t.tsTypeLiteral(props)
|
806
|
-
)
|
807
|
-
))
|
808
|
-
}
|
957
|
+
// merge the user options with the defaults
|
958
|
+
const options = context.options.reactQuery;
|
809
959
|
|
960
|
+
const body = [];
|
810
961
|
|
811
|
-
|
812
|
-
|
813
|
-
|
814
|
-
|
815
|
-
|
816
|
-
|
817
|
-
|
818
|
-
|
819
|
-
t.identifier(queryInterfaceName),
|
820
|
-
t.tsTypeParameterInstantiation([
|
821
|
-
// 1: response
|
822
|
-
t.tsTypeReference(t.identifier(responseType)),
|
823
|
-
// 2: select generic
|
824
|
-
t.tSTypeReference(t.identifier('TData'))
|
825
|
-
])
|
826
|
-
)
|
827
|
-
],
|
828
|
-
t.tsInterfaceBody(
|
829
|
-
body
|
830
|
-
)
|
962
|
+
const props = getProps(context, jsonschema);
|
963
|
+
if (props.length) {
|
964
|
+
body.push(
|
965
|
+
t.tsPropertySignature(
|
966
|
+
t.identifier('args'),
|
967
|
+
t.tsTypeAnnotation(
|
968
|
+
// @ts-ignore:next-line
|
969
|
+
t.tsTypeLiteral(props)
|
831
970
|
)
|
971
|
+
)
|
972
|
+
);
|
973
|
+
}
|
974
|
+
|
975
|
+
return t.exportNamedDeclaration(
|
976
|
+
t.tsInterfaceDeclaration(
|
977
|
+
t.identifier(hookParamsTypeName),
|
978
|
+
t.tsTypeParameterDeclaration([
|
979
|
+
t.tSTypeParameter(undefined, undefined, GENERIC_SELECT_RESPONSE_NAME)
|
980
|
+
]),
|
981
|
+
[
|
982
|
+
t.tSExpressionWithTypeArguments(
|
983
|
+
t.identifier(queryInterfaceName),
|
984
|
+
t.tsTypeParameterInstantiation([
|
985
|
+
// 1: response
|
986
|
+
t.tsTypeReference(t.identifier(responseType)),
|
987
|
+
// 2: select generic
|
988
|
+
t.tSTypeReference(t.identifier(GENERIC_SELECT_RESPONSE_NAME))
|
989
|
+
])
|
990
|
+
)
|
991
|
+
],
|
992
|
+
t.tsInterfaceBody(body)
|
832
993
|
)
|
994
|
+
);
|
833
995
|
};
|
834
996
|
|
835
|
-
const getProps = (
|
836
|
-
|
837
|
-
|
838
|
-
|
839
|
-
|
840
|
-
|
841
|
-
|
842
|
-
|
843
|
-
|
844
|
-
|
845
|
-
|
846
|
-
|
847
|
-
|
848
|
-
),
|
849
|
-
optional
|
850
|
-
)
|
851
|
-
});
|
852
|
-
}
|
997
|
+
const getProps = (context: RenderContext, jsonschema: JSONSchema) => {
|
998
|
+
const keys = Object.keys(jsonschema.properties ?? {});
|
999
|
+
if (!keys.length) return [];
|
1000
|
+
|
1001
|
+
return keys.map((prop) => {
|
1002
|
+
const { type, optional } = getPropertyType(context, jsonschema, prop);
|
1003
|
+
return propertySignature(
|
1004
|
+
context.options.reactQuery.camelize ? camel(prop) : prop,
|
1005
|
+
t.tsTypeAnnotation(type),
|
1006
|
+
optional
|
1007
|
+
);
|
1008
|
+
});
|
1009
|
+
};
|
853
1010
|
|
854
1011
|
interface GenerateUseQueryQueryKeyParams {
|
855
|
-
|
856
|
-
|
857
|
-
|
858
|
-
|
859
|
-
|
1012
|
+
hookKeyName: string;
|
1013
|
+
queryKeysName: string;
|
1014
|
+
methodName: string;
|
1015
|
+
props: string[];
|
1016
|
+
options: ReactQueryOptions;
|
860
1017
|
}
|
861
1018
|
|
862
1019
|
const generateUseQueryQueryKey = ({
|
863
|
-
|
864
|
-
|
865
|
-
|
866
|
-
|
867
|
-
|
1020
|
+
hookKeyName,
|
1021
|
+
queryKeysName,
|
1022
|
+
methodName,
|
1023
|
+
props,
|
1024
|
+
options
|
868
1025
|
}: GenerateUseQueryQueryKeyParams): t.ArrayExpression | t.CallExpression => {
|
869
|
-
|
870
|
-
|
871
|
-
const hasArgs = props.includes('args')
|
872
|
-
|
873
|
-
const contractAddressExpression =
|
874
|
-
t.optionalMemberExpression(
|
875
|
-
t.identifier('client'),
|
876
|
-
t.identifier('contractAddress'),
|
877
|
-
false,
|
878
|
-
optionalClient
|
879
|
-
)
|
1026
|
+
const { optionalClient, queryKeys } = options;
|
880
1027
|
|
881
|
-
|
1028
|
+
const hasArgs = props.includes('args');
|
882
1029
|
|
883
|
-
|
1030
|
+
const contractAddressExpression = t.optionalMemberExpression(
|
1031
|
+
t.identifier('client'),
|
1032
|
+
t.identifier('contractAddress'),
|
1033
|
+
false,
|
1034
|
+
optionalClient
|
1035
|
+
);
|
884
1036
|
|
885
|
-
|
1037
|
+
if (queryKeys) {
|
1038
|
+
const callArgs: Array<Expression> = [contractAddressExpression];
|
886
1039
|
|
887
|
-
|
888
|
-
t.memberExpression(
|
889
|
-
t.identifier(queryKeysName),
|
890
|
-
t.identifier(camel(methodName))
|
891
|
-
),
|
892
|
-
callArgs
|
893
|
-
)
|
894
|
-
}
|
1040
|
+
if (hasArgs) callArgs.push(t.identifier('args'));
|
895
1041
|
|
896
|
-
|
897
|
-
|
898
|
-
|
899
|
-
|
1042
|
+
return t.callExpression(
|
1043
|
+
t.memberExpression(
|
1044
|
+
t.identifier(queryKeysName),
|
1045
|
+
t.identifier(camel(methodName))
|
1046
|
+
),
|
1047
|
+
callArgs
|
1048
|
+
);
|
1049
|
+
}
|
900
1050
|
|
901
|
-
|
902
|
-
|
903
|
-
|
904
|
-
|
905
|
-
|
906
|
-
|
907
|
-
|
908
|
-
|
909
|
-
|
910
|
-
)
|
911
|
-
|
912
|
-
|
913
|
-
}
|
1051
|
+
const queryKey: Array<Expression> = [
|
1052
|
+
t.stringLiteral(hookKeyName),
|
1053
|
+
contractAddressExpression
|
1054
|
+
];
|
1055
|
+
|
1056
|
+
if (hasArgs) {
|
1057
|
+
queryKey.push(
|
1058
|
+
t.callExpression(
|
1059
|
+
t.memberExpression(t.identifier('JSON'), t.identifier('stringify')),
|
1060
|
+
[t.identifier('args')]
|
1061
|
+
)
|
1062
|
+
);
|
1063
|
+
}
|
1064
|
+
return t.arrayExpression(queryKey);
|
1065
|
+
};
|