wasm-ast-types 0.8.1 → 0.10.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.
Files changed (39) hide show
  1. package/main/client/client.js +2 -2
  2. package/main/client/test/ts-client.arrays-ref.spec.js +1 -3
  3. package/main/client/test/ts-client.arrays.spec.js +1 -3
  4. package/main/client/test/ts-client.cw-named-groups.test.js +1 -3
  5. package/main/client/test/ts-client.cw-proposal-single.test.js +4 -6
  6. package/main/client/test/ts-client.empty-enums.spec.js +1 -3
  7. package/main/client/test/ts-client.issues.test.js +103 -0
  8. package/main/client/test/ts-client.sg721.spec.js +1 -3
  9. package/main/client/test/ts-client.spec.js +17 -19
  10. package/main/client/test/ts-client.vectis.spec.js +9 -11
  11. package/main/context/context.js +27 -5
  12. package/main/message-composer/message-composer.spec.js +2 -4
  13. package/main/react-query/react-query.js +106 -28
  14. package/main/react-query/react-query.spec.js +5 -7
  15. package/main/recoil/recoil.js +3 -3
  16. package/main/recoil/recoil.spec.js +2 -4
  17. package/main/utils/types.js +13 -1
  18. package/module/client/client.js +4 -4
  19. package/module/client/test/ts-client.arrays-ref.spec.js +2 -3
  20. package/module/client/test/ts-client.arrays.spec.js +2 -3
  21. package/module/client/test/ts-client.cw-named-groups.test.js +2 -3
  22. package/module/client/test/ts-client.cw-proposal-single.test.js +5 -6
  23. package/module/client/test/ts-client.empty-enums.spec.js +2 -3
  24. package/module/client/test/ts-client.issues.test.js +21 -0
  25. package/module/client/test/ts-client.sg721.spec.js +2 -3
  26. package/module/client/test/ts-client.spec.js +18 -19
  27. package/module/client/test/ts-client.vectis.spec.js +10 -11
  28. package/module/context/context.js +25 -4
  29. package/module/message-composer/message-composer.spec.js +3 -4
  30. package/module/react-query/react-query.js +99 -20
  31. package/module/react-query/react-query.spec.js +6 -7
  32. package/module/recoil/recoil.js +4 -4
  33. package/module/recoil/recoil.spec.js +3 -4
  34. package/module/utils/types.js +7 -1
  35. package/package.json +2 -2
  36. package/types/context/context.d.ts +26 -3
  37. package/types/react-query/react-query.d.ts +3 -2
  38. package/types/recoil/recoil.d.ts +1 -1
  39. package/types/utils/types.d.ts +1 -0
@@ -1,7 +1,13 @@
1
1
  import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
2
+
3
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
4
+
5
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
6
+
2
7
  import { convertUtilsToImportList, getImportStatements } from "./imports";
3
8
  import deepmerge from "deepmerge"; /// Plugin Types
4
9
 
10
+ ;
5
11
  export const defaultOptions = {
6
12
  types: {
7
13
  enabled: true,
@@ -21,16 +27,31 @@ export const defaultOptions = {
21
27
  optionalClient: false,
22
28
  version: 'v3',
23
29
  mutations: false,
24
- camelize: true
30
+ camelize: true,
31
+ queryKeys: false
25
32
  }
26
33
  };
34
+ export const getDefinitionSchema = schemas => {
35
+ const aggregateSchema = {
36
+ definitions: {//
37
+ }
38
+ };
39
+ schemas.forEach(schema => {
40
+ schema.definitions = schema.definitions || {};
41
+ aggregateSchema.definitions = _objectSpread(_objectSpread({}, aggregateSchema.definitions), schema.definitions);
42
+ });
43
+ return aggregateSchema;
44
+ };
27
45
  export class RenderContext {
28
- constructor(schema, options) {
29
- _defineProperty(this, "schema", void 0);
46
+ constructor(contract, options) {
47
+ _defineProperty(this, "contract", void 0);
30
48
 
31
49
  _defineProperty(this, "utils", []);
32
50
 
33
- this.schema = schema;
51
+ _defineProperty(this, "schema", void 0);
52
+
53
+ this.contract = contract;
54
+ this.schema = getDefinitionSchema(contract.schemas);
34
55
  this.options = deepmerge(defaultOptions, options ?? {});
35
56
  }
36
57
 
@@ -1,12 +1,11 @@
1
1
  import execute_msg from '../../../../__fixtures__/basic/execute_msg_for__empty.json';
2
2
  import { createMessageComposerClass, createMessageComposerInterface } from './message-composer';
3
- import { RenderContext } from '../context';
4
- import { expectCode } from '../../test-utils';
3
+ import { expectCode, makeContext } from '../../test-utils';
5
4
  it('execute classes', () => {
6
- const ctx = new RenderContext(execute_msg);
5
+ const ctx = makeContext(execute_msg);
7
6
  expectCode(createMessageComposerClass(ctx, 'SG721MessageComposer', 'SG721Message', execute_msg));
8
7
  });
9
8
  it('createMessageComposerInterface', () => {
10
- const ctx = new RenderContext(execute_msg);
9
+ const ctx = makeContext(execute_msg);
11
10
  expectCode(createMessageComposerInterface(ctx, 'SG721Message', execute_msg));
12
11
  });
@@ -2,7 +2,7 @@ import * as t from '@babel/types';
2
2
  import { camel, pascal } from 'case';
3
3
  import { callExpression, getMessageProperties, identifier, tsObjectPattern, tsPropertySignature } from '../utils';
4
4
  import { omitTypeReference, optionalConditionalExpression, propertySignature, shorthandProperty } from '../utils/babel';
5
- import { getParamsTypeAnnotation, getPropertyType } from '../utils/types';
5
+ import { getParamsTypeAnnotation, getPropertyType, getResponseType } from '../utils/types';
6
6
  import { FIXED_EXECUTE_PARAMS } from '../client';
7
7
  export const createReactQueryHooks = ({
8
8
  context,
@@ -12,17 +12,36 @@ export const createReactQueryHooks = ({
12
12
  }) => {
13
13
  const options = context.options.reactQuery;
14
14
  const genericQueryInterfaceName = `${pascal(contractName)}ReactQuery`;
15
- const body = [createReactQueryHookGenericInterface({
15
+ const underscoreNames = getMessageProperties(queryMsg).map(schema => Object.keys(schema.properties)[0]);
16
+ const body = [];
17
+ const queryKeysName = `${camel(contractName)}QueryKeys`;
18
+
19
+ if (options.queryKeys) {
20
+ body.push(createReactQueryKeys({
21
+ context,
22
+ queryKeysName,
23
+ camelContractName: camel(contractName),
24
+ underscoreNames
25
+ }));
26
+ }
27
+
28
+ body.push(createReactQueryHookGenericInterface({
16
29
  context,
17
30
  QueryClient,
18
31
  genericQueryInterfaceName
19
- })];
32
+ }));
20
33
  body.push(...getMessageProperties(queryMsg).reduce((m, schema) => {
21
- const underscoreName = Object.keys(schema.properties)[0];
22
- const methodName = camel(underscoreName);
23
- const hookParamsTypeName = `${pascal(contractName)}${pascal(methodName)}Query`;
24
- const hookName = `use${hookParamsTypeName}`;
25
- const responseType = pascal(`${methodName}Response`);
34
+ // list_voters
35
+ const underscoreName = Object.keys(schema.properties)[0]; // listVoters
36
+
37
+ const methodName = camel(underscoreName); // Cw3FlexMultisigListVotersQuery
38
+
39
+ const hookParamsTypeName = `${pascal(contractName)}${pascal(methodName)}Query`; // useCw3FlexMultisigListVotersQuery
40
+
41
+ const hookName = `use${hookParamsTypeName}`; // listVotersResponse
42
+
43
+ const responseType = getResponseType(context, underscoreName); // cw3FlexMultisigListVoters
44
+
26
45
  const getterKey = camel(`${contractName}${pascal(methodName)}`);
27
46
  const jsonschema = schema.properties[underscoreName];
28
47
  return [createReactQueryHookInterface({
@@ -37,6 +56,7 @@ export const createReactQueryHooks = ({
37
56
  methodName,
38
57
  hookName,
39
58
  hookParamsTypeName,
59
+ queryKeysName,
40
60
  responseType,
41
61
  hookKeyName: getterKey,
42
62
  jsonschema
@@ -50,6 +70,7 @@ export const createReactQueryHook = ({
50
70
  hookParamsTypeName,
51
71
  responseType,
52
72
  hookKeyName,
73
+ queryKeysName,
53
74
  methodName,
54
75
  jsonschema
55
76
  }) => {
@@ -71,10 +92,20 @@ export const createReactQueryHook = ({
71
92
  props = ['client', 'args', 'options'];
72
93
  }
73
94
 
74
- return t.exportNamedDeclaration(t.functionDeclaration(t.identifier(hookName), [tsObjectPattern([...props.map(prop => {
95
+ const selectResponseGenericTypeName = 'TData';
96
+ const queryFunctionDeclaration = t.functionDeclaration(t.identifier(hookName), [tsObjectPattern([...props.map(prop => {
75
97
  return t.objectProperty(t.identifier(prop), t.identifier(prop), false, true);
76
- })], t.tsTypeAnnotation(t.tsTypeReference(t.identifier(hookParamsTypeName))))], t.blockStatement([t.returnStatement(callExpression(t.identifier('useQuery'), [t.arrayExpression(generateUseQueryQueryKey(hookKeyName, props, options.optionalClient)), 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
77
- 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(responseType)), t.tsArrayType(t.tsParenthesizedType(t.tsUnionType([t.tsStringKeyword(), t.tsUndefinedKeyword()])))])))])));
98
+ })], t.tsTypeAnnotation(t.tsTypeReference(t.identifier(hookParamsTypeName), t.tsTypeParameterInstantiation([t.tsTypeReference(t.identifier(selectResponseGenericTypeName))]))))], t.blockStatement([t.returnStatement(callExpression(t.identifier('useQuery'), [generateUseQueryQueryKey({
99
+ hookKeyName,
100
+ queryKeysName,
101
+ methodName,
102
+ props,
103
+ 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
106
+
107
+ queryFunctionDeclaration.typeParameters = t.tsTypeParameterDeclaration([t.tsTypeParameter(undefined, t.tSTypeReference(t.identifier(responseType)), selectResponseGenericTypeName)]);
108
+ return t.exportNamedDeclaration(queryFunctionDeclaration);
78
109
  };
79
110
 
80
111
  /**
@@ -196,17 +227,44 @@ export const createReactQueryMutationHook = ({
196
227
  ), t.identifier('options')], useMutationTypeParameter))])));
197
228
  };
198
229
 
230
+ function createReactQueryKeys({
231
+ context,
232
+ queryKeysName,
233
+ camelContractName,
234
+ underscoreNames
235
+ }) {
236
+ const options = context.options.reactQuery;
237
+ const contractAddressTypeAnnotation = t.tsTypeAnnotation(options.optionalClient ? t.tsUnionType([t.tsStringKeyword(), t.tsUndefinedKeyword()]) : t.tSStringKeyword());
238
+ return t.exportNamedDeclaration(t.variableDeclaration('const', [t.variableDeclarator(t.identifier(queryKeysName), t.objectExpression([// 1: contract
239
+ t.objectProperty(t.identifier('contract'), t.tSAsExpression(t.arrayExpression([t.objectExpression([t.objectProperty(t.identifier('contract'), t.stringLiteral(camelContractName))])]), t.tSTypeReference(t.identifier('const')))), // 2: address
240
+ t.objectProperty(t.identifier('address'), t.arrowFunctionExpression([identifier('contractAddress', contractAddressTypeAnnotation)], t.tSAsExpression(t.arrayExpression([t.objectExpression([// 1
241
+ t.spreadElement(t.memberExpression(t.memberExpression(t.identifier(queryKeysName), t.identifier('contract')), t.numericLiteral(0), true // computed
242
+ )), t.objectProperty(t.identifier('address'), t.identifier('contractAddress'))])]), t.tSTypeReference(t.identifier('const'))))), // 3: methods
243
+ ...underscoreNames.map(underscoreMethodName => t.objectProperty( // key id is the camel method name
244
+ t.identifier(camel(underscoreMethodName)), t.arrowFunctionExpression([identifier('contractAddress', contractAddressTypeAnnotation), identifier('args', // Record<string, unknown>
245
+ t.tSTypeAnnotation(t.tsTypeReference(t.identifier('Record'), t.tsTypeParameterInstantiation([t.tsStringKeyword(), t.tsUnknownKeyword()]))), true // optional
246
+ )], t.tSAsExpression(t.arrayExpression([t.objectExpression([//...cw3FlexMultisigQueryKeys.address(contractAddress)[0]
247
+ t.spreadElement(t.memberExpression(t.callExpression(t.memberExpression(t.identifier(queryKeysName), t.identifier('address')), [t.identifier('contractAddress')]), t.numericLiteral(0), true // computed
248
+ )), // method: list_voters
249
+ t.objectProperty(t.identifier('method'), t.stringLiteral(underscoreMethodName)), // args
250
+ shorthandProperty('args')])]), t.tSTypeReference(t.identifier('const'))))))]))]));
251
+ }
252
+
199
253
  function createReactQueryHookGenericInterface({
200
254
  context,
201
255
  QueryClient,
202
256
  genericQueryInterfaceName
203
257
  }) {
204
258
  const options = context.options.reactQuery;
205
- const genericTypeName = 'TResponse';
206
- context.addUtil('UseQueryOptions');
207
- const typedUseQueryOptions = t.tsTypeReference(t.identifier('UseQueryOptions'), t.tsTypeParameterInstantiation([t.tsTypeReference(t.identifier(genericTypeName)), t.tsTypeReference(t.identifier('Error')), t.tsTypeReference(t.identifier(genericTypeName)), t.tsArrayType(t.tsParenthesizedType(t.tsUnionType([t.tsStringKeyword(), t.tsUndefinedKeyword()])))]));
259
+ const genericResponseTypeName = 'TResponse';
260
+ const genericSelectResponseTypeName = 'TData';
261
+ context.addUtil('UseQueryOptions'); // UseQueryOptions<TResponse, Error, TData>,
262
+
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))]));
208
264
  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)];
209
- return t.exportNamedDeclaration(t.tsInterfaceDeclaration(t.identifier(genericQueryInterfaceName), t.tsTypeParameterDeclaration([t.tsTypeParameter(undefined, undefined, genericTypeName)]), [], t.tSInterfaceBody(body)));
265
+ return t.exportNamedDeclaration(t.tsInterfaceDeclaration(t.identifier(genericQueryInterfaceName), t.tsTypeParameterDeclaration([// 1: TResponse
266
+ t.tsTypeParameter(undefined, undefined, genericResponseTypeName), // 2: TData
267
+ t.tsTypeParameter(undefined, t.tSTypeReference(t.identifier(genericResponseTypeName)), genericSelectResponseTypeName)]), [], t.tSInterfaceBody(body)));
210
268
  }
211
269
 
212
270
  export const createReactQueryHookInterface = ({
@@ -227,7 +285,9 @@ export const createReactQueryHookInterface = ({
227
285
  t.tsTypeLiteral(props))));
228
286
  }
229
287
 
230
- return t.exportNamedDeclaration(t.tsInterfaceDeclaration(t.identifier(hookParamsTypeName), null, [t.tSExpressionWithTypeArguments(t.identifier(queryInterfaceName), t.tsTypeParameterInstantiation([t.tsTypeReference(t.identifier(responseType))]))], t.tsInterfaceBody(body)));
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
289
+ t.tsTypeReference(t.identifier(responseType)), // 2: select generic
290
+ t.tSTypeReference(t.identifier('TData'))]))], t.tsInterfaceBody(body)));
231
291
  };
232
292
 
233
293
  const getProps = (context, jsonschema) => {
@@ -242,12 +302,31 @@ const getProps = (context, jsonschema) => {
242
302
  });
243
303
  };
244
304
 
245
- const generateUseQueryQueryKey = (hookKeyName, props, optionalClient) => {
246
- const queryKey = [t.stringLiteral(hookKeyName), t.optionalMemberExpression(t.identifier('client'), t.identifier('contractAddress'), false, optionalClient)];
305
+ const generateUseQueryQueryKey = ({
306
+ hookKeyName,
307
+ queryKeysName,
308
+ methodName,
309
+ props,
310
+ options
311
+ }) => {
312
+ const {
313
+ optionalClient,
314
+ queryKeys
315
+ } = options;
316
+ const hasArgs = props.includes('args');
317
+ const contractAddressExpression = t.optionalMemberExpression(t.identifier('client'), t.identifier('contractAddress'), false, optionalClient);
318
+
319
+ if (queryKeys) {
320
+ const callArgs = [contractAddressExpression];
321
+ if (hasArgs) callArgs.push(t.identifier('args'));
322
+ return t.callExpression(t.memberExpression(t.identifier(queryKeysName), t.identifier(camel(methodName))), callArgs);
323
+ }
324
+
325
+ const queryKey = [t.stringLiteral(hookKeyName), contractAddressExpression];
247
326
 
248
- if (props.includes('args')) {
327
+ if (hasArgs) {
249
328
  queryKey.push(t.callExpression(t.memberExpression(t.identifier('JSON'), t.identifier('stringify')), [t.identifier('args')]));
250
329
  }
251
330
 
252
- return queryKey;
331
+ return t.arrayExpression(queryKey);
253
332
  };
@@ -1,11 +1,10 @@
1
1
  import * as t from '@babel/types';
2
2
  import query_msg from '../../../../__fixtures__/basic/query_msg.json';
3
3
  import execute_msg from '../../../../__fixtures__/basic/execute_msg_for__empty.json';
4
- import { RenderContext } from '../context';
5
4
  import { createReactQueryHooks, createReactQueryMutationHooks } from './react-query';
6
- import { expectCode } from '../../test-utils';
7
- const execCtx = new RenderContext(execute_msg);
8
- const queryCtx = new RenderContext(query_msg);
5
+ import { expectCode, makeContext } from '../../test-utils';
6
+ const execCtx = makeContext(execute_msg);
7
+ const queryCtx = makeContext(query_msg);
9
8
  it('createReactQueryHooks', () => {
10
9
  expectCode(t.program(createReactQueryHooks({
11
10
  context: queryCtx,
@@ -14,7 +13,7 @@ it('createReactQueryHooks', () => {
14
13
  QueryClient: 'Sg721QueryClient'
15
14
  })));
16
15
  expectCode(t.program(createReactQueryHooks({
17
- context: new RenderContext(query_msg, {
16
+ context: makeContext(query_msg, {
18
17
  reactQuery: {
19
18
  optionalClient: true
20
19
  }
@@ -24,7 +23,7 @@ it('createReactQueryHooks', () => {
24
23
  QueryClient: 'Sg721QueryClient'
25
24
  })));
26
25
  expectCode(t.program(createReactQueryHooks({
27
- context: new RenderContext(query_msg, {
26
+ context: makeContext(query_msg, {
28
27
  reactQuery: {
29
28
  version: 'v4'
30
29
  }
@@ -34,7 +33,7 @@ it('createReactQueryHooks', () => {
34
33
  QueryClient: 'Sg721QueryClient'
35
34
  })));
36
35
  expectCode(t.program(createReactQueryHooks({
37
- context: new RenderContext(query_msg, {
36
+ context: makeContext(query_msg, {
38
37
  reactQuery: {
39
38
  optionalClient: true,
40
39
  version: 'v4'
@@ -1,10 +1,9 @@
1
1
  import * as t from '@babel/types';
2
2
  import { camel, pascal } from 'case';
3
- import { callExpression, getMessageProperties } from '../utils';
4
- export const createRecoilSelector = (context, keyPrefix, QueryClient, methodName) => {
3
+ import { callExpression, getMessageProperties, getResponseType } from '../utils';
4
+ export const createRecoilSelector = (context, keyPrefix, QueryClient, methodName, responseType) => {
5
5
  context.addUtil('selectorFamily');
6
6
  const selectorName = camel(`${methodName}Selector`);
7
- const responseType = pascal(`${methodName}Response`);
8
7
  const getterKey = camel(`${keyPrefix}${pascal(methodName)}`);
9
8
  return t.exportNamedDeclaration(t.variableDeclaration('const', [t.variableDeclarator(t.identifier(selectorName), callExpression(t.identifier('selectorFamily'), [t.objectExpression([t.objectProperty(t.identifier('key'), t.stringLiteral(getterKey)), t.objectProperty(t.identifier('get'), t.arrowFunctionExpression([t.objectPattern([t.objectProperty(t.identifier('params'), t.identifier('params'), false, true), t.restElement(t.identifier('queryClientParams'))])], t.arrowFunctionExpression([t.objectPattern([t.objectProperty(t.identifier('get'), t.identifier('get'), false, true)])], t.blockStatement([t.variableDeclaration('const', [t.variableDeclarator(t.identifier('client'), t.callExpression(t.identifier('get'), [t.callExpression(t.identifier('queryClient'), [t.identifier('queryClientParams')])]))]), t.returnStatement(t.awaitExpression(t.callExpression(t.memberExpression(t.identifier('client'), t.identifier(methodName)), [t.spreadElement(t.identifier('params'))])))]), true)))])], t.tsTypeParameterInstantiation([t.tsTypeReference(t.identifier(responseType)), t.tsIntersectionType([t.tsTypeReference(t.identifier('QueryClientParams')), t.tsTypeLiteral([t.tsPropertySignature(t.identifier('params'), t.tsTypeAnnotation(t.tsTypeReference(t.identifier('Parameters'), t.tsTypeParameterInstantiation([t.tsIndexedAccessType(t.tsTypeReference(t.identifier(QueryClient)), t.tsLiteralType(t.stringLiteral(methodName)))]))))])])])))]));
10
9
  };
@@ -12,7 +11,8 @@ export const createRecoilSelectors = (context, keyPrefix, QueryClient, queryMsg)
12
11
  return getMessageProperties(queryMsg).map(schema => {
13
12
  const underscoreName = Object.keys(schema.properties)[0];
14
13
  const methodName = camel(underscoreName);
15
- return createRecoilSelector(context, keyPrefix, QueryClient, methodName);
14
+ const responseType = getResponseType(context, underscoreName);
15
+ return createRecoilSelector(context, keyPrefix, QueryClient, methodName, responseType);
16
16
  });
17
17
  };
18
18
  export const createRecoilQueryClientType = () => ({
@@ -1,11 +1,10 @@
1
1
  import * as t from '@babel/types';
2
2
  import query_msg from '../../../../__fixtures__/basic/query_msg.json';
3
3
  import { createRecoilSelector, createRecoilSelectors, createRecoilQueryClient } from './recoil';
4
- import { RenderContext } from '../context';
5
- import { expectCode } from '../../test-utils';
6
- const ctx = new RenderContext(query_msg);
4
+ import { expectCode, makeContext } from '../../test-utils';
5
+ const ctx = makeContext(query_msg);
7
6
  it('selector', () => {
8
- expectCode(createRecoilSelector(ctx, 'SG721', 'SG721QueryClient', 'governanceModules'));
7
+ expectCode(createRecoilSelector(ctx, 'SG721', 'SG721QueryClient', 'governanceModules', 'GovernanceModulesResponse'));
9
8
  });
10
9
  it('selectors', () => {
11
10
  expectCode(t.program(createRecoilSelectors(ctx, 'SG721', 'SG721QueryClient', query_msg)));
@@ -1,6 +1,12 @@
1
1
  import * as t from '@babel/types';
2
- import { camel } from 'case';
2
+ import { camel, pascal } from 'case';
3
3
  import { propertySignature } from './babel';
4
+ export function getResponseType(context, underscoreName) {
5
+ const methodName = camel(underscoreName);
6
+ return pascal(context.contract?.responses?.[underscoreName]?.title ?? // after v1.1 is adopted, we can deprecate this and require the above response
7
+ `${methodName}Response`);
8
+ }
9
+ ;
4
10
 
5
11
  const getTypeStrFromRef = $ref => {
6
12
  if ($ref?.startsWith('#/definitions/')) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wasm-ast-types",
3
- "version": "0.8.1",
3
+ "version": "0.10.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",
@@ -86,5 +86,5 @@
86
86
  "case": "1.6.3",
87
87
  "deepmerge": "4.2.2"
88
88
  },
89
- "gitHead": "548ad021d449f695ecc4f946a11e2ebe8af53780"
89
+ "gitHead": "94c5785d947e329df1cc9108cc33bb7a0c21af60"
90
90
  }
@@ -5,6 +5,7 @@ export interface ReactQueryOptions {
5
5
  version?: 'v3' | 'v4';
6
6
  mutations?: boolean;
7
7
  camelize?: boolean;
8
+ queryKeys?: boolean;
8
9
  }
9
10
  export interface TSClientOptions {
10
11
  enabled?: boolean;
@@ -19,6 +20,25 @@ export interface TSTypesOptions {
19
20
  enabled?: boolean;
20
21
  aliasExecuteMsg?: boolean;
21
22
  }
23
+ interface KeyedSchema {
24
+ [key: string]: JSONSchema;
25
+ }
26
+ export interface IDLObject {
27
+ contract_name: string;
28
+ contract_version: string;
29
+ idl_version: string;
30
+ instantiate: JSONSchema;
31
+ execute: JSONSchema;
32
+ query: JSONSchema;
33
+ migrate: JSONSchema;
34
+ sudo: JSONSchema;
35
+ responses: KeyedSchema;
36
+ }
37
+ export interface ContractInfo {
38
+ schemas: JSONSchema[];
39
+ responses?: Record<string, JSONSchema>;
40
+ idlObject?: IDLObject;
41
+ }
22
42
  export interface RenderOptions {
23
43
  types?: TSTypesOptions;
24
44
  recoil?: RecoilOptions;
@@ -27,15 +47,18 @@ export interface RenderOptions {
27
47
  reactQuery?: ReactQueryOptions;
28
48
  }
29
49
  export interface RenderContext {
30
- schema: JSONSchema;
50
+ contract: ContractInfo;
31
51
  options: RenderOptions;
32
52
  }
33
53
  export declare const defaultOptions: RenderOptions;
54
+ export declare const getDefinitionSchema: (schemas: JSONSchema[]) => JSONSchema;
34
55
  export declare class RenderContext implements RenderContext {
35
- schema: JSONSchema;
56
+ contract: ContractInfo;
36
57
  utils: string[];
37
- constructor(schema: JSONSchema, options?: RenderOptions);
58
+ schema: JSONSchema;
59
+ constructor(contract: ContractInfo, options?: RenderOptions);
38
60
  refLookup($ref: string): JSONSchema;
39
61
  addUtil(util: string): void;
40
62
  getImports(): any[];
41
63
  }
64
+ export {};
@@ -7,6 +7,7 @@ interface ReactQueryHookQuery {
7
7
  hookName: string;
8
8
  hookParamsTypeName: string;
9
9
  hookKeyName: string;
10
+ queryKeysName: string;
10
11
  responseType: string;
11
12
  methodName: string;
12
13
  jsonschema: any;
@@ -17,8 +18,8 @@ interface ReactQueryHooks {
17
18
  contractName: string;
18
19
  QueryClient: string;
19
20
  }
20
- export declare const createReactQueryHooks: ({ context, queryMsg, contractName, QueryClient }: ReactQueryHooks) => t.ExportNamedDeclaration[];
21
- export declare const createReactQueryHook: ({ context, hookName, hookParamsTypeName, responseType, hookKeyName, methodName, jsonschema }: ReactQueryHookQuery) => t.ExportNamedDeclaration;
21
+ export declare const createReactQueryHooks: ({ context, queryMsg, contractName, QueryClient }: ReactQueryHooks) => any[];
22
+ export declare const createReactQueryHook: ({ context, hookName, hookParamsTypeName, responseType, hookKeyName, queryKeysName, methodName, jsonschema }: ReactQueryHookQuery) => t.ExportNamedDeclaration;
22
23
  interface ReactQueryMutationHookInterface {
23
24
  context: RenderContext;
24
25
  ExecuteClient: string;
@@ -1,7 +1,7 @@
1
1
  import * as t from '@babel/types';
2
2
  import { QueryMsg } from '../types';
3
3
  import { RenderContext } from '../context';
4
- export declare const createRecoilSelector: (context: RenderContext, keyPrefix: string, QueryClient: string, methodName: string) => t.ExportNamedDeclaration;
4
+ export declare const createRecoilSelector: (context: RenderContext, keyPrefix: string, QueryClient: string, methodName: string, responseType: string) => t.ExportNamedDeclaration;
5
5
  export declare const createRecoilSelectors: (context: RenderContext, keyPrefix: string, QueryClient: string, queryMsg: QueryMsg) => any;
6
6
  export declare const createRecoilQueryClientType: () => {
7
7
  type: string;
@@ -2,6 +2,7 @@ import * as t from '@babel/types';
2
2
  import { TSTypeAnnotation } from '@babel/types';
3
3
  import { RenderContext } from '../context';
4
4
  import { JSONSchema } from '../types';
5
+ export declare function getResponseType(context: RenderContext, underscoreName: string): string;
5
6
  export declare const getTypeFromRef: ($ref: any) => t.TSTypeReference;
6
7
  export declare const getType: (type: any) => t.TSBooleanKeyword | t.TSNumberKeyword | t.TSStringKeyword;
7
8
  export declare const getPropertyType: (context: RenderContext, schema: JSONSchema, prop: string) => {