wasm-ast-types
Advanced tools
Comparing version 0.2.5 to 0.2.6
@@ -14,4 +14,2 @@ "use strict"; | ||
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray")); | ||
var t = _interopRequireWildcard(require("@babel/types")); | ||
@@ -23,2 +21,6 @@ | ||
var _types2 = require("./utils/types"); | ||
var _babel = require("./utils/babel"); | ||
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); } | ||
@@ -28,136 +30,3 @@ | ||
var getTypeFromRef = function getTypeFromRef($ref) { | ||
switch ($ref) { | ||
case '#/definitions/Binary': | ||
return t.tsTypeReference(t.identifier('Binary')); | ||
default: | ||
if ($ref.startsWith('#/definitions/')) { | ||
return t.tsTypeReference(t.identifier($ref.replace('#/definitions/', ''))); | ||
} | ||
throw new Error('what is $ref: ' + $ref); | ||
} | ||
}; | ||
var getArrayTypeFromRef = function getArrayTypeFromRef($ref) { | ||
return t.tsArrayType(getTypeFromRef($ref)); | ||
}; | ||
var getArrayTypeFromType = function getArrayTypeFromType(type) { | ||
return t.tsArrayType(getType(type)); | ||
}; // MARKED AS NOT DRY | ||
var identifier = function identifier(name, typeAnnotation) { | ||
var optional = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false; | ||
var type = t.identifier(name); | ||
type.typeAnnotation = typeAnnotation; | ||
type.optional = optional; | ||
return type; | ||
}; | ||
var getType = function getType(type) { | ||
switch (type) { | ||
case 'string': | ||
return t.tsStringKeyword(); | ||
case 'boolean': | ||
return t.tSBooleanKeyword(); | ||
case 'integer': | ||
return t.tsNumberKeyword(); | ||
default: | ||
throw new Error('what is type: ' + type); | ||
} | ||
}; // MARKED AS NOT DRY | ||
var getPropertyType = function getPropertyType(schema, prop) { | ||
var _schema$properties, _schema$required, _schema$required2; | ||
var props = (_schema$properties = schema.properties) !== null && _schema$properties !== void 0 ? _schema$properties : {}; | ||
var info = props[prop]; | ||
var type = null; | ||
var optional = (_schema$required = schema.required) === null || _schema$required === void 0 ? void 0 : _schema$required.includes(prop); | ||
if (info.allOf && info.allOf.length === 1) { | ||
info = info.allOf[0]; | ||
} | ||
if (typeof info.$ref === 'string') { | ||
type = getTypeFromRef(info.$ref); | ||
} | ||
if (Array.isArray(info.anyOf)) { | ||
// assuming 2nd is null, but let's check to ensure | ||
if (info.anyOf.length !== 2) { | ||
throw new Error('case not handled by transpiler. contact maintainers.'); | ||
} | ||
var _info$anyOf = (0, _slicedToArray2["default"])(info.anyOf, 2), | ||
nullableType = _info$anyOf[0], | ||
nullType = _info$anyOf[1]; | ||
if ((nullType === null || nullType === void 0 ? void 0 : nullType.type) !== 'null') { | ||
throw new Error('case not handled by transpiler. contact maintainers.'); | ||
} | ||
type = getTypeFromRef(nullableType === null || nullableType === void 0 ? void 0 : nullableType.$ref); | ||
optional = true; | ||
} | ||
if (typeof info.type === 'string') { | ||
if (info.type === 'array') { | ||
if (info.items.$ref) { | ||
type = getArrayTypeFromRef(info.items.$ref); | ||
} else { | ||
type = getArrayTypeFromType(info.items.type); | ||
} | ||
} else { | ||
type = getType(info.type); | ||
} | ||
} | ||
if (Array.isArray(info.type)) { | ||
// assuming 2nd is null, but let's check to ensure | ||
if (info.type.length !== 2) { | ||
throw new Error('case not handled by transpiler. contact maintainers.'); | ||
} | ||
var _info$type = (0, _slicedToArray2["default"])(info.type, 2), | ||
_nullableType = _info$type[0], | ||
_nullType = _info$type[1]; | ||
if (_nullType !== 'null') { | ||
throw new Error('case not handled by transpiler. contact maintainers.'); | ||
} | ||
type = getType(_nullableType); | ||
optional = true; | ||
} | ||
if (!type) { | ||
throw new Error('cannot find type for ' + JSON.stringify(info)); | ||
} | ||
if ((_schema$required2 = schema.required) !== null && _schema$required2 !== void 0 && _schema$required2.includes(prop)) { | ||
optional = false; | ||
} | ||
return { | ||
type: type, | ||
optional: optional | ||
}; | ||
}; | ||
var tsTypeOperator = function tsTypeOperator(typeAnnotation, operator) { | ||
var obj = t.tsTypeOperator(typeAnnotation); | ||
obj.operator = operator; | ||
return obj; | ||
}; // MARKED AS NOT DRY | ||
var createWasmExecMethod = function createWasmExecMethod(jsonschema) { | ||
var createWasmExecMethodPartial = function createWasmExecMethodPartial(jsonschema) { | ||
var _jsonschema$propertie; | ||
@@ -195,3 +64,3 @@ | ||
// ), true), | ||
identifier('funds', t.tsTypeAnnotation(tsTypeOperator(t.tsArrayType(t.tsTypeReference(t.identifier('Coin'))), 'readonly')), true)]; | ||
(0, _babel.identifier)('funds', t.tsTypeAnnotation((0, _babel.tsTypeOperator)(t.tsArrayType(t.tsTypeReference(t.identifier('Coin'))), 'readonly')), true)]; | ||
return t.classProperty(t.identifier(methodName), (0, _utils.arrowFunctionExpression)(obj ? [// props | ||
@@ -210,3 +79,3 @@ obj].concat(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 | ||
var methods = (0, _utils.getMessageProperties)(execMsg).map(function (schema) { | ||
return createWasmExecMethod(schema); | ||
return createWasmExecMethodPartial(schema); | ||
}); | ||
@@ -238,21 +107,2 @@ var blockStmt = []; | ||
var propertySignature = function propertySignature(name, typeAnnotation) { | ||
var optional = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false; | ||
// prop.leadingComments = [{ | ||
// type: 'Comment', | ||
// value: ' Data on the token itself' | ||
// }]; | ||
// prop.leadingComments = [{ | ||
// type: 'CommentBlock', | ||
// value: '* Data on the token itself' | ||
// }]; | ||
return { | ||
type: 'TSPropertySignature', | ||
key: t.identifier(name), | ||
typeAnnotation: typeAnnotation, | ||
optional: optional | ||
}; | ||
}; // MARKED AS NOT DRY | ||
var createTypedObjectParams = function createTypedObjectParams(jsonschema) { | ||
@@ -265,7 +115,7 @@ var _jsonschema$propertie2; | ||
var typedParams = keys.map(function (prop) { | ||
var _getPropertyType = getPropertyType(jsonschema, prop), | ||
var _getPropertyType = (0, _types2.getPropertyType)(jsonschema, prop), | ||
type = _getPropertyType.type, | ||
optional = _getPropertyType.optional; | ||
return propertySignature(camelize ? (0, _case.camel)(prop) : prop, t.tsTypeAnnotation(type), optional); | ||
return (0, _babel.propertySignature)(camelize ? (0, _case.camel)(prop) : prop, t.tsTypeAnnotation(type), optional); | ||
}); | ||
@@ -298,3 +148,3 @@ var params = keys.map(function (prop) { | ||
// ), true), | ||
identifier('funds', t.tsTypeAnnotation(tsTypeOperator(t.tsArrayType(t.tsTypeReference(t.identifier('Coin'))), 'readonly')), true)]; | ||
(0, _babel.identifier)('funds', t.tsTypeAnnotation((0, _babel.tsTypeOperator)(t.tsArrayType(t.tsTypeReference(t.identifier('Coin'))), 'readonly')), true)]; | ||
var func = { | ||
@@ -301,0 +151,0 @@ type: 'TSFunctionType', |
@@ -7,3 +7,3 @@ "use strict"; | ||
var _execute_msg_for__empty = _interopRequireDefault(require("./__fixtures__/schema/execute_msg_for__empty.json")); | ||
var _execute_msg_for__empty = _interopRequireDefault(require("./../../../__fixtures__/basic/execute_msg_for__empty.json")); | ||
@@ -10,0 +10,0 @@ var _messages = require("./messages"); |
@@ -20,4 +20,6 @@ "use strict"; | ||
var _wasm = require("./wasm"); | ||
var _babel = require("./utils/babel"); | ||
var _types2 = require("./utils/types"); | ||
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); } | ||
@@ -108,8 +110,8 @@ | ||
return keys.map(function (prop) { | ||
var _getPropertyType = (0, _wasm.getPropertyType)(jsonschema, prop), | ||
var _getPropertyType = (0, _types2.getPropertyType)(jsonschema, prop), | ||
type = _getPropertyType.type, | ||
optional = _getPropertyType.optional; | ||
return (0, _wasm.propertySignature)(camelize ? (0, _case.camel)(prop) : prop, t.tsTypeAnnotation(type), optional); | ||
return (0, _babel.propertySignature)(camelize ? (0, _case.camel)(prop) : prop, t.tsTypeAnnotation(type), optional); | ||
}); | ||
}; |
@@ -11,3 +11,3 @@ "use strict"; | ||
var _query_msg = _interopRequireDefault(require("./__fixtures__/schema/query_msg.json")); | ||
var _query_msg = _interopRequireDefault(require("./../../../__fixtures__/basic/query_msg.json")); | ||
@@ -14,0 +14,0 @@ var _reactQuery = require("./react-query"); |
@@ -11,3 +11,3 @@ "use strict"; | ||
var _query_msg = _interopRequireDefault(require("./__fixtures__/schema/query_msg.json")); | ||
var _query_msg = _interopRequireDefault(require("./../../../__fixtures__/basic/query_msg.json")); | ||
@@ -14,0 +14,0 @@ var _recoil = require("./recoil"); |
176
main/wasm.js
@@ -10,8 +10,6 @@ "use strict"; | ||
}); | ||
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; | ||
exports.createWasmQueryMethod = exports.createWasmExecMethod = exports.createTypedObjectParams = exports.createTypeOrInterface = exports.createTypeInterface = exports.createQueryInterface = exports.createQueryClass = exports.createPropertyFunctionWithObjectParamsForExec = exports.createPropertyFunctionWithObjectParams = exports.createExecuteInterface = exports.createExecuteClass = void 0; | ||
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray")); | ||
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray")); | ||
var t = _interopRequireWildcard(require("@babel/types")); | ||
@@ -23,2 +21,6 @@ | ||
var _types2 = require("./utils/types"); | ||
var _babel = require("./utils/babel"); | ||
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); } | ||
@@ -28,130 +30,2 @@ | ||
var getTypeFromRef = function getTypeFromRef($ref) { | ||
switch ($ref) { | ||
case '#/definitions/Binary': | ||
return t.tsTypeReference(t.identifier('Binary')); | ||
default: | ||
if ($ref.startsWith('#/definitions/')) { | ||
return t.tsTypeReference(t.identifier($ref.replace('#/definitions/', ''))); | ||
} | ||
throw new Error('what is $ref: ' + $ref); | ||
} | ||
}; | ||
var getArrayTypeFromRef = function getArrayTypeFromRef($ref) { | ||
return t.tsArrayType(getTypeFromRef($ref)); | ||
}; | ||
var getArrayTypeFromType = function getArrayTypeFromType(type) { | ||
return t.tsArrayType(getType(type)); | ||
}; | ||
var identifier = function identifier(name, typeAnnotation) { | ||
var optional = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false; | ||
var type = t.identifier(name); | ||
type.typeAnnotation = typeAnnotation; | ||
type.optional = optional; | ||
return type; | ||
}; | ||
exports.identifier = identifier; | ||
var getType = function getType(type) { | ||
switch (type) { | ||
case 'string': | ||
return t.tsStringKeyword(); | ||
case 'boolean': | ||
return t.tSBooleanKeyword(); | ||
case 'integer': | ||
return t.tsNumberKeyword(); | ||
default: | ||
throw new Error('what is type: ' + type); | ||
} | ||
}; | ||
var getPropertyType = function getPropertyType(schema, prop) { | ||
var _schema$properties, _schema$required, _schema$required2; | ||
var props = (_schema$properties = schema.properties) !== null && _schema$properties !== void 0 ? _schema$properties : {}; | ||
var info = props[prop]; | ||
var type = null; | ||
var optional = (_schema$required = schema.required) === null || _schema$required === void 0 ? void 0 : _schema$required.includes(prop); | ||
if (info.allOf && info.allOf.length === 1) { | ||
info = info.allOf[0]; | ||
} | ||
if (typeof info.$ref === 'string') { | ||
type = getTypeFromRef(info.$ref); | ||
} | ||
if (Array.isArray(info.anyOf)) { | ||
// assuming 2nd is null, but let's check to ensure | ||
if (info.anyOf.length !== 2) { | ||
throw new Error('case not handled by transpiler. contact maintainers.'); | ||
} | ||
var _info$anyOf = (0, _slicedToArray2["default"])(info.anyOf, 2), | ||
nullableType = _info$anyOf[0], | ||
nullType = _info$anyOf[1]; | ||
if ((nullType === null || nullType === void 0 ? void 0 : nullType.type) !== 'null') { | ||
throw new Error('case not handled by transpiler. contact maintainers.'); | ||
} | ||
type = getTypeFromRef(nullableType === null || nullableType === void 0 ? void 0 : nullableType.$ref); | ||
optional = true; | ||
} | ||
if (typeof info.type === 'string') { | ||
if (info.type === 'array') { | ||
if (info.items.$ref) { | ||
type = getArrayTypeFromRef(info.items.$ref); | ||
} else { | ||
type = getArrayTypeFromType(info.items.type); | ||
} | ||
} else { | ||
type = getType(info.type); | ||
} | ||
} | ||
if (Array.isArray(info.type)) { | ||
// assuming 2nd is null, but let's check to ensure | ||
if (info.type.length !== 2) { | ||
throw new Error('case not handled by transpiler. contact maintainers.'); | ||
} | ||
var _info$type = (0, _slicedToArray2["default"])(info.type, 2), | ||
_nullableType = _info$type[0], | ||
_nullType = _info$type[1]; | ||
if (_nullType !== 'null') { | ||
throw new Error('case not handled by transpiler. contact maintainers.'); | ||
} | ||
type = getType(_nullableType); | ||
optional = true; | ||
} | ||
if (!type) { | ||
throw new Error('cannot find type for ' + JSON.stringify(info)); | ||
} | ||
if ((_schema$required2 = schema.required) !== null && _schema$required2 !== void 0 && _schema$required2.includes(prop)) { | ||
optional = false; | ||
} | ||
return { | ||
type: type, | ||
optional: optional | ||
}; | ||
}; | ||
exports.getPropertyType = getPropertyType; | ||
var createWasmQueryMethod = function createWasmQueryMethod(jsonschema) { | ||
@@ -193,8 +67,2 @@ var _jsonschema$propertie; | ||
var tsTypeOperator = function tsTypeOperator(typeAnnotation, operator) { | ||
var obj = t.tsTypeOperator(typeAnnotation); | ||
obj.operator = operator; | ||
return obj; | ||
}; | ||
var createWasmExecMethod = function createWasmExecMethod(jsonschema) { | ||
@@ -210,3 +78,3 @@ var _jsonschema$propertie2; | ||
}); | ||
var constantParams = [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(tsTypeOperator(t.tsArrayType(t.tsTypeReference(t.identifier('Coin'))), 'readonly')), true)]; | ||
var constantParams = [t.assignmentPattern((0, _babel.identifier)('fee', t.tsTypeAnnotation(t.tsUnionType([t.tSNumberKeyword(), t.tsTypeReference(t.identifier('StdFee')), t.tsLiteralType(t.stringLiteral('auto'))])), false), t.stringLiteral('auto')), (0, _babel.identifier)('memo', t.tsTypeAnnotation(t.tsStringKeyword()), true), (0, _babel.identifier)('funds', t.tsTypeAnnotation((0, _babel.tsTypeOperator)(t.tsArrayType(t.tsTypeReference(t.identifier('Coin'))), 'readonly')), true)]; | ||
return t.classProperty(t.identifier(methodName), (0, _utils.arrowFunctionExpression)(obj ? [// props | ||
@@ -261,22 +129,2 @@ obj].concat(constantParams) : constantParams, 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 | ||
var propertySignature = function propertySignature(name, typeAnnotation) { | ||
var optional = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false; | ||
// prop.leadingComments = [{ | ||
// type: 'Comment', | ||
// value: ' Data on the token itself' | ||
// }]; | ||
// prop.leadingComments = [{ | ||
// type: 'CommentBlock', | ||
// value: '* Data on the token itself' | ||
// }]; | ||
return { | ||
type: 'TSPropertySignature', | ||
key: t.identifier(name), | ||
typeAnnotation: typeAnnotation, | ||
optional: optional | ||
}; | ||
}; | ||
exports.propertySignature = propertySignature; | ||
var createTypedObjectParams = function createTypedObjectParams(jsonschema) { | ||
@@ -289,7 +137,7 @@ var _jsonschema$propertie3; | ||
var typedParams = keys.map(function (prop) { | ||
var _getPropertyType = getPropertyType(jsonschema, prop), | ||
var _getPropertyType = (0, _types2.getPropertyType)(jsonschema, prop), | ||
type = _getPropertyType.type, | ||
optional = _getPropertyType.optional; | ||
return propertySignature(camelize ? (0, _case.camel)(prop) : prop, t.tsTypeAnnotation(type), optional); | ||
return (0, _babel.propertySignature)(camelize ? (0, _case.camel)(prop) : prop, t.tsTypeAnnotation(type), optional); | ||
}); | ||
@@ -320,3 +168,3 @@ var params = keys.map(function (prop) { | ||
var obj = createTypedObjectParams(jsonschema); | ||
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)]; | ||
var fixedParams = [(0, _babel.identifier)('fee', t.tsTypeAnnotation(t.tsUnionType([t.tsNumberKeyword(), t.tsTypeReference(t.identifier('StdFee')), t.tsLiteralType(t.stringLiteral('auto'))])), true), (0, _babel.identifier)('memo', t.tsTypeAnnotation(t.tsStringKeyword()), true), (0, _babel.identifier)('funds', t.tsTypeAnnotation((0, _babel.tsTypeOperator)(t.tsArrayType(t.tsTypeReference(t.identifier('Coin'))), 'readonly')), true)]; | ||
var func = { | ||
@@ -348,11 +196,11 @@ type: 'TSFunctionType', | ||
if (jsonschema.type !== 'object') { | ||
return t.exportNamedDeclaration(t.tsTypeAliasDeclaration(t.identifier(Type), null, getType(jsonschema.type))); | ||
return t.exportNamedDeclaration(t.tsTypeAliasDeclaration(t.identifier(Type), null, (0, _types2.getType)(jsonschema.type))); | ||
} | ||
var props = Object.keys((_jsonschema$propertie4 = jsonschema.properties) !== null && _jsonschema$propertie4 !== void 0 ? _jsonschema$propertie4 : {}).map(function (prop) { | ||
var _getPropertyType2 = getPropertyType(jsonschema, prop), | ||
var _getPropertyType2 = (0, _types2.getPropertyType)(jsonschema, prop), | ||
type = _getPropertyType2.type, | ||
optional = _getPropertyType2.optional; | ||
return propertySignature((0, _case.camel)(prop), t.tsTypeAnnotation(type), optional); | ||
return (0, _babel.propertySignature)((0, _case.camel)(prop), t.tsTypeAnnotation(type), optional); | ||
}); | ||
@@ -359,0 +207,0 @@ return t.exportNamedDeclaration(t.tsInterfaceDeclaration(t.identifier(Type), null, [], t.tsInterfaceBody((0, _toConsumableArray2["default"])(props)))); |
@@ -7,28 +7,30 @@ "use strict"; | ||
var _query_msg = _interopRequireDefault(require("./__fixtures__/schema/query_msg.json")); | ||
var _execute_msg = _interopRequireDefault(require("./../../../__fixtures__/cw-named-groups/execute_msg.json")); | ||
var _execute_msg_for__empty = _interopRequireDefault(require("./__fixtures__/schema/execute_msg_for__empty.json")); | ||
var _query_msg = _interopRequireDefault(require("./../../../__fixtures__/basic/query_msg.json")); | ||
var _approval_response = _interopRequireDefault(require("./__fixtures__/schema/approval_response.json")); | ||
var _execute_msg_for__empty = _interopRequireDefault(require("./../../../__fixtures__/basic/execute_msg_for__empty.json")); | ||
var _all_nft_info_response = _interopRequireDefault(require("./__fixtures__/schema/all_nft_info_response.json")); | ||
var _approval_response = _interopRequireDefault(require("./../../../__fixtures__/basic/approval_response.json")); | ||
var _approvals_response = _interopRequireDefault(require("./__fixtures__/schema/approvals_response.json")); | ||
var _all_nft_info_response = _interopRequireDefault(require("./../../../__fixtures__/basic/all_nft_info_response.json")); | ||
var _collection_info_response = _interopRequireDefault(require("./__fixtures__/schema/collection_info_response.json")); | ||
var _approvals_response = _interopRequireDefault(require("./../../../__fixtures__/basic/approvals_response.json")); | ||
var _contract_info_response = _interopRequireDefault(require("./__fixtures__/schema/contract_info_response.json")); | ||
var _collection_info_response = _interopRequireDefault(require("./../../../__fixtures__/basic/collection_info_response.json")); | ||
var _instantiate_msg = _interopRequireDefault(require("./__fixtures__/schema/instantiate_msg.json")); | ||
var _contract_info_response = _interopRequireDefault(require("./../../../__fixtures__/basic/contract_info_response.json")); | ||
var _nft_info_response = _interopRequireDefault(require("./__fixtures__/schema/nft_info_response.json")); | ||
var _instantiate_msg = _interopRequireDefault(require("./../../../__fixtures__/basic/instantiate_msg.json")); | ||
var _num_tokens_response = _interopRequireDefault(require("./__fixtures__/schema/num_tokens_response.json")); | ||
var _nft_info_response = _interopRequireDefault(require("./../../../__fixtures__/basic/nft_info_response.json")); | ||
var _operators_response = _interopRequireDefault(require("./__fixtures__/schema/operators_response.json")); | ||
var _num_tokens_response = _interopRequireDefault(require("./../../../__fixtures__/basic/num_tokens_response.json")); | ||
var _owner_of_response = _interopRequireDefault(require("./__fixtures__/schema/owner_of_response.json")); | ||
var _operators_response = _interopRequireDefault(require("./../../../__fixtures__/basic/operators_response.json")); | ||
var _tokens_response = _interopRequireDefault(require("./__fixtures__/schema/tokens_response.json")); | ||
var _owner_of_response = _interopRequireDefault(require("./../../../__fixtures__/basic/owner_of_response.json")); | ||
var _tokens_response = _interopRequireDefault(require("./../../../__fixtures__/basic/tokens_response.json")); | ||
var _wasm = require("./wasm"); | ||
@@ -86,2 +88,5 @@ | ||
}); | ||
it('execute classes array types', function () { | ||
expectCode((0, _wasm.createExecuteClass)('SG721Client', 'SG721Instance', null, _execute_msg["default"])); | ||
}); | ||
it('execute interfaces no extends', function () { | ||
@@ -88,0 +93,0 @@ expectCode((0, _wasm.createExecuteInterface)('SG721Instance', null, _execute_msg_for__empty["default"])); |
import * as t from '@babel/types'; | ||
import { camel } from 'case'; | ||
import { bindMethod, typedIdentifier, classDeclaration, classProperty, arrowFunctionExpression, getMessageProperties } from './utils'; | ||
import { getPropertyType } from './utils/types'; | ||
import { identifier, tsTypeOperator, propertySignature } from './utils/babel'; | ||
const getTypeFromRef = $ref => { | ||
switch ($ref) { | ||
case '#/definitions/Binary': | ||
return t.tsTypeReference(t.identifier('Binary')); | ||
default: | ||
if ($ref.startsWith('#/definitions/')) { | ||
return t.tsTypeReference(t.identifier($ref.replace('#/definitions/', ''))); | ||
} | ||
throw new Error('what is $ref: ' + $ref); | ||
} | ||
}; | ||
const getArrayTypeFromRef = $ref => { | ||
return t.tsArrayType(getTypeFromRef($ref)); | ||
}; | ||
const getArrayTypeFromType = type => { | ||
return t.tsArrayType(getType(type)); | ||
}; // MARKED AS NOT DRY | ||
const identifier = (name, typeAnnotation, optional = false) => { | ||
const type = t.identifier(name); | ||
type.typeAnnotation = typeAnnotation; | ||
type.optional = optional; | ||
return type; | ||
}; | ||
const getType = type => { | ||
switch (type) { | ||
case 'string': | ||
return t.tsStringKeyword(); | ||
case 'boolean': | ||
return t.tSBooleanKeyword(); | ||
case 'integer': | ||
return t.tsNumberKeyword(); | ||
default: | ||
throw new Error('what is type: ' + type); | ||
} | ||
}; // MARKED AS NOT DRY | ||
const getPropertyType = (schema, prop) => { | ||
const props = schema.properties ?? {}; | ||
let info = props[prop]; | ||
let type = null; | ||
let optional = schema.required?.includes(prop); | ||
if (info.allOf && info.allOf.length === 1) { | ||
info = info.allOf[0]; | ||
} | ||
if (typeof info.$ref === 'string') { | ||
type = getTypeFromRef(info.$ref); | ||
} | ||
if (Array.isArray(info.anyOf)) { | ||
// assuming 2nd is null, but let's check to ensure | ||
if (info.anyOf.length !== 2) { | ||
throw new Error('case not handled by transpiler. contact maintainers.'); | ||
} | ||
const [nullableType, nullType] = info.anyOf; | ||
if (nullType?.type !== 'null') { | ||
throw new Error('case not handled by transpiler. contact maintainers.'); | ||
} | ||
type = getTypeFromRef(nullableType?.$ref); | ||
optional = true; | ||
} | ||
if (typeof info.type === 'string') { | ||
if (info.type === 'array') { | ||
if (info.items.$ref) { | ||
type = getArrayTypeFromRef(info.items.$ref); | ||
} else { | ||
type = getArrayTypeFromType(info.items.type); | ||
} | ||
} else { | ||
type = getType(info.type); | ||
} | ||
} | ||
if (Array.isArray(info.type)) { | ||
// assuming 2nd is null, but let's check to ensure | ||
if (info.type.length !== 2) { | ||
throw new Error('case not handled by transpiler. contact maintainers.'); | ||
} | ||
const [nullableType, nullType] = info.type; | ||
if (nullType !== 'null') { | ||
throw new Error('case not handled by transpiler. contact maintainers.'); | ||
} | ||
type = getType(nullableType); | ||
optional = true; | ||
} | ||
if (!type) { | ||
throw new Error('cannot find type for ' + JSON.stringify(info)); | ||
} | ||
if (schema.required?.includes(prop)) { | ||
optional = false; | ||
} | ||
return { | ||
type, | ||
optional | ||
}; | ||
}; | ||
const tsTypeOperator = (typeAnnotation, operator) => { | ||
const obj = t.tsTypeOperator(typeAnnotation); | ||
obj.operator = operator; | ||
return obj; | ||
}; // MARKED AS NOT DRY | ||
const createWasmExecMethod = jsonschema => { | ||
const createWasmExecMethodPartial = jsonschema => { | ||
const underscoreName = Object.keys(jsonschema.properties)[0]; | ||
@@ -172,3 +48,3 @@ const methodName = camel(underscoreName); | ||
const methods = getMessageProperties(execMsg).map(schema => { | ||
return createWasmExecMethod(schema); | ||
return createWasmExecMethodPartial(schema); | ||
}); | ||
@@ -194,20 +70,2 @@ const blockStmt = []; | ||
const propertySignature = (name, typeAnnotation, optional = false) => { | ||
// prop.leadingComments = [{ | ||
// type: 'Comment', | ||
// value: ' Data on the token itself' | ||
// }]; | ||
// prop.leadingComments = [{ | ||
// type: 'CommentBlock', | ||
// value: '* Data on the token itself' | ||
// }]; | ||
return { | ||
type: 'TSPropertySignature', | ||
key: t.identifier(name), | ||
typeAnnotation, | ||
optional | ||
}; | ||
}; // MARKED AS NOT DRY | ||
const createTypedObjectParams = (jsonschema, camelize = true) => { | ||
@@ -214,0 +72,0 @@ const keys = Object.keys(jsonschema.properties ?? {}); |
import generate from '@babel/generator'; | ||
import execute_msg from './__fixtures__/schema/execute_msg_for__empty.json'; | ||
import execute_msg from './../../../__fixtures__/basic/execute_msg_for__empty.json'; | ||
import { createFromPartialClass, createFromPartialInterface } from './messages'; | ||
@@ -4,0 +4,0 @@ |
import * as t from '@babel/types'; | ||
import { camel, pascal } from 'case'; | ||
import { tsPropertySignature, tsObjectPattern, callExpression, getMessageProperties } from './utils'; | ||
import { propertySignature, getPropertyType } from './wasm'; | ||
import { propertySignature } from './utils/babel'; | ||
import { getPropertyType } from './utils/types'; | ||
export const createReactQueryHooks = (queryMsg, contractName, QueryClient) => { | ||
@@ -6,0 +7,0 @@ return getMessageProperties(queryMsg).reduce((m, schema) => { |
import generate from '@babel/generator'; | ||
import * as t from '@babel/types'; | ||
import query_msg from './__fixtures__/schema/query_msg.json'; | ||
import query_msg from './../../../__fixtures__/basic/query_msg.json'; | ||
import { createReactQueryHooks } from './react-query'; | ||
@@ -5,0 +5,0 @@ |
import generate from '@babel/generator'; | ||
import * as t from '@babel/types'; | ||
import query_msg from './__fixtures__/schema/query_msg.json'; | ||
import query_msg from './../../../__fixtures__/basic/query_msg.json'; | ||
import { createRecoilSelector, createRecoilSelectors, createRecoilQueryClient } from './recoil'; | ||
@@ -5,0 +5,0 @@ |
import * as t from '@babel/types'; | ||
import { camel, pascal } from 'case'; | ||
import { bindMethod, typedIdentifier, promiseTypeAnnotation, classDeclaration, classProperty, arrowFunctionExpression, getMessageProperties } from './utils'; | ||
const getTypeFromRef = $ref => { | ||
switch ($ref) { | ||
case '#/definitions/Binary': | ||
return t.tsTypeReference(t.identifier('Binary')); | ||
default: | ||
if ($ref.startsWith('#/definitions/')) { | ||
return t.tsTypeReference(t.identifier($ref.replace('#/definitions/', ''))); | ||
} | ||
throw new Error('what is $ref: ' + $ref); | ||
} | ||
}; | ||
const getArrayTypeFromRef = $ref => { | ||
return t.tsArrayType(getTypeFromRef($ref)); | ||
}; | ||
const getArrayTypeFromType = type => { | ||
return t.tsArrayType(getType(type)); | ||
}; | ||
export const identifier = (name, typeAnnotation, optional = false) => { | ||
const type = t.identifier(name); | ||
type.typeAnnotation = typeAnnotation; | ||
type.optional = optional; | ||
return type; | ||
}; | ||
const getType = type => { | ||
switch (type) { | ||
case 'string': | ||
return t.tsStringKeyword(); | ||
case 'boolean': | ||
return t.tSBooleanKeyword(); | ||
case 'integer': | ||
return t.tsNumberKeyword(); | ||
default: | ||
throw new Error('what is type: ' + type); | ||
} | ||
}; | ||
export const getPropertyType = (schema, prop) => { | ||
const props = schema.properties ?? {}; | ||
let info = props[prop]; | ||
let type = null; | ||
let optional = schema.required?.includes(prop); | ||
if (info.allOf && info.allOf.length === 1) { | ||
info = info.allOf[0]; | ||
} | ||
if (typeof info.$ref === 'string') { | ||
type = getTypeFromRef(info.$ref); | ||
} | ||
if (Array.isArray(info.anyOf)) { | ||
// assuming 2nd is null, but let's check to ensure | ||
if (info.anyOf.length !== 2) { | ||
throw new Error('case not handled by transpiler. contact maintainers.'); | ||
} | ||
const [nullableType, nullType] = info.anyOf; | ||
if (nullType?.type !== 'null') { | ||
throw new Error('case not handled by transpiler. contact maintainers.'); | ||
} | ||
type = getTypeFromRef(nullableType?.$ref); | ||
optional = true; | ||
} | ||
if (typeof info.type === 'string') { | ||
if (info.type === 'array') { | ||
if (info.items.$ref) { | ||
type = getArrayTypeFromRef(info.items.$ref); | ||
} else { | ||
type = getArrayTypeFromType(info.items.type); | ||
} | ||
} else { | ||
type = getType(info.type); | ||
} | ||
} | ||
if (Array.isArray(info.type)) { | ||
// assuming 2nd is null, but let's check to ensure | ||
if (info.type.length !== 2) { | ||
throw new Error('case not handled by transpiler. contact maintainers.'); | ||
} | ||
const [nullableType, nullType] = info.type; | ||
if (nullType !== 'null') { | ||
throw new Error('case not handled by transpiler. contact maintainers.'); | ||
} | ||
type = getType(nullableType); | ||
optional = true; | ||
} | ||
if (!type) { | ||
throw new Error('cannot find type for ' + JSON.stringify(info)); | ||
} | ||
if (schema.required?.includes(prop)) { | ||
optional = false; | ||
} | ||
return { | ||
type, | ||
optional | ||
}; | ||
}; | ||
import { getPropertyType, getType } from './utils/types'; | ||
import { identifier, tsTypeOperator, propertySignature } from './utils/babel'; | ||
export const createWasmQueryMethod = jsonschema => { | ||
@@ -145,9 +30,2 @@ const underscoreName = Object.keys(jsonschema.properties)[0]; | ||
}; | ||
const tsTypeOperator = (typeAnnotation, operator) => { | ||
const obj = t.tsTypeOperator(typeAnnotation); | ||
obj.operator = operator; | ||
return obj; | ||
}; | ||
export const createWasmExecMethod = jsonschema => { | ||
@@ -198,18 +76,2 @@ const underscoreName = Object.keys(jsonschema.properties)[0]; | ||
}; | ||
export const propertySignature = (name, typeAnnotation, optional = false) => { | ||
// prop.leadingComments = [{ | ||
// type: 'Comment', | ||
// value: ' Data on the token itself' | ||
// }]; | ||
// prop.leadingComments = [{ | ||
// type: 'CommentBlock', | ||
// value: '* Data on the token itself' | ||
// }]; | ||
return { | ||
type: 'TSPropertySignature', | ||
key: t.identifier(name), | ||
typeAnnotation, | ||
optional | ||
}; | ||
}; | ||
export const createTypedObjectParams = (jsonschema, camelize = true) => { | ||
@@ -216,0 +78,0 @@ const keys = Object.keys(jsonschema.properties ?? {}); |
import generate from '@babel/generator'; | ||
import query_msg from './__fixtures__/schema/query_msg.json'; | ||
import execute_msg from './__fixtures__/schema/execute_msg_for__empty.json'; | ||
import approval_response from './__fixtures__/schema/approval_response.json'; | ||
import all_nft_info_response from './__fixtures__/schema/all_nft_info_response.json'; | ||
import approvals_response from './__fixtures__/schema/approvals_response.json'; | ||
import collection_info_response from './__fixtures__/schema/collection_info_response.json'; | ||
import contract_info_response from './__fixtures__/schema/contract_info_response.json'; | ||
import instantiate_msg from './__fixtures__/schema/instantiate_msg.json'; | ||
import nft_info_response from './__fixtures__/schema/nft_info_response.json'; | ||
import num_tokens_response from './__fixtures__/schema/num_tokens_response.json'; | ||
import operators_response from './__fixtures__/schema/operators_response.json'; | ||
import owner_of_response from './__fixtures__/schema/owner_of_response.json'; | ||
import tokens_response from './__fixtures__/schema/tokens_response.json'; | ||
import execute_msg_named_groups from './../../../__fixtures__/cw-named-groups/execute_msg.json'; | ||
import query_msg from './../../../__fixtures__/basic/query_msg.json'; | ||
import execute_msg from './../../../__fixtures__/basic/execute_msg_for__empty.json'; | ||
import approval_response from './../../../__fixtures__/basic/approval_response.json'; | ||
import all_nft_info_response from './../../../__fixtures__/basic/all_nft_info_response.json'; | ||
import approvals_response from './../../../__fixtures__/basic/approvals_response.json'; | ||
import collection_info_response from './../../../__fixtures__/basic/collection_info_response.json'; | ||
import contract_info_response from './../../../__fixtures__/basic/contract_info_response.json'; | ||
import instantiate_msg from './../../../__fixtures__/basic/instantiate_msg.json'; | ||
import nft_info_response from './../../../__fixtures__/basic/nft_info_response.json'; | ||
import num_tokens_response from './../../../__fixtures__/basic/num_tokens_response.json'; | ||
import operators_response from './../../../__fixtures__/basic/operators_response.json'; | ||
import owner_of_response from './../../../__fixtures__/basic/owner_of_response.json'; | ||
import tokens_response from './../../../__fixtures__/basic/tokens_response.json'; | ||
import { createQueryClass, createQueryInterface, createExecuteClass, createExecuteInterface, createTypeInterface } from './wasm'; | ||
@@ -67,2 +68,5 @@ | ||
}); | ||
it('execute classes array types', () => { | ||
expectCode(createExecuteClass('SG721Client', 'SG721Instance', null, execute_msg_named_groups)); | ||
}); | ||
it('execute interfaces no extends', () => { | ||
@@ -69,0 +73,0 @@ expectCode(createExecuteInterface('SG721Instance', null, execute_msg)); |
{ | ||
"name": "wasm-ast-types", | ||
"version": "0.2.5", | ||
"version": "0.2.6", | ||
"description": "CosmWasm TypeScript AST generation", | ||
@@ -88,3 +88,3 @@ "author": "Dan Lynch <pyramation@gmail.com>", | ||
}, | ||
"gitHead": "4e067214d530188822eca31ddbeb6eb9ec3ba462" | ||
"gitHead": "9a94ff9a623b4f4d9f8c1f742b15b43eb525295f" | ||
} |
42
137468
2133