wasm-ast-types 0.3.7 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -25,7 +25,16 @@ function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "functio
25
25
 
26
26
  function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
27
27
 
28
- var createReactQueryHooks = function createReactQueryHooks(queryMsg, contractName, QueryClient) {
28
+ var DEFAULT_OPTIONS = {
29
+ optionalClient: false
30
+ };
31
+
32
+ var createReactQueryHooks = function createReactQueryHooks(_ref) {
33
+ var queryMsg = _ref.queryMsg,
34
+ contractName = _ref.contractName,
35
+ QueryClient = _ref.QueryClient,
36
+ _ref$options = _ref.options,
37
+ options = _ref$options === void 0 ? DEFAULT_OPTIONS : _ref$options;
29
38
  return (0, _utils.getMessageProperties)(queryMsg).reduce(function (m, schema) {
30
39
  var underscoreName = Object.keys(schema.properties)[0];
31
40
  var methodName = (0, _case.camel)(underscoreName);
@@ -38,29 +47,33 @@ var createReactQueryHooks = function createReactQueryHooks(queryMsg, contractNam
38
47
  hookParamsTypeName: hookParamsTypeName,
39
48
  responseType: responseType,
40
49
  QueryClient: QueryClient,
41
- jsonschema: jsonschema
50
+ jsonschema: jsonschema,
51
+ options: options
42
52
  }), createReactQueryHook({
43
53
  methodName: methodName,
44
54
  hookName: hookName,
45
55
  hookParamsTypeName: hookParamsTypeName,
46
56
  responseType: responseType,
47
57
  hookKeyName: getterKey,
48
- jsonschema: jsonschema
58
+ jsonschema: jsonschema,
59
+ options: options
49
60
  })].concat((0, _toConsumableArray2["default"])(m));
50
61
  }, []);
51
62
  };
52
63
 
53
64
  exports.createReactQueryHooks = createReactQueryHooks;
54
65
 
55
- var createReactQueryHook = function createReactQueryHook(_ref) {
66
+ var createReactQueryHook = function createReactQueryHook(_ref2) {
56
67
  var _jsonschema$propertie;
57
68
 
58
- var hookName = _ref.hookName,
59
- hookParamsTypeName = _ref.hookParamsTypeName,
60
- responseType = _ref.responseType,
61
- hookKeyName = _ref.hookKeyName,
62
- methodName = _ref.methodName,
63
- jsonschema = _ref.jsonschema;
69
+ var hookName = _ref2.hookName,
70
+ hookParamsTypeName = _ref2.hookParamsTypeName,
71
+ responseType = _ref2.responseType,
72
+ hookKeyName = _ref2.hookKeyName,
73
+ methodName = _ref2.methodName,
74
+ jsonschema = _ref2.jsonschema,
75
+ _ref2$options = _ref2.options,
76
+ options = _ref2$options === void 0 ? DEFAULT_OPTIONS : _ref2$options;
64
77
  var keys = Object.keys((_jsonschema$propertie = jsonschema.properties) !== null && _jsonschema$propertie !== void 0 ? _jsonschema$propertie : {});
65
78
  var args = [];
66
79
 
@@ -78,17 +91,19 @@ var createReactQueryHook = function createReactQueryHook(_ref) {
78
91
 
79
92
  return t.exportNamedDeclaration(t.functionDeclaration(t.identifier(hookName), [(0, _utils.tsObjectPattern)((0, _toConsumableArray2["default"])(props.map(function (prop) {
80
93
  return t.objectProperty(t.identifier(prop), t.identifier(prop), false, true);
81
- })), t.tsTypeAnnotation(t.tsTypeReference(t.identifier(hookParamsTypeName))))], t.blockStatement([t.returnStatement((0, _utils.callExpression)(t.identifier('useQuery'), [t.arrayExpression([t.stringLiteral(hookKeyName), t.memberExpression(t.identifier('client'), t.identifier('contractAddress'))]), t.arrowFunctionExpression([], t.callExpression(t.memberExpression(t.identifier('client'), t.identifier(methodName)), args), false), 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()])))])))])));
94
+ })), t.tsTypeAnnotation(t.tsTypeReference(t.identifier(hookParamsTypeName))))], t.blockStatement([t.returnStatement((0, _utils.callExpression)(t.identifier('useQuery'), [t.arrayExpression([t.stringLiteral(hookKeyName), t.optionalMemberExpression(t.identifier('client'), t.identifier('contractAddress'), false, options.optionalClient)]), t.arrowFunctionExpression([], (0, _babel.optionalConditionalExpression)(t.identifier('client'), t.callExpression(t.memberExpression(t.identifier('client'), t.identifier(methodName)), args), t.identifier('undefined'), 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.optionalMemberExpression(t.identifier('options'), t.identifier('enabled'), false, true)))]) : t.identifier('options')], t.tsTypeParameterInstantiation([(0, _babel.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()])))])))])));
82
95
  };
83
96
 
84
97
  exports.createReactQueryHook = createReactQueryHook;
85
98
 
86
- var createReactQueryHookInterface = function createReactQueryHookInterface(_ref2) {
87
- var QueryClient = _ref2.QueryClient,
88
- hookParamsTypeName = _ref2.hookParamsTypeName,
89
- responseType = _ref2.responseType,
90
- jsonschema = _ref2.jsonschema;
91
- var body = [t.tsPropertySignature(t.identifier('client'), t.tsTypeAnnotation(t.tsTypeReference(t.identifier(QueryClient)))), (0, _utils.tsPropertySignature)(t.identifier('options'), t.tsTypeAnnotation(t.tsTypeReference(t.identifier('UseQueryOptions'), 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()])))]))), true)];
99
+ var createReactQueryHookInterface = function createReactQueryHookInterface(_ref3) {
100
+ var QueryClient = _ref3.QueryClient,
101
+ hookParamsTypeName = _ref3.hookParamsTypeName,
102
+ responseType = _ref3.responseType,
103
+ jsonschema = _ref3.jsonschema,
104
+ _ref3$options = _ref3.options,
105
+ options = _ref3$options === void 0 ? DEFAULT_OPTIONS : _ref3$options;
106
+ var body = [(0, _utils.tsPropertySignature)(t.identifier('client'), t.tsTypeAnnotation(t.tsTypeReference(t.identifier(QueryClient))), options.optionalClient), (0, _utils.tsPropertySignature)(t.identifier('options'), t.tsTypeAnnotation(t.tsTypeReference(t.identifier('UseQueryOptions'), t.tsTypeParameterInstantiation([(0, _babel.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()])))]))), true)];
92
107
  var props = getProps(jsonschema, true);
93
108
 
94
109
  if (props.length) {
@@ -34,6 +34,22 @@ var printCode = function printCode(ast) {
34
34
 
35
35
 
36
36
  it('createReactQueryHooks', function () {
37
- expectCode(t.program((0, _reactQuery.createReactQueryHooks)(_query_msg["default"], 'Sg721', 'Sg721QueryClient')));
38
- expectCode(t.program((0, _reactQuery.createReactQueryHooks)(_query_msg["default"], 'Sg721', 'Sg721QueryClient')));
37
+ expectCode(t.program((0, _reactQuery.createReactQueryHooks)({
38
+ queryMsg: _query_msg["default"],
39
+ contractName: 'Sg721',
40
+ QueryClient: 'Sg721QueryClient'
41
+ })));
42
+ expectCode(t.program((0, _reactQuery.createReactQueryHooks)({
43
+ queryMsg: _query_msg["default"],
44
+ contractName: 'Sg721',
45
+ QueryClient: 'Sg721QueryClient'
46
+ })));
47
+ expectCode(t.program((0, _reactQuery.createReactQueryHooks)({
48
+ queryMsg: _query_msg["default"],
49
+ contractName: 'Sg721',
50
+ QueryClient: 'Sg721QueryClient',
51
+ options: {
52
+ optionalClient: true
53
+ }
54
+ })));
39
55
  });
@@ -7,7 +7,7 @@ var _typeof = require("@babel/runtime/helpers/typeof");
7
7
  Object.defineProperty(exports, "__esModule", {
8
8
  value: true
9
9
  });
10
- exports.typedIdentifier = exports.tsTypeOperator = exports.tsPropertySignature = exports.tsObjectPattern = exports.shorthandProperty = exports.recursiveNamespace = exports.propertySignature = exports.promiseTypeAnnotation = exports.memberExpressionOrIdentifierSnake = exports.memberExpressionOrIdentifier = exports.importStmt = exports.importAminoMsg = exports.identifier = exports.getMessageProperties = exports.getFieldDimensionality = exports.classProperty = exports.classDeclaration = exports.callExpression = exports.bindMethod = exports.arrowFunctionExpression = exports.arrayTypeNDimensions = exports.FieldTypeAsts = void 0;
10
+ exports.typedIdentifier = exports.typeRefOrOptionalUnion = exports.tsTypeOperator = exports.tsPropertySignature = exports.tsObjectPattern = exports.shorthandProperty = exports.recursiveNamespace = exports.propertySignature = exports.promiseTypeAnnotation = exports.optionalConditionalExpression = exports.memberExpressionOrIdentifierSnake = exports.memberExpressionOrIdentifier = exports.importStmt = exports.importAminoMsg = exports.identifier = exports.getMessageProperties = exports.getFieldDimensionality = exports.classProperty = exports.classDeclaration = exports.callExpression = exports.bindMethod = exports.arrowFunctionExpression = exports.arrayTypeNDimensions = exports.FieldTypeAsts = void 0;
11
11
 
12
12
  var _toArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toArray"));
13
13
 
@@ -259,5 +259,24 @@ var memberExpressionOrIdentifierSnake = function memberExpressionOrIdentifierSna
259
259
 
260
260
  return t.memberExpression(memberExpressionOrIdentifierSnake(rest), t.identifier((0, _case.snake)(name)));
261
261
  };
262
+ /**
263
+ * If optional, return a conditional, otherwise just the expression
264
+ */
262
265
 
263
- exports.memberExpressionOrIdentifierSnake = memberExpressionOrIdentifierSnake;
266
+
267
+ exports.memberExpressionOrIdentifierSnake = memberExpressionOrIdentifierSnake;
268
+
269
+ var optionalConditionalExpression = function optionalConditionalExpression(test, expression, alternate) {
270
+ var optional = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
271
+ return optional ? t.conditionalExpression(test, expression, alternate) : expression;
272
+ };
273
+
274
+ exports.optionalConditionalExpression = optionalConditionalExpression;
275
+
276
+ var typeRefOrOptionalUnion = function typeRefOrOptionalUnion(identifier) {
277
+ var optional = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
278
+ var typeReference = t.tsTypeReference(identifier);
279
+ return optional ? t.tsUnionType([typeReference, t.tsUndefinedKeyword()]) : typeReference;
280
+ };
281
+
282
+ exports.typeRefOrOptionalUnion = typeRefOrOptionalUnion;
@@ -1,9 +1,17 @@
1
1
  import * as t from '@babel/types';
2
2
  import { camel, pascal } from 'case';
3
3
  import { tsPropertySignature, tsObjectPattern, callExpression, getMessageProperties } from './utils';
4
- import { propertySignature } from './utils/babel';
4
+ import { typeRefOrOptionalUnion, propertySignature, optionalConditionalExpression } from './utils/babel';
5
5
  import { getPropertyType } from './utils/types';
6
- export const createReactQueryHooks = (queryMsg, contractName, QueryClient) => {
6
+ const DEFAULT_OPTIONS = {
7
+ optionalClient: false
8
+ };
9
+ export const createReactQueryHooks = ({
10
+ queryMsg,
11
+ contractName,
12
+ QueryClient,
13
+ options = DEFAULT_OPTIONS
14
+ }) => {
7
15
  return getMessageProperties(queryMsg).reduce((m, schema) => {
8
16
  const underscoreName = Object.keys(schema.properties)[0];
9
17
  const methodName = camel(underscoreName);
@@ -16,14 +24,16 @@ export const createReactQueryHooks = (queryMsg, contractName, QueryClient) => {
16
24
  hookParamsTypeName,
17
25
  responseType,
18
26
  QueryClient,
19
- jsonschema
27
+ jsonschema,
28
+ options
20
29
  }), createReactQueryHook({
21
30
  methodName,
22
31
  hookName,
23
32
  hookParamsTypeName,
24
33
  responseType,
25
34
  hookKeyName: getterKey,
26
- jsonschema
35
+ jsonschema,
36
+ options
27
37
  }), ...m];
28
38
  }, []);
29
39
  };
@@ -33,7 +43,8 @@ export const createReactQueryHook = ({
33
43
  responseType,
34
44
  hookKeyName,
35
45
  methodName,
36
- jsonschema
46
+ jsonschema,
47
+ options = DEFAULT_OPTIONS
37
48
  }) => {
38
49
  const keys = Object.keys(jsonschema.properties ?? {});
39
50
  let args = [];
@@ -52,15 +63,16 @@ export const createReactQueryHook = ({
52
63
 
53
64
  return t.exportNamedDeclaration(t.functionDeclaration(t.identifier(hookName), [tsObjectPattern([...props.map(prop => {
54
65
  return t.objectProperty(t.identifier(prop), t.identifier(prop), false, true);
55
- })], t.tsTypeAnnotation(t.tsTypeReference(t.identifier(hookParamsTypeName))))], t.blockStatement([t.returnStatement(callExpression(t.identifier('useQuery'), [t.arrayExpression([t.stringLiteral(hookKeyName), t.memberExpression(t.identifier('client'), t.identifier('contractAddress'))]), t.arrowFunctionExpression([], t.callExpression(t.memberExpression(t.identifier('client'), t.identifier(methodName)), args), false), 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()])))])))])));
66
+ })], t.tsTypeAnnotation(t.tsTypeReference(t.identifier(hookParamsTypeName))))], t.blockStatement([t.returnStatement(callExpression(t.identifier('useQuery'), [t.arrayExpression([t.stringLiteral(hookKeyName), t.optionalMemberExpression(t.identifier('client'), t.identifier('contractAddress'), false, options.optionalClient)]), t.arrowFunctionExpression([], optionalConditionalExpression(t.identifier('client'), t.callExpression(t.memberExpression(t.identifier('client'), t.identifier(methodName)), args), t.identifier('undefined'), 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.optionalMemberExpression(t.identifier('options'), t.identifier('enabled'), false, true)))]) : t.identifier('options')], 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()])))])))])));
56
67
  };
57
68
  export const createReactQueryHookInterface = ({
58
69
  QueryClient,
59
70
  hookParamsTypeName,
60
71
  responseType,
61
- jsonschema
72
+ jsonschema,
73
+ options = DEFAULT_OPTIONS
62
74
  }) => {
63
- const body = [t.tsPropertySignature(t.identifier('client'), t.tsTypeAnnotation(t.tsTypeReference(t.identifier(QueryClient)))), tsPropertySignature(t.identifier('options'), t.tsTypeAnnotation(t.tsTypeReference(t.identifier('UseQueryOptions'), 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()])))]))), true)];
75
+ const body = [tsPropertySignature(t.identifier('client'), t.tsTypeAnnotation(t.tsTypeReference(t.identifier(QueryClient))), options.optionalClient), tsPropertySignature(t.identifier('options'), t.tsTypeAnnotation(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()])))]))), true)];
64
76
  const props = getProps(jsonschema, true);
65
77
 
66
78
  if (props.length) {
@@ -21,6 +21,22 @@ const printCode = ast => {
21
21
 
22
22
 
23
23
  it('createReactQueryHooks', () => {
24
- expectCode(t.program(createReactQueryHooks(query_msg, 'Sg721', 'Sg721QueryClient')));
25
- expectCode(t.program(createReactQueryHooks(query_msg, 'Sg721', 'Sg721QueryClient')));
24
+ expectCode(t.program(createReactQueryHooks({
25
+ queryMsg: query_msg,
26
+ contractName: 'Sg721',
27
+ QueryClient: 'Sg721QueryClient'
28
+ })));
29
+ expectCode(t.program(createReactQueryHooks({
30
+ queryMsg: query_msg,
31
+ contractName: 'Sg721',
32
+ QueryClient: 'Sg721QueryClient'
33
+ })));
34
+ expectCode(t.program(createReactQueryHooks({
35
+ queryMsg: query_msg,
36
+ contractName: 'Sg721',
37
+ QueryClient: 'Sg721QueryClient',
38
+ options: {
39
+ optionalClient: true
40
+ }
41
+ })));
26
42
  });
@@ -153,4 +153,15 @@ export const memberExpressionOrIdentifierSnake = names => {
153
153
 
154
154
  const [name, ...rest] = names;
155
155
  return t.memberExpression(memberExpressionOrIdentifierSnake(rest), t.identifier(snake(name)));
156
+ };
157
+ /**
158
+ * If optional, return a conditional, otherwise just the expression
159
+ */
160
+
161
+ export const optionalConditionalExpression = (test, expression, alternate, optional = false) => {
162
+ return optional ? t.conditionalExpression(test, expression, alternate) : expression;
163
+ };
164
+ export const typeRefOrOptionalUnion = (identifier, optional = false) => {
165
+ const typeReference = t.tsTypeReference(identifier);
166
+ return optional ? t.tsUnionType([typeReference, t.tsUndefinedKeyword()]) : typeReference;
156
167
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wasm-ast-types",
3
- "version": "0.3.7",
3
+ "version": "0.4.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",
@@ -84,5 +84,5 @@
84
84
  "ast-stringify": "0.1.0",
85
85
  "case": "1.6.3"
86
86
  },
87
- "gitHead": "1f32ebfe7996630272adbbb54a324ea4a0699267"
87
+ "gitHead": "bf5da4cc0e89f0d718d4d02fcc00d3db5424321b"
88
88
  }
@@ -1,5 +1,12 @@
1
1
  import * as t from '@babel/types';
2
2
  import { QueryMsg } from './types';
3
+
4
+ interface ReactQueryHooks {
5
+ queryMsg: QueryMsg
6
+ contractName: string
7
+ QueryClient: string
8
+ options?: ReactQueryOptions
9
+ }
3
10
  interface ReactQueryHookQuery {
4
11
  hookName: string;
5
12
  hookParamsTypeName: string;
@@ -8,7 +15,11 @@ interface ReactQueryHookQuery {
8
15
  methodName: string;
9
16
  jsonschema: any;
10
17
  }
11
- export declare const createReactQueryHooks: (queryMsg: QueryMsg, contractName: string, QueryClient: string) => any;
18
+
19
+ export declare interface ReactQueryOptions {
20
+ optionalClient?: boolean
21
+ }
22
+ export declare const createReactQueryHooks: ({ queryMsg, contractName, QueryClient, options }: ReactQueryHooks) => any;
12
23
  export declare const createReactQueryHook: ({ hookName, hookParamsTypeName, responseType, hookKeyName, methodName, jsonschema }: ReactQueryHookQuery) => t.ExportNamedDeclaration;
13
24
  interface ReactQueryHookQueryInterface {
14
25
  QueryClient: string;
@@ -17,4 +28,4 @@ interface ReactQueryHookQueryInterface {
17
28
  jsonschema: any;
18
29
  }
19
30
  export declare const createReactQueryHookInterface: ({ QueryClient, hookParamsTypeName, responseType, jsonschema }: ReactQueryHookQueryInterface) => t.ExportNamedDeclaration;
20
- export {};
31
+ export { };