wasm-ast-types 0.2.0 → 0.2.1

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.
package/main/wasm.js CHANGED
@@ -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.propertySignature = exports.getPropertyType = exports.createWasmQueryMethod = exports.createWasmExecMethod = exports.createTypedObjectParams = exports.createTypeOrInterface = exports.createTypeInterface = exports.createQueryInterface = exports.createQueryClass = exports.createPropertyFunctionWithObjectParams = exports.createExecuteInterface = exports.createExecuteClass = void 0;
10
+ exports.propertySignature = exports.identifier = exports.getPropertyType = exports.createWasmQueryMethod = exports.createWasmExecMethod = exports.createTypedObjectParams = exports.createTypeOrInterface = exports.createTypeInterface = exports.createQueryInterface = exports.createQueryClass = exports.createPropertyFunctionWithObjectParamsForExec = exports.createPropertyFunctionWithObjectParams = exports.createExecuteInterface = exports.createExecuteClass = void 0;
11
11
 
12
12
  var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
13
13
 
@@ -48,6 +48,16 @@ var getArrayTypeFromType = function getArrayTypeFromType(type) {
48
48
  return t.tsArrayType(getType(type));
49
49
  };
50
50
 
51
+ var identifier = function identifier(name, typeAnnotation) {
52
+ var optional = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
53
+ var type = t.identifier(name);
54
+ type.typeAnnotation = typeAnnotation;
55
+ type.optional = optional;
56
+ return type;
57
+ };
58
+
59
+ exports.identifier = identifier;
60
+
51
61
  var getType = function getType(type) {
52
62
  switch (type) {
53
63
  case 'string':
@@ -180,6 +190,12 @@ var createQueryClass = function createQueryClass(className, implementsClassName,
180
190
 
181
191
  exports.createQueryClass = createQueryClass;
182
192
 
193
+ var tsTypeOperator = function tsTypeOperator(typeAnnotation, operator) {
194
+ var obj = t.tsTypeOperator(typeAnnotation);
195
+ obj.operator = operator;
196
+ return obj;
197
+ };
198
+
183
199
  var createWasmExecMethod = function createWasmExecMethod(jsonschema) {
184
200
  var _jsonschema$propertie2;
185
201
 
@@ -191,7 +207,7 @@ var createWasmExecMethod = function createWasmExecMethod(jsonschema) {
191
207
  return t.objectProperty(t.identifier(prop), t.identifier((0, _case.camel)(prop)), false, prop === (0, _case.camel)(prop));
192
208
  });
193
209
  return t.classProperty(t.identifier(methodName), (0, _utils.arrowFunctionExpression)(obj ? [// props
194
- obj] : [], 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((0, _toConsumableArray2["default"])(args)))]), t.stringLiteral('auto')])))]), // return type
210
+ obj, t.assignmentPattern(identifier('fee', t.tsTypeAnnotation(t.tsUnionType([t.tSNumberKeyword(), t.tsTypeReference(t.identifier('StdFee'))])), false), t.stringLiteral('auto')), identifier('memo', t.tsTypeAnnotation(t.tsStringKeyword()), true), identifier('funds', t.tsTypeAnnotation(tsTypeOperator(t.tsArrayType(t.tsTypeReference(t.identifier('Coin'))), 'readonly')), true)] : [], 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((0, _toConsumableArray2["default"])(args)))]), t.identifier('fee'), t.identifier('memo'), t.identifier('funds')])))]), // return type
195
211
  t.tsTypeAnnotation(t.tsTypeReference(t.identifier('Promise'), t.tsTypeParameterInstantiation([t.tSTypeReference(t.identifier('ExecuteResult'))]))), true));
196
212
  };
197
213
 
@@ -229,7 +245,7 @@ var createExecuteInterface = function createExecuteInterface(className, extendsC
229
245
  var methods = (0, _utils.getMessageProperties)(execMsg).map(function (jsonschema) {
230
246
  var underscoreName = Object.keys(jsonschema.properties)[0];
231
247
  var methodName = (0, _case.camel)(underscoreName);
232
- return createPropertyFunctionWithObjectParams(methodName, 'ExecuteResult', jsonschema.properties[underscoreName]);
248
+ return createPropertyFunctionWithObjectParamsForExec(methodName, 'ExecuteResult', jsonschema.properties[underscoreName]);
233
249
  });
234
250
  var extendsAst = extendsClassName ? [t.tSExpressionWithTypeArguments(t.identifier(extendsClassName))] : [];
235
251
  return t.exportNamedDeclaration(t.tsInterfaceDeclaration(t.identifier(className), null, extendsAst, t.tSInterfaceBody([// contract address
@@ -294,6 +310,19 @@ var createPropertyFunctionWithObjectParams = function createPropertyFunctionWith
294
310
 
295
311
  exports.createPropertyFunctionWithObjectParams = createPropertyFunctionWithObjectParams;
296
312
 
313
+ var createPropertyFunctionWithObjectParamsForExec = function createPropertyFunctionWithObjectParamsForExec(methodName, responseType, jsonschema) {
314
+ var obj = createTypedObjectParams(jsonschema);
315
+ var fixedParams = [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(tsTypeOperator(t.tsArrayType(t.tsTypeReference(t.identifier('Coin'))), 'readonly')), true)];
316
+ var func = {
317
+ type: 'TSFunctionType',
318
+ typeAnnotation: (0, _utils.promiseTypeAnnotation)(responseType),
319
+ parameters: obj ? [obj].concat(fixedParams) : fixedParams
320
+ };
321
+ return t.tSPropertySignature(t.identifier(methodName), t.tsTypeAnnotation(func));
322
+ };
323
+
324
+ exports.createPropertyFunctionWithObjectParamsForExec = createPropertyFunctionWithObjectParamsForExec;
325
+
297
326
  var createQueryInterface = function createQueryInterface(className, queryMsg) {
298
327
  var methods = (0, _utils.getMessageProperties)(queryMsg).map(function (jsonschema) {
299
328
  var underscoreName = Object.keys(jsonschema.properties)[0];
package/module/wasm.js CHANGED
@@ -27,6 +27,13 @@ const getArrayTypeFromType = type => {
27
27
  return t.tsArrayType(getType(type));
28
28
  };
29
29
 
30
+ export const identifier = (name, typeAnnotation, optional = false) => {
31
+ const type = t.identifier(name);
32
+ type.typeAnnotation = typeAnnotation;
33
+ type.optional = optional;
34
+ return type;
35
+ };
36
+
30
37
  const getType = type => {
31
38
  switch (type) {
32
39
  case 'string':
@@ -138,6 +145,13 @@ export const createQueryClass = (className, implementsClassName, queryMsg) => {
138
145
  t.classMethod('constructor', t.identifier('constructor'), [typedIdentifier('client', t.tsTypeAnnotation(t.tsTypeReference(t.identifier('CosmWasmClient')))), typedIdentifier('contractAddress', t.tsTypeAnnotation(t.tsStringKeyword()))], t.blockStatement([// client/contract set
139
146
  t.expressionStatement(t.assignmentExpression('=', t.memberExpression(t.thisExpression(), t.identifier('client')), t.identifier('client'))), t.expressionStatement(t.assignmentExpression('=', t.memberExpression(t.thisExpression(), t.identifier('contractAddress')), t.identifier('contractAddress'))), ...bindings])), ...methods], [t.tSExpressionWithTypeArguments(t.identifier(implementsClassName))]));
140
147
  };
148
+
149
+ const tsTypeOperator = (typeAnnotation, operator) => {
150
+ const obj = t.tsTypeOperator(typeAnnotation);
151
+ obj.operator = operator;
152
+ return obj;
153
+ };
154
+
141
155
  export const createWasmExecMethod = jsonschema => {
142
156
  const underscoreName = Object.keys(jsonschema.properties)[0];
143
157
  const methodName = camel(underscoreName);
@@ -147,7 +161,7 @@ export const createWasmExecMethod = jsonschema => {
147
161
  return t.objectProperty(t.identifier(prop), t.identifier(camel(prop)), false, prop === camel(prop));
148
162
  });
149
163
  return t.classProperty(t.identifier(methodName), arrowFunctionExpression(obj ? [// props
150
- obj] : [], 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.stringLiteral('auto')])))]), // return type
164
+ obj, t.assignmentPattern(identifier('fee', t.tsTypeAnnotation(t.tsUnionType([t.tSNumberKeyword(), t.tsTypeReference(t.identifier('StdFee'))])), false), t.stringLiteral('auto')), identifier('memo', t.tsTypeAnnotation(t.tsStringKeyword()), true), identifier('funds', t.tsTypeAnnotation(tsTypeOperator(t.tsArrayType(t.tsTypeReference(t.identifier('Coin'))), 'readonly')), true)] : [], 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
151
165
  t.tsTypeAnnotation(t.tsTypeReference(t.identifier('Promise'), t.tsTypeParameterInstantiation([t.tSTypeReference(t.identifier('ExecuteResult'))]))), true));
152
166
  };
153
167
  export const createExecuteClass = (className, implementsClassName, extendsClassName, execMsg) => {
@@ -175,12 +189,52 @@ export const createExecuteInterface = (className, extendsClassName, execMsg) =>
175
189
  const methods = getMessageProperties(execMsg).map(jsonschema => {
176
190
  const underscoreName = Object.keys(jsonschema.properties)[0];
177
191
  const methodName = camel(underscoreName);
178
- return createPropertyFunctionWithObjectParams(methodName, 'ExecuteResult', jsonschema.properties[underscoreName]);
192
+ return createPropertyFunctionWithObjectParamsForExec(methodName, 'ExecuteResult', jsonschema.properties[underscoreName]);
179
193
  });
180
194
  const extendsAst = extendsClassName ? [t.tSExpressionWithTypeArguments(t.identifier(extendsClassName))] : [];
181
195
  return t.exportNamedDeclaration(t.tsInterfaceDeclaration(t.identifier(className), null, extendsAst, t.tSInterfaceBody([// contract address
182
196
  t.tSPropertySignature(t.identifier('contractAddress'), t.tsTypeAnnotation(t.tsStringKeyword())), // contract address
183
- t.tSPropertySignature(t.identifier('sender'), t.tsTypeAnnotation(t.tsStringKeyword())), ...methods])));
197
+ t.tSPropertySignature(t.identifier('sender'), t.tsTypeAnnotation(t.tsStringKeyword())), ...methods // // fee
198
+ // t.tSPropertySignature(
199
+ // t.identifier('fee'),
200
+ // t.tsTypeAnnotation(
201
+ // t.tsUnionType(
202
+ // [
203
+ // t.tsNumberKeyword(),
204
+ // t.tsTypeReference(
205
+ // t.identifier('StdFee')
206
+ // ),
207
+ // t.tsLiteralType(
208
+ // t.stringLiteral('auto')
209
+ // )
210
+ // ]
211
+ // )
212
+ // )
213
+ // ),
214
+ // // memo
215
+ // propertySignature(
216
+ // 'memo',
217
+ // t.tsTypeAnnotation(
218
+ // t.tsStringKeyword()
219
+ // ),
220
+ // true
221
+ // ),
222
+ // // funds
223
+ // propertySignature(
224
+ // 'funds',
225
+ // t.tsTypeAnnotation(
226
+ // tsTypeOperator(
227
+ // t.tsArrayType(
228
+ // t.tsTypeReference(
229
+ // t.identifier('Coin')
230
+ // )
231
+ // ),
232
+ // 'readonly'
233
+ // )
234
+ // ),
235
+ // true
236
+ // )
237
+ ])));
184
238
  };
185
239
  export const propertySignature = (name, typeAnnotation, optional = false) => {
186
240
  // prop.leadingComments = [{
@@ -224,6 +278,16 @@ export const createPropertyFunctionWithObjectParams = (methodName, responseType,
224
278
  };
225
279
  return t.tSPropertySignature(t.identifier(methodName), t.tsTypeAnnotation(func));
226
280
  };
281
+ export const createPropertyFunctionWithObjectParamsForExec = (methodName, responseType, jsonschema) => {
282
+ const obj = createTypedObjectParams(jsonschema);
283
+ const fixedParams = [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(tsTypeOperator(t.tsArrayType(t.tsTypeReference(t.identifier('Coin'))), 'readonly')), true)];
284
+ const func = {
285
+ type: 'TSFunctionType',
286
+ typeAnnotation: promiseTypeAnnotation(responseType),
287
+ parameters: obj ? [obj, ...fixedParams] : fixedParams
288
+ };
289
+ return t.tSPropertySignature(t.identifier(methodName), t.tsTypeAnnotation(func));
290
+ };
227
291
  export const createQueryInterface = (className, queryMsg) => {
228
292
  const methods = getMessageProperties(queryMsg).map(jsonschema => {
229
293
  const underscoreName = Object.keys(jsonschema.properties)[0];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wasm-ast-types",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
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",
@@ -85,5 +85,5 @@
85
85
  "ast-stringify": "0.1.0",
86
86
  "case": "1.6.3"
87
87
  },
88
- "gitHead": "93a3bca65ba5aec086efeb3ae85950a346ddf0ff"
88
+ "gitHead": "1ba0a66724ba90b1107aaccb9d075e1c59401a69"
89
89
  }