wasm-ast-types 0.13.0 → 0.15.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -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,
@@ -62,11 +62,12 @@ export const classDeclaration = (name, body, implementsExressions = [], superCla
62
62
 
63
63
  return declaration;
64
64
  };
65
- export const classProperty = (name, typeAnnotation = null, isReadonly = false, isStatic = false) => {
65
+ export const classProperty = (name, typeAnnotation = null, isReadonly = false, isStatic = false, noImplicitOverride = false) => {
66
66
  const prop = t.classProperty(t.identifier(name));
67
67
  if (isReadonly) prop.readonly = true;
68
68
  if (isStatic) prop.static = true;
69
69
  if (typeAnnotation) prop.typeAnnotation = typeAnnotation;
70
+ if (noImplicitOverride) prop.override = true;
70
71
  return prop;
71
72
  };
72
73
  export const arrowFunctionExpression = (params, body, returnType, isAsync = false) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wasm-ast-types",
3
- "version": "0.13.0",
3
+ "version": "0.15.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": "ef4b39134cf220c860469ae3cae49aeab91ee80d"
92
92
  }
@@ -414,13 +414,15 @@ export const createExecuteClass = (
414
414
  ...bindings
415
415
  ]);
416
416
 
417
+ const noImplicitOverride = context.options.client.noImplicitOverride && extendsClassName && context.options.client.execExtendsQuery;
418
+
417
419
  return t.exportNamedDeclaration(
418
420
  classDeclaration(className,
419
421
  [
420
422
  // client
421
423
  classProperty('client', t.tsTypeAnnotation(
422
424
  t.tsTypeReference(t.identifier('SigningCosmWasmClient'))
423
- )),
425
+ ), false, false, noImplicitOverride),
424
426
 
425
427
  // sender
426
428
  classProperty('sender', t.tsTypeAnnotation(
@@ -430,7 +432,7 @@ export const createExecuteClass = (
430
432
  // contractAddress
431
433
  classProperty('contractAddress', t.tsTypeAnnotation(
432
434
  t.tsStringKeyword()
433
- )),
435
+ ), false, false, noImplicitOverride),
434
436
 
435
437
  // constructor
436
438
  t.classMethod('constructor',