wasm-ast-types 0.0.11 → 0.1.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
@@ -23,6 +23,13 @@ function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "functio
23
23
 
24
24
  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; }
25
25
 
26
+ var getMessageProperties = function getMessageProperties(msg) {
27
+ if (msg.anyOf) return msg.anyOf;
28
+ if (msg.oneOf) return msg.oneOf;
29
+ if (msg.allOf) return msg.allOf;
30
+ throw new Error('Message case not handled yet, contact maintainers.');
31
+ };
32
+
26
33
  var getTypeFromRef = function getTypeFromRef($ref) {
27
34
  switch ($ref) {
28
35
  case '#/definitions/Binary':
@@ -65,7 +72,7 @@ var getType = function getType(type) {
65
72
  };
66
73
 
67
74
  var getPropertyType = function getPropertyType(schema, prop) {
68
- var _schema$properties, _schema$required;
75
+ var _schema$properties, _schema$required, _schema$required2;
69
76
 
70
77
  var props = (_schema$properties = schema.properties) !== null && _schema$properties !== void 0 ? _schema$properties : {};
71
78
  var info = props[prop];
@@ -132,20 +139,16 @@ var getPropertyType = function getPropertyType(schema, prop) {
132
139
  throw new Error('cannot find type for ' + JSON.stringify(info));
133
140
  }
134
141
 
142
+ if ((_schema$required2 = schema.required) !== null && _schema$required2 !== void 0 && _schema$required2.includes(prop)) {
143
+ optional = false;
144
+ }
145
+
135
146
  return {
136
147
  type: type,
137
148
  optional: optional
138
149
  };
139
150
  };
140
151
 
141
- var getProperty = function getProperty(schema, prop) {
142
- var _getPropertyType = getPropertyType(schema, prop),
143
- type = _getPropertyType.type,
144
- optional = _getPropertyType.optional;
145
-
146
- return (0, _utils.typedIdentifier)((0, _case.camel)(prop), t.tsTypeAnnotation(type), optional);
147
- };
148
-
149
152
  var createWasmQueryMethod = function createWasmQueryMethod(jsonschema) {
150
153
  var _jsonschema$propertie;
151
154
 
@@ -164,13 +167,13 @@ var createWasmQueryMethod = function createWasmQueryMethod(jsonschema) {
164
167
  exports.createWasmQueryMethod = createWasmQueryMethod;
165
168
 
166
169
  var createQueryClass = function createQueryClass(className, implementsClassName, queryMsg) {
167
- var propertyNames = queryMsg.oneOf.map(function (method) {
170
+ var propertyNames = getMessageProperties(queryMsg).map(function (method) {
168
171
  var _Object$keys;
169
172
 
170
173
  return (_Object$keys = Object.keys(method.properties)) === null || _Object$keys === void 0 ? void 0 : _Object$keys[0];
171
174
  }).filter(Boolean);
172
175
  var bindings = propertyNames.map(_case.camel).map(_utils.bindMethod);
173
- var methods = queryMsg.oneOf.map(function (schema) {
176
+ var methods = getMessageProperties(queryMsg).map(function (schema) {
174
177
  return createWasmQueryMethod(schema);
175
178
  });
176
179
  return t.exportNamedDeclaration((0, _utils.classDeclaration)(className, [// client
@@ -200,13 +203,13 @@ var createWasmExecMethod = function createWasmExecMethod(jsonschema) {
200
203
  exports.createWasmExecMethod = createWasmExecMethod;
201
204
 
202
205
  var createExecuteClass = function createExecuteClass(className, implementsClassName, extendsClassName, execMsg) {
203
- var propertyNames = execMsg.oneOf.map(function (method) {
206
+ var propertyNames = getMessageProperties(execMsg).map(function (method) {
204
207
  var _Object$keys2;
205
208
 
206
209
  return (_Object$keys2 = Object.keys(method.properties)) === null || _Object$keys2 === void 0 ? void 0 : _Object$keys2[0];
207
210
  }).filter(Boolean);
208
211
  var bindings = propertyNames.map(_case.camel).map(_utils.bindMethod);
209
- var methods = execMsg.oneOf.map(function (schema) {
212
+ var methods = getMessageProperties(execMsg).map(function (schema) {
210
213
  return createWasmExecMethod(schema);
211
214
  });
212
215
  var blockStmt = [];
@@ -228,7 +231,7 @@ var createExecuteClass = function createExecuteClass(className, implementsClassN
228
231
  exports.createExecuteClass = createExecuteClass;
229
232
 
230
233
  var createExecuteInterface = function createExecuteInterface(className, extendsClassName, execMsg) {
231
- var methods = execMsg.oneOf.map(function (jsonschema) {
234
+ var methods = getMessageProperties(execMsg).map(function (jsonschema) {
232
235
  var underscoreName = Object.keys(jsonschema.properties)[0];
233
236
  var methodName = (0, _case.camel)(underscoreName);
234
237
  return createPropertyFunctionWithObjectParams(methodName, 'ExecuteResult', jsonschema.properties[underscoreName]);
@@ -241,6 +244,26 @@ var createExecuteInterface = function createExecuteInterface(className, extendsC
241
244
 
242
245
  exports.createExecuteInterface = createExecuteInterface;
243
246
 
247
+ var propertySignature = function propertySignature(name, typeAnnotation) {
248
+ var optional = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
249
+ // prop.leadingComments = [{
250
+ // type: 'Comment',
251
+ // value: ' Data on the token itself'
252
+ // }];
253
+ // prop.leadingComments = [{
254
+ // type: 'CommentBlock',
255
+ // value: '* Data on the token itself'
256
+ // }];
257
+ return {
258
+ type: 'TSPropertySignature',
259
+ key: t.identifier(name),
260
+ typeAnnotation: typeAnnotation,
261
+ optional: optional
262
+ };
263
+ };
264
+
265
+ exports.propertySignature = propertySignature;
266
+
244
267
  var createTypedObjectParams = function createTypedObjectParams(jsonschema) {
245
268
  var _jsonschema$propertie3;
246
269
 
@@ -248,11 +271,11 @@ var createTypedObjectParams = function createTypedObjectParams(jsonschema) {
248
271
  var keys = Object.keys((_jsonschema$propertie3 = jsonschema.properties) !== null && _jsonschema$propertie3 !== void 0 ? _jsonschema$propertie3 : {});
249
272
  if (!keys.length) return;
250
273
  var typedParams = keys.map(function (prop) {
251
- var _getPropertyType2 = getPropertyType(jsonschema, prop),
252
- type = _getPropertyType2.type,
253
- optional = _getPropertyType2.optional;
274
+ var _getPropertyType = getPropertyType(jsonschema, prop),
275
+ type = _getPropertyType.type,
276
+ optional = _getPropertyType.optional;
254
277
 
255
- return t.tsPropertySignature(camelize ? t.identifier((0, _case.camel)(prop)) : t.identifier(prop), t.tsTypeAnnotation(type));
278
+ return propertySignature(camelize ? (0, _case.camel)(prop) : prop, t.tsTypeAnnotation(type), optional);
256
279
  });
257
280
  var params = keys.map(function (prop) {
258
281
  return t.objectProperty(camelize ? t.identifier((0, _case.camel)(prop)) : t.identifier(prop), camelize ? t.identifier((0, _case.camel)(prop)) : t.identifier(prop), false, true);
@@ -277,7 +300,7 @@ var createPropertyFunctionWithObjectParams = function createPropertyFunctionWith
277
300
  exports.createPropertyFunctionWithObjectParams = createPropertyFunctionWithObjectParams;
278
301
 
279
302
  var createQueryInterface = function createQueryInterface(className, queryMsg) {
280
- var methods = queryMsg.oneOf.map(function (jsonschema) {
303
+ var methods = getMessageProperties(queryMsg).map(function (jsonschema) {
281
304
  var underscoreName = Object.keys(jsonschema.properties)[0];
282
305
  var methodName = (0, _case.camel)(underscoreName);
283
306
  var responseType = (0, _case.pascal)("".concat(methodName, "Response"));
@@ -289,22 +312,6 @@ var createQueryInterface = function createQueryInterface(className, queryMsg) {
289
312
 
290
313
  exports.createQueryInterface = createQueryInterface;
291
314
 
292
- var propertySignature = function propertySignature(name, typeAnnotation) {
293
- var optional = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
294
- var prop = t.tsPropertySignature(t.identifier(name), typeAnnotation); // prop.leadingComments = [{
295
- // type: 'Comment',
296
- // value: ' Data on the token itself'
297
- // }];
298
- // prop.leadingComments = [{
299
- // type: 'CommentBlock',
300
- // value: '* Data on the token itself'
301
- // }];
302
-
303
- return prop;
304
- };
305
-
306
- exports.propertySignature = propertySignature;
307
-
308
315
  var createTypeOrInterface = function createTypeOrInterface(Type, jsonschema) {
309
316
  var _jsonschema$propertie4;
310
317
 
@@ -313,9 +320,9 @@ var createTypeOrInterface = function createTypeOrInterface(Type, jsonschema) {
313
320
  }
314
321
 
315
322
  var props = Object.keys((_jsonschema$propertie4 = jsonschema.properties) !== null && _jsonschema$propertie4 !== void 0 ? _jsonschema$propertie4 : {}).map(function (prop) {
316
- var _getPropertyType3 = getPropertyType(jsonschema, prop),
317
- type = _getPropertyType3.type,
318
- optional = _getPropertyType3.optional;
323
+ var _getPropertyType2 = getPropertyType(jsonschema, prop),
324
+ type = _getPropertyType2.type,
325
+ optional = _getPropertyType2.optional;
319
326
 
320
327
  return propertySignature((0, _case.camel)(prop), t.tsTypeAnnotation(type), optional);
321
328
  });
package/module/wasm.js CHANGED
@@ -2,6 +2,13 @@ import * as t from '@babel/types';
2
2
  import { camel, pascal } from 'case';
3
3
  import { bindMethod, typedIdentifier, promiseTypeAnnotation, classDeclaration, classProperty, arrowFunctionExpression } from './utils';
4
4
 
5
+ const getMessageProperties = msg => {
6
+ if (msg.anyOf) return msg.anyOf;
7
+ if (msg.oneOf) return msg.oneOf;
8
+ if (msg.allOf) return msg.allOf;
9
+ throw new Error('Message case not handled yet, contact maintainers.');
10
+ };
11
+
5
12
  const getTypeFromRef = $ref => {
6
13
  switch ($ref) {
7
14
  case '#/definitions/Binary':
@@ -105,20 +112,16 @@ const getPropertyType = (schema, prop) => {
105
112
  throw new Error('cannot find type for ' + JSON.stringify(info));
106
113
  }
107
114
 
115
+ if (schema.required?.includes(prop)) {
116
+ optional = false;
117
+ }
118
+
108
119
  return {
109
120
  type,
110
121
  optional
111
122
  };
112
123
  };
113
124
 
114
- const getProperty = (schema, prop) => {
115
- const {
116
- type,
117
- optional
118
- } = getPropertyType(schema, prop);
119
- return typedIdentifier(camel(prop), t.tsTypeAnnotation(type), optional);
120
- };
121
-
122
125
  export const createWasmQueryMethod = jsonschema => {
123
126
  const underscoreName = Object.keys(jsonschema.properties)[0];
124
127
  const methodName = camel(underscoreName);
@@ -132,9 +135,9 @@ export const createWasmQueryMethod = jsonschema => {
132
135
  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));
133
136
  };
134
137
  export const createQueryClass = (className, implementsClassName, queryMsg) => {
135
- const propertyNames = queryMsg.oneOf.map(method => Object.keys(method.properties)?.[0]).filter(Boolean);
138
+ const propertyNames = getMessageProperties(queryMsg).map(method => Object.keys(method.properties)?.[0]).filter(Boolean);
136
139
  const bindings = propertyNames.map(camel).map(bindMethod);
137
- const methods = queryMsg.oneOf.map(schema => {
140
+ const methods = getMessageProperties(queryMsg).map(schema => {
138
141
  return createWasmQueryMethod(schema);
139
142
  });
140
143
  return t.exportNamedDeclaration(classDeclaration(className, [// client
@@ -156,9 +159,9 @@ export const createWasmExecMethod = jsonschema => {
156
159
  t.tsTypeAnnotation(t.tsTypeReference(t.identifier('Promise'), t.tsTypeParameterInstantiation([t.tSTypeReference(t.identifier('ExecuteResult'))]))), true));
157
160
  };
158
161
  export const createExecuteClass = (className, implementsClassName, extendsClassName, execMsg) => {
159
- const propertyNames = execMsg.oneOf.map(method => Object.keys(method.properties)?.[0]).filter(Boolean);
162
+ const propertyNames = getMessageProperties(execMsg).map(method => Object.keys(method.properties)?.[0]).filter(Boolean);
160
163
  const bindings = propertyNames.map(camel).map(bindMethod);
161
- const methods = execMsg.oneOf.map(schema => {
164
+ const methods = getMessageProperties(execMsg).map(schema => {
162
165
  return createWasmExecMethod(schema);
163
166
  });
164
167
  const blockStmt = [];
@@ -177,7 +180,7 @@ export const createExecuteClass = (className, implementsClassName, extendsClassN
177
180
  t.classMethod('constructor', t.identifier('constructor'), [typedIdentifier('client', t.tsTypeAnnotation(t.tsTypeReference(t.identifier('SigningCosmWasmClient')))), typedIdentifier('sender', t.tsTypeAnnotation(t.tsStringKeyword())), typedIdentifier('contractAddress', t.tsTypeAnnotation(t.tsStringKeyword()))], t.blockStatement(blockStmt)), ...methods], [t.tSExpressionWithTypeArguments(t.identifier(implementsClassName))], extendsClassName ? t.identifier(extendsClassName) : null));
178
181
  };
179
182
  export const createExecuteInterface = (className, extendsClassName, execMsg) => {
180
- const methods = execMsg.oneOf.map(jsonschema => {
183
+ const methods = getMessageProperties(execMsg).map(jsonschema => {
181
184
  const underscoreName = Object.keys(jsonschema.properties)[0];
182
185
  const methodName = camel(underscoreName);
183
186
  return createPropertyFunctionWithObjectParams(methodName, 'ExecuteResult', jsonschema.properties[underscoreName]);
@@ -187,6 +190,22 @@ export const createExecuteInterface = (className, extendsClassName, execMsg) =>
187
190
  t.tSPropertySignature(t.identifier('contractAddress'), t.tsTypeAnnotation(t.tsStringKeyword())), // contract address
188
191
  t.tSPropertySignature(t.identifier('sender'), t.tsTypeAnnotation(t.tsStringKeyword())), ...methods])));
189
192
  };
193
+ export const propertySignature = (name, typeAnnotation, optional = false) => {
194
+ // prop.leadingComments = [{
195
+ // type: 'Comment',
196
+ // value: ' Data on the token itself'
197
+ // }];
198
+ // prop.leadingComments = [{
199
+ // type: 'CommentBlock',
200
+ // value: '* Data on the token itself'
201
+ // }];
202
+ return {
203
+ type: 'TSPropertySignature',
204
+ key: t.identifier(name),
205
+ typeAnnotation,
206
+ optional
207
+ };
208
+ };
190
209
  export const createTypedObjectParams = (jsonschema, camelize = true) => {
191
210
  const keys = Object.keys(jsonschema.properties ?? {});
192
211
  if (!keys.length) return;
@@ -195,7 +214,7 @@ export const createTypedObjectParams = (jsonschema, camelize = true) => {
195
214
  type,
196
215
  optional
197
216
  } = getPropertyType(jsonschema, prop);
198
- return t.tsPropertySignature(camelize ? t.identifier(camel(prop)) : t.identifier(prop), t.tsTypeAnnotation(type));
217
+ return propertySignature(camelize ? camel(prop) : prop, t.tsTypeAnnotation(type), optional);
199
218
  });
200
219
  const params = keys.map(prop => {
201
220
  return t.objectProperty(camelize ? t.identifier(camel(prop)) : t.identifier(prop), camelize ? t.identifier(camel(prop)) : t.identifier(prop), false, true);
@@ -214,7 +233,7 @@ export const createPropertyFunctionWithObjectParams = (methodName, responseType,
214
233
  return t.tSPropertySignature(t.identifier(methodName), t.tsTypeAnnotation(func));
215
234
  };
216
235
  export const createQueryInterface = (className, queryMsg) => {
217
- const methods = queryMsg.oneOf.map(jsonschema => {
236
+ const methods = getMessageProperties(queryMsg).map(jsonschema => {
218
237
  const underscoreName = Object.keys(jsonschema.properties)[0];
219
238
  const methodName = camel(underscoreName);
220
239
  const responseType = pascal(`${methodName}Response`);
@@ -223,18 +242,6 @@ export const createQueryInterface = (className, queryMsg) => {
223
242
  });
224
243
  return t.exportNamedDeclaration(t.tsInterfaceDeclaration(t.identifier(className), null, [], t.tSInterfaceBody([t.tSPropertySignature(t.identifier('contractAddress'), t.tsTypeAnnotation(t.tsStringKeyword())), ...methods])));
225
244
  };
226
- export const propertySignature = (name, typeAnnotation, optional = false) => {
227
- const prop = t.tsPropertySignature(t.identifier(name), typeAnnotation); // prop.leadingComments = [{
228
- // type: 'Comment',
229
- // value: ' Data on the token itself'
230
- // }];
231
- // prop.leadingComments = [{
232
- // type: 'CommentBlock',
233
- // value: '* Data on the token itself'
234
- // }];
235
-
236
- return prop;
237
- };
238
245
  export const createTypeOrInterface = (Type, jsonschema) => {
239
246
  if (jsonschema.type !== 'object') {
240
247
  return t.exportNamedDeclaration(t.tsTypeAliasDeclaration(t.identifier(Type), null, getType(jsonschema.type)));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wasm-ast-types",
3
- "version": "0.0.11",
3
+ "version": "0.1.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": "972a1b8c212c531cd23d7f15ba8944841e938ba9"
88
+ "gitHead": "205c770b11ce9ea868d58af0567e5ff006cc5381"
89
89
  }