wasm-ast-types 0.13.0 → 0.14.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -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
- omitTypeReference,
8
- optionalConditionalExpression,
9
- propertySignature,
10
- shorthandProperty
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 { getParamsTypeAnnotation, getPropertyType, getResponseType } from '../utils/types';
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
- context: RenderContext,
19
- hookName: string;
20
- hookParamsTypeName: string;
21
- hookKeyName: string;
22
- queryKeysName: string
23
- responseType: string;
24
- methodName: string;
25
- jsonschema: any;
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
- context: RenderContext;
30
- queryMsg: QueryMsg;
31
- contractName: string;
32
- QueryClient: string;
51
+ context: RenderContext;
52
+ queryMsg: QueryMsg;
53
+ contractName: string;
54
+ QueryClient: string;
33
55
  }
34
56
 
35
57
  export const createReactQueryHooks = ({
36
- context,
37
- queryMsg,
38
- contractName,
39
- QueryClient
58
+ context,
59
+ queryMsg,
60
+ contractName,
61
+ QueryClient
40
62
  }: ReactQueryHooks) => {
41
- const options = context.options.reactQuery;
42
-
43
- const genericQueryInterfaceName = `${pascal(contractName)}ReactQuery`;
44
- const underscoreNames: string[] = getMessageProperties(queryMsg).map((schema) => (Object.keys(schema.properties)[0]))
45
-
46
- const body = []
47
-
48
- const queryKeysName = `${camel(contractName)}QueryKeys`
49
- if (options.queryKeys) {
50
- body.push(
51
- createReactQueryKeys({
52
- context,
53
- queryKeysName,
54
- camelContractName: camel(contractName),
55
- underscoreNames,
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
- createReactQueryHookGenericInterface({
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
- genericQueryInterfaceName,
64
- }))
65
-
66
- body.push(...getMessageProperties(queryMsg)
67
- .reduce((m, schema) => {
68
- // list_voters
69
- const underscoreName = Object.keys(schema.properties)[0];
70
- // listVoters
71
- const methodName = camel(underscoreName);
72
- // Cw3FlexMultisigListVotersQuery
73
- const hookParamsTypeName = `${pascal(contractName)}${pascal(methodName)}Query`;
74
- // useCw3FlexMultisigListVotersQuery
75
- const hookName = `use${hookParamsTypeName}`;
76
- // listVotersResponse
77
- const responseType = getResponseType(context, underscoreName);
78
- // cw3FlexMultisigListVoters
79
- const getterKey = camel(`${contractName}${pascal(methodName)}`);
80
- const jsonschema = schema.properties[underscoreName];
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
- return body
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
- export const createReactQueryHook = ({
110
- context,
111
- hookName,
112
- hookParamsTypeName,
113
- responseType,
114
- hookKeyName,
115
- queryKeysName,
116
- methodName,
117
- jsonschema
118
- }: ReactQueryHookQuery) => {
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
- context.addUtil('useQuery');
121
- context.addUtil('UseQueryOptions');
122
-
123
- const options = context.options.reactQuery;
124
- const keys = Object.keys(jsonschema.properties ?? {});
125
- let args = [];
126
- if (keys.length) {
127
- args = [
128
- t.objectExpression([
129
- ...keys.map(prop => {
130
- return t.objectProperty(
131
- t.identifier(camel(prop)),
132
- t.memberExpression(
133
- t.identifier('args'),
134
- t.identifier(camel(prop))
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
- let props = ['client', 'options'];
143
- if (keys.length) {
144
- props = ['client', 'args', 'options'];
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
- const selectResponseGenericTypeName = 'TData';
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
- const queryFunctionDeclaration =
150
- t.functionDeclaration(
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
- t.returnStatement(
176
- callExpression(
177
- t.identifier('useQuery'),
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
- // Add the TData type parameters
264
- queryFunctionDeclaration.typeParameters = t.tsTypeParameterDeclaration([
265
- t.tsTypeParameter(
266
- undefined,
267
- t.tSTypeReference(t.identifier(responseType)),
268
- selectResponseGenericTypeName
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
- return t.exportNamedDeclaration(queryFunctionDeclaration)
273
-
360
+ return t.exportNamedDeclaration(queryFunctionDeclaration);
274
361
  };
275
362
 
276
363
  interface ReactQueryMutationHookInterface {
277
- context: RenderContext;
278
- ExecuteClient: string;
279
- mutationHookParamsTypeName: string;
280
- jsonschema: JSONSchema;
281
- useMutationTypeParameter: t.TSTypeParameterInstantiation;
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
- context,
302
- ExecuteClient,
303
- mutationHookParamsTypeName,
304
- useMutationTypeParameter,
305
- jsonschema,
388
+ context,
389
+ ExecuteClient,
390
+ mutationHookParamsTypeName,
391
+ useMutationTypeParameter,
392
+ jsonschema
306
393
  }: ReactQueryMutationHookInterface) => {
307
-
308
- const typedUseMutationOptions = t.tsTypeReference(
309
- t.identifier('UseMutationOptions'),
310
- useMutationTypeParameter
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
- const body = [
314
- tsPropertySignature(
315
- t.identifier('client'),
316
- t.tsTypeAnnotation(
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
- context.addUtil('StdFee')
336
- context.addUtil('Coin');
337
- // fee: number | StdFee | "auto" = "auto", memo?: string, funds?: Coin[]
412
+ if (msgType) {
413
+ body.push(t.tsPropertySignature(t.identifier('msg'), msgType));
414
+ }
338
415
 
339
- const optionalArgs = t.tsPropertySignature(
340
- t.identifier('args'),
341
- t.tsTypeAnnotation(
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
- t.tsTypeLiteral(FIXED_EXECUTE_PARAMS.map(param => propertySignature(
344
- param.name,
345
- // @ts-ignore:next-line
346
- param.typeAnnotation,
347
- param.optional
348
- )))
429
+ param.typeAnnotation,
430
+ param.optional
431
+ )
349
432
  )
433
+ )
350
434
  )
435
+ );
351
436
 
352
- optionalArgs.optional = true
353
-
354
- body.push(optionalArgs)
437
+ optionalArgs.optional = true;
355
438
 
439
+ body.push(optionalArgs);
356
440
 
357
- return t.exportNamedDeclaration(t.tsInterfaceDeclaration(
358
- t.identifier(mutationHookParamsTypeName),
359
- null,
360
- [],
361
- t.tsInterfaceBody(
362
- body
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
- context: RenderContext;
370
- execMsg: ExecuteMsg;
371
- contractName: string;
372
- ExecuteClient: string;
452
+ context: RenderContext;
453
+ execMsg: ExecuteMsg;
454
+ contractName: string;
455
+ ExecuteClient: string;
373
456
  }
374
457
 
375
458
  export const createReactQueryMutationHooks = ({
376
- context,
377
- execMsg,
378
- contractName,
379
- ExecuteClient
459
+ context,
460
+ execMsg,
461
+ contractName,
462
+ ExecuteClient
380
463
  }: ReactQueryMutationHooks) => {
381
- // merge the user options with the defaults
382
- return getMessageProperties(execMsg)
383
- .reduce((m, schema) => {
384
- // update_members
385
- const execMethodUnderscoreName = Object.keys(schema.properties)[0];
386
- // updateMembers
387
- const execMethodName = camel(execMethodUnderscoreName);
388
- // Cw20UpdateMembersMutation
389
- const mutationHookParamsTypeName = `${pascal(contractName)}${pascal(execMethodName)}Mutation`;
390
- // useCw20UpdateMembersMutation
391
- const mutationHookName = `use${mutationHookParamsTypeName}`;
392
-
393
- const jsonschema = schema.properties[execMethodUnderscoreName];
394
-
395
- const properties = jsonschema.properties ?? {};
396
-
397
- // TODO: there should be a better way to do this
398
- const hasMsg = !!(Object.keys(properties)?.length || jsonschema?.$ref)
399
-
400
- // <ExecuteResult, Error, Cw4UpdateMembersMutation>
401
- const useMutationTypeParameter = generateMutationTypeParameter(
402
- context,
403
- mutationHookParamsTypeName
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
- return [
408
- createReactQueryMutationArgsInterface({
409
- context,
410
- mutationHookParamsTypeName,
411
- ExecuteClient,
412
- jsonschema,
413
- useMutationTypeParameter
414
- }),
415
- createReactQueryMutationHook({
416
- context,
417
- execMethodName,
418
- mutationHookName,
419
- mutationHookParamsTypeName,
420
- hasMsg,
421
- useMutationTypeParameter,
422
- }),
423
- ...m,
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
- context: RenderContext,
433
- mutationHookParamsTypeName: string
515
+ context: RenderContext,
516
+ mutationHookParamsTypeName: string
434
517
  ) => {
435
-
436
- context.addUtil('ExecuteResult');
437
-
438
- return t.tsTypeParameterInstantiation([
439
- // Data
440
- t.tSTypeReference(
441
- t.identifier('ExecuteResult')
442
- ),
443
- // Error
444
- t.tsTypeReference(
445
- t.identifier('Error')
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
- context: RenderContext;
457
- mutationHookName: string;
458
- mutationHookParamsTypeName: string;
459
- execMethodName: string;
460
- useMutationTypeParameter: t.TSTypeParameterInstantiation;
461
- hasMsg: boolean;
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
- context,
477
- mutationHookName,
478
- mutationHookParamsTypeName,
479
- execMethodName,
480
- useMutationTypeParameter,
481
- hasMsg,
551
+ context,
552
+ mutationHookName,
553
+ mutationHookParamsTypeName,
554
+ execMethodName,
555
+ useMutationTypeParameter,
556
+ hasMsg
482
557
  }: ReactQueryMutationHook) => {
483
-
484
- context.addUtil('useMutation');
485
- context.addUtil('UseMutationOptions');
486
-
487
- const useMutationFunctionArgs = [shorthandProperty('client')]
488
- if (hasMsg) useMutationFunctionArgs.push(shorthandProperty('msg'))
489
- useMutationFunctionArgs.push(
490
- t.objectProperty(
491
- t.identifier('args'),
492
- t.assignmentPattern(
493
- t.objectPattern(FIXED_EXECUTE_PARAMS.map(param => shorthandProperty(param.name))),
494
- t.objectExpression([])
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
- return t.exportNamedDeclaration(
500
- t.functionDeclaration(
501
- t.identifier(mutationHookName),
592
+ ],
593
+ t.blockStatement([
594
+ t.returnStatement(
595
+ callExpression(
596
+ t.identifier('useMutation'),
502
597
  [
503
- identifier('options', t.tsTypeAnnotation(
504
- omitTypeReference(
505
- t.tsTypeReference(
506
- t.identifier('UseMutationOptions'),
507
- useMutationTypeParameter
508
- ),
509
- 'mutationFn'
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
- ), true)
609
+ )
610
+ ),
611
+ false // not async
612
+ ),
613
+ t.identifier('options')
512
614
  ],
513
- t.blockStatement(
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
- context,
549
- queryKeysName,
550
- camelContractName,
551
- underscoreNames
624
+ context,
625
+ queryKeysName,
626
+ camelContractName,
627
+ underscoreNames
552
628
  }: {
553
- context: RenderContext;
554
- queryKeysName: string,
555
- camelContractName: string;
556
- underscoreNames: string[];
629
+ context: RenderContext;
630
+ queryKeysName: string;
631
+ camelContractName: string;
632
+ underscoreNames: string[];
557
633
  }) {
558
- const options = context.options.reactQuery
559
-
560
- const contractAddressTypeAnnotation = t.tsTypeAnnotation(
561
- options.optionalClient
562
- ? t.tsUnionType([
563
- t.tsStringKeyword(),
564
- t.tsUndefinedKeyword()
565
- ])
566
- : t.tSStringKeyword()
567
- )
568
-
569
- return t.exportNamedDeclaration(
570
- t.variableDeclaration('const', [
571
- t.variableDeclarator(
572
- t.identifier(queryKeysName),
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
- // 1: contract
575
- t.objectProperty(
576
- t.identifier('contract'),
577
- t.tSAsExpression(
578
- t.arrayExpression([
579
- t.objectExpression([
580
- t.objectProperty(
581
- t.identifier('contract'),
582
- t.stringLiteral(camelContractName)
583
- )
584
- ])
585
- ]),
586
- t.tSTypeReference(t.identifier('const'))
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
- t.identifier('address'),
592
- t.arrowFunctionExpression(
593
- [
594
- identifier(
595
- 'contractAddress',
596
- contractAddressTypeAnnotation
597
- )
598
- ],
599
- t.tSAsExpression(
600
- t.arrayExpression([
601
- t.objectExpression([
602
- // 1
603
- t.spreadElement(
604
- t.memberExpression(
605
- t.memberExpression(
606
- t.identifier(queryKeysName),
607
- t.identifier('contract')
608
- ),
609
- t.numericLiteral(0),
610
- true // computed
611
- )
612
- ),
613
- t.objectProperty(
614
- t.identifier('address'),
615
- t.identifier('contractAddress')
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
- // 3: methods
624
- ...underscoreNames.map((underscoreMethodName) =>
625
- t.objectProperty(
626
- // key id is the camel method name
627
- t.identifier(camel(underscoreMethodName)),
628
- t.arrowFunctionExpression(
629
- [
630
- identifier(
631
- 'contractAddress',
632
- contractAddressTypeAnnotation
633
- ),
634
- identifier(
635
- 'args',
636
- // Record<string, unknown>
637
- t.tSTypeAnnotation(
638
- t.tsTypeReference(
639
- t.identifier('Record'),
640
- t.tsTypeParameterInstantiation([
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
- context: RenderContext,
688
- QueryClient: string,
689
- genericQueryInterfaceName: string
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
- context,
694
- QueryClient,
695
- genericQueryInterfaceName
866
+ context,
867
+ QueryClient,
868
+ genericQueryInterfaceName
696
869
  }: ReactQueryHookGenericInterface) {
697
870
 
698
- const options = context.options.reactQuery;
871
+ const options = context.options.reactQuery;
872
+ const genericResponseTypeName = 'TResponse';
699
873
 
700
- const genericResponseTypeName = 'TResponse'
701
- const genericSelectResponseTypeName = 'TData'
874
+ context.addUtil('UseQueryOptions');
702
875
 
703
- context.addUtil('UseQueryOptions');
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
- // UseQueryOptions<TResponse, Error, TData>,
706
- const typedUseQueryOptions = t.tsTypeReference(
707
- t.identifier('UseQueryOptions'),
708
- t.tsTypeParameterInstantiation(
709
- [
710
- t.tsTypeReference(
711
- t.identifier(genericResponseTypeName)
712
- ),
713
- t.tsTypeReference(t.identifier('Error')),
714
- t.tsTypeReference(
715
- t.identifier(genericSelectResponseTypeName)
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
- const body = [
721
- tsPropertySignature(
722
- t.identifier('client'),
723
- t.tsTypeAnnotation(
724
- options.optionalClient
725
- ? t.tsUnionType([
726
- t.tsTypeReference(
727
- t.identifier(QueryClient)
728
- ),
729
- t.tsUndefinedKeyword()
730
- ])
731
- : t.tsTypeReference(
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
- context: RenderContext,
779
- QueryClient: string;
780
- hookParamsTypeName: string;
781
- queryInterfaceName: string
782
- responseType: string;
783
- jsonschema: any;
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
- context,
788
- QueryClient,
789
- hookParamsTypeName,
790
- queryInterfaceName,
791
- responseType,
792
- jsonschema
950
+ context,
951
+ QueryClient,
952
+ hookParamsTypeName,
953
+ queryInterfaceName,
954
+ responseType,
955
+ jsonschema
793
956
  }: ReactQueryHookQueryInterface) => {
794
- // merge the user options with the defaults
795
- const options = context.options.reactQuery;
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
- return t.exportNamedDeclaration(
812
- t.tsInterfaceDeclaration(
813
- t.identifier(hookParamsTypeName),
814
- t.tsTypeParameterDeclaration([
815
- t.tSTypeParameter(undefined, undefined, 'TData')
816
- ]),
817
- [
818
- t.tSExpressionWithTypeArguments(
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
- context: RenderContext,
837
- jsonschema: JSONSchema
838
- ) => {
839
- const keys = Object.keys(jsonschema.properties ?? {});
840
- if (!keys.length) return [];
841
-
842
- return keys.map(prop => {
843
- const { type, optional } = getPropertyType(context, jsonschema, prop);
844
- return propertySignature(
845
- context.options.reactQuery.camelize ? camel(prop) : prop,
846
- t.tsTypeAnnotation(
847
- type
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
- hookKeyName: string;
856
- queryKeysName: string;
857
- methodName: string;
858
- props: string[];
859
- options: ReactQueryOptions;
1012
+ hookKeyName: string;
1013
+ queryKeysName: string;
1014
+ methodName: string;
1015
+ props: string[];
1016
+ options: ReactQueryOptions;
860
1017
  }
861
1018
 
862
1019
  const generateUseQueryQueryKey = ({
863
- hookKeyName,
864
- queryKeysName,
865
- methodName,
866
- props,
867
- options,
1020
+ hookKeyName,
1021
+ queryKeysName,
1022
+ methodName,
1023
+ props,
1024
+ options
868
1025
  }: GenerateUseQueryQueryKeyParams): t.ArrayExpression | t.CallExpression => {
869
- const { optionalClient, queryKeys } = options
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
- if (queryKeys) {
1028
+ const hasArgs = props.includes('args');
882
1029
 
883
- const callArgs: Array<Expression> = [contractAddressExpression]
1030
+ const contractAddressExpression = t.optionalMemberExpression(
1031
+ t.identifier('client'),
1032
+ t.identifier('contractAddress'),
1033
+ false,
1034
+ optionalClient
1035
+ );
884
1036
 
885
- if (hasArgs) callArgs.push(t.identifier('args'))
1037
+ if (queryKeys) {
1038
+ const callArgs: Array<Expression> = [contractAddressExpression];
886
1039
 
887
- return t.callExpression(
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
- const queryKey: Array<Expression> = [
897
- t.stringLiteral(hookKeyName),
898
- contractAddressExpression
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
- if (hasArgs) {
902
- queryKey.push(t.callExpression(
903
- t.memberExpression(
904
- t.identifier('JSON'),
905
- t.identifier('stringify')
906
- ),
907
- [
908
- t.identifier('args')
909
- ]
910
- ))
911
- }
912
- return t.arrayExpression(queryKey)
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
+ };