wasm-ast-types
Advanced tools
Comparing version 0.3.7 to 0.4.0
@@ -28,3 +28,12 @@ "use strict"; | ||
var createReactQueryHooks = function createReactQueryHooks(queryMsg, contractName, QueryClient) { | ||
var DEFAULT_OPTIONS = { | ||
optionalClient: false | ||
}; | ||
var createReactQueryHooks = function createReactQueryHooks(_ref) { | ||
var queryMsg = _ref.queryMsg, | ||
contractName = _ref.contractName, | ||
QueryClient = _ref.QueryClient, | ||
_ref$options = _ref.options, | ||
options = _ref$options === void 0 ? DEFAULT_OPTIONS : _ref$options; | ||
return (0, _utils.getMessageProperties)(queryMsg).reduce(function (m, schema) { | ||
@@ -42,3 +51,4 @@ var underscoreName = Object.keys(schema.properties)[0]; | ||
QueryClient: QueryClient, | ||
jsonschema: jsonschema | ||
jsonschema: jsonschema, | ||
options: options | ||
}), createReactQueryHook({ | ||
@@ -50,3 +60,4 @@ methodName: methodName, | ||
hookKeyName: getterKey, | ||
jsonschema: jsonschema | ||
jsonschema: jsonschema, | ||
options: options | ||
})].concat((0, _toConsumableArray2["default"])(m)); | ||
@@ -58,11 +69,13 @@ }, []); | ||
var createReactQueryHook = function createReactQueryHook(_ref) { | ||
var createReactQueryHook = function createReactQueryHook(_ref2) { | ||
var _jsonschema$propertie; | ||
var hookName = _ref.hookName, | ||
hookParamsTypeName = _ref.hookParamsTypeName, | ||
responseType = _ref.responseType, | ||
hookKeyName = _ref.hookKeyName, | ||
methodName = _ref.methodName, | ||
jsonschema = _ref.jsonschema; | ||
var hookName = _ref2.hookName, | ||
hookParamsTypeName = _ref2.hookParamsTypeName, | ||
responseType = _ref2.responseType, | ||
hookKeyName = _ref2.hookKeyName, | ||
methodName = _ref2.methodName, | ||
jsonschema = _ref2.jsonschema, | ||
_ref2$options = _ref2.options, | ||
options = _ref2$options === void 0 ? DEFAULT_OPTIONS : _ref2$options; | ||
var keys = Object.keys((_jsonschema$propertie = jsonschema.properties) !== null && _jsonschema$propertie !== void 0 ? _jsonschema$propertie : {}); | ||
@@ -85,3 +98,3 @@ var args = []; | ||
return t.objectProperty(t.identifier(prop), t.identifier(prop), false, true); | ||
})), 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()])))])))]))); | ||
})), 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()])))])))]))); | ||
}; | ||
@@ -91,8 +104,10 @@ | ||
var createReactQueryHookInterface = function createReactQueryHookInterface(_ref2) { | ||
var QueryClient = _ref2.QueryClient, | ||
hookParamsTypeName = _ref2.hookParamsTypeName, | ||
responseType = _ref2.responseType, | ||
jsonschema = _ref2.jsonschema; | ||
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)]; | ||
var createReactQueryHookInterface = function createReactQueryHookInterface(_ref3) { | ||
var QueryClient = _ref3.QueryClient, | ||
hookParamsTypeName = _ref3.hookParamsTypeName, | ||
responseType = _ref3.responseType, | ||
jsonschema = _ref3.jsonschema, | ||
_ref3$options = _ref3.options, | ||
options = _ref3$options === void 0 ? DEFAULT_OPTIONS : _ref3$options; | ||
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)]; | ||
var props = getProps(jsonschema, true); | ||
@@ -99,0 +114,0 @@ |
@@ -37,4 +37,20 @@ "use strict"; | ||
it('createReactQueryHooks', function () { | ||
expectCode(t.program((0, _reactQuery.createReactQueryHooks)(_query_msg["default"], 'Sg721', 'Sg721QueryClient'))); | ||
expectCode(t.program((0, _reactQuery.createReactQueryHooks)(_query_msg["default"], 'Sg721', 'Sg721QueryClient'))); | ||
expectCode(t.program((0, _reactQuery.createReactQueryHooks)({ | ||
queryMsg: _query_msg["default"], | ||
contractName: 'Sg721', | ||
QueryClient: 'Sg721QueryClient' | ||
}))); | ||
expectCode(t.program((0, _reactQuery.createReactQueryHooks)({ | ||
queryMsg: _query_msg["default"], | ||
contractName: 'Sg721', | ||
QueryClient: 'Sg721QueryClient' | ||
}))); | ||
expectCode(t.program((0, _reactQuery.createReactQueryHooks)({ | ||
queryMsg: _query_msg["default"], | ||
contractName: 'Sg721', | ||
QueryClient: 'Sg721QueryClient', | ||
options: { | ||
optionalClient: true | ||
} | ||
}))); | ||
}); |
@@ -10,3 +10,3 @@ "use strict"; | ||
}); | ||
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; | ||
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; | ||
@@ -263,3 +263,22 @@ var _toArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toArray")); | ||
}; | ||
/** | ||
* If optional, return a conditional, otherwise just the expression | ||
*/ | ||
exports.memberExpressionOrIdentifierSnake = memberExpressionOrIdentifierSnake; | ||
exports.memberExpressionOrIdentifierSnake = memberExpressionOrIdentifierSnake; | ||
var optionalConditionalExpression = function optionalConditionalExpression(test, expression, alternate) { | ||
var optional = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false; | ||
return optional ? t.conditionalExpression(test, expression, alternate) : expression; | ||
}; | ||
exports.optionalConditionalExpression = optionalConditionalExpression; | ||
var typeRefOrOptionalUnion = function typeRefOrOptionalUnion(identifier) { | ||
var optional = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false; | ||
var typeReference = t.tsTypeReference(identifier); | ||
return optional ? t.tsUnionType([typeReference, t.tsUndefinedKeyword()]) : typeReference; | ||
}; | ||
exports.typeRefOrOptionalUnion = typeRefOrOptionalUnion; |
import * as t from '@babel/types'; | ||
import { camel, pascal } from 'case'; | ||
import { tsPropertySignature, tsObjectPattern, callExpression, getMessageProperties } from './utils'; | ||
import { propertySignature } from './utils/babel'; | ||
import { typeRefOrOptionalUnion, propertySignature, optionalConditionalExpression } from './utils/babel'; | ||
import { getPropertyType } from './utils/types'; | ||
export const createReactQueryHooks = (queryMsg, contractName, QueryClient) => { | ||
const DEFAULT_OPTIONS = { | ||
optionalClient: false | ||
}; | ||
export const createReactQueryHooks = ({ | ||
queryMsg, | ||
contractName, | ||
QueryClient, | ||
options = DEFAULT_OPTIONS | ||
}) => { | ||
return getMessageProperties(queryMsg).reduce((m, schema) => { | ||
@@ -19,3 +27,4 @@ const underscoreName = Object.keys(schema.properties)[0]; | ||
QueryClient, | ||
jsonschema | ||
jsonschema, | ||
options | ||
}), createReactQueryHook({ | ||
@@ -27,3 +36,4 @@ methodName, | ||
hookKeyName: getterKey, | ||
jsonschema | ||
jsonschema, | ||
options | ||
}), ...m]; | ||
@@ -38,3 +48,4 @@ }, []); | ||
methodName, | ||
jsonschema | ||
jsonschema, | ||
options = DEFAULT_OPTIONS | ||
}) => { | ||
@@ -58,3 +69,3 @@ const keys = Object.keys(jsonschema.properties ?? {}); | ||
return t.objectProperty(t.identifier(prop), t.identifier(prop), false, true); | ||
})], 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()])))])))]))); | ||
})], 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()])))])))]))); | ||
}; | ||
@@ -65,5 +76,6 @@ export const createReactQueryHookInterface = ({ | ||
responseType, | ||
jsonschema | ||
jsonschema, | ||
options = DEFAULT_OPTIONS | ||
}) => { | ||
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)]; | ||
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)]; | ||
const props = getProps(jsonschema, true); | ||
@@ -70,0 +82,0 @@ |
@@ -24,4 +24,20 @@ import generate from '@babel/generator'; | ||
it('createReactQueryHooks', () => { | ||
expectCode(t.program(createReactQueryHooks(query_msg, 'Sg721', 'Sg721QueryClient'))); | ||
expectCode(t.program(createReactQueryHooks(query_msg, 'Sg721', 'Sg721QueryClient'))); | ||
expectCode(t.program(createReactQueryHooks({ | ||
queryMsg: query_msg, | ||
contractName: 'Sg721', | ||
QueryClient: 'Sg721QueryClient' | ||
}))); | ||
expectCode(t.program(createReactQueryHooks({ | ||
queryMsg: query_msg, | ||
contractName: 'Sg721', | ||
QueryClient: 'Sg721QueryClient' | ||
}))); | ||
expectCode(t.program(createReactQueryHooks({ | ||
queryMsg: query_msg, | ||
contractName: 'Sg721', | ||
QueryClient: 'Sg721QueryClient', | ||
options: { | ||
optionalClient: true | ||
} | ||
}))); | ||
}); |
@@ -156,2 +156,13 @@ import * as t from '@babel/types'; | ||
return t.memberExpression(memberExpressionOrIdentifierSnake(rest), t.identifier(snake(name))); | ||
}; | ||
/** | ||
* If optional, return a conditional, otherwise just the expression | ||
*/ | ||
export const optionalConditionalExpression = (test, expression, alternate, optional = false) => { | ||
return optional ? t.conditionalExpression(test, expression, alternate) : expression; | ||
}; | ||
export const typeRefOrOptionalUnion = (identifier, optional = false) => { | ||
const typeReference = t.tsTypeReference(identifier); | ||
return optional ? t.tsUnionType([typeReference, t.tsUndefinedKeyword()]) : typeReference; | ||
}; |
{ | ||
"name": "wasm-ast-types", | ||
"version": "0.3.7", | ||
"version": "0.4.0", | ||
"description": "CosmWasm TypeScript AST generation", | ||
@@ -87,3 +87,3 @@ "author": "Dan Lynch <pyramation@gmail.com>", | ||
}, | ||
"gitHead": "1f32ebfe7996630272adbbb54a324ea4a0699267" | ||
"gitHead": "bf5da4cc0e89f0d718d4d02fcc00d3db5424321b" | ||
} |
import * as t from '@babel/types'; | ||
import { QueryMsg } from './types'; | ||
interface ReactQueryHooks { | ||
queryMsg: QueryMsg | ||
contractName: string | ||
QueryClient: string | ||
options?: ReactQueryOptions | ||
} | ||
interface ReactQueryHookQuery { | ||
@@ -11,3 +18,7 @@ hookName: string; | ||
} | ||
export declare const createReactQueryHooks: (queryMsg: QueryMsg, contractName: string, QueryClient: string) => any; | ||
export declare interface ReactQueryOptions { | ||
optionalClient?: boolean | ||
} | ||
export declare const createReactQueryHooks: ({ queryMsg, contractName, QueryClient, options }: ReactQueryHooks) => any; | ||
export declare const createReactQueryHook: ({ hookName, hookParamsTypeName, responseType, hookKeyName, methodName, jsonschema }: ReactQueryHookQuery) => t.ExportNamedDeclaration; | ||
@@ -21,2 +32,2 @@ interface ReactQueryHookQueryInterface { | ||
export declare const createReactQueryHookInterface: ({ QueryClient, hookParamsTypeName, responseType, jsonschema }: ReactQueryHookQueryInterface) => t.ExportNamedDeclaration; | ||
export {}; | ||
export { }; |
157384
2579