wasm-ast-types 0.5.1 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (93) hide show
  1. package/main/{wasm.js → client/client.js} +54 -40
  2. package/main/client/index.js +18 -0
  3. package/main/client/test/ts-client.arrays.spec.js +25 -0
  4. package/main/client/test/ts-client.cw-named-groups.test.js +25 -0
  5. package/main/client/test/ts-client.cw-proposal-single.test.js +30 -0
  6. package/main/client/test/ts-client.empty-enums.spec.js +19 -0
  7. package/main/client/test/ts-client.sg721.spec.js +25 -0
  8. package/main/client/test/ts-client.spec.js +106 -0
  9. package/main/client/test/ts-client.vectis.spec.js +56 -0
  10. package/main/context/context.js +48 -0
  11. package/main/context/index.js +18 -0
  12. package/main/{messages.js → from-partial/from-partial.js} +17 -60
  13. package/main/from-partial/from-partial.spec.js +20 -0
  14. package/main/from-partial/index.js +18 -0
  15. package/main/index.js +34 -8
  16. package/main/react-query/index.js +18 -0
  17. package/main/{react-query.js → react-query/react-query.js} +39 -64
  18. package/main/{react-query.spec.js → react-query/react-query.spec.js} +33 -43
  19. package/main/recoil/index.js +18 -0
  20. package/main/{recoil.js → recoil/recoil.js} +7 -26
  21. package/main/{recoil.spec.js → recoil/recoil.spec.js} +9 -14
  22. package/main/utils/babel.spec.js +7 -15
  23. package/main/utils/index.js +13 -0
  24. package/main/utils/types.js +34 -32
  25. package/module/{wasm.js → client/client.js} +43 -33
  26. package/module/client/index.js +1 -0
  27. package/module/client/test/ts-client.arrays.spec.js +17 -0
  28. package/module/client/test/ts-client.cw-named-groups.test.js +17 -0
  29. package/module/client/test/ts-client.cw-proposal-single.test.js +21 -0
  30. package/module/client/test/ts-client.empty-enums.spec.js +11 -0
  31. package/module/client/test/ts-client.sg721.spec.js +17 -0
  32. package/module/client/test/ts-client.spec.js +85 -0
  33. package/module/client/test/ts-client.vectis.spec.js +44 -0
  34. package/module/context/context.js +25 -0
  35. package/module/context/index.js +1 -0
  36. package/module/{messages.js → from-partial/from-partial.js} +16 -57
  37. package/module/from-partial/from-partial.spec.js +12 -0
  38. package/module/from-partial/index.js +1 -0
  39. package/module/index.js +5 -3
  40. package/module/react-query/index.js +1 -0
  41. package/module/{react-query.js → react-query/react-query.js} +30 -49
  42. package/module/react-query/react-query.spec.js +53 -0
  43. package/module/recoil/index.js +1 -0
  44. package/module/{recoil.js → recoil/recoil.js} +6 -20
  45. package/module/recoil/recoil.spec.js +15 -0
  46. package/module/utils/babel.spec.js +1 -10
  47. package/module/utils/index.js +2 -1
  48. package/module/utils/types.js +32 -30
  49. package/package.json +4 -3
  50. package/types/src/client/client.d.ts +17 -0
  51. package/types/src/client/index.d.ts +1 -0
  52. package/types/src/client/test/ts-client.cw-named-groups.test.d.ts +1 -0
  53. package/types/src/client/test/ts-client.cw-proposal-single.test.d.ts +1 -0
  54. package/types/src/context/context.d.ts +20 -0
  55. package/types/src/context/index.d.ts +1 -0
  56. package/types/src/from-partial/from-partial.d.ts +4 -0
  57. package/types/src/from-partial/index.d.ts +1 -0
  58. package/types/src/index.d.ts +7 -0
  59. package/types/src/react-query/index.d.ts +1 -0
  60. package/types/src/react-query/react-query.d.ts +81 -0
  61. package/types/src/recoil/index.d.ts +1 -0
  62. package/types/{recoil.d.ts → src/recoil/recoil.d.ts} +5 -5
  63. package/types/{types.d.ts → src/types.d.ts} +17 -0
  64. package/types/{utils.d.ts → src/utils/babel.d.ts} +21 -1
  65. package/types/src/utils/index.d.ts +2 -0
  66. package/types/src/utils/types.d.ts +18 -0
  67. package/types/test-utils/index.d.ts +2 -0
  68. package/main/messages.spec.js +0 -24
  69. package/main/wasm.arrays.spec.js +0 -30
  70. package/main/wasm.cw-named-groups.test.js +0 -30
  71. package/main/wasm.cw-proposal-single.test.js +0 -32
  72. package/main/wasm.empty-enums.spec.js +0 -24
  73. package/main/wasm.sg721.spec.js +0 -30
  74. package/main/wasm.spec.js +0 -95
  75. package/main/wasm.vectis.spec.js +0 -53
  76. package/module/messages.spec.js +0 -18
  77. package/module/react-query.spec.js +0 -65
  78. package/module/recoil.spec.js +0 -22
  79. package/module/wasm.arrays.spec.js +0 -24
  80. package/module/wasm.cw-named-groups.test.js +0 -24
  81. package/module/wasm.cw-proposal-single.test.js +0 -25
  82. package/module/wasm.empty-enums.spec.js +0 -18
  83. package/module/wasm.sg721.spec.js +0 -24
  84. package/module/wasm.spec.js +0 -76
  85. package/module/wasm.vectis.spec.js +0 -43
  86. package/types/amino/amino-interfaces.d.ts +0 -6
  87. package/types/amino/from-amino.d.ts +0 -13
  88. package/types/amino/index.d.ts +0 -3
  89. package/types/amino/to-amino.d.ts +0 -11
  90. package/types/index.d.ts +0 -5
  91. package/types/messages.d.ts +0 -4
  92. package/types/react-query.d.ts +0 -70
  93. package/types/wasm.d.ts +0 -24
@@ -0,0 +1,85 @@
1
+ import execute_msg_named_groups from '../../../../../__fixtures__/daodao/cw-named-groups/execute_msg.json';
2
+ import query_msg from '../../../../../__fixtures__/basic/query_msg.json';
3
+ import execute_msg from '../../../../../__fixtures__/basic/execute_msg_for__empty.json';
4
+ import approval_response from '../../../../../__fixtures__/basic/approval_response.json';
5
+ import all_nft_info_response from '../../../../../__fixtures__/basic/all_nft_info_response.json';
6
+ import approvals_response from '../../../../../__fixtures__/basic/approvals_response.json';
7
+ import collection_info_response from '../../../../../__fixtures__/basic/collection_info_response.json';
8
+ import contract_info_response from '../../../../../__fixtures__/basic/contract_info_response.json';
9
+ import instantiate_msg from '../../../../../__fixtures__/basic/instantiate_msg.json';
10
+ import nft_info_response from '../../../../../__fixtures__/basic/nft_info_response.json';
11
+ import num_tokens_response from '../../../../../__fixtures__/basic/num_tokens_response.json';
12
+ import operators_response from '../../../../../__fixtures__/basic/operators_response.json';
13
+ import owner_of_response from '../../../../../__fixtures__/basic/owner_of_response.json';
14
+ import tokens_response from '../../../../../__fixtures__/basic/tokens_response.json';
15
+ import { createQueryClass, createQueryInterface, createExecuteClass, createExecuteInterface, createTypeInterface } from '../client';
16
+ import { RenderContext } from '../../context';
17
+ import { expectCode } from '../../../test-utils';
18
+ it('approval_response', () => {
19
+ const ctx = new RenderContext(approval_response);
20
+ expectCode(createTypeInterface(ctx, approval_response));
21
+ });
22
+ it('all_nft_info_response', () => {
23
+ const ctx = new RenderContext(all_nft_info_response);
24
+ expectCode(createTypeInterface(ctx, all_nft_info_response));
25
+ });
26
+ it('approvals_response', () => {
27
+ const ctx = new RenderContext(approvals_response);
28
+ expectCode(createTypeInterface(ctx, approvals_response));
29
+ });
30
+ it('collection_info_response', () => {
31
+ const ctx = new RenderContext(collection_info_response);
32
+ expectCode(createTypeInterface(ctx, collection_info_response));
33
+ });
34
+ it('contract_info_response', () => {
35
+ const ctx = new RenderContext(contract_info_response);
36
+ expectCode(createTypeInterface(ctx, contract_info_response));
37
+ });
38
+ it('instantiate_msg', () => {
39
+ const ctx = new RenderContext(instantiate_msg);
40
+ expectCode(createTypeInterface(ctx, instantiate_msg));
41
+ });
42
+ it('nft_info_response', () => {
43
+ const ctx = new RenderContext(nft_info_response);
44
+ expectCode(createTypeInterface(ctx, nft_info_response));
45
+ });
46
+ it('num_tokens_response', () => {
47
+ const ctx = new RenderContext(num_tokens_response);
48
+ expectCode(createTypeInterface(ctx, num_tokens_response));
49
+ });
50
+ it('operators_response', () => {
51
+ const ctx = new RenderContext(operators_response);
52
+ expectCode(createTypeInterface(ctx, operators_response));
53
+ });
54
+ it('owner_of_response', () => {
55
+ const ctx = new RenderContext(owner_of_response);
56
+ expectCode(createTypeInterface(ctx, owner_of_response));
57
+ });
58
+ it('tokens_response', () => {
59
+ const ctx = new RenderContext(tokens_response);
60
+ expectCode(createTypeInterface(ctx, tokens_response));
61
+ });
62
+ it('query classes', () => {
63
+ const ctx = new RenderContext(query_msg);
64
+ expectCode(createQueryClass(ctx, 'SG721QueryClient', 'SG721ReadOnlyInstance', query_msg));
65
+ });
66
+ it('execute classes', () => {
67
+ const ctx = new RenderContext(execute_msg);
68
+ expectCode(createExecuteClass(ctx, 'SG721Client', 'SG721Instance', 'SG721QueryClient', execute_msg));
69
+ });
70
+ it('execute classes no extends', () => {
71
+ const ctx = new RenderContext(execute_msg);
72
+ expectCode(createExecuteClass(ctx, 'SG721Client', 'SG721Instance', null, execute_msg));
73
+ });
74
+ it('execute classes array types', () => {
75
+ const ctx = new RenderContext(execute_msg_named_groups);
76
+ expectCode(createExecuteClass(ctx, 'SG721Client', 'SG721Instance', null, execute_msg_named_groups));
77
+ });
78
+ it('execute interfaces no extends', () => {
79
+ const ctx = new RenderContext(execute_msg);
80
+ expectCode(createExecuteInterface(ctx, 'SG721Instance', null, execute_msg));
81
+ });
82
+ it('query interfaces', () => {
83
+ const ctx = new RenderContext(query_msg);
84
+ expectCode(createQueryInterface(ctx, 'SG721ReadOnlyInstance', query_msg));
85
+ });
@@ -0,0 +1,44 @@
1
+ import cosmos_msg_for__empty from '../../../../../__fixtures__/vectis/govec/cosmos_msg_for__empty.json';
2
+ import execute_msg_for__empty from '../../../../../__fixtures__/vectis/govec/execute_msg_for__empty.json';
3
+ import can_execute_relay_response from '../../../../../__fixtures__/vectis/govec/can_execute_relay_response.json';
4
+ import info_response from '../../../../../__fixtures__/vectis/govec/info_response.json';
5
+ import relay_transaction from '../../../../../__fixtures__/vectis/govec/relay_transaction.json';
6
+ import { createQueryClass, createExecuteClass, createExecuteInterface, createTypeInterface } from '../client';
7
+ import { RenderContext } from '../../context';
8
+ import { expectCode } from '../../../test-utils';
9
+ it('cosmos_msg_for__empty', () => {
10
+ const ctx = new RenderContext(cosmos_msg_for__empty);
11
+ expectCode(createTypeInterface(ctx, cosmos_msg_for__empty));
12
+ });
13
+ it('execute_msg_for__empty', () => {
14
+ const ctx = new RenderContext(execute_msg_for__empty);
15
+ expectCode(createTypeInterface(ctx, execute_msg_for__empty));
16
+ });
17
+ it('can_execute_relay_response', () => {
18
+ const ctx = new RenderContext(can_execute_relay_response);
19
+ expectCode(createTypeInterface(ctx, can_execute_relay_response));
20
+ });
21
+ it('info_response', () => {
22
+ const ctx = new RenderContext(info_response);
23
+ expectCode(createTypeInterface(ctx, info_response));
24
+ });
25
+ it('relay_transaction', () => {
26
+ const ctx = new RenderContext(relay_transaction);
27
+ expectCode(createTypeInterface(ctx, relay_transaction));
28
+ });
29
+ it('query classes', () => {
30
+ const ctx = new RenderContext(cosmos_msg_for__empty);
31
+ expectCode(createQueryClass(ctx, 'SG721QueryClient', 'SG721ReadOnlyInstance', cosmos_msg_for__empty));
32
+ });
33
+ it('query classes', () => {
34
+ const ctx = new RenderContext(execute_msg_for__empty);
35
+ expectCode(createQueryClass(ctx, 'SG721QueryClient', 'SG721ReadOnlyInstance', execute_msg_for__empty));
36
+ });
37
+ it('execute classes array types', () => {
38
+ const ctx = new RenderContext(execute_msg_for__empty);
39
+ expectCode(createExecuteClass(ctx, 'SG721Client', 'SG721Instance', null, execute_msg_for__empty));
40
+ });
41
+ it('execute interfaces no extends', () => {
42
+ const ctx = new RenderContext(execute_msg_for__empty);
43
+ expectCode(createExecuteInterface(ctx, 'SG721Instance', null, execute_msg_for__empty));
44
+ });
@@ -0,0 +1,25 @@
1
+ import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
2
+ import deepmerge from "deepmerge"; /// Plugin Types
3
+
4
+ export const defaultOptions = {
5
+ reactQuery: {
6
+ optionalClient: false,
7
+ v4: false,
8
+ mutations: false,
9
+ camelize: true
10
+ }
11
+ };
12
+ export class RenderContext {
13
+ constructor(schema, options) {
14
+ _defineProperty(this, "schema", void 0);
15
+
16
+ this.schema = schema;
17
+ this.options = deepmerge(defaultOptions, options ?? {});
18
+ }
19
+
20
+ refLookup($ref) {
21
+ const refName = $ref.replace('#/definitions/', '');
22
+ return this.schema.definitions?.[refName];
23
+ }
24
+
25
+ }
@@ -0,0 +1 @@
1
+ export * from './context';
@@ -1,51 +1,26 @@
1
1
  import * as t from '@babel/types';
2
2
  import { camel } from 'case';
3
- import { bindMethod, typedIdentifier, classDeclaration, classProperty, arrowFunctionExpression, getMessageProperties } from './utils';
4
- import { createTypedObjectParams } from './utils/types';
5
- import { identifier, tsTypeOperator } from './utils/babel';
3
+ import { bindMethod, typedIdentifier, classDeclaration, classProperty, arrowFunctionExpression, getMessageProperties } from '../utils';
4
+ import { createTypedObjectParams } from '../utils/types';
5
+ import { identifier } from '../utils/babel';
6
+ import { getWasmMethodArgs } from '../client/client';
6
7
 
7
- const createWasmExecMethodPartial = jsonschema => {
8
+ const createWasmExecMethodPartial = (context, jsonschema) => {
8
9
  const underscoreName = Object.keys(jsonschema.properties)[0];
9
10
  const methodName = camel(underscoreName);
10
- const properties = jsonschema.properties[underscoreName].properties ?? {};
11
- const obj = createTypedObjectParams(jsonschema.properties[underscoreName]);
12
- const args = Object.keys(properties).map(prop => {
13
- return t.objectProperty(t.identifier(prop), t.identifier(camel(prop)), false, prop === camel(prop));
14
- });
15
- const constantParams = [// t.assignmentPattern(
16
- // identifier(
17
- // 'fee',
18
- // t.tsTypeAnnotation(
19
- // t.tsUnionType(
20
- // [
21
- // t.tSNumberKeyword(),
22
- // t.tsTypeReference(
23
- // t.identifier('StdFee')
24
- // ),
25
- // t.tsLiteralType(
26
- // t.stringLiteral('auto')
27
- // )
28
- // ]
29
- // )
30
- // ),
31
- // false
32
- // ),
33
- // t.stringLiteral('auto')
34
- // ),
35
- // identifier('memo', t.tsTypeAnnotation(
36
- // t.tsStringKeyword()
37
- // ), true),
38
- identifier('funds', t.tsTypeAnnotation(tsTypeOperator(t.tsArrayType(t.tsTypeReference(t.identifier('Coin'))), 'readonly')), true)];
11
+ const obj = createTypedObjectParams(context, jsonschema.properties[underscoreName]);
12
+ const args = getWasmMethodArgs(context, jsonschema.properties[underscoreName]);
13
+ const constantParams = [identifier('funds', t.tsTypeAnnotation(t.tsArrayType(t.tsTypeReference(t.identifier('Coin')))), true)];
39
14
  return t.classProperty(t.identifier(methodName), arrowFunctionExpression(obj ? [// props
40
15
  obj, ...constantParams] : constantParams, t.blockStatement([t.returnStatement(t.objectExpression([t.objectProperty(t.identifier('typeUrl'), t.stringLiteral('/cosmwasm.wasm.v1.MsgExecuteContract')), t.objectProperty(t.identifier('value'), t.callExpression(t.memberExpression(t.identifier('MsgExecuteContract'), t.identifier('fromPartial')), [t.objectExpression([t.objectProperty(t.identifier('sender'), t.memberExpression(t.thisExpression(), t.identifier('sender'))), t.objectProperty(t.identifier('contract'), t.memberExpression(t.thisExpression(), t.identifier('contractAddress'))), t.objectProperty(t.identifier('msg'), t.callExpression(t.identifier('toUtf8'), [t.callExpression(t.memberExpression(t.identifier('JSON'), t.identifier('stringify')), [t.objectExpression([t.objectProperty(t.identifier(underscoreName), t.objectExpression(args))])])])), t.objectProperty(t.identifier('funds'), t.identifier('funds'), false, true)])]))]))]), // return type
41
16
  t.tsTypeAnnotation(t.tsTypeReference(t.identifier('MsgExecuteContractEncodeObject'))), false));
42
17
  };
43
18
 
44
- export const createFromPartialClass = (className, implementsClassName, execMsg) => {
19
+ export const createFromPartialClass = (context, className, implementsClassName, execMsg) => {
45
20
  const propertyNames = getMessageProperties(execMsg).map(method => Object.keys(method.properties)?.[0]).filter(Boolean);
46
21
  const bindings = propertyNames.map(camel).map(bindMethod);
47
22
  const methods = getMessageProperties(execMsg).map(schema => {
48
- return createWasmExecMethodPartial(schema);
23
+ return createWasmExecMethodPartial(context, schema);
49
24
  });
50
25
  const blockStmt = [];
51
26
  [].push.apply(blockStmt, [t.expressionStatement(t.assignmentExpression('=', t.memberExpression(t.thisExpression(), t.identifier('sender')), t.identifier('sender'))), t.expressionStatement(t.assignmentExpression('=', t.memberExpression(t.thisExpression(), t.identifier('contractAddress')), t.identifier('contractAddress'))), ...bindings]);
@@ -54,37 +29,21 @@ export const createFromPartialClass = (className, implementsClassName, execMsg)
54
29
  classProperty('contractAddress', t.tsTypeAnnotation(t.tsStringKeyword())), // constructor
55
30
  t.classMethod('constructor', t.identifier('constructor'), [typedIdentifier('sender', t.tsTypeAnnotation(t.tsStringKeyword())), typedIdentifier('contractAddress', t.tsTypeAnnotation(t.tsStringKeyword()))], t.blockStatement(blockStmt)), ...methods], [t.tSExpressionWithTypeArguments(t.identifier(implementsClassName))], null));
56
31
  };
57
- export const createFromPartialInterface = (className, execMsg) => {
32
+ export const createFromPartialInterface = (context, className, execMsg) => {
58
33
  const methods = getMessageProperties(execMsg).map(jsonschema => {
59
34
  const underscoreName = Object.keys(jsonschema.properties)[0];
60
35
  const methodName = camel(underscoreName);
61
- return createPropertyFunctionWithObjectParamsForPartial(methodName, 'MsgExecuteContractEncodeObject', jsonschema.properties[underscoreName]);
36
+ return createPropertyFunctionWithObjectParamsForPartial(context, methodName, 'MsgExecuteContractEncodeObject', jsonschema.properties[underscoreName]);
62
37
  });
63
38
  const extendsAst = [];
64
39
  return t.exportNamedDeclaration(t.tsInterfaceDeclaration(t.identifier(className), null, extendsAst, t.tSInterfaceBody([// contract address
65
40
  t.tSPropertySignature(t.identifier('contractAddress'), t.tsTypeAnnotation(t.tsStringKeyword())), // contract address
66
41
  t.tSPropertySignature(t.identifier('sender'), t.tsTypeAnnotation(t.tsStringKeyword())), ...methods])));
67
- }; // MARKED AS NOT DRY
42
+ };
68
43
 
69
- const createPropertyFunctionWithObjectParamsForPartial = (methodName, responseType, jsonschema) => {
70
- const obj = createTypedObjectParams(jsonschema);
71
- const fixedParams = [// identifier('fee', t.tsTypeAnnotation(
72
- // t.tsUnionType(
73
- // [
74
- // t.tsNumberKeyword(),
75
- // t.tsTypeReference(
76
- // t.identifier('StdFee')
77
- // ),
78
- // t.tsLiteralType(
79
- // t.stringLiteral('auto')
80
- // )
81
- // ]
82
- // )
83
- // ), true),
84
- // identifier('memo', t.tsTypeAnnotation(
85
- // t.tsStringKeyword()
86
- // ), true),
87
- identifier('funds', t.tsTypeAnnotation(tsTypeOperator(t.tsArrayType(t.tsTypeReference(t.identifier('Coin'))), 'readonly')), true)];
44
+ const createPropertyFunctionWithObjectParamsForPartial = (context, methodName, responseType, jsonschema) => {
45
+ const obj = createTypedObjectParams(context, jsonschema);
46
+ const fixedParams = [identifier('funds', t.tsTypeAnnotation(t.tsArrayType(t.tsTypeReference(t.identifier('Coin')))), true)];
88
47
  const func = {
89
48
  type: 'TSFunctionType',
90
49
  typeAnnotation: t.tsTypeAnnotation(t.tsTypeReference(t.identifier(responseType))),
@@ -0,0 +1,12 @@
1
+ import execute_msg from '../../../../__fixtures__/basic/execute_msg_for__empty.json';
2
+ import { createFromPartialClass, createFromPartialInterface } from './from-partial';
3
+ import { RenderContext } from '../context';
4
+ import { expectCode } from '../../test-utils';
5
+ it('execute classes', () => {
6
+ const ctx = new RenderContext(execute_msg);
7
+ expectCode(createFromPartialClass(ctx, 'SG721MessageComposer', 'SG721Message', execute_msg));
8
+ });
9
+ it('createFromPartialInterface', () => {
10
+ const ctx = new RenderContext(execute_msg);
11
+ expectCode(createFromPartialInterface(ctx, 'SG721Message', execute_msg));
12
+ });
@@ -0,0 +1 @@
1
+ export * from './from-partial';
package/module/index.js CHANGED
@@ -1,5 +1,7 @@
1
1
  export * from './utils';
2
- export * from './wasm';
2
+ export * from './client';
3
+ export * from './context';
3
4
  export * from './recoil';
4
- export * from './messages';
5
- export * from './react-query';
5
+ export * from './from-partial';
6
+ export * from './react-query';
7
+ export * from './types';
@@ -0,0 +1 @@
1
+ export * from './react-query';
@@ -1,29 +1,16 @@
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
-
7
1
  import * as t from '@babel/types';
8
2
  import { camel, pascal } from 'case';
9
- import { callExpression, getMessageProperties, identifier, tsObjectPattern, tsPropertySignature } from './utils';
10
- import { omitTypeReference, optionalConditionalExpression, propertySignature, shorthandProperty, typeRefOrOptionalUnion } from './utils/babel';
11
- import { getParamsTypeAnnotation, getPropertyType, getTypeFromRef } from './utils/types';
12
- import { FIXED_EXECUTE_PARAMS } from './wasm'; // TODO: this mutations boolean is not actually used here and only at a higher level
13
-
14
- const DEFAULT_OPTIONS = {
15
- optionalClient: false,
16
- v4: false,
17
- mutations: false
18
- };
3
+ import { callExpression, getMessageProperties, identifier, tsObjectPattern, tsPropertySignature } from '../utils';
4
+ import { omitTypeReference, optionalConditionalExpression, propertySignature, shorthandProperty, typeRefOrOptionalUnion } from '../utils/babel';
5
+ import { getParamsTypeAnnotation, getPropertyType } from '../utils/types';
6
+ import { FIXED_EXECUTE_PARAMS } from '../client';
19
7
  export const createReactQueryHooks = ({
8
+ context,
20
9
  queryMsg,
21
10
  contractName,
22
- QueryClient,
23
- options = {}
11
+ QueryClient
24
12
  }) => {
25
- // merge the user options with the defaults
26
- options = _objectSpread(_objectSpread({}, DEFAULT_OPTIONS), options);
13
+ const options = context.options.reactQuery;
27
14
  return getMessageProperties(queryMsg).reduce((m, schema) => {
28
15
  const underscoreName = Object.keys(schema.properties)[0];
29
16
  const methodName = camel(underscoreName);
@@ -33,33 +20,33 @@ export const createReactQueryHooks = ({
33
20
  const getterKey = camel(`${contractName}${pascal(methodName)}`);
34
21
  const jsonschema = schema.properties[underscoreName];
35
22
  return [createReactQueryHookInterface({
23
+ context,
36
24
  hookParamsTypeName,
37
25
  responseType,
38
26
  QueryClient,
39
27
  jsonschema,
40
28
  options
41
29
  }), createReactQueryHook({
30
+ context,
42
31
  methodName,
43
32
  hookName,
44
33
  hookParamsTypeName,
45
34
  responseType,
46
35
  hookKeyName: getterKey,
47
- jsonschema,
48
- options
36
+ jsonschema
49
37
  }), ...m];
50
38
  }, []);
51
39
  };
52
40
  export const createReactQueryHook = ({
41
+ context,
53
42
  hookName,
54
43
  hookParamsTypeName,
55
44
  responseType,
56
45
  hookKeyName,
57
46
  methodName,
58
- jsonschema,
59
- options = {}
47
+ jsonschema
60
48
  }) => {
61
- // merge the user options with the defaults
62
- options = _objectSpread(_objectSpread({}, DEFAULT_OPTIONS), options);
49
+ const options = context.options.reactQuery;
63
50
  const keys = Object.keys(jsonschema.properties ?? {});
64
51
  let args = [];
65
52
 
@@ -98,6 +85,7 @@ export interface Cw4UpdateMembersMutation {
98
85
  ```
99
86
  */
100
87
  export const createReactQueryMutationArgsInterface = ({
88
+ context,
101
89
  ExecuteClient,
102
90
  mutationHookParamsTypeName,
103
91
  useMutationTypeParameter,
@@ -105,21 +93,11 @@ export const createReactQueryMutationArgsInterface = ({
105
93
  }) => {
106
94
  const typedUseMutationOptions = t.tsTypeReference(t.identifier('UseMutationOptions'), useMutationTypeParameter);
107
95
  const body = [tsPropertySignature(t.identifier('client'), t.tsTypeAnnotation(t.tsTypeReference(t.identifier(ExecuteClient))), false)];
108
- let msgType = getParamsTypeAnnotation(jsonschema); // TODO: this should not have to be done manually.
109
-
110
- if (!msgType && jsonschema?.$ref?.startsWith('#/definitions/')) {
111
- let refName = jsonschema?.$ref;
112
-
113
- if (/_for_[A-Z]/.test(refName)) {
114
- refName = refName.replace(/_for_/, 'For');
115
- }
116
-
117
- msgType = t.tsTypeAnnotation(getTypeFromRef(refName));
118
- }
96
+ const msgType = getParamsTypeAnnotation(context, jsonschema);
119
97
 
120
98
  if (msgType) {
121
99
  body.push(t.tsPropertySignature(t.identifier('msg'), msgType));
122
- } // fee: number | StdFee | "auto" = "auto", memo?: string, funds?: readonly Coin[]
100
+ } // fee: number | StdFee | "auto" = "auto", memo?: string, funds?: Coin[]
123
101
 
124
102
 
125
103
  const optionalArgs = t.tsPropertySignature(t.identifier('args'), t.tsTypeAnnotation(t.tsTypeLiteral(FIXED_EXECUTE_PARAMS.map(param => propertySignature(param.name, param.typeAnnotation, param.optional)))));
@@ -128,10 +106,10 @@ export const createReactQueryMutationArgsInterface = ({
128
106
  return t.exportNamedDeclaration(t.tsInterfaceDeclaration(t.identifier(mutationHookParamsTypeName), null, [], t.tsInterfaceBody(body)));
129
107
  };
130
108
  export const createReactQueryMutationHooks = ({
109
+ context,
131
110
  execMsg,
132
111
  contractName,
133
- ExecuteClient,
134
- options = {}
112
+ ExecuteClient
135
113
  }) => {
136
114
  // merge the user options with the defaults
137
115
  return getMessageProperties(execMsg).reduce((m, schema) => {
@@ -150,11 +128,13 @@ export const createReactQueryMutationHooks = ({
150
128
 
151
129
  const useMutationTypeParameter = generateMutationTypeParameter(mutationHookParamsTypeName, hasMsg);
152
130
  return [createReactQueryMutationArgsInterface({
131
+ context,
153
132
  mutationHookParamsTypeName,
154
133
  ExecuteClient,
155
134
  jsonschema,
156
135
  useMutationTypeParameter
157
136
  }), createReactQueryMutationHook({
137
+ context,
158
138
  execMethodName,
159
139
  mutationHookName,
160
140
  mutationHookParamsTypeName,
@@ -186,6 +166,7 @@ export const useCw4UpdateMembersMutation = ({ client, options }: Omit<Cw4UpdateM
186
166
  ```
187
167
  */
188
168
  export const createReactQueryMutationHook = ({
169
+ context,
189
170
  mutationHookName,
190
171
  mutationHookParamsTypeName,
191
172
  execMethodName,
@@ -199,17 +180,17 @@ export const createReactQueryMutationHook = ({
199
180
  ), t.identifier('options')], useMutationTypeParameter))])));
200
181
  };
201
182
  export const createReactQueryHookInterface = ({
183
+ context,
202
184
  QueryClient,
203
185
  hookParamsTypeName,
204
186
  responseType,
205
- jsonschema,
206
- options = {}
187
+ jsonschema
207
188
  }) => {
208
189
  // merge the user options with the defaults
209
- options = _objectSpread(_objectSpread({}, DEFAULT_OPTIONS), options);
190
+ const options = context.options.reactQuery;
210
191
  const typedUseQueryOptions = t.tsTypeReference(t.identifier('UseQueryOptions'), t.tsTypeParameterInstantiation([typeRefOrOptionalUnion(t.identifier(responseType), options.optionalClient), t.tsTypeReference(t.identifier('Error')), t.tsTypeReference(t.identifier(responseType)), t.tsArrayType(t.tsParenthesizedType(t.tsUnionType([t.tsStringKeyword(), t.tsUndefinedKeyword()])))]));
211
192
  const body = [tsPropertySignature(t.identifier('client'), t.tsTypeAnnotation(t.tsTypeReference(t.identifier(QueryClient))), options.optionalClient), tsPropertySignature(t.identifier('options'), t.tsTypeAnnotation(options.v4 ? t.tSIntersectionType([omitTypeReference(typedUseQueryOptions, "'queryKey' | 'queryFn' | 'initialData'"), t.tSTypeLiteral([t.tsPropertySignature(t.identifier('initialData?'), t.tsTypeAnnotation(t.tsUndefinedKeyword()))])]) : typedUseQueryOptions), true)];
212
- const props = getProps(jsonschema, true);
193
+ const props = getProps(context, jsonschema);
213
194
 
214
195
  if (props.length) {
215
196
  body.push(t.tsPropertySignature(t.identifier('args'), t.tsTypeAnnotation(t.tsTypeLiteral(props))));
@@ -218,19 +199,19 @@ export const createReactQueryHookInterface = ({
218
199
  return t.exportNamedDeclaration(t.tsInterfaceDeclaration(t.identifier(hookParamsTypeName), null, [], t.tsInterfaceBody(body)));
219
200
  };
220
201
 
221
- const getProps = (jsonschema, camelize) => {
202
+ const getProps = (context, jsonschema) => {
222
203
  const keys = Object.keys(jsonschema.properties ?? {});
223
204
  if (!keys.length) return [];
224
205
  return keys.map(prop => {
225
206
  const {
226
207
  type,
227
208
  optional
228
- } = getPropertyType(jsonschema, prop);
229
- return propertySignature(camelize ? camel(prop) : prop, t.tsTypeAnnotation(type), optional);
209
+ } = getPropertyType(context, jsonschema, prop);
210
+ return propertySignature(context.options.reactQuery.camelize ? camel(prop) : prop, t.tsTypeAnnotation(type), optional);
230
211
  });
231
212
  };
232
213
 
233
- function generateUseQueryQueryKey(hookKeyName, props, optionalClient) {
214
+ const generateUseQueryQueryKey = (hookKeyName, props, optionalClient) => {
234
215
  const queryKey = [t.stringLiteral(hookKeyName), t.optionalMemberExpression(t.identifier('client'), t.identifier('contractAddress'), false, optionalClient)];
235
216
 
236
217
  if (props.includes('args')) {
@@ -238,4 +219,4 @@ function generateUseQueryQueryKey(hookKeyName, props, optionalClient) {
238
219
  }
239
220
 
240
221
  return queryKey;
241
- }
222
+ };
@@ -0,0 +1,53 @@
1
+ import * as t from '@babel/types';
2
+ import query_msg from '../../../../__fixtures__/basic/query_msg.json';
3
+ import execute_msg from '../../../../__fixtures__/basic/execute_msg_for__empty.json';
4
+ import { RenderContext } from '../context';
5
+ 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);
9
+ it('createReactQueryHooks', () => {
10
+ expectCode(t.program(createReactQueryHooks({
11
+ context: queryCtx,
12
+ queryMsg: query_msg,
13
+ contractName: 'Sg721',
14
+ QueryClient: 'Sg721QueryClient'
15
+ })));
16
+ expectCode(t.program(createReactQueryHooks({
17
+ context: new RenderContext(query_msg, {
18
+ reactQuery: {
19
+ optionalClient: true
20
+ }
21
+ }),
22
+ queryMsg: query_msg,
23
+ contractName: 'Sg721',
24
+ QueryClient: 'Sg721QueryClient'
25
+ })));
26
+ expectCode(t.program(createReactQueryHooks({
27
+ context: new RenderContext(query_msg, {
28
+ reactQuery: {
29
+ v4: true
30
+ }
31
+ }),
32
+ queryMsg: query_msg,
33
+ contractName: 'Sg721',
34
+ QueryClient: 'Sg721QueryClient'
35
+ })));
36
+ expectCode(t.program(createReactQueryHooks({
37
+ context: new RenderContext(query_msg, {
38
+ reactQuery: {
39
+ optionalClient: true,
40
+ v4: true
41
+ }
42
+ }),
43
+ queryMsg: query_msg,
44
+ contractName: 'Sg721',
45
+ QueryClient: 'Sg721QueryClient'
46
+ })));
47
+ expectCode(t.program(createReactQueryMutationHooks({
48
+ context: execCtx,
49
+ execMsg: execute_msg,
50
+ contractName: 'Sg721',
51
+ ExecuteClient: 'Sg721Client'
52
+ })));
53
+ });
@@ -0,0 +1 @@
1
+ export * from './recoil';
@@ -1,31 +1,17 @@
1
1
  import * as t from '@babel/types';
2
2
  import { camel, pascal } from 'case';
3
- import { callExpression, getMessageProperties } from './utils';
4
- export const createWasmRecoilMethod = jsonschema => {
5
- const underscoreName = Object.keys(jsonschema.properties)[0];
6
- const methodName = camel(underscoreName);
7
- const responseType = pascal(`${methodName}Response`);
8
- const properties = jsonschema.properties[underscoreName].properties ?? {};
9
-
10
- if (Object.keys(properties).length > 0) {} else {}
11
- };
12
- export const createRecoilSelector = (keyPrefix, QueryClient, methodName) => {
13
- // const propertyNames = getMessageProperties(queryMsg)
14
- // .map(method => Object.keys(method.properties)?.[0])
15
- // .filter(Boolean);
16
- // const methods = getMessageProperties(queryMsg)
17
- // .map(schema => createWasmRecoilMethod(schema))
3
+ import { callExpression, getMessageProperties } from '../utils';
4
+ export const createRecoilSelector = (context, keyPrefix, QueryClient, methodName) => {
18
5
  const selectorName = camel(`${methodName}Selector`);
19
6
  const responseType = pascal(`${methodName}Response`);
20
7
  const getterKey = camel(`${keyPrefix}${pascal(methodName)}`);
21
- 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.identifier('params')
22
- 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)))]))))])])])))]));
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)))]))))])])])))]));
23
9
  };
24
- export const createRecoilSelectors = (keyPrefix, QueryClient, queryMsg) => {
10
+ export const createRecoilSelectors = (context, keyPrefix, QueryClient, queryMsg) => {
25
11
  return getMessageProperties(queryMsg).map(schema => {
26
12
  const underscoreName = Object.keys(schema.properties)[0];
27
13
  const methodName = camel(underscoreName);
28
- return createRecoilSelector(keyPrefix, QueryClient, methodName);
14
+ return createRecoilSelector(context, keyPrefix, QueryClient, methodName);
29
15
  });
30
16
  };
31
17
  export const createRecoilQueryClientType = () => ({
@@ -52,7 +38,7 @@ export const createRecoilQueryClientType = () => ({
52
38
  }]
53
39
  }
54
40
  });
55
- export const createRecoilQueryClient = (keyPrefix, QueryClient) => {
41
+ export const createRecoilQueryClient = (context, keyPrefix, QueryClient) => {
56
42
  const getterKey = camel(`${keyPrefix}${'QueryClient'}`);
57
43
  return t.exportNamedDeclaration(t.variableDeclaration('const', [t.variableDeclarator(t.identifier('queryClient'), 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('contractAddress'), t.identifier('contractAddress'), false, true)])], 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.identifier('cosmWasmClient')]))]), t.returnStatement(t.newExpression(t.identifier(QueryClient), [t.identifier('client'), t.identifier('contractAddress')]))]), false)))])], t.tsTypeParameterInstantiation([t.tsTypeReference(t.identifier(QueryClient)), t.tsTypeReference(t.identifier('QueryClientParams'))])))]));
58
44
  };
@@ -0,0 +1,15 @@
1
+ import * as t from '@babel/types';
2
+ import query_msg from '../../../../__fixtures__/basic/query_msg.json';
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);
7
+ it('selector', () => {
8
+ expectCode(createRecoilSelector(ctx, 'SG721', 'SG721QueryClient', 'governanceModules'));
9
+ });
10
+ it('selectors', () => {
11
+ expectCode(t.program(createRecoilSelectors(ctx, 'SG721', 'SG721QueryClient', query_msg)));
12
+ });
13
+ it('client', () => {
14
+ expectCode(createRecoilQueryClient(ctx, 'SG721', 'SG721QueryClient'));
15
+ });
@@ -1,16 +1,7 @@
1
1
  import { importStmt } from './babel';
2
- import generate from '@babel/generator';
3
2
  import * as t from '@babel/types';
4
3
  import { bindMethod, typedIdentifier, promiseTypeAnnotation, classDeclaration, classProperty, arrowFunctionExpression } from './babel';
5
-
6
- const expectCode = ast => {
7
- expect(generate(ast).code).toMatchSnapshot();
8
- };
9
-
10
- const printCode = ast => {
11
- console.log(generate(ast).code);
12
- };
13
-
4
+ import { expectCode } from '../../test-utils';
14
5
  it('top import', async () => {
15
6
  expectCode(importStmt(['CosmWasmClient', 'ExecuteResult', 'SigningCosmWasmClient'], '@cosmjs/cosmwasm-stargate'));
16
7
  });
@@ -1 +1,2 @@
1
- export * from './babel';
1
+ export * from './babel';
2
+ export * from './types';