wasm-ast-types 0.18.2 → 0.20.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 (66) hide show
  1. package/main/client/client.js +16 -7
  2. package/main/client/test/ts-client.issue-101.spec.js +27 -0
  3. package/main/client/test/ts-client.issue-98.test.js +30 -0
  4. package/main/context/context.js +59 -13
  5. package/main/context/imports.js +37 -15
  6. package/main/index.js +13 -0
  7. package/main/message-composer/message-composer.js +13 -4
  8. package/main/message-composer/message-composer.spec.js +14 -4
  9. package/main/msg-builder/index.js +18 -0
  10. package/main/msg-builder/msg-builder.js +68 -0
  11. package/main/msg-builder/msg-builder.spec.js +26 -0
  12. package/main/react-query/react-query.spec.js +11 -0
  13. package/main/utils/babel.js +56 -5
  14. package/main/utils/index.js +13 -0
  15. package/main/utils/ref.js +15 -0
  16. package/main/utils/types.js +10 -2
  17. package/module/client/client.js +18 -9
  18. package/module/client/test/ts-client.issue-101.spec.js +20 -0
  19. package/module/client/test/ts-client.issue-98.test.js +23 -0
  20. package/module/context/context.js +29 -6
  21. package/module/context/imports.js +33 -14
  22. package/module/index.js +2 -1
  23. package/module/message-composer/message-composer.js +13 -4
  24. package/module/message-composer/message-composer.spec.js +16 -7
  25. package/module/msg-builder/index.js +1 -0
  26. package/module/msg-builder/msg-builder.js +47 -0
  27. package/module/msg-builder/msg-builder.spec.js +17 -0
  28. package/module/react-query/react-query.spec.js +10 -0
  29. package/module/utils/babel.js +31 -5
  30. package/module/utils/index.js +2 -1
  31. package/module/utils/ref.js +4 -0
  32. package/module/utils/types.js +10 -3
  33. package/package.json +2 -2
  34. package/src/client/client.ts +29 -24
  35. package/src/client/test/__snapshots__/ts-client.issue-101.spec.ts.snap +47 -0
  36. package/src/client/test/__snapshots__/ts-client.issue-98.test.ts.snap +117 -0
  37. package/src/client/test/__snapshots__/ts-client.vectis.spec.ts.snap +8 -8
  38. package/src/client/test/ts-client.issue-101.spec.ts +37 -0
  39. package/src/client/test/ts-client.issue-98.test.ts +55 -0
  40. package/src/context/context.ts +45 -10
  41. package/src/context/imports.ts +49 -15
  42. package/src/index.ts +2 -1
  43. package/src/message-composer/__snapshots__/message-composer.spec.ts.snap +60 -0
  44. package/src/message-composer/message-composer.spec.ts +41 -20
  45. package/src/message-composer/message-composer.ts +13 -5
  46. package/src/msg-builder/__snapshots__/msg-builder.spec.ts.snap +270 -0
  47. package/src/msg-builder/index.ts +1 -0
  48. package/src/msg-builder/msg-builder.spec.ts +25 -0
  49. package/src/msg-builder/msg-builder.ts +119 -0
  50. package/src/react-query/__snapshots__/react-query.spec.ts.snap +45 -0
  51. package/src/react-query/react-query.spec.ts +17 -1
  52. package/src/react-query/react-query.ts +1 -1
  53. package/src/utils/babel.ts +34 -5
  54. package/src/utils/index.ts +1 -0
  55. package/src/utils/ref.ts +6 -0
  56. package/src/utils/types.ts +11 -4
  57. package/types/context/context.d.ts +30 -5
  58. package/types/context/imports.d.ts +7 -2
  59. package/types/index.d.ts +1 -0
  60. package/types/msg-builder/index.d.ts +1 -0
  61. package/types/msg-builder/msg-builder.d.ts +4 -0
  62. package/types/react-query/react-query.d.ts +11 -10
  63. package/types/recoil/recoil.d.ts +1 -1
  64. package/types/utils/babel.d.ts +3 -2
  65. package/types/utils/index.d.ts +1 -0
  66. package/types/utils/ref.d.ts +2 -0
@@ -28,4 +28,17 @@ Object.keys(_types).forEach(function (key) {
28
28
  return _types[key];
29
29
  }
30
30
  });
31
+ });
32
+
33
+ var _ref = require("./ref");
34
+
35
+ Object.keys(_ref).forEach(function (key) {
36
+ if (key === "default" || key === "__esModule") return;
37
+ if (key in exports && exports[key] === _ref[key]) return;
38
+ Object.defineProperty(exports, key, {
39
+ enumerable: true,
40
+ get: function get() {
41
+ return _ref[key];
42
+ }
43
+ });
31
44
  });
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.refLookup = void 0;
7
+
8
+ var refLookup = function refLookup($ref, schema) {
9
+ var _schema$definitions;
10
+
11
+ var refName = $ref.replace('#/definitions/', '');
12
+ return (_schema$definitions = schema.definitions) === null || _schema$definitions === void 0 ? void 0 : _schema$definitions[refName];
13
+ };
14
+
15
+ exports.refLookup = refLookup;
@@ -400,9 +400,17 @@ var createTypedObjectParams = function createTypedObjectParams(context, jsonsche
400
400
  if (!keys.length) {
401
401
  // is there a ref?
402
402
  if (jsonschema.$ref) {
403
- var _obj = context.refLookup(jsonschema.$ref);
403
+ var _obj = context.refLookup(jsonschema.$ref); // If there is a oneOf, then we need to create a type for it
404
404
 
405
- if (_obj) {
405
+
406
+ if (_obj !== null && _obj !== void 0 && _obj.oneOf) {
407
+ // the actual type of the ref
408
+ var refType = jsonschema.$ref.split('/').pop();
409
+ var refName = (0, _case.camel)(refType);
410
+ var id = t.identifier(refName);
411
+ id.typeAnnotation = t.tsTypeAnnotation(t.tsTypeReference(t.identifier(refType)));
412
+ return id;
413
+ } else if (_obj) {
406
414
  return createTypedObjectParams(context, _obj, camelize);
407
415
  }
408
416
  } // no results...
@@ -1,7 +1,7 @@
1
1
  import * as t from '@babel/types';
2
2
  import { camel } from 'case';
3
- import { bindMethod, typedIdentifier, promiseTypeAnnotation, classDeclaration, classProperty, arrowFunctionExpression, getMessageProperties } from '../utils';
4
- import { getPropertyType, getType, createTypedObjectParams, getResponseType } from '../utils/types';
3
+ import { arrowFunctionExpression, bindMethod, classDeclaration, classProperty, getMessageProperties, promiseTypeAnnotation, typedIdentifier } from '../utils';
4
+ import { createTypedObjectParams, getPropertyType, getResponseType, getType } from '../utils/types';
5
5
  import { identifier, propertySignature } from '../utils/babel';
6
6
  export const CONSTANT_EXEC_PARAMS = [t.assignmentPattern(identifier('fee', t.tsTypeAnnotation(t.tsUnionType([t.tSNumberKeyword(), t.tsTypeReference(t.identifier('StdFee')), t.tsLiteralType(t.stringLiteral('auto'))])), false), t.stringLiteral('auto')), identifier('memo', t.tsTypeAnnotation(t.tsStringKeyword()), true), identifier('funds', t.tsTypeAnnotation(t.tsArrayType(t.tsTypeReference(t.identifier('Coin')))), true)];
7
7
  export const FIXED_EXECUTE_PARAMS = [identifier('fee', t.tsTypeAnnotation(t.tsUnionType([t.tsNumberKeyword(), t.tsTypeReference(t.identifier('StdFee')), t.tsLiteralType(t.stringLiteral('auto'))])), true), identifier('memo', t.tsTypeAnnotation(t.tsStringKeyword()), true), identifier('funds', t.tsTypeAnnotation(t.tsArrayType(t.tsTypeReference(t.identifier('Coin')))), true)];
@@ -9,10 +9,12 @@ export const createWasmQueryMethod = (context, jsonschema) => {
9
9
  const underscoreName = Object.keys(jsonschema.properties)[0];
10
10
  const methodName = camel(underscoreName);
11
11
  const responseType = getResponseType(context, underscoreName);
12
- const obj = createTypedObjectParams(context, jsonschema.properties[underscoreName]);
12
+ const param = createTypedObjectParams(context, jsonschema.properties[underscoreName]);
13
13
  const args = getWasmMethodArgs(context, jsonschema.properties[underscoreName]);
14
- const actionArg = t.objectProperty(t.identifier(underscoreName), t.objectExpression(args));
15
- return t.classProperty(t.identifier(methodName), arrowFunctionExpression(obj ? [obj] : [], t.blockStatement([t.returnStatement(t.callExpression(t.memberExpression(t.memberExpression(t.thisExpression(), t.identifier('client')), t.identifier('queryContractSmart')), [t.memberExpression(t.thisExpression(), t.identifier('contractAddress')), t.objectExpression([actionArg])]))]), t.tsTypeAnnotation(t.tsTypeReference(t.identifier('Promise'), t.tsTypeParameterInstantiation([t.tSTypeReference(t.identifier(responseType))]))), true));
14
+ const msgAction = t.identifier(underscoreName); // If the param is an identifier, we can just use it as is
15
+
16
+ const msgActionValue = param?.type === 'Identifier' ? t.identifier(param.name) : t.objectExpression(args);
17
+ return t.classProperty(t.identifier(methodName), arrowFunctionExpression(param ? [param] : [], t.blockStatement([t.returnStatement(t.callExpression(t.memberExpression(t.memberExpression(t.thisExpression(), t.identifier('client')), t.identifier('queryContractSmart')), [t.memberExpression(t.thisExpression(), t.identifier('contractAddress')), t.objectExpression([t.objectProperty(msgAction, msgActionValue)])]))]), t.tsTypeAnnotation(t.tsTypeReference(t.identifier('Promise'), t.tsTypeParameterInstantiation([t.tSTypeReference(t.identifier(responseType))]))), true));
16
18
  };
17
19
  export const createQueryClass = (context, className, implementsClassName, queryMsg) => {
18
20
  context.addUtil('CosmWasmClient');
@@ -31,10 +33,14 @@ export const getWasmMethodArgs = (context, jsonschema) => {
31
33
  let keys = Object.keys(jsonschema.properties ?? {}); // only 1 degree $ref-lookup
32
34
 
33
35
  if (!keys.length && jsonschema.$ref) {
34
- const obj = context.refLookup(jsonschema.$ref);
36
+ const obj = context.refLookup(jsonschema.$ref); // properties
35
37
 
36
38
  if (obj) {
37
39
  keys = Object.keys(obj.properties ?? {});
40
+ } // tuple struct or otherwise, use the name of the reference
41
+
42
+
43
+ if (!keys.length && obj?.oneOf) {// TODO????? ADAIR
38
44
  }
39
45
  }
40
46
 
@@ -49,10 +55,13 @@ export const createWasmExecMethod = (context, jsonschema) => {
49
55
  context.addUtil('Coin');
50
56
  const underscoreName = Object.keys(jsonschema.properties)[0];
51
57
  const methodName = camel(underscoreName);
52
- const obj = createTypedObjectParams(context, jsonschema.properties[underscoreName]);
58
+ const param = createTypedObjectParams(context, jsonschema.properties[underscoreName]);
53
59
  const args = getWasmMethodArgs(context, jsonschema.properties[underscoreName]);
54
- return t.classProperty(t.identifier(methodName), arrowFunctionExpression(obj ? [// props
55
- obj, ...CONSTANT_EXEC_PARAMS] : CONSTANT_EXEC_PARAMS, t.blockStatement([t.returnStatement(t.awaitExpression(t.callExpression(t.memberExpression(t.memberExpression(t.thisExpression(), t.identifier('client')), t.identifier('execute')), [t.memberExpression(t.thisExpression(), t.identifier('sender')), t.memberExpression(t.thisExpression(), t.identifier('contractAddress')), t.objectExpression([t.objectProperty(t.identifier(underscoreName), t.objectExpression([...args]))]), t.identifier('fee'), t.identifier('memo'), t.identifier('funds')])))]), // return type
60
+ const msgAction = t.identifier(underscoreName); // If the param is an identifier, we can just use it as is
61
+
62
+ const msgActionValue = param?.type === 'Identifier' ? t.identifier(param.name) : t.objectExpression(args);
63
+ return t.classProperty(t.identifier(methodName), arrowFunctionExpression(param ? [// props
64
+ param, ...CONSTANT_EXEC_PARAMS] : CONSTANT_EXEC_PARAMS, t.blockStatement([t.returnStatement(t.awaitExpression(t.callExpression(t.memberExpression(t.memberExpression(t.thisExpression(), t.identifier('client')), t.identifier('execute')), [t.memberExpression(t.thisExpression(), t.identifier('sender')), t.memberExpression(t.thisExpression(), t.identifier('contractAddress')), t.objectExpression([t.objectProperty(msgAction, msgActionValue)]), t.identifier('fee'), t.identifier('memo'), t.identifier('funds')])))]), // return type
56
65
  t.tsTypeAnnotation(t.tsTypeReference(t.identifier('Promise'), t.tsTypeParameterInstantiation([t.tSTypeReference(t.identifier('ExecuteResult'))]))), true));
57
66
  };
58
67
  export const createExecuteClass = (context, className, implementsClassName, extendsClassName, execMsg) => {
@@ -0,0 +1,20 @@
1
+ import { createExecuteClass, createExecuteInterface } from '../client';
2
+ import { expectCode, makeContext } from '../../../test-utils';
3
+ import ownership from '../../../../../__fixtures__/basic/ownership.json'; // it('query classes', () => {
4
+ // const ctx = makeContext(cosmos_msg_for__empty);
5
+ // expectCode(createQueryClass(
6
+ // ctx,
7
+ // 'SG721QueryClient',
8
+ // 'SG721ReadOnlyInstance',
9
+ // cosmos_msg_for__empty
10
+ // ))
11
+ // });
12
+
13
+ it('execute interfaces no extends', () => {
14
+ const ctx = makeContext(ownership);
15
+ expectCode(createExecuteInterface(ctx, 'OwnershipInstance', null, ownership));
16
+ });
17
+ it('ownership client with tuple', () => {
18
+ const ctx = makeContext(ownership);
19
+ expectCode(createExecuteClass(ctx, 'OwnershipClient', 'OwnershipInstance', null, ownership));
20
+ });
@@ -0,0 +1,23 @@
1
+ import contract from '../../../../../__fixtures__/issues/98/schema.json';
2
+ import { createQueryClass, createExecuteClass, createExecuteInterface, createTypeInterface } from '../client';
3
+ import { expectCode, makeContext } from '../../../test-utils';
4
+ const message = contract.query;
5
+ const ctx = makeContext(message);
6
+ it('execute_msg_for__empty', () => {
7
+ expectCode(createTypeInterface(ctx, message));
8
+ });
9
+ it('query classes', () => {
10
+ expectCode(createQueryClass(ctx, 'SG721QueryClient', 'SG721ReadOnlyInstance', message));
11
+ }); // it('query classes response', () => {
12
+ // expectCode(createTypeInterface(
13
+ // ctx,
14
+ // contract.responses.all_debt_shares
15
+ // ))
16
+ // });
17
+
18
+ it('execute classes array types', () => {
19
+ expectCode(createExecuteClass(ctx, 'SG721Client', 'SG721Instance', null, message));
20
+ });
21
+ it('execute interfaces no extends', () => {
22
+ expectCode(createExecuteInterface(ctx, 'SG721Instance', null, message));
23
+ });
@@ -4,11 +4,13 @@ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (O
4
4
 
5
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
6
 
7
+ import { refLookup } from "../utils";
7
8
  import { convertUtilsToImportList, getImportStatements } from "./imports";
8
9
  import deepmerge from "deepmerge"; /// Plugin Types
9
10
 
10
11
  ;
11
12
  export const defaultOptions = {
13
+ enabled: true,
12
14
  types: {
13
15
  enabled: true,
14
16
  aliasExecuteMsg: false
@@ -24,6 +26,9 @@ export const defaultOptions = {
24
26
  messageComposer: {
25
27
  enabled: false
26
28
  },
29
+ msgBuilder: {
30
+ enabled: false
31
+ },
27
32
  reactQuery: {
28
33
  enabled: false,
29
34
  optionalClient: false,
@@ -44,7 +49,13 @@ export const getDefinitionSchema = schemas => {
44
49
  });
45
50
  return aggregateSchema;
46
51
  };
47
- export class RenderContext {
52
+ /**
53
+ * context object for generating code.
54
+ * only mergeDefaultOpt needs to implementing for combine options and default options.
55
+ * @param TOpt option type
56
+ */
57
+
58
+ export class RenderContextBase {
48
59
  constructor(contract, options) {
49
60
  _defineProperty(this, "contract", void 0);
50
61
 
@@ -52,22 +63,34 @@ export class RenderContext {
52
63
 
53
64
  _defineProperty(this, "schema", void 0);
54
65
 
66
+ _defineProperty(this, "options", void 0);
67
+
55
68
  this.contract = contract;
56
69
  this.schema = getDefinitionSchema(contract.schemas);
57
- this.options = deepmerge(defaultOptions, options ?? {});
70
+ this.options = this.mergeDefaultOpt(options);
58
71
  }
72
+ /**
73
+ * merge options and default options
74
+ * @param options
75
+ */
76
+
59
77
 
60
78
  refLookup($ref) {
61
- const refName = $ref.replace('#/definitions/', '');
62
- return this.schema.definitions?.[refName];
79
+ return refLookup($ref, this.schema);
63
80
  }
64
81
 
65
82
  addUtil(util) {
66
83
  this.utils[util] = true;
67
84
  }
68
85
 
69
- getImports() {
70
- return getImportStatements(convertUtilsToImportList(this, Object.keys(this.utils)));
86
+ getImports(registeredUtils) {
87
+ return getImportStatements(convertUtilsToImportList(this, Object.keys(this.utils), registeredUtils));
88
+ }
89
+
90
+ }
91
+ export class RenderContext extends RenderContextBase {
92
+ mergeDefaultOpt(options) {
93
+ return deepmerge(defaultOptions, options ?? {});
71
94
  }
72
95
 
73
96
  }
@@ -23,11 +23,11 @@ const makeReactQuerySwitch = varName => {
23
23
  };
24
24
 
25
25
  export const UTILS = {
26
+ selectorFamily: 'recoil',
26
27
  MsgExecuteContract: 'cosmjs-types/cosmwasm/wasm/v1/tx',
27
28
  MsgExecuteContractEncodeObject: 'cosmwasm',
28
29
  Coin: '@cosmjs/amino',
29
30
  toUtf8: '@cosmjs/encoding',
30
- selectorFamily: 'recoil',
31
31
  StdFee: '@cosmjs/amino',
32
32
  CosmWasmClient: '@cosmjs/cosmwasm-stargate',
33
33
  ExecuteResult: '@cosmjs/cosmwasm-stargate',
@@ -38,23 +38,42 @@ export const UTILS = {
38
38
  useMutation: makeReactQuerySwitch('useMutation'),
39
39
  UseMutationOptions: makeReactQuerySwitch('UseMutationOptions')
40
40
  };
41
- export const convertUtilsToImportList = (context, utils) => {
41
+ export const convertUtilsToImportList = (context, utils, registeredUtils) => {
42
42
  return utils.map(util => {
43
- if (!UTILS.hasOwnProperty(util)) throw new Error(`missing Util! ::[${util}]`);
44
-
45
- if (typeof UTILS[util] === 'string') {
46
- return {
47
- type: 'import',
48
- path: UTILS[util],
49
- name: util
50
- };
51
- } else if (typeof UTILS[util] === 'function') {
52
- return UTILS[util](context);
53
- } else {
54
- UTILS[util];
43
+ let result = null;
44
+
45
+ if (registeredUtils) {
46
+ result = convertUtil(context, util, registeredUtils);
47
+
48
+ if (result) {
49
+ return result;
50
+ }
51
+ }
52
+
53
+ result = convertUtil(context, util, UTILS);
54
+
55
+ if (result) {
56
+ return result;
55
57
  }
58
+
59
+ throw new Error(`missing Util! ::[${util}]`);
56
60
  });
57
61
  };
62
+ export const convertUtil = (context, util, registeredUtils) => {
63
+ if (!registeredUtils.hasOwnProperty(util)) return null;
64
+
65
+ if (typeof registeredUtils[util] === 'string') {
66
+ return {
67
+ type: 'import',
68
+ path: registeredUtils[util],
69
+ name: util
70
+ };
71
+ } else if (typeof registeredUtils[util] === 'function') {
72
+ return registeredUtils[util](context);
73
+ } else {
74
+ return registeredUtils[util];
75
+ }
76
+ };
58
77
  export const getImportStatements = list => {
59
78
  const imports = list.reduce((m, obj) => {
60
79
  m[obj.path] = m[obj.path] || [];
package/module/index.js CHANGED
@@ -4,4 +4,5 @@ export * from './context';
4
4
  export * from './recoil';
5
5
  export * from './message-composer';
6
6
  export * from './react-query';
7
- export * from './types';
7
+ export * from './types';
8
+ export * from './msg-builder';
@@ -12,11 +12,20 @@ const createWasmExecMethodMessageComposer = (context, jsonschema) => {
12
12
  context.addUtil('toUtf8');
13
13
  const underscoreName = Object.keys(jsonschema.properties)[0];
14
14
  const methodName = camel(underscoreName);
15
- const obj = createTypedObjectParams(context, jsonschema.properties[underscoreName]);
16
- const args = getWasmMethodArgs(context, jsonschema.properties[underscoreName]);
15
+ const param = createTypedObjectParams(context, jsonschema.properties[underscoreName]);
16
+ const args = getWasmMethodArgs(context, jsonschema.properties[underscoreName]); // what the underscore named property in the message is assigned to
17
+
18
+ let actionValue;
19
+
20
+ if (param?.type === 'Identifier') {
21
+ actionValue = t.identifier(param.name);
22
+ } else {
23
+ actionValue = t.objectExpression(args);
24
+ }
25
+
17
26
  const constantParams = [identifier('funds', t.tsTypeAnnotation(t.tsArrayType(t.tsTypeReference(t.identifier('Coin')))), true)];
18
- return t.classProperty(t.identifier(methodName), arrowFunctionExpression(obj ? [// props
19
- 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
27
+ return t.classProperty(t.identifier(methodName), arrowFunctionExpression(param ? [// props
28
+ param, ...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), actionValue)])])])), t.objectProperty(t.identifier('funds'), t.identifier('funds'), false, true)])]))]))]), // return type
20
29
  t.tsTypeAnnotation(t.tsTypeReference(t.identifier('MsgExecuteContractEncodeObject'))), false));
21
30
  };
22
31
 
@@ -1,11 +1,20 @@
1
- import execute_msg from '../../../../__fixtures__/basic/execute_msg_for__empty.json';
2
- import { createMessageComposerClass, createMessageComposerInterface } from './message-composer';
3
- import { expectCode, makeContext } from '../../test-utils';
4
- it('execute classes', () => {
1
+ import execute_msg from "../../../../__fixtures__/basic/execute_msg_for__empty.json";
2
+ import ownership from "../../../../__fixtures__/basic/ownership.json";
3
+ import { createMessageComposerClass, createMessageComposerInterface } from "./message-composer";
4
+ import { expectCode, makeContext } from "../../test-utils";
5
+ it("execute classes", () => {
5
6
  const ctx = makeContext(execute_msg);
6
- expectCode(createMessageComposerClass(ctx, 'SG721MessageComposer', 'SG721Message', execute_msg));
7
+ expectCode(createMessageComposerClass(ctx, "SG721MessageComposer", "SG721Message", execute_msg));
7
8
  });
8
- it('createMessageComposerInterface', () => {
9
+ it("createMessageComposerInterface", () => {
9
10
  const ctx = makeContext(execute_msg);
10
- expectCode(createMessageComposerInterface(ctx, 'SG721Message', execute_msg));
11
+ expectCode(createMessageComposerInterface(ctx, "SG721Message", execute_msg));
12
+ });
13
+ it("ownershipClass", () => {
14
+ const ctx = makeContext(ownership);
15
+ expectCode(createMessageComposerClass(ctx, "OwnershipMessageComposer", "OwnershipMessage", ownership));
16
+ });
17
+ it("ownershipInterface", () => {
18
+ const ownershipCtx = makeContext(ownership);
19
+ expectCode(createMessageComposerInterface(ownershipCtx, "OwnershipMessage", ownership));
11
20
  });
@@ -0,0 +1 @@
1
+ export * from './msg-builder';
@@ -0,0 +1,47 @@
1
+ import * as t from '@babel/types';
2
+ import { camel } from 'case';
3
+ import { abstractClassDeclaration, arrowFunctionExpression, getMessageProperties } from '../utils';
4
+ import { createTypedObjectParams } from '../utils/types';
5
+ import { getWasmMethodArgs } from '../client/client';
6
+ export const createMsgBuilderClass = (context, className, msg) => {
7
+ const staticMethods = getMessageProperties(msg).map(schema => {
8
+ return createStaticExecMethodMsgBuilder(context, schema, msg.title);
9
+ }); // const blockStmt = bindings;
10
+
11
+ return t.exportNamedDeclaration(abstractClassDeclaration(className, staticMethods, [], null));
12
+ };
13
+ /**
14
+ * CamelCasedProperties<Extract<ExecuteMsg, { exec_on_module: unknown }>['exec_on_module']>
15
+ */
16
+
17
+ function createExtractTypeAnnotation(underscoreName, msgTitle) {
18
+ return t.tsTypeAnnotation(t.tsTypeReference(t.identifier('CamelCasedProperties'), t.tsTypeParameterInstantiation([t.tsIndexedAccessType(t.tsTypeReference(t.identifier('Extract'), t.tsTypeParameterInstantiation([t.tsTypeReference(t.identifier(msgTitle)), t.tsTypeLiteral([t.tsPropertySignature(t.identifier(underscoreName), t.tsTypeAnnotation(t.tsUnknownKeyword()))])])), t.tsLiteralType(t.stringLiteral(underscoreName)))])));
19
+ }
20
+
21
+ const createStaticExecMethodMsgBuilder = (context, jsonschema, msgTitle) => {
22
+ const underscoreName = Object.keys(jsonschema.properties)[0];
23
+ const methodName = camel(underscoreName);
24
+ const param = createTypedObjectParams(context, jsonschema.properties[underscoreName]);
25
+ const args = getWasmMethodArgs(context, jsonschema.properties[underscoreName]); // what the underscore named property in the message is assigned to
26
+
27
+ let actionValue;
28
+
29
+ if (param?.type === 'Identifier') {
30
+ actionValue = t.identifier(param.name);
31
+ } else {
32
+ actionValue = t.tsAsExpression(t.objectExpression(args), t.tsTypeReference(t.identifier('const')));
33
+ } // TODO: this is a hack to get the type annotation to work
34
+ // all type annotations in the future should be the extracted and camelized type
35
+
36
+
37
+ if (param && param.typeAnnotation.type === 'TSTypeAnnotation' && param.typeAnnotation.typeAnnotation.type === 'TSTypeLiteral') {
38
+ param.typeAnnotation = createExtractTypeAnnotation(underscoreName, msgTitle);
39
+ }
40
+
41
+ return t.classProperty(t.identifier(methodName), arrowFunctionExpression( // params
42
+ param ? [// props
43
+ param] : [], // body
44
+ t.blockStatement([t.returnStatement(t.objectExpression([t.objectProperty(t.identifier(underscoreName), actionValue)]))]), // return type
45
+ t.tsTypeAnnotation(t.tsTypeReference(t.identifier(msgTitle))), false), null, null, false, // static
46
+ true);
47
+ };
@@ -0,0 +1,17 @@
1
+ import execute_msg from '../../../../__fixtures__/basic/execute_msg_for__empty.json';
2
+ import query_msg from '../../../../__fixtures__/basic/query_msg.json';
3
+ import ownership from '../../../../__fixtures__/basic/ownership.json';
4
+ import { createMsgBuilderClass } from './msg-builder';
5
+ import { expectCode, makeContext } from '../../test-utils';
6
+ it('execute class', () => {
7
+ const ctx = makeContext(execute_msg);
8
+ expectCode(createMsgBuilderClass(ctx, 'SG721MsgBuilder', execute_msg));
9
+ });
10
+ it('query class', () => {
11
+ const ctx = makeContext(query_msg);
12
+ expectCode(createMsgBuilderClass(ctx, 'SG721MsgBuilder', query_msg));
13
+ });
14
+ it('ownership', () => {
15
+ const ctx = makeContext(ownership);
16
+ expectCode(createMsgBuilderClass(ctx, 'Ownership', ownership));
17
+ });
@@ -1,6 +1,7 @@
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 ownership from '../../../../__fixtures__/basic/ownership.json';
4
5
  import { createReactQueryHooks, createReactQueryMutationHooks } from './react-query';
5
6
  import { expectCode, makeContext } from '../../test-utils';
6
7
  const execCtx = makeContext(execute_msg);
@@ -62,4 +63,13 @@ it('createReactQueryHooks', () => {
62
63
  contractName: 'Sg721',
63
64
  ExecuteClient: 'Sg721Client'
64
65
  })));
66
+ });
67
+ it('ownership', () => {
68
+ const ownershipCtx = makeContext(ownership);
69
+ expectCode(t.program(createReactQueryMutationHooks({
70
+ context: ownershipCtx,
71
+ execMsg: ownership,
72
+ contractName: 'Ownership',
73
+ ExecuteClient: 'OwnershipClient'
74
+ })));
65
75
  });
@@ -1,6 +1,7 @@
1
1
  import * as t from '@babel/types';
2
2
  import { snake } from "case";
3
- // t.TSPropertySignature - kind?
3
+ import { refLookup } from './ref'; // t.TSPropertySignature - kind?
4
+
4
5
  export const propertySignature = (name, typeAnnotation, optional = false) => {
5
6
  return {
6
7
  type: 'TSPropertySignature',
@@ -21,10 +22,30 @@ export const tsTypeOperator = (typeAnnotation, operator) => {
21
22
  return obj;
22
23
  };
23
24
  export const getMessageProperties = msg => {
24
- if (msg.anyOf) return msg.anyOf;
25
- if (msg.oneOf) return msg.oneOf;
26
- if (msg.allOf) return msg.allOf;
27
- return [];
25
+ let results = [];
26
+ let objs = [];
27
+
28
+ if (msg.anyOf) {
29
+ objs = msg.anyOf;
30
+ } else if (msg.oneOf) {
31
+ objs = msg.oneOf;
32
+ } else if (msg.allOf) {
33
+ objs = msg.allOf;
34
+ }
35
+
36
+ for (const obj of objs) {
37
+ if (obj.properties) {
38
+ results.push(obj);
39
+ } else {
40
+ if (obj.$ref) {
41
+ const ref = refLookup(obj.$ref, msg);
42
+ const refProps = getMessageProperties(ref);
43
+ results = [...results, ...refProps];
44
+ }
45
+ }
46
+ }
47
+
48
+ return results;
28
49
  };
29
50
  export const tsPropertySignature = (key, typeAnnotation, optional) => {
30
51
  const obj = t.tsPropertySignature(key, typeAnnotation);
@@ -53,6 +74,11 @@ export const typedIdentifier = (name, typeAnnotation, optional = false) => {
53
74
  export const promiseTypeAnnotation = name => {
54
75
  return t.tsTypeAnnotation(t.tsTypeReference(t.identifier('Promise'), t.tsTypeParameterInstantiation([t.tsTypeReference(t.identifier(name))])));
55
76
  };
77
+ export const abstractClassDeclaration = (name, body, implementsExressions = [], superClass = null) => {
78
+ const declaration = classDeclaration(name, body, implementsExressions, superClass);
79
+ declaration.abstract = true;
80
+ return declaration;
81
+ };
56
82
  export const classDeclaration = (name, body, implementsExressions = [], superClass = null) => {
57
83
  const declaration = t.classDeclaration(t.identifier(name), superClass, t.classBody(body));
58
84
 
@@ -1,2 +1,3 @@
1
1
  export * from './babel';
2
- export * from './types';
2
+ export * from './types';
3
+ export * from './ref';
@@ -0,0 +1,4 @@
1
+ export const refLookup = ($ref, schema) => {
2
+ const refName = $ref.replace('#/definitions/', '');
3
+ return schema.definitions?.[refName];
4
+ };
@@ -322,9 +322,16 @@ export const createTypedObjectParams = (context, jsonschema, camelize = true) =>
322
322
  if (!keys.length) {
323
323
  // is there a ref?
324
324
  if (jsonschema.$ref) {
325
- const obj = context.refLookup(jsonschema.$ref);
326
-
327
- if (obj) {
325
+ const obj = context.refLookup(jsonschema.$ref); // If there is a oneOf, then we need to create a type for it
326
+
327
+ if (obj?.oneOf) {
328
+ // the actual type of the ref
329
+ const refType = jsonschema.$ref.split('/').pop();
330
+ const refName = camel(refType);
331
+ const id = t.identifier(refName);
332
+ id.typeAnnotation = t.tsTypeAnnotation(t.tsTypeReference(t.identifier(refType)));
333
+ return id;
334
+ } else if (obj) {
328
335
  return createTypedObjectParams(context, obj, camelize);
329
336
  }
330
337
  } // no results...
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wasm-ast-types",
3
- "version": "0.18.2",
3
+ "version": "0.20.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": "a980b8040099876a056027f4bb45e83de86fb278"
91
+ "gitHead": "d2edba320c63e2db5fe2db0bfb1f4cd5e44f2188"
92
92
  }