wasm-ast-types 0.4.1 → 0.4.2
Sign up to get free protection for your applications and to get access to all the features.
- package/main/react-query.js +32 -7
- package/main/react-query.spec.js +17 -0
- package/main/recoil.spec.js +1 -1
- package/module/react-query.js +32 -7
- package/module/react-query.spec.js +17 -0
- package/module/recoil.spec.js +1 -1
- package/package.json +2 -2
- package/types/react-query.d.ts +1 -0
package/main/react-query.js
CHANGED
@@ -11,6 +11,8 @@ exports.createReactQueryHooks = exports.createReactQueryHookInterface = exports.
|
|
11
11
|
|
12
12
|
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
|
13
13
|
|
14
|
+
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
15
|
+
|
14
16
|
var t = _interopRequireWildcard(require("@babel/types"));
|
15
17
|
|
16
18
|
var _case = require("case");
|
@@ -25,8 +27,13 @@ function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "functio
|
|
25
27
|
|
26
28
|
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; }
|
27
29
|
|
30
|
+
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; }
|
31
|
+
|
32
|
+
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; }
|
33
|
+
|
28
34
|
var DEFAULT_OPTIONS = {
|
29
|
-
optionalClient: false
|
35
|
+
optionalClient: false,
|
36
|
+
v4: false
|
30
37
|
};
|
31
38
|
|
32
39
|
var createReactQueryHooks = function createReactQueryHooks(_ref) {
|
@@ -34,7 +41,9 @@ var createReactQueryHooks = function createReactQueryHooks(_ref) {
|
|
34
41
|
contractName = _ref.contractName,
|
35
42
|
QueryClient = _ref.QueryClient,
|
36
43
|
_ref$options = _ref.options,
|
37
|
-
options = _ref$options === void 0 ?
|
44
|
+
options = _ref$options === void 0 ? {} : _ref$options;
|
45
|
+
// merge the user options with the defaults
|
46
|
+
options = _objectSpread(_objectSpread({}, DEFAULT_OPTIONS), options);
|
38
47
|
return (0, _utils.getMessageProperties)(queryMsg).reduce(function (m, schema) {
|
39
48
|
var underscoreName = Object.keys(schema.properties)[0];
|
40
49
|
var methodName = (0, _case.camel)(underscoreName);
|
@@ -73,7 +82,9 @@ var createReactQueryHook = function createReactQueryHook(_ref2) {
|
|
73
82
|
methodName = _ref2.methodName,
|
74
83
|
jsonschema = _ref2.jsonschema,
|
75
84
|
_ref2$options = _ref2.options,
|
76
|
-
options = _ref2$options === void 0 ?
|
85
|
+
options = _ref2$options === void 0 ? {} : _ref2$options;
|
86
|
+
// merge the user options with the defaults
|
87
|
+
options = _objectSpread(_objectSpread({}, DEFAULT_OPTIONS), options);
|
77
88
|
var keys = Object.keys((_jsonschema$propertie = jsonschema.properties) !== null && _jsonschema$propertie !== void 0 ? _jsonschema$propertie : {});
|
78
89
|
var args = [];
|
79
90
|
|
@@ -91,7 +102,8 @@ var createReactQueryHook = function createReactQueryHook(_ref2) {
|
|
91
102
|
|
92
103
|
return t.exportNamedDeclaration(t.functionDeclaration(t.identifier(hookName), [(0, _utils.tsObjectPattern)((0, _toConsumableArray2["default"])(props.map(function (prop) {
|
93
104
|
return t.objectProperty(t.identifier(prop), t.identifier(prop), false, true);
|
94
|
-
})), t.tsTypeAnnotation(t.tsTypeReference(t.identifier(hookParamsTypeName))))], t.blockStatement([t.returnStatement((0, _utils.callExpression)(t.identifier('useQuery'), [t.arrayExpression(
|
105
|
+
})), 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
|
106
|
+
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()])))])))])));
|
95
107
|
};
|
96
108
|
|
97
109
|
exports.createReactQueryHook = createReactQueryHook;
|
@@ -102,8 +114,11 @@ var createReactQueryHookInterface = function createReactQueryHookInterface(_ref3
|
|
102
114
|
responseType = _ref3.responseType,
|
103
115
|
jsonschema = _ref3.jsonschema,
|
104
116
|
_ref3$options = _ref3.options,
|
105
|
-
options = _ref3$options === void 0 ?
|
106
|
-
|
117
|
+
options = _ref3$options === void 0 ? {} : _ref3$options;
|
118
|
+
// merge the user options with the defaults
|
119
|
+
options = _objectSpread(_objectSpread({}, DEFAULT_OPTIONS), options);
|
120
|
+
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()])))]));
|
121
|
+
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)];
|
107
122
|
var props = getProps(jsonschema, true);
|
108
123
|
|
109
124
|
if (props.length) {
|
@@ -127,4 +142,14 @@ var getProps = function getProps(jsonschema, camelize) {
|
|
127
142
|
|
128
143
|
return (0, _babel.propertySignature)(camelize ? (0, _case.camel)(prop) : prop, t.tsTypeAnnotation(type), optional);
|
129
144
|
});
|
130
|
-
};
|
145
|
+
};
|
146
|
+
|
147
|
+
function generateUseQueryQueryKey(hookKeyName, props, optionalClient) {
|
148
|
+
var queryKey = [t.stringLiteral(hookKeyName), t.optionalMemberExpression(t.identifier('client'), t.identifier('contractAddress'), false, optionalClient)];
|
149
|
+
|
150
|
+
if (props.includes('args')) {
|
151
|
+
queryKey.push(t.callExpression(t.memberExpression(t.identifier('JSON'), t.identifier('stringify')), [t.identifier('args')]));
|
152
|
+
}
|
153
|
+
|
154
|
+
return queryKey;
|
155
|
+
}
|
package/main/react-query.spec.js
CHANGED
@@ -52,4 +52,21 @@ it('createReactQueryHooks', function () {
|
|
52
52
|
optionalClient: true
|
53
53
|
}
|
54
54
|
})));
|
55
|
+
expectCode(t.program((0, _reactQuery.createReactQueryHooks)({
|
56
|
+
queryMsg: _query_msg["default"],
|
57
|
+
contractName: 'Sg721',
|
58
|
+
QueryClient: 'Sg721QueryClient',
|
59
|
+
options: {
|
60
|
+
v4: true
|
61
|
+
}
|
62
|
+
})));
|
63
|
+
expectCode(t.program((0, _reactQuery.createReactQueryHooks)({
|
64
|
+
queryMsg: _query_msg["default"],
|
65
|
+
contractName: 'Sg721',
|
66
|
+
QueryClient: 'Sg721QueryClient',
|
67
|
+
options: {
|
68
|
+
optionalClient: true,
|
69
|
+
v4: true
|
70
|
+
}
|
71
|
+
})));
|
55
72
|
});
|
package/main/recoil.spec.js
CHANGED
@@ -31,5 +31,5 @@ it('selectors', function () {
|
|
31
31
|
expectCode(t.program((0, _recoil.createRecoilSelectors)('SG721', 'SG721QueryClient', _query_msg["default"])));
|
32
32
|
});
|
33
33
|
it('client', function () {
|
34
|
-
|
34
|
+
expectCode((0, _recoil.createRecoilQueryClient)('SG721', 'SG721QueryClient'));
|
35
35
|
});
|
package/module/react-query.js
CHANGED
@@ -1,17 +1,26 @@
|
|
1
|
+
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
|
2
|
+
|
3
|
+
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; }
|
4
|
+
|
5
|
+
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; }
|
6
|
+
|
1
7
|
import * as t from '@babel/types';
|
2
8
|
import { camel, pascal } from 'case';
|
3
9
|
import { tsPropertySignature, tsObjectPattern, callExpression, getMessageProperties } from './utils';
|
4
10
|
import { typeRefOrOptionalUnion, propertySignature, optionalConditionalExpression } from './utils/babel';
|
5
11
|
import { getPropertyType } from './utils/types';
|
6
12
|
const DEFAULT_OPTIONS = {
|
7
|
-
optionalClient: false
|
13
|
+
optionalClient: false,
|
14
|
+
v4: false
|
8
15
|
};
|
9
16
|
export const createReactQueryHooks = ({
|
10
17
|
queryMsg,
|
11
18
|
contractName,
|
12
19
|
QueryClient,
|
13
|
-
options =
|
20
|
+
options = {}
|
14
21
|
}) => {
|
22
|
+
// merge the user options with the defaults
|
23
|
+
options = _objectSpread(_objectSpread({}, DEFAULT_OPTIONS), options);
|
15
24
|
return getMessageProperties(queryMsg).reduce((m, schema) => {
|
16
25
|
const underscoreName = Object.keys(schema.properties)[0];
|
17
26
|
const methodName = camel(underscoreName);
|
@@ -44,8 +53,10 @@ export const createReactQueryHook = ({
|
|
44
53
|
hookKeyName,
|
45
54
|
methodName,
|
46
55
|
jsonschema,
|
47
|
-
options =
|
56
|
+
options = {}
|
48
57
|
}) => {
|
58
|
+
// merge the user options with the defaults
|
59
|
+
options = _objectSpread(_objectSpread({}, DEFAULT_OPTIONS), options);
|
49
60
|
const keys = Object.keys(jsonschema.properties ?? {});
|
50
61
|
let args = [];
|
51
62
|
|
@@ -63,16 +74,20 @@ export const createReactQueryHook = ({
|
|
63
74
|
|
64
75
|
return t.exportNamedDeclaration(t.functionDeclaration(t.identifier(hookName), [tsObjectPattern([...props.map(prop => {
|
65
76
|
return t.objectProperty(t.identifier(prop), t.identifier(prop), false, true);
|
66
|
-
})], t.tsTypeAnnotation(t.tsTypeReference(t.identifier(hookParamsTypeName))))], t.blockStatement([t.returnStatement(callExpression(t.identifier('useQuery'), [t.arrayExpression(
|
77
|
+
})], 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
|
78
|
+
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()])))])))])));
|
67
79
|
};
|
68
80
|
export const createReactQueryHookInterface = ({
|
69
81
|
QueryClient,
|
70
82
|
hookParamsTypeName,
|
71
83
|
responseType,
|
72
84
|
jsonschema,
|
73
|
-
options =
|
85
|
+
options = {}
|
74
86
|
}) => {
|
75
|
-
|
87
|
+
// merge the user options with the defaults
|
88
|
+
options = _objectSpread(_objectSpread({}, DEFAULT_OPTIONS), options);
|
89
|
+
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()])))]));
|
90
|
+
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)];
|
76
91
|
const props = getProps(jsonschema, true);
|
77
92
|
|
78
93
|
if (props.length) {
|
@@ -92,4 +107,14 @@ const getProps = (jsonschema, camelize) => {
|
|
92
107
|
} = getPropertyType(jsonschema, prop);
|
93
108
|
return propertySignature(camelize ? camel(prop) : prop, t.tsTypeAnnotation(type), optional);
|
94
109
|
});
|
95
|
-
};
|
110
|
+
};
|
111
|
+
|
112
|
+
function generateUseQueryQueryKey(hookKeyName, props, optionalClient) {
|
113
|
+
const queryKey = [t.stringLiteral(hookKeyName), t.optionalMemberExpression(t.identifier('client'), t.identifier('contractAddress'), false, optionalClient)];
|
114
|
+
|
115
|
+
if (props.includes('args')) {
|
116
|
+
queryKey.push(t.callExpression(t.memberExpression(t.identifier('JSON'), t.identifier('stringify')), [t.identifier('args')]));
|
117
|
+
}
|
118
|
+
|
119
|
+
return queryKey;
|
120
|
+
}
|
@@ -39,4 +39,21 @@ it('createReactQueryHooks', () => {
|
|
39
39
|
optionalClient: true
|
40
40
|
}
|
41
41
|
})));
|
42
|
+
expectCode(t.program(createReactQueryHooks({
|
43
|
+
queryMsg: query_msg,
|
44
|
+
contractName: 'Sg721',
|
45
|
+
QueryClient: 'Sg721QueryClient',
|
46
|
+
options: {
|
47
|
+
v4: true
|
48
|
+
}
|
49
|
+
})));
|
50
|
+
expectCode(t.program(createReactQueryHooks({
|
51
|
+
queryMsg: query_msg,
|
52
|
+
contractName: 'Sg721',
|
53
|
+
QueryClient: 'Sg721QueryClient',
|
54
|
+
options: {
|
55
|
+
optionalClient: true,
|
56
|
+
v4: true
|
57
|
+
}
|
58
|
+
})));
|
42
59
|
});
|
package/module/recoil.spec.js
CHANGED
@@ -18,5 +18,5 @@ it('selectors', () => {
|
|
18
18
|
expectCode(t.program(createRecoilSelectors('SG721', 'SG721QueryClient', query_msg)));
|
19
19
|
});
|
20
20
|
it('client', () => {
|
21
|
-
|
21
|
+
expectCode(createRecoilQueryClient('SG721', 'SG721QueryClient'));
|
22
22
|
});
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "wasm-ast-types",
|
3
|
-
"version": "0.4.
|
3
|
+
"version": "0.4.2",
|
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",
|
@@ -84,5 +84,5 @@
|
|
84
84
|
"ast-stringify": "0.1.0",
|
85
85
|
"case": "1.6.3"
|
86
86
|
},
|
87
|
-
"gitHead": "
|
87
|
+
"gitHead": "aafba6dadee2e5a14b304f0dd53858e8b509046d"
|
88
88
|
}
|
package/types/react-query.d.ts
CHANGED
@@ -18,6 +18,7 @@ interface ReactQueryHookQuery {
|
|
18
18
|
|
19
19
|
export declare interface ReactQueryOptions {
|
20
20
|
optionalClient?: boolean
|
21
|
+
v4?: boolean
|
21
22
|
}
|
22
23
|
export declare const createReactQueryHooks: ({ queryMsg, contractName, QueryClient, options }: ReactQueryHooks) => any;
|
23
24
|
export declare const createReactQueryHook: ({ hookName, hookParamsTypeName, responseType, hookKeyName, methodName, jsonschema }: ReactQueryHookQuery) => t.ExportNamedDeclaration;
|