wasm-ast-types
Advanced tools
Comparing version 0.0.8 to 0.0.9
@@ -7,15 +7,2 @@ "use strict"; | ||
var _types = require("./types"); | ||
Object.keys(_types).forEach(function (key) { | ||
if (key === "default" || key === "__esModule") return; | ||
if (key in exports && exports[key] === _types[key]) return; | ||
Object.defineProperty(exports, key, { | ||
enumerable: true, | ||
get: function get() { | ||
return _types[key]; | ||
} | ||
}); | ||
}); | ||
var _utils = require("./utils"); | ||
@@ -22,0 +9,0 @@ |
@@ -106,2 +106,8 @@ "use strict"; | ||
}, _callee4); | ||
}))); | ||
}))); | ||
it('object parms', function () { | ||
var obj = t.objectPattern([t.objectProperty(t.identifier('includeExpired'), t.identifier('includeExpired'), false, true), t.objectProperty(t.identifier('tokenId'), t.identifier('tokenId'), false, true)]); | ||
obj.typeAnnotation = t.tsTypeAnnotation(t.tsTypeLiteral([t.tsPropertySignature(t.identifier('includeExpired'), t.tsTypeAnnotation(t.tsBooleanKeyword())), t.tsPropertySignature(t.identifier('tokenId'), t.tsTypeAnnotation(t.tsStringKeyword()))])); | ||
expectCode(t.program([t.expressionStatement(t.assignmentExpression('=', t.identifier('ownerOf'), (0, _utils.arrowFunctionExpression)([obj], t.blockStatement([// body | ||
], []), t.tsTypeAnnotation(t.tsTypeReference(t.identifier('Promise'), t.tsTypeParameterInstantiation([t.tsTypeReference(t.identifier('OwnerOfResponse'))]))))))])); | ||
}); |
@@ -10,3 +10,3 @@ "use strict"; | ||
}); | ||
exports.propertySignature = exports.createWasmQueryMethod = exports.createWasmExecMethod = exports.createTypeOrInterface = exports.createTypeInterface = exports.createQueryInterface = exports.createQueryClass = exports.createExecuteInterface = exports.createExecuteClass = void 0; | ||
exports.propertySignature = exports.createWasmQueryMethod = exports.createWasmExecMethod = exports.createTypedObjectParams = exports.createTypeOrInterface = exports.createTypeInterface = exports.createQueryInterface = exports.createQueryClass = exports.createPropertyFunctionWithObjectParams = exports.createExecuteInterface = exports.createExecuteClass = void 0; | ||
@@ -156,7 +156,4 @@ var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray")); | ||
var responseType = (0, _case.pascal)("".concat(methodName, "Response")); | ||
var properties = (_jsonschema$propertie = jsonschema.properties[underscoreName].properties) !== null && _jsonschema$propertie !== void 0 ? _jsonschema$propertie : {}; // console.log({ jsonschema, methodName, underscoreName, properties }); | ||
var params = Object.keys(properties).map(function (prop) { | ||
return getProperty(jsonschema.properties[underscoreName], prop); | ||
}); | ||
var properties = (_jsonschema$propertie = jsonschema.properties[underscoreName].properties) !== null && _jsonschema$propertie !== void 0 ? _jsonschema$propertie : {}; | ||
var obj = createTypedObjectParams(jsonschema.properties[underscoreName]); | ||
var args = Object.keys(properties).map(function (prop) { | ||
@@ -166,3 +163,3 @@ return t.objectProperty(t.identifier(prop), t.identifier((0, _case.camel)(prop)), false, true); | ||
var actionArg = t.objectProperty(t.identifier(underscoreName), t.objectExpression(args)); | ||
return t.classProperty(t.identifier(methodName), (0, _utils.arrowFunctionExpression)(params, 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)); | ||
return t.classProperty(t.identifier(methodName), (0, _utils.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)); | ||
}; | ||
@@ -197,9 +194,8 @@ | ||
var properties = (_jsonschema$propertie2 = jsonschema.properties[underscoreName].properties) !== null && _jsonschema$propertie2 !== void 0 ? _jsonschema$propertie2 : {}; | ||
var params = Object.keys(properties).map(function (prop) { | ||
return getProperty(jsonschema.properties[underscoreName], prop); | ||
}); | ||
var obj = createTypedObjectParams(jsonschema.properties[underscoreName]); | ||
var args = Object.keys(properties).map(function (prop) { | ||
return t.objectProperty(t.identifier(prop), t.identifier((0, _case.camel)(prop)), false, prop === (0, _case.camel)(prop)); | ||
}); | ||
return t.classProperty(t.identifier(methodName), (0, _utils.arrowFunctionExpression)((0, _toConsumableArray2["default"])(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((0, _toConsumableArray2["default"])(args)))]), t.stringLiteral('auto')])))]), // return type | ||
return t.classProperty(t.identifier(methodName), (0, _utils.arrowFunctionExpression)(obj ? [// props | ||
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 | ||
t.tsTypeAnnotation(t.tsTypeReference(t.identifier('Promise'), t.tsTypeParameterInstantiation([t.tSTypeReference(t.identifier('ExecuteResult'))]))), true)); | ||
@@ -240,11 +236,5 @@ }; | ||
var methods = execMsg.oneOf.map(function (jsonschema) { | ||
var _jsonschema$propertie3; | ||
var underscoreName = Object.keys(jsonschema.properties)[0]; | ||
var methodName = (0, _case.camel)(underscoreName); | ||
var properties = (_jsonschema$propertie3 = jsonschema.properties[underscoreName].properties) !== null && _jsonschema$propertie3 !== void 0 ? _jsonschema$propertie3 : {}; | ||
var params = Object.keys(properties).map(function (prop) { | ||
return getProperty(jsonschema.properties[underscoreName], prop); | ||
}); | ||
return t.tSPropertySignature(t.identifier(methodName), t.tsTypeAnnotation(t.tsFunctionType(null, (0, _toConsumableArray2["default"])(params), (0, _utils.promiseTypeAnnotation)('ExecuteResult')))); | ||
return createPropertyFunctionWithObjectParams(methodName, 'ExecuteResult', jsonschema.properties[underscoreName]); | ||
}); | ||
@@ -259,14 +249,43 @@ var extendsAst = extendsClassName ? [t.tSExpressionWithTypeArguments(t.identifier(extendsClassName))] : []; | ||
var createTypedObjectParams = function createTypedObjectParams(jsonschema) { | ||
var _jsonschema$propertie3; | ||
var keys = Object.keys((_jsonschema$propertie3 = jsonschema.properties) !== null && _jsonschema$propertie3 !== void 0 ? _jsonschema$propertie3 : {}); | ||
if (!keys.length) return; | ||
var typedParams = keys.map(function (prop) { | ||
var _getPropertyType2 = getPropertyType(jsonschema, prop), | ||
type = _getPropertyType2.type, | ||
optional = _getPropertyType2.optional; | ||
return t.tsPropertySignature(t.identifier(prop), t.tsTypeAnnotation(type)); | ||
}); | ||
var params = keys.map(function (prop) { | ||
return t.objectProperty(t.identifier(prop), t.identifier(prop), false, true); | ||
}); | ||
var obj = t.objectPattern((0, _toConsumableArray2["default"])(params)); | ||
obj.typeAnnotation = t.tsTypeAnnotation(t.tsTypeLiteral((0, _toConsumableArray2["default"])(typedParams))); | ||
return obj; | ||
}; | ||
exports.createTypedObjectParams = createTypedObjectParams; | ||
var createPropertyFunctionWithObjectParams = function createPropertyFunctionWithObjectParams(methodName, responseType, jsonschema) { | ||
var obj = createTypedObjectParams(jsonschema); | ||
var func = { | ||
type: 'TSFunctionType', | ||
typeAnnotation: (0, _utils.promiseTypeAnnotation)(responseType), | ||
parameters: obj ? [obj] : [] | ||
}; | ||
return t.tSPropertySignature(t.identifier(methodName), t.tsTypeAnnotation(func)); | ||
}; | ||
exports.createPropertyFunctionWithObjectParams = createPropertyFunctionWithObjectParams; | ||
var createQueryInterface = function createQueryInterface(className, queryMsg) { | ||
var methods = queryMsg.oneOf.map(function (jsonschema) { | ||
var _jsonschema$propertie4; | ||
var underscoreName = Object.keys(jsonschema.properties)[0]; | ||
var methodName = (0, _case.camel)(underscoreName); | ||
var responseType = (0, _case.pascal)("".concat(methodName, "Response")); | ||
var properties = (_jsonschema$propertie4 = jsonschema.properties[underscoreName].properties) !== null && _jsonschema$propertie4 !== void 0 ? _jsonschema$propertie4 : {}; | ||
var params = Object.keys(properties).map(function (prop) { | ||
return getProperty(jsonschema.properties[underscoreName], prop); | ||
}); | ||
return t.tSPropertySignature(t.identifier(methodName), t.tsTypeAnnotation(t.tsFunctionType(null, (0, _toConsumableArray2["default"])(params), (0, _utils.promiseTypeAnnotation)(responseType)))); | ||
var obj = createTypedObjectParams(jsonschema.properties[underscoreName]); | ||
return createPropertyFunctionWithObjectParams(methodName, responseType, jsonschema.properties[underscoreName]); | ||
}); | ||
@@ -295,3 +314,3 @@ return t.exportNamedDeclaration(t.tsInterfaceDeclaration(t.identifier(className), null, [], t.tSInterfaceBody([t.tSPropertySignature(t.identifier('contractAddress'), t.tsTypeAnnotation(t.tsStringKeyword()))].concat((0, _toConsumableArray2["default"])(methods))))); | ||
var createTypeOrInterface = function createTypeOrInterface(Type, jsonschema) { | ||
var _jsonschema$propertie5; | ||
var _jsonschema$propertie4; | ||
@@ -302,6 +321,6 @@ if (jsonschema.type !== 'object') { | ||
var props = Object.keys((_jsonschema$propertie5 = jsonschema.properties) !== null && _jsonschema$propertie5 !== void 0 ? _jsonschema$propertie5 : {}).map(function (prop) { | ||
var _getPropertyType2 = getPropertyType(jsonschema, prop), | ||
type = _getPropertyType2.type, | ||
optional = _getPropertyType2.optional; | ||
var props = Object.keys((_jsonschema$propertie4 = jsonschema.properties) !== null && _jsonschema$propertie4 !== void 0 ? _jsonschema$propertie4 : {}).map(function (prop) { | ||
var _getPropertyType3 = getPropertyType(jsonschema, prop), | ||
type = _getPropertyType3.type, | ||
optional = _getPropertyType3.optional; | ||
@@ -308,0 +327,0 @@ return propertySignature((0, _case.camel)(prop), t.tsTypeAnnotation(type), optional); |
@@ -80,3 +80,3 @@ "use strict"; | ||
it('execute classes', function () { | ||
expectCode((0, _wasm.createExecuteClass)('SG721Client', 'SG721Instance', 'SG721QueryClient', _execute_msg_for__empty["default"])); | ||
printCode((0, _wasm.createExecuteClass)('SG721Client', 'SG721Instance', 'SG721QueryClient', _execute_msg_for__empty["default"])); | ||
}); | ||
@@ -83,0 +83,0 @@ it('execute classes no extends', function () { |
@@ -1,3 +0,2 @@ | ||
export * from './types'; | ||
export * from './utils'; | ||
export * from './wasm'; |
@@ -47,2 +47,8 @@ import { importStmt } from './utils'; | ||
t.tsTypeAnnotation(t.tsTypeReference(t.identifier('Promise'), t.tsTypeParameterInstantiation([t.tSTypeReference(t.identifier('ExecuteResult'))]))), true))], [t.tSExpressionWithTypeArguments(t.identifier('SG721ReadOnlyInstance'))], t.identifier('SG721QueryClient')))])); | ||
}); | ||
it('object parms', () => { | ||
const obj = t.objectPattern([t.objectProperty(t.identifier('includeExpired'), t.identifier('includeExpired'), false, true), t.objectProperty(t.identifier('tokenId'), t.identifier('tokenId'), false, true)]); | ||
obj.typeAnnotation = t.tsTypeAnnotation(t.tsTypeLiteral([t.tsPropertySignature(t.identifier('includeExpired'), t.tsTypeAnnotation(t.tsBooleanKeyword())), t.tsPropertySignature(t.identifier('tokenId'), t.tsTypeAnnotation(t.tsStringKeyword()))])); | ||
expectCode(t.program([t.expressionStatement(t.assignmentExpression('=', t.identifier('ownerOf'), arrowFunctionExpression([obj], t.blockStatement([// body | ||
], []), t.tsTypeAnnotation(t.tsTypeReference(t.identifier('Promise'), t.tsTypeParameterInstantiation([t.tsTypeReference(t.identifier('OwnerOfResponse'))]))))))])); | ||
}); |
@@ -126,7 +126,4 @@ import * as t from '@babel/types'; | ||
const responseType = pascal(`${methodName}Response`); | ||
const properties = jsonschema.properties[underscoreName].properties ?? {}; // console.log({ jsonschema, methodName, underscoreName, properties }); | ||
const params = Object.keys(properties).map(prop => { | ||
return getProperty(jsonschema.properties[underscoreName], prop); | ||
}); | ||
const properties = jsonschema.properties[underscoreName].properties ?? {}; | ||
const obj = createTypedObjectParams(jsonschema.properties[underscoreName]); | ||
const args = Object.keys(properties).map(prop => { | ||
@@ -136,3 +133,3 @@ return t.objectProperty(t.identifier(prop), t.identifier(camel(prop)), false, true); | ||
const actionArg = t.objectProperty(t.identifier(underscoreName), t.objectExpression(args)); | ||
return t.classProperty(t.identifier(methodName), arrowFunctionExpression(params, 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)); | ||
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)); | ||
}; | ||
@@ -155,10 +152,8 @@ export const createQueryClass = (className, implementsClassName, queryMsg) => { | ||
const properties = jsonschema.properties[underscoreName].properties ?? {}; | ||
const params = Object.keys(properties).map(prop => { | ||
return getProperty(jsonschema.properties[underscoreName], prop); | ||
}); | ||
const obj = createTypedObjectParams(jsonschema.properties[underscoreName]); | ||
const args = Object.keys(properties).map(prop => { | ||
return t.objectProperty(t.identifier(prop), t.identifier(camel(prop)), false, prop === camel(prop)); | ||
}); | ||
return t.classProperty(t.identifier(methodName), arrowFunctionExpression([// props | ||
...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.stringLiteral('auto')])))]), // return type | ||
return t.classProperty(t.identifier(methodName), arrowFunctionExpression(obj ? [// props | ||
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 | ||
t.tsTypeAnnotation(t.tsTypeReference(t.identifier('Promise'), t.tsTypeParameterInstantiation([t.tSTypeReference(t.identifier('ExecuteResult'))]))), true)); | ||
@@ -191,7 +186,3 @@ }; | ||
const methodName = camel(underscoreName); | ||
const properties = jsonschema.properties[underscoreName].properties ?? {}; | ||
const params = Object.keys(properties).map(prop => { | ||
return getProperty(jsonschema.properties[underscoreName], prop); | ||
}); | ||
return t.tSPropertySignature(t.identifier(methodName), t.tsTypeAnnotation(t.tsFunctionType(null, [...params], promiseTypeAnnotation('ExecuteResult')))); | ||
return createPropertyFunctionWithObjectParams(methodName, 'ExecuteResult', jsonschema.properties[underscoreName]); | ||
}); | ||
@@ -203,2 +194,28 @@ const extendsAst = extendsClassName ? [t.tSExpressionWithTypeArguments(t.identifier(extendsClassName))] : []; | ||
}; | ||
export const createTypedObjectParams = jsonschema => { | ||
const keys = Object.keys(jsonschema.properties ?? {}); | ||
if (!keys.length) return; | ||
const typedParams = keys.map(prop => { | ||
const { | ||
type, | ||
optional | ||
} = getPropertyType(jsonschema, prop); | ||
return t.tsPropertySignature(t.identifier(prop), t.tsTypeAnnotation(type)); | ||
}); | ||
const params = keys.map(prop => { | ||
return t.objectProperty(t.identifier(prop), t.identifier(prop), false, true); | ||
}); | ||
const obj = t.objectPattern([...params]); | ||
obj.typeAnnotation = t.tsTypeAnnotation(t.tsTypeLiteral([...typedParams])); | ||
return obj; | ||
}; | ||
export const createPropertyFunctionWithObjectParams = (methodName, responseType, jsonschema) => { | ||
const obj = createTypedObjectParams(jsonschema); | ||
const func = { | ||
type: 'TSFunctionType', | ||
typeAnnotation: promiseTypeAnnotation(responseType), | ||
parameters: obj ? [obj] : [] | ||
}; | ||
return t.tSPropertySignature(t.identifier(methodName), t.tsTypeAnnotation(func)); | ||
}; | ||
export const createQueryInterface = (className, queryMsg) => { | ||
@@ -209,7 +226,4 @@ const methods = queryMsg.oneOf.map(jsonschema => { | ||
const responseType = pascal(`${methodName}Response`); | ||
const properties = jsonschema.properties[underscoreName].properties ?? {}; | ||
const params = Object.keys(properties).map(prop => { | ||
return getProperty(jsonschema.properties[underscoreName], prop); | ||
}); | ||
return t.tSPropertySignature(t.identifier(methodName), t.tsTypeAnnotation(t.tsFunctionType(null, [...params], promiseTypeAnnotation(responseType)))); | ||
const obj = createTypedObjectParams(jsonschema.properties[underscoreName]); | ||
return createPropertyFunctionWithObjectParams(methodName, responseType, jsonschema.properties[underscoreName]); | ||
}); | ||
@@ -216,0 +230,0 @@ return t.exportNamedDeclaration(t.tsInterfaceDeclaration(t.identifier(className), null, [], t.tSInterfaceBody([t.tSPropertySignature(t.identifier('contractAddress'), t.tsTypeAnnotation(t.tsStringKeyword())), ...methods]))); |
@@ -62,3 +62,3 @@ import generate from '@babel/generator'; | ||
it('execute classes', () => { | ||
expectCode(createExecuteClass('SG721Client', 'SG721Instance', 'SG721QueryClient', execute_msg)); | ||
printCode(createExecuteClass('SG721Client', 'SG721Instance', 'SG721QueryClient', execute_msg)); | ||
}); | ||
@@ -65,0 +65,0 @@ it('execute classes no extends', () => { |
{ | ||
"name": "wasm-ast-types", | ||
"version": "0.0.8", | ||
"version": "0.0.9", | ||
"description": "CosmWasm TypeScript AST generation", | ||
@@ -88,3 +88,3 @@ "author": "Dan Lynch <pyramation@gmail.com>", | ||
}, | ||
"gitHead": "f3aed801c61181b4592442cbf0be24c5a7322e5b" | ||
"gitHead": "1d8886f8c415d2123010166ee491f88935c8ec1b" | ||
} |
79041
1230
22