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.
@@ -49,12 +49,7 @@ var createReactQueryHooks = function createReactQueryHooks(_ref) {
49
49
  }));
50
50
  }
51
51
 
52
- body.push(createReactQueryHookGenericInterface({
53
- context: context,
54
- QueryClient: QueryClient,
55
- genericQueryInterfaceName: genericQueryInterfaceName
56
- }));
57
- body.push.apply(body, (0, _toConsumableArray2["default"])((0, _utils.getMessageProperties)(queryMsg).reduce(function (m, schema) {
52
+ var queryMsgs = (0, _utils.getMessageProperties)(queryMsg).map(function (schema) {
58
53
  // list_voters
59
54
  var underscoreName = Object.keys(schema.properties)[0]; // listVoters
60
55
 
@@ -68,6 +63,39 @@ var createReactQueryHooks = function createReactQueryHooks(_ref) {
68
63
 
69
64
  var getterKey = (0, _case.camel)("".concat(contractName).concat((0, _case.pascal)(methodName)));
70
65
  var jsonschema = schema.properties[underscoreName];
66
+ return {
67
+ underscoreName: underscoreName,
68
+ methodName: methodName,
69
+ hookParamsTypeName: hookParamsTypeName,
70
+ hookName: hookName,
71
+ responseType: responseType,
72
+ getterKey: getterKey,
73
+ jsonschema: jsonschema
74
+ };
75
+ });
76
+ var queryFactoryName = "".concat((0, _case.camel)(contractName), "Queries");
77
+
78
+ if (options.queryFactory) {
79
+ body.push(createReactQueryFactory({
80
+ context: context,
81
+ queryFactoryName: queryFactoryName,
82
+ queryKeysName: queryKeysName,
83
+ queryMsgs: queryMsgs
84
+ }));
85
+ }
86
+
87
+ body.push(createReactQueryHookGenericInterface({
88
+ context: context,
89
+ QueryClient: QueryClient,
90
+ genericQueryInterfaceName: genericQueryInterfaceName
91
+ }));
92
+ body.push.apply(body, (0, _toConsumableArray2["default"])(queryMsgs.reduce(function (m, _ref2) {
93
+ var methodName = _ref2.methodName,
94
+ hookParamsTypeName = _ref2.hookParamsTypeName,
95
+ hookName = _ref2.hookName,
96
+ responseType = _ref2.responseType,
97
+ getterKey = _ref2.getterKey,
98
+ jsonschema = _ref2.jsonschema;
71
99
  return [createReactQueryHookInterface({
72
100
  context: context,
73
101
  hookParamsTypeName: hookParamsTypeName,
@@ -91,20 +119,9 @@ var createReactQueryHooks = function createReactQueryHooks(_ref) {
91
119
 
92
120
  exports.createReactQueryHooks = createReactQueryHooks;
93
121
 
94
- var createReactQueryHook = function createReactQueryHook(_ref2) {
122
+ function buildQueryFn(methodName, jsonschema, options) {
95
123
  var _jsonschema$propertie;
96
124
 
97
- var context = _ref2.context,
98
- hookName = _ref2.hookName,
99
- hookParamsTypeName = _ref2.hookParamsTypeName,
100
- responseType = _ref2.responseType,
101
- hookKeyName = _ref2.hookKeyName,
102
- queryKeysName = _ref2.queryKeysName,
103
- methodName = _ref2.methodName,
104
- jsonschema = _ref2.jsonschema;
105
- context.addUtil('useQuery');
106
- context.addUtil('UseQueryOptions');
107
- var options = context.options.reactQuery;
108
125
  var keys = Object.keys((_jsonschema$propertie = jsonschema.properties) !== null && _jsonschema$propertie !== void 0 ? _jsonschema$propertie : {});
109
126
  var args = [];
110
127
 
@@ -114,13 +131,40 @@ var createReactQueryHook = function createReactQueryHook(_ref2) {
114
131
  })))];
115
132
  }
116
133
 
134
+ var rejectInvalidClient = t.callExpression(t.memberExpression(t.identifier('Promise'), t.identifier('reject')), [t.newExpression(t.identifier('Error'), [t.stringLiteral('Invalid client')])]);
135
+ return t.arrowFunctionExpression([], (0, _babel.optionalConditionalExpression)(t.identifier('client'), t.callExpression(t.memberExpression(t.identifier('client'), t.identifier(methodName)), args), rejectInvalidClient, options.optionalClient), false);
136
+ }
137
+
138
+ var ENABLED_QUERY_OPTION = t.objectProperty(t.identifier('enabled'), t.logicalExpression('&&', t.unaryExpression('!', t.unaryExpression('!', t.identifier('client'))), t.conditionalExpression( // explicitly check for undefined
139
+ t.binaryExpression('!=', t.optionalMemberExpression(t.identifier('options'), t.identifier('enabled'), false, true), t.identifier('undefined')), t.memberExpression(t.identifier('options'), t.identifier('enabled')), t.booleanLiteral(true))));
140
+
141
+ function buildQueryOptions(options) {
142
+ return options.optionalClient ? t.objectExpression([t.spreadElement(t.identifier('options')), t.objectProperty(t.identifier('enabled'), t.logicalExpression('&&', t.unaryExpression('!', t.unaryExpression('!', t.identifier('client'))), t.conditionalExpression( // explicitly check for undefined
143
+ t.binaryExpression('!=', t.optionalMemberExpression(t.identifier('options'), t.identifier('enabled'), false, true), t.identifier('undefined')), t.memberExpression(t.identifier('options'), t.identifier('enabled')), t.booleanLiteral(true))))]) : t.identifier('options');
144
+ }
145
+
146
+ var createReactQueryHook = function createReactQueryHook(_ref3) {
147
+ var _jsonschema$propertie2;
148
+
149
+ var context = _ref3.context,
150
+ hookName = _ref3.hookName,
151
+ hookParamsTypeName = _ref3.hookParamsTypeName,
152
+ responseType = _ref3.responseType,
153
+ hookKeyName = _ref3.hookKeyName,
154
+ queryKeysName = _ref3.queryKeysName,
155
+ methodName = _ref3.methodName,
156
+ jsonschema = _ref3.jsonschema;
157
+ context.addUtil('useQuery');
158
+ context.addUtil('UseQueryOptions');
159
+ var options = context.options.reactQuery;
160
+ var keys = Object.keys((_jsonschema$propertie2 = jsonschema.properties) !== null && _jsonschema$propertie2 !== void 0 ? _jsonschema$propertie2 : {});
117
161
  var props = ['client', 'options'];
118
162
 
119
163
  if (keys.length) {
120
164
  props = ['client', 'args', 'options'];
121
165
  }
122
166
 
123
- var selectResponseGenericTypeName = 'TData';
167
+ var selectResponseGenericTypeName = GENERIC_SELECT_RESPONSE_NAME;
124
168
  var queryFunctionDeclaration = t.functionDeclaration(t.identifier(hookName), [(0, _utils.tsObjectPattern)((0, _toConsumableArray2["default"])(props.map(function (prop) {
125
169
  return t.objectProperty(t.identifier(prop), t.identifier(prop), false, true);
126
170
  })), t.tsTypeAnnotation(t.tsTypeReference(t.identifier(hookParamsTypeName), t.tsTypeParameterInstantiation([t.tsTypeReference(t.identifier(selectResponseGenericTypeName))]))))], t.blockStatement([t.returnStatement((0, _utils.callExpression)(t.identifier('useQuery'), [generateUseQueryQueryKey({
@@ -129,8 +173,7 @@ var createReactQueryHook = function createReactQueryHook(_ref2) {
129
173
  methodName: methodName,
130
174
  props: props,
131
175
  options: options
132
- }), t.arrowFunctionExpression([], (0, _babel.optionalConditionalExpression)(t.identifier('client'), t.callExpression(t.memberExpression(t.identifier('client'), t.identifier(methodName)), args), t.callExpression(t.memberExpression(t.identifier('Promise'), t.identifier('reject')), [t.newExpression(t.identifier('Error'), [t.stringLiteral('Invalid client')])]), options.optionalClient), false), options.optionalClient ? t.objectExpression([t.spreadElement(t.identifier('options')), t.objectProperty(t.identifier('enabled'), t.logicalExpression('&&', t.unaryExpression('!', t.unaryExpression('!', t.identifier('client'))), t.conditionalExpression( // explicitly check for undefined
133
- t.binaryExpression('!=', t.optionalMemberExpression(t.identifier('options'), t.identifier('enabled'), false, true), t.identifier('undefined')), t.memberExpression(t.identifier('options'), t.identifier('enabled')), t.booleanLiteral(true))))]) : t.identifier('options')], t.tsTypeParameterInstantiation([t.tsTypeReference(t.identifier(responseType)), t.tsTypeReference(t.identifier('Error')), t.tsTypeReference(t.identifier(selectResponseGenericTypeName))])))])); // Add the TData type parameters
176
+ }), buildQueryFn(methodName, jsonschema, options), buildQueryOptions(options)], t.tsTypeParameterInstantiation([t.tsTypeReference(t.identifier(responseType)), t.tsTypeReference(t.identifier('Error')), t.tsTypeReference(t.identifier(selectResponseGenericTypeName))])))])); // Add the TData type parameters
134
177
 
135
178
  queryFunctionDeclaration.typeParameters = t.tsTypeParameterDeclaration([t.tsTypeParameter(undefined, t.tSTypeReference(t.identifier(responseType)), selectResponseGenericTypeName)]);
136
179
  return t.exportNamedDeclaration(queryFunctionDeclaration);
@@ -154,12 +197,12 @@ export interface Cw4UpdateMembersMutation {
154
197
  }
155
198
  ```
156
199
  */
157
- var createReactQueryMutationArgsInterface = function createReactQueryMutationArgsInterface(_ref3) {
158
- var context = _ref3.context,
159
- ExecuteClient = _ref3.ExecuteClient,
160
- mutationHookParamsTypeName = _ref3.mutationHookParamsTypeName,
161
- useMutationTypeParameter = _ref3.useMutationTypeParameter,
162
- jsonschema = _ref3.jsonschema;
200
+ var createReactQueryMutationArgsInterface = function createReactQueryMutationArgsInterface(_ref4) {
201
+ var context = _ref4.context,
202
+ ExecuteClient = _ref4.ExecuteClient,
203
+ mutationHookParamsTypeName = _ref4.mutationHookParamsTypeName,
204
+ useMutationTypeParameter = _ref4.useMutationTypeParameter,
205
+ jsonschema = _ref4.jsonschema;
163
206
  var typedUseMutationOptions = t.tsTypeReference(t.identifier('UseMutationOptions'), useMutationTypeParameter);
164
207
  var body = [(0, _utils.tsPropertySignature)(t.identifier('client'), t.tsTypeAnnotation(t.tsTypeReference(t.identifier(ExecuteClient))), false)];
165
208
  var msgType = (0, _types2.getParamsTypeAnnotation)(context, jsonschema);
@@ -183,14 +226,14 @@ var createReactQueryMutationArgsInterface = function createReactQueryMutationArg
183
226
 
184
227
  exports.createReactQueryMutationArgsInterface = createReactQueryMutationArgsInterface;
185
228
 
186
- var createReactQueryMutationHooks = function createReactQueryMutationHooks(_ref4) {
187
- var context = _ref4.context,
188
- execMsg = _ref4.execMsg,
189
- contractName = _ref4.contractName,
190
- ExecuteClient = _ref4.ExecuteClient;
229
+ var createReactQueryMutationHooks = function createReactQueryMutationHooks(_ref5) {
230
+ var context = _ref5.context,
231
+ execMsg = _ref5.execMsg,
232
+ contractName = _ref5.contractName,
233
+ ExecuteClient = _ref5.ExecuteClient;
191
234
  // merge the user options with the defaults
192
235
  return (0, _utils.getMessageProperties)(execMsg).reduce(function (m, schema) {
193
- var _jsonschema$propertie2, _Object$keys;
236
+ var _jsonschema$propertie3, _Object$keys;
194
237
 
195
238
  // update_members
196
239
  var execMethodUnderscoreName = Object.keys(schema.properties)[0]; // updateMembers
@@ -201,7 +244,7 @@ var createReactQueryMutationHooks = function createReactQueryMutationHooks(_ref4
201
244
 
202
245
  var mutationHookName = "use".concat(mutationHookParamsTypeName);
203
246
  var jsonschema = schema.properties[execMethodUnderscoreName];
204
- var properties = (_jsonschema$propertie2 = jsonschema.properties) !== null && _jsonschema$propertie2 !== void 0 ? _jsonschema$propertie2 : {}; // TODO: there should be a better way to do this
247
+ var properties = (_jsonschema$propertie3 = jsonschema.properties) !== null && _jsonschema$propertie3 !== void 0 ? _jsonschema$propertie3 : {}; // TODO: there should be a better way to do this
205
248
 
206
249
  var hasMsg = !!((_Object$keys = Object.keys(properties)) !== null && _Object$keys !== void 0 && _Object$keys.length || jsonschema !== null && jsonschema !== void 0 && jsonschema.$ref); // <ExecuteResult, Error, Cw4UpdateMembersMutation>
207
250
 
@@ -248,13 +291,13 @@ export const useCw4UpdateMembersMutation = ({ client, options }: Omit<Cw4UpdateM
248
291
  )
249
292
  ```
250
293
  */
251
- var createReactQueryMutationHook = function createReactQueryMutationHook(_ref5) {
252
- var context = _ref5.context,
253
- mutationHookName = _ref5.mutationHookName,
254
- mutationHookParamsTypeName = _ref5.mutationHookParamsTypeName,
255
- execMethodName = _ref5.execMethodName,
256
- useMutationTypeParameter = _ref5.useMutationTypeParameter,
257
- hasMsg = _ref5.hasMsg;
294
+ var createReactQueryMutationHook = function createReactQueryMutationHook(_ref6) {
295
+ var context = _ref6.context,
296
+ mutationHookName = _ref6.mutationHookName,
297
+ mutationHookParamsTypeName = _ref6.mutationHookParamsTypeName,
298
+ execMethodName = _ref6.execMethodName,
299
+ useMutationTypeParameter = _ref6.useMutationTypeParameter,
300
+ hasMsg = _ref6.hasMsg;
258
301
  context.addUtil('useMutation');
259
302
  context.addUtil('UseMutationOptions');
260
303
  var useMutationFunctionArgs = [(0, _babel.shorthandProperty)('client')];
@@ -270,11 +313,11 @@ var createReactQueryMutationHook = function createReactQueryMutationHook(_ref5)
270
313
 
271
314
  exports.createReactQueryMutationHook = createReactQueryMutationHook;
272
315
 
273
- function createReactQueryKeys(_ref6) {
274
- var context = _ref6.context,
275
- queryKeysName = _ref6.queryKeysName,
276
- camelContractName = _ref6.camelContractName,
277
- underscoreNames = _ref6.underscoreNames;
316
+ function createReactQueryKeys(_ref7) {
317
+ var context = _ref7.context,
318
+ queryKeysName = _ref7.queryKeysName,
319
+ camelContractName = _ref7.camelContractName,
320
+ underscoreNames = _ref7.underscoreNames;
278
321
  var options = context.options.reactQuery;
279
322
  var contractAddressTypeAnnotation = t.tsTypeAnnotation(options.optionalClient ? t.tsUnionType([t.tsStringKeyword(), t.tsUndefinedKeyword()]) : t.tSStringKeyword());
280
323
  return t.exportNamedDeclaration(t.variableDeclaration('const', [t.variableDeclarator(t.identifier(queryKeysName), t.objectExpression([// 1: contract
@@ -293,29 +336,56 @@ function createReactQueryKeys(_ref6) {
293
336
  })))))]));
294
337
  }
295
338
 
296
- function createReactQueryHookGenericInterface(_ref7) {
297
- var context = _ref7.context,
298
- QueryClient = _ref7.QueryClient,
299
- genericQueryInterfaceName = _ref7.genericQueryInterfaceName;
339
+ function createReactQueryFactory(_ref8) {
340
+ var context = _ref8.context,
341
+ queryFactoryName = _ref8.queryFactoryName,
342
+ queryKeysName = _ref8.queryKeysName,
343
+ queryMsgs = _ref8.queryMsgs;
344
+ var options = context.options.reactQuery;
345
+ return t.exportNamedDeclaration(t.variableDeclaration('const', [t.variableDeclarator(t.identifier(queryFactoryName), t.objectExpression((0, _toConsumableArray2["default"])(queryMsgs.map(function (_ref9) {
346
+ var _jsonschema$propertie4;
347
+
348
+ var methodName = _ref9.methodName,
349
+ hookParamsTypeName = _ref9.hookParamsTypeName,
350
+ responseType = _ref9.responseType,
351
+ jsonschema = _ref9.jsonschema;
352
+ var hasArgs = Object.keys((_jsonschema$propertie4 = jsonschema.properties) !== null && _jsonschema$propertie4 !== void 0 ? _jsonschema$propertie4 : {}).length > 0;
353
+ var methodQueryOptionsFn = t.arrowFunctionExpression([(0, _utils.tsObjectPattern)([(0, _babel.shorthandProperty)('client')].concat((0, _toConsumableArray2["default"])(hasArgs ? [(0, _babel.shorthandProperty)('args')] : []), [(0, _babel.shorthandProperty)('options')]), t.tsTypeAnnotation(t.tsTypeReference(t.identifier(hookParamsTypeName), t.tsTypeParameterInstantiation([t.tsTypeReference(t.identifier(GENERIC_SELECT_RESPONSE_NAME))]))))], t.objectExpression([// 1: queryKey
354
+ t.objectProperty(t.identifier('queryKey'), t.callExpression(t.memberExpression(t.identifier(queryKeysName), t.identifier(methodName)), [t.optionalMemberExpression(t.identifier('client'), t.identifier('contractAddress'), false, true)].concat((0, _toConsumableArray2["default"])(hasArgs ? [t.identifier('args')] : [])))), // 2: queryFn
355
+ t.objectProperty(t.identifier('queryFn'), buildQueryFn(methodName, jsonschema, options)), // 3: spread options
356
+ t.spreadElement(t.identifier('options')), // 4. enabled
357
+ ENABLED_QUERY_OPTION]));
358
+ methodQueryOptionsFn.typeParameters = t.tsTypeParameterDeclaration([t.tsTypeParameter(undefined, t.tsTypeReference(t.identifier(responseType)), GENERIC_SELECT_RESPONSE_NAME)]);
359
+ methodQueryOptionsFn.returnType = t.tsTypeAnnotation(t.tsTypeReference(t.identifier('UseQueryOptions'), t.tsTypeParameterInstantiation([t.tsTypeReference(t.identifier(responseType)), t.tsTypeReference(t.identifier('Error')), t.tsTypeReference(t.identifier(GENERIC_SELECT_RESPONSE_NAME))])));
360
+ return t.objectProperty( // key id is the camel method name
361
+ t.identifier((0, _case.camel)(methodName)), methodQueryOptionsFn);
362
+ }))))]));
363
+ }
364
+
365
+ var GENERIC_SELECT_RESPONSE_NAME = 'TData';
366
+
367
+ function createReactQueryHookGenericInterface(_ref10) {
368
+ var context = _ref10.context,
369
+ QueryClient = _ref10.QueryClient,
370
+ genericQueryInterfaceName = _ref10.genericQueryInterfaceName;
300
371
  var options = context.options.reactQuery;
301
372
  var genericResponseTypeName = 'TResponse';
302
- var genericSelectResponseTypeName = 'TData';
303
373
  context.addUtil('UseQueryOptions'); // UseQueryOptions<TResponse, Error, TData>,
304
374
 
305
- var typedUseQueryOptions = t.tsTypeReference(t.identifier('UseQueryOptions'), t.tsTypeParameterInstantiation([t.tsTypeReference(t.identifier(genericResponseTypeName)), t.tsTypeReference(t.identifier('Error')), t.tsTypeReference(t.identifier(genericSelectResponseTypeName))]));
375
+ var typedUseQueryOptions = t.tsTypeReference(t.identifier('UseQueryOptions'), t.tsTypeParameterInstantiation([t.tsTypeReference(t.identifier(genericResponseTypeName)), t.tsTypeReference(t.identifier('Error')), t.tsTypeReference(t.identifier(GENERIC_SELECT_RESPONSE_NAME))]));
306
376
  var body = [(0, _utils.tsPropertySignature)(t.identifier('client'), t.tsTypeAnnotation(options.optionalClient ? t.tsUnionType([t.tsTypeReference(t.identifier(QueryClient)), t.tsUndefinedKeyword()]) : t.tsTypeReference(t.identifier(QueryClient))), false), (0, _utils.tsPropertySignature)(t.identifier('options'), t.tsTypeAnnotation(options.version === 'v4' ? t.tSIntersectionType([(0, _babel.omitTypeReference)(typedUseQueryOptions, "'queryKey' | 'queryFn' | 'initialData'"), t.tSTypeLiteral([t.tsPropertySignature(t.identifier('initialData?'), t.tsTypeAnnotation(t.tsUndefinedKeyword()))])]) : typedUseQueryOptions), true)];
307
377
  return t.exportNamedDeclaration(t.tsInterfaceDeclaration(t.identifier(genericQueryInterfaceName), t.tsTypeParameterDeclaration([// 1: TResponse
308
378
  t.tsTypeParameter(undefined, undefined, genericResponseTypeName), // 2: TData
309
- t.tsTypeParameter(undefined, t.tSTypeReference(t.identifier(genericResponseTypeName)), genericSelectResponseTypeName)]), [], t.tSInterfaceBody(body)));
379
+ t.tsTypeParameter(undefined, t.tSTypeReference(t.identifier(genericResponseTypeName)), GENERIC_SELECT_RESPONSE_NAME)]), [], t.tSInterfaceBody(body)));
310
380
  }
311
381
 
312
- var createReactQueryHookInterface = function createReactQueryHookInterface(_ref8) {
313
- var context = _ref8.context,
314
- QueryClient = _ref8.QueryClient,
315
- hookParamsTypeName = _ref8.hookParamsTypeName,
316
- queryInterfaceName = _ref8.queryInterfaceName,
317
- responseType = _ref8.responseType,
318
- jsonschema = _ref8.jsonschema;
382
+ var createReactQueryHookInterface = function createReactQueryHookInterface(_ref11) {
383
+ var context = _ref11.context,
384
+ QueryClient = _ref11.QueryClient,
385
+ hookParamsTypeName = _ref11.hookParamsTypeName,
386
+ queryInterfaceName = _ref11.queryInterfaceName,
387
+ responseType = _ref11.responseType,
388
+ jsonschema = _ref11.jsonschema;
319
389
  // merge the user options with the defaults
320
390
  var options = context.options.reactQuery;
321
391
  var body = [];
@@ -326,17 +396,17 @@ var createReactQueryHookInterface = function createReactQueryHookInterface(_ref8
326
396
  t.tsTypeLiteral(props))));
327
397
  }
328
398
 
329
- return t.exportNamedDeclaration(t.tsInterfaceDeclaration(t.identifier(hookParamsTypeName), t.tsTypeParameterDeclaration([t.tSTypeParameter(undefined, undefined, 'TData')]), [t.tSExpressionWithTypeArguments(t.identifier(queryInterfaceName), t.tsTypeParameterInstantiation([// 1: response
399
+ return t.exportNamedDeclaration(t.tsInterfaceDeclaration(t.identifier(hookParamsTypeName), t.tsTypeParameterDeclaration([t.tSTypeParameter(undefined, undefined, GENERIC_SELECT_RESPONSE_NAME)]), [t.tSExpressionWithTypeArguments(t.identifier(queryInterfaceName), t.tsTypeParameterInstantiation([// 1: response
330
400
  t.tsTypeReference(t.identifier(responseType)), // 2: select generic
331
- t.tSTypeReference(t.identifier('TData'))]))], t.tsInterfaceBody(body)));
401
+ t.tSTypeReference(t.identifier(GENERIC_SELECT_RESPONSE_NAME))]))], t.tsInterfaceBody(body)));
332
402
  };
333
403
 
334
404
  exports.createReactQueryHookInterface = createReactQueryHookInterface;
335
405
 
336
406
  var getProps = function getProps(context, jsonschema) {
337
- var _jsonschema$propertie3;
407
+ var _jsonschema$propertie5;
338
408
 
339
- var keys = Object.keys((_jsonschema$propertie3 = jsonschema.properties) !== null && _jsonschema$propertie3 !== void 0 ? _jsonschema$propertie3 : {});
409
+ var keys = Object.keys((_jsonschema$propertie5 = jsonschema.properties) !== null && _jsonschema$propertie5 !== void 0 ? _jsonschema$propertie5 : {});
340
410
  if (!keys.length) return [];
341
411
  return keys.map(function (prop) {
342
412
  var _getPropertyType = (0, _types2.getPropertyType)(context, jsonschema, prop),
@@ -347,12 +417,12 @@ var getProps = function getProps(context, jsonschema) {
347
417
  });
348
418
  };
349
419
 
350
- var generateUseQueryQueryKey = function generateUseQueryQueryKey(_ref9) {
351
- var hookKeyName = _ref9.hookKeyName,
352
- queryKeysName = _ref9.queryKeysName,
353
- methodName = _ref9.methodName,
354
- props = _ref9.props,
355
- options = _ref9.options;
420
+ var generateUseQueryQueryKey = function generateUseQueryQueryKey(_ref12) {
421
+ var hookKeyName = _ref12.hookKeyName,
422
+ queryKeysName = _ref12.queryKeysName,
423
+ methodName = _ref12.methodName,
424
+ props = _ref12.props,
425
+ options = _ref12.options;
356
426
  var optionalClient = options.optionalClient,
357
427
  queryKeys = options.queryKeys;
358
428
  var hasArgs = props.includes('args');
@@ -58,6 +58,19 @@ it('createReactQueryHooks', function () {
58
58
  contractName: 'Sg721',
59
59
  QueryClient: 'Sg721QueryClient'
60
60
  })));
61
+ (0, _testUtils.expectCode)(t.program((0, _reactQuery.createReactQueryHooks)({
62
+ context: (0, _testUtils.makeContext)(_query_msg["default"], {
63
+ reactQuery: {
64
+ optionalClient: true,
65
+ version: 'v4',
66
+ queryKeys: true,
67
+ queryFactory: true
68
+ }
69
+ }),
70
+ queryMsg: _query_msg["default"],
71
+ contractName: 'Sg721',
72
+ QueryClient: 'Sg721QueryClient'
73
+ })));
61
74
  (0, _testUtils.expectCode)(t.program((0, _reactQuery.createReactQueryMutationHooks)({
62
75
  context: execCtx,
63
76
  execMsg: _execute_msg_for__empty["default"],
@@ -25,12 +25,7 @@ export const createReactQueryHooks = ({
25
25
  }));
26
26
  }
27
27
 
28
- body.push(createReactQueryHookGenericInterface({
29
- context,
30
- QueryClient,
31
- genericQueryInterfaceName
32
- }));
33
- body.push(...getMessageProperties(queryMsg).reduce((m, schema) => {
28
+ const queryMsgs = getMessageProperties(queryMsg).map(schema => {
34
29
  // list_voters
35
30
  const underscoreName = Object.keys(schema.properties)[0]; // listVoters
36
31
 
@@ -44,6 +39,40 @@ export const createReactQueryHooks = ({
44
39
 
45
40
  const getterKey = camel(`${contractName}${pascal(methodName)}`);
46
41
  const jsonschema = schema.properties[underscoreName];
42
+ return {
43
+ underscoreName,
44
+ methodName,
45
+ hookParamsTypeName,
46
+ hookName,
47
+ responseType,
48
+ getterKey,
49
+ jsonschema
50
+ };
51
+ });
52
+ const queryFactoryName = `${camel(contractName)}Queries`;
53
+
54
+ if (options.queryFactory) {
55
+ body.push(createReactQueryFactory({
56
+ context,
57
+ queryFactoryName,
58
+ queryKeysName,
59
+ queryMsgs
60
+ }));
61
+ }
62
+
63
+ body.push(createReactQueryHookGenericInterface({
64
+ context,
65
+ QueryClient,
66
+ genericQueryInterfaceName
67
+ }));
68
+ body.push(...queryMsgs.reduce((m, {
69
+ methodName,
70
+ hookParamsTypeName,
71
+ hookName,
72
+ responseType,
73
+ getterKey,
74
+ jsonschema
75
+ }) => {
47
76
  return [createReactQueryHookInterface({
48
77
  context,
49
78
  hookParamsTypeName,
@@ -64,6 +93,29 @@ export const createReactQueryHooks = ({
64
93
  }, []));
65
94
  return body;
66
95
  };
96
+
97
+ function buildQueryFn(methodName, jsonschema, options) {
98
+ const keys = Object.keys(jsonschema.properties ?? {});
99
+ let args = [];
100
+
101
+ if (keys.length) {
102
+ args = [t.objectExpression([...keys.map(prop => {
103
+ return t.objectProperty(t.identifier(camel(prop)), t.memberExpression(t.identifier('args'), t.identifier(camel(prop))));
104
+ })])];
105
+ }
106
+
107
+ const rejectInvalidClient = t.callExpression(t.memberExpression(t.identifier('Promise'), t.identifier('reject')), [t.newExpression(t.identifier('Error'), [t.stringLiteral('Invalid client')])]);
108
+ return t.arrowFunctionExpression([], optionalConditionalExpression(t.identifier('client'), t.callExpression(t.memberExpression(t.identifier('client'), t.identifier(methodName)), args), rejectInvalidClient, options.optionalClient), false);
109
+ }
110
+
111
+ const ENABLED_QUERY_OPTION = t.objectProperty(t.identifier('enabled'), t.logicalExpression('&&', t.unaryExpression('!', t.unaryExpression('!', t.identifier('client'))), t.conditionalExpression( // explicitly check for undefined
112
+ t.binaryExpression('!=', t.optionalMemberExpression(t.identifier('options'), t.identifier('enabled'), false, true), t.identifier('undefined')), t.memberExpression(t.identifier('options'), t.identifier('enabled')), t.booleanLiteral(true))));
113
+
114
+ function buildQueryOptions(options) {
115
+ return options.optionalClient ? t.objectExpression([t.spreadElement(t.identifier('options')), t.objectProperty(t.identifier('enabled'), t.logicalExpression('&&', t.unaryExpression('!', t.unaryExpression('!', t.identifier('client'))), t.conditionalExpression( // explicitly check for undefined
116
+ t.binaryExpression('!=', t.optionalMemberExpression(t.identifier('options'), t.identifier('enabled'), false, true), t.identifier('undefined')), t.memberExpression(t.identifier('options'), t.identifier('enabled')), t.booleanLiteral(true))))]) : t.identifier('options');
117
+ }
118
+
67
119
  export const createReactQueryHook = ({
68
120
  context,
69
121
  hookName,
@@ -78,21 +130,13 @@ export const createReactQueryHook = ({
78
130
  context.addUtil('UseQueryOptions');
79
131
  const options = context.options.reactQuery;
80
132
  const keys = Object.keys(jsonschema.properties ?? {});
81
- let args = [];
82
-
83
- if (keys.length) {
84
- args = [t.objectExpression([...keys.map(prop => {
85
- return t.objectProperty(t.identifier(camel(prop)), t.memberExpression(t.identifier('args'), t.identifier(camel(prop))));
86
- })])];
87
- }
88
-
89
133
  let props = ['client', 'options'];
90
134
 
91
135
  if (keys.length) {
92
136
  props = ['client', 'args', 'options'];
93
137
  }
94
138
 
95
- const selectResponseGenericTypeName = 'TData';
139
+ const selectResponseGenericTypeName = GENERIC_SELECT_RESPONSE_NAME;
96
140
  const queryFunctionDeclaration = t.functionDeclaration(t.identifier(hookName), [tsObjectPattern([...props.map(prop => {
97
141
  return t.objectProperty(t.identifier(prop), t.identifier(prop), false, true);
98
142
  })], t.tsTypeAnnotation(t.tsTypeReference(t.identifier(hookParamsTypeName), t.tsTypeParameterInstantiation([t.tsTypeReference(t.identifier(selectResponseGenericTypeName))]))))], t.blockStatement([t.returnStatement(callExpression(t.identifier('useQuery'), [generateUseQueryQueryKey({
@@ -101,8 +145,7 @@ export const createReactQueryHook = ({
101
145
  methodName,
102
146
  props,
103
147
  options
104
- }), t.arrowFunctionExpression([], optionalConditionalExpression(t.identifier('client'), t.callExpression(t.memberExpression(t.identifier('client'), t.identifier(methodName)), args), t.callExpression(t.memberExpression(t.identifier('Promise'), t.identifier('reject')), [t.newExpression(t.identifier('Error'), [t.stringLiteral('Invalid client')])]), options.optionalClient), false), options.optionalClient ? t.objectExpression([t.spreadElement(t.identifier('options')), t.objectProperty(t.identifier('enabled'), t.logicalExpression('&&', t.unaryExpression('!', t.unaryExpression('!', t.identifier('client'))), t.conditionalExpression( // explicitly check for undefined
105
- t.binaryExpression('!=', t.optionalMemberExpression(t.identifier('options'), t.identifier('enabled'), false, true), t.identifier('undefined')), t.memberExpression(t.identifier('options'), t.identifier('enabled')), t.booleanLiteral(true))))]) : t.identifier('options')], t.tsTypeParameterInstantiation([t.tsTypeReference(t.identifier(responseType)), t.tsTypeReference(t.identifier('Error')), t.tsTypeReference(t.identifier(selectResponseGenericTypeName))])))])); // Add the TData type parameters
148
+ }), buildQueryFn(methodName, jsonschema, options), buildQueryOptions(options)], t.tsTypeParameterInstantiation([t.tsTypeReference(t.identifier(responseType)), t.tsTypeReference(t.identifier('Error')), t.tsTypeReference(t.identifier(selectResponseGenericTypeName))])))])); // Add the TData type parameters
106
149
 
107
150
  queryFunctionDeclaration.typeParameters = t.tsTypeParameterDeclaration([t.tsTypeParameter(undefined, t.tSTypeReference(t.identifier(responseType)), selectResponseGenericTypeName)]);
108
151
  return t.exportNamedDeclaration(queryFunctionDeclaration);
@@ -250,6 +293,34 @@ function createReactQueryKeys({
250
293
  shorthandProperty('args')])]), t.tSTypeReference(t.identifier('const'))))))]))]));
251
294
  }
252
295
 
296
+ function createReactQueryFactory({
297
+ context,
298
+ queryFactoryName,
299
+ queryKeysName,
300
+ queryMsgs
301
+ }) {
302
+ const options = context.options.reactQuery;
303
+ return t.exportNamedDeclaration(t.variableDeclaration('const', [t.variableDeclarator(t.identifier(queryFactoryName), t.objectExpression([...queryMsgs.map(({
304
+ methodName,
305
+ hookParamsTypeName,
306
+ responseType,
307
+ jsonschema
308
+ }) => {
309
+ const hasArgs = Object.keys(jsonschema.properties ?? {}).length > 0;
310
+ const methodQueryOptionsFn = t.arrowFunctionExpression([tsObjectPattern([shorthandProperty('client'), ...(hasArgs ? [shorthandProperty('args')] : []), shorthandProperty('options')], t.tsTypeAnnotation(t.tsTypeReference(t.identifier(hookParamsTypeName), t.tsTypeParameterInstantiation([t.tsTypeReference(t.identifier(GENERIC_SELECT_RESPONSE_NAME))]))))], t.objectExpression([// 1: queryKey
311
+ t.objectProperty(t.identifier('queryKey'), t.callExpression(t.memberExpression(t.identifier(queryKeysName), t.identifier(methodName)), [t.optionalMemberExpression(t.identifier('client'), t.identifier('contractAddress'), false, true), ...(hasArgs ? [t.identifier('args')] : [])])), // 2: queryFn
312
+ t.objectProperty(t.identifier('queryFn'), buildQueryFn(methodName, jsonschema, options)), // 3: spread options
313
+ t.spreadElement(t.identifier('options')), // 4. enabled
314
+ ENABLED_QUERY_OPTION]));
315
+ methodQueryOptionsFn.typeParameters = t.tsTypeParameterDeclaration([t.tsTypeParameter(undefined, t.tsTypeReference(t.identifier(responseType)), GENERIC_SELECT_RESPONSE_NAME)]);
316
+ methodQueryOptionsFn.returnType = t.tsTypeAnnotation(t.tsTypeReference(t.identifier('UseQueryOptions'), t.tsTypeParameterInstantiation([t.tsTypeReference(t.identifier(responseType)), t.tsTypeReference(t.identifier('Error')), t.tsTypeReference(t.identifier(GENERIC_SELECT_RESPONSE_NAME))])));
317
+ return t.objectProperty( // key id is the camel method name
318
+ t.identifier(camel(methodName)), methodQueryOptionsFn);
319
+ })]))]));
320
+ }
321
+
322
+ const GENERIC_SELECT_RESPONSE_NAME = 'TData';
323
+
253
324
  function createReactQueryHookGenericInterface({
254
325
  context,
255
326
  QueryClient,
@@ -257,14 +328,13 @@ function createReactQueryHookGenericInterface({
257
328
  }) {
258
329
  const options = context.options.reactQuery;
259
330
  const genericResponseTypeName = 'TResponse';
260
- const genericSelectResponseTypeName = 'TData';
261
331
  context.addUtil('UseQueryOptions'); // UseQueryOptions<TResponse, Error, TData>,
262
332
 
263
- const typedUseQueryOptions = t.tsTypeReference(t.identifier('UseQueryOptions'), t.tsTypeParameterInstantiation([t.tsTypeReference(t.identifier(genericResponseTypeName)), t.tsTypeReference(t.identifier('Error')), t.tsTypeReference(t.identifier(genericSelectResponseTypeName))]));
333
+ const typedUseQueryOptions = t.tsTypeReference(t.identifier('UseQueryOptions'), t.tsTypeParameterInstantiation([t.tsTypeReference(t.identifier(genericResponseTypeName)), t.tsTypeReference(t.identifier('Error')), t.tsTypeReference(t.identifier(GENERIC_SELECT_RESPONSE_NAME))]));
264
334
  const body = [tsPropertySignature(t.identifier('client'), t.tsTypeAnnotation(options.optionalClient ? t.tsUnionType([t.tsTypeReference(t.identifier(QueryClient)), t.tsUndefinedKeyword()]) : t.tsTypeReference(t.identifier(QueryClient))), false), tsPropertySignature(t.identifier('options'), t.tsTypeAnnotation(options.version === 'v4' ? t.tSIntersectionType([omitTypeReference(typedUseQueryOptions, "'queryKey' | 'queryFn' | 'initialData'"), t.tSTypeLiteral([t.tsPropertySignature(t.identifier('initialData?'), t.tsTypeAnnotation(t.tsUndefinedKeyword()))])]) : typedUseQueryOptions), true)];
265
335
  return t.exportNamedDeclaration(t.tsInterfaceDeclaration(t.identifier(genericQueryInterfaceName), t.tsTypeParameterDeclaration([// 1: TResponse
266
336
  t.tsTypeParameter(undefined, undefined, genericResponseTypeName), // 2: TData
267
- t.tsTypeParameter(undefined, t.tSTypeReference(t.identifier(genericResponseTypeName)), genericSelectResponseTypeName)]), [], t.tSInterfaceBody(body)));
337
+ t.tsTypeParameter(undefined, t.tSTypeReference(t.identifier(genericResponseTypeName)), GENERIC_SELECT_RESPONSE_NAME)]), [], t.tSInterfaceBody(body)));
268
338
  }
269
339
 
270
340
  export const createReactQueryHookInterface = ({
@@ -285,9 +355,9 @@ export const createReactQueryHookInterface = ({
285
355
  t.tsTypeLiteral(props))));
286
356
  }
287
357
 
288
- return t.exportNamedDeclaration(t.tsInterfaceDeclaration(t.identifier(hookParamsTypeName), t.tsTypeParameterDeclaration([t.tSTypeParameter(undefined, undefined, 'TData')]), [t.tSExpressionWithTypeArguments(t.identifier(queryInterfaceName), t.tsTypeParameterInstantiation([// 1: response
358
+ return t.exportNamedDeclaration(t.tsInterfaceDeclaration(t.identifier(hookParamsTypeName), t.tsTypeParameterDeclaration([t.tSTypeParameter(undefined, undefined, GENERIC_SELECT_RESPONSE_NAME)]), [t.tSExpressionWithTypeArguments(t.identifier(queryInterfaceName), t.tsTypeParameterInstantiation([// 1: response
289
359
  t.tsTypeReference(t.identifier(responseType)), // 2: select generic
290
- t.tSTypeReference(t.identifier('TData'))]))], t.tsInterfaceBody(body)));
360
+ t.tSTypeReference(t.identifier(GENERIC_SELECT_RESPONSE_NAME))]))], t.tsInterfaceBody(body)));
291
361
  };
292
362
 
293
363
  const getProps = (context, jsonschema) => {
@@ -43,6 +43,19 @@ it('createReactQueryHooks', () => {
43
43
  contractName: 'Sg721',
44
44
  QueryClient: 'Sg721QueryClient'
45
45
  })));
46
+ expectCode(t.program(createReactQueryHooks({
47
+ context: makeContext(query_msg, {
48
+ reactQuery: {
49
+ optionalClient: true,
50
+ version: 'v4',
51
+ queryKeys: true,
52
+ queryFactory: true
53
+ }
54
+ }),
55
+ queryMsg: query_msg,
56
+ contractName: 'Sg721',
57
+ QueryClient: 'Sg721QueryClient'
58
+ })));
46
59
  expectCode(t.program(createReactQueryMutationHooks({
47
60
  context: execCtx,
48
61
  execMsg: execute_msg,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wasm-ast-types",
3
- "version": "0.13.0",
3
+ "version": "0.14.0",
4
4
  "description": "CosmWasm TypeScript AST generation",
5
5
  "author": "Dan Lynch <pyramation@gmail.com>",
6
6
  "homepage": "https://github.com/pyramation/cosmwasm-typescript-gen/tree/master/packages/wasm-ast-types#readme",
@@ -88,5 +88,5 @@
88
88
  "case": "1.6.3",
89
89
  "deepmerge": "4.2.2"
90
90
  },
91
- "gitHead": "e46828bd476d30465a35a8021e611938abf89846"
91
+ "gitHead": "e58a660b6265fc94bdd11f311afe9fac11708c51"
92
92
  }
@@ -10,6 +10,7 @@ export interface ReactQueryOptions {
10
10
  mutations?: boolean;
11
11
  camelize?: boolean;
12
12
  queryKeys?: boolean
13
+ queryFactory?: boolean
13
14
  }
14
15
 
15
16
  export interface TSClientOptions {