wasm-ast-types
Advanced tools
Comparing version 0.4.1 to 0.4.2
@@ -14,2 +14,4 @@ "use strict"; | ||
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); | ||
var t = _interopRequireWildcard(require("@babel/types")); | ||
@@ -29,4 +31,9 @@ | ||
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; } | ||
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { (0, _defineProperty2["default"])(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; } | ||
var DEFAULT_OPTIONS = { | ||
optionalClient: false | ||
optionalClient: false, | ||
v4: false | ||
}; | ||
@@ -39,3 +46,5 @@ | ||
_ref$options = _ref.options, | ||
options = _ref$options === void 0 ? DEFAULT_OPTIONS : _ref$options; | ||
options = _ref$options === void 0 ? {} : _ref$options; | ||
// merge the user options with the defaults | ||
options = _objectSpread(_objectSpread({}, DEFAULT_OPTIONS), options); | ||
return (0, _utils.getMessageProperties)(queryMsg).reduce(function (m, schema) { | ||
@@ -79,3 +88,5 @@ var underscoreName = Object.keys(schema.properties)[0]; | ||
_ref2$options = _ref2.options, | ||
options = _ref2$options === void 0 ? DEFAULT_OPTIONS : _ref2$options; | ||
options = _ref2$options === void 0 ? {} : _ref2$options; | ||
// merge the user options with the defaults | ||
options = _objectSpread(_objectSpread({}, DEFAULT_OPTIONS), options); | ||
var keys = Object.keys((_jsonschema$propertie = jsonschema.properties) !== null && _jsonschema$propertie !== void 0 ? _jsonschema$propertie : {}); | ||
@@ -98,3 +109,4 @@ 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.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()])))])))]))); | ||
})), t.tsTypeAnnotation(t.tsTypeReference(t.identifier(hookParamsTypeName))))], t.blockStatement([t.returnStatement((0, _utils.callExpression)(t.identifier('useQuery'), [t.arrayExpression(generateUseQueryQueryKey(hookKeyName, props, 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.conditionalExpression( // explicitly check for undefined | ||
t.binaryExpression('!=', t.optionalMemberExpression(t.identifier('options'), t.identifier('enabled'), false, true), t.identifier('undefined')), t.memberExpression(t.identifier('options'), t.identifier('enabled')), t.booleanLiteral(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()])))])))]))); | ||
}; | ||
@@ -110,4 +122,7 @@ | ||
_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)]; | ||
options = _ref3$options === void 0 ? {} : _ref3$options; | ||
// merge the user options with the defaults | ||
options = _objectSpread(_objectSpread({}, DEFAULT_OPTIONS), options); | ||
var typedUseQueryOptions = 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()])))])); | ||
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(options.v4 ? t.tSIntersectionType([t.tsTypeReference(t.identifier('Omit'), t.tsTypeParameterInstantiation([typedUseQueryOptions, t.tsLiteralType(t.stringLiteral("'queryKey' | 'queryFn' | 'initialData'"))])), t.tSTypeLiteral([t.tsPropertySignature(t.identifier('initialData?'), t.tsTypeAnnotation(t.tsUndefinedKeyword()))])]) : typedUseQueryOptions), true)]; | ||
var props = getProps(jsonschema, true); | ||
@@ -136,2 +151,12 @@ | ||
}); | ||
}; | ||
}; | ||
function generateUseQueryQueryKey(hookKeyName, props, optionalClient) { | ||
var queryKey = [t.stringLiteral(hookKeyName), t.optionalMemberExpression(t.identifier('client'), t.identifier('contractAddress'), false, optionalClient)]; | ||
if (props.includes('args')) { | ||
queryKey.push(t.callExpression(t.memberExpression(t.identifier('JSON'), t.identifier('stringify')), [t.identifier('args')])); | ||
} | ||
return queryKey; | ||
} |
@@ -55,2 +55,19 @@ "use strict"; | ||
}))); | ||
expectCode(t.program((0, _reactQuery.createReactQueryHooks)({ | ||
queryMsg: _query_msg["default"], | ||
contractName: 'Sg721', | ||
QueryClient: 'Sg721QueryClient', | ||
options: { | ||
v4: true | ||
} | ||
}))); | ||
expectCode(t.program((0, _reactQuery.createReactQueryHooks)({ | ||
queryMsg: _query_msg["default"], | ||
contractName: 'Sg721', | ||
QueryClient: 'Sg721QueryClient', | ||
options: { | ||
optionalClient: true, | ||
v4: true | ||
} | ||
}))); | ||
}); |
@@ -34,3 +34,3 @@ "use strict"; | ||
it('client', function () { | ||
printCode((0, _recoil.createRecoilQueryClient)('SG721', 'SG721QueryClient')); | ||
expectCode((0, _recoil.createRecoilQueryClient)('SG721', 'SG721QueryClient')); | ||
}); |
@@ -0,1 +1,7 @@ | ||
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty"; | ||
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; } | ||
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; } | ||
import * as t from '@babel/types'; | ||
@@ -7,3 +13,4 @@ import { camel, pascal } from 'case'; | ||
const DEFAULT_OPTIONS = { | ||
optionalClient: false | ||
optionalClient: false, | ||
v4: false | ||
}; | ||
@@ -14,4 +21,6 @@ export const createReactQueryHooks = ({ | ||
QueryClient, | ||
options = DEFAULT_OPTIONS | ||
options = {} | ||
}) => { | ||
// merge the user options with the defaults | ||
options = _objectSpread(_objectSpread({}, DEFAULT_OPTIONS), options); | ||
return getMessageProperties(queryMsg).reduce((m, schema) => { | ||
@@ -49,4 +58,6 @@ const underscoreName = Object.keys(schema.properties)[0]; | ||
jsonschema, | ||
options = DEFAULT_OPTIONS | ||
options = {} | ||
}) => { | ||
// merge the user options with the defaults | ||
options = _objectSpread(_objectSpread({}, DEFAULT_OPTIONS), options); | ||
const keys = Object.keys(jsonschema.properties ?? {}); | ||
@@ -69,3 +80,4 @@ let args = []; | ||
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.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()])))])))]))); | ||
})], t.tsTypeAnnotation(t.tsTypeReference(t.identifier(hookParamsTypeName))))], t.blockStatement([t.returnStatement(callExpression(t.identifier('useQuery'), [t.arrayExpression(generateUseQueryQueryKey(hookKeyName, props, 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.conditionalExpression( // explicitly check for undefined | ||
t.binaryExpression('!=', t.optionalMemberExpression(t.identifier('options'), t.identifier('enabled'), false, true), t.identifier('undefined')), t.memberExpression(t.identifier('options'), t.identifier('enabled')), t.booleanLiteral(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()])))])))]))); | ||
}; | ||
@@ -77,5 +89,8 @@ export const createReactQueryHookInterface = ({ | ||
jsonschema, | ||
options = DEFAULT_OPTIONS | ||
options = {} | ||
}) => { | ||
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)]; | ||
// merge the user options with the defaults | ||
options = _objectSpread(_objectSpread({}, DEFAULT_OPTIONS), options); | ||
const typedUseQueryOptions = 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()])))])); | ||
const body = [tsPropertySignature(t.identifier('client'), t.tsTypeAnnotation(t.tsTypeReference(t.identifier(QueryClient))), options.optionalClient), tsPropertySignature(t.identifier('options'), t.tsTypeAnnotation(options.v4 ? t.tSIntersectionType([t.tsTypeReference(t.identifier('Omit'), t.tsTypeParameterInstantiation([typedUseQueryOptions, t.tsLiteralType(t.stringLiteral("'queryKey' | 'queryFn' | 'initialData'"))])), t.tSTypeLiteral([t.tsPropertySignature(t.identifier('initialData?'), t.tsTypeAnnotation(t.tsUndefinedKeyword()))])]) : typedUseQueryOptions), true)]; | ||
const props = getProps(jsonschema, true); | ||
@@ -100,2 +115,12 @@ | ||
}); | ||
}; | ||
}; | ||
function generateUseQueryQueryKey(hookKeyName, props, optionalClient) { | ||
const queryKey = [t.stringLiteral(hookKeyName), t.optionalMemberExpression(t.identifier('client'), t.identifier('contractAddress'), false, optionalClient)]; | ||
if (props.includes('args')) { | ||
queryKey.push(t.callExpression(t.memberExpression(t.identifier('JSON'), t.identifier('stringify')), [t.identifier('args')])); | ||
} | ||
return queryKey; | ||
} |
@@ -42,2 +42,19 @@ import generate from '@babel/generator'; | ||
}))); | ||
expectCode(t.program(createReactQueryHooks({ | ||
queryMsg: query_msg, | ||
contractName: 'Sg721', | ||
QueryClient: 'Sg721QueryClient', | ||
options: { | ||
v4: true | ||
} | ||
}))); | ||
expectCode(t.program(createReactQueryHooks({ | ||
queryMsg: query_msg, | ||
contractName: 'Sg721', | ||
QueryClient: 'Sg721QueryClient', | ||
options: { | ||
optionalClient: true, | ||
v4: true | ||
} | ||
}))); | ||
}); |
@@ -21,3 +21,3 @@ import generate from '@babel/generator'; | ||
it('client', () => { | ||
printCode(createRecoilQueryClient('SG721', 'SG721QueryClient')); | ||
expectCode(createRecoilQueryClient('SG721', 'SG721QueryClient')); | ||
}); |
{ | ||
"name": "wasm-ast-types", | ||
"version": "0.4.1", | ||
"version": "0.4.2", | ||
"description": "CosmWasm TypeScript AST generation", | ||
@@ -87,3 +87,3 @@ "author": "Dan Lynch <pyramation@gmail.com>", | ||
}, | ||
"gitHead": "b0312f7e16b8fd1c4892da6d91d3f7cf6c1bd49e" | ||
"gitHead": "aafba6dadee2e5a14b304f0dd53858e8b509046d" | ||
} |
@@ -21,2 +21,3 @@ import * as t from '@babel/types'; | ||
optionalClient?: boolean | ||
v4?: boolean | ||
} | ||
@@ -23,0 +24,0 @@ export declare const createReactQueryHooks: ({ queryMsg, contractName, QueryClient, options }: ReactQueryHooks) => any; |
162104
2653