wasm-ast-types 0.4.3 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (98) hide show
  1. package/README.md +129 -0
  2. package/main/{wasm.js → client/client.js} +56 -40
  3. package/main/client/index.js +18 -0
  4. package/main/client/test/ts-client.arrays.spec.js +25 -0
  5. package/main/client/test/ts-client.cw-named-groups.test.js +25 -0
  6. package/main/client/test/ts-client.cw-proposal-single.test.js +30 -0
  7. package/main/client/test/ts-client.empty-enums.spec.js +19 -0
  8. package/main/client/test/ts-client.sg721.spec.js +25 -0
  9. package/main/client/test/ts-client.spec.js +106 -0
  10. package/main/client/test/ts-client.vectis.spec.js +56 -0
  11. package/main/context/context.js +48 -0
  12. package/main/context/index.js +18 -0
  13. package/main/{messages.js → from-partial/from-partial.js} +17 -60
  14. package/main/from-partial/from-partial.spec.js +20 -0
  15. package/main/from-partial/index.js +18 -0
  16. package/main/index.js +34 -8
  17. package/main/react-query/index.js +18 -0
  18. package/main/react-query/react-query.js +267 -0
  19. package/main/{react-query.spec.js → react-query/react-query.spec.js} +36 -39
  20. package/main/recoil/index.js +18 -0
  21. package/main/{recoil.js → recoil/recoil.js} +7 -26
  22. package/main/{recoil.spec.js → recoil/recoil.spec.js} +9 -14
  23. package/main/utils/babel.js +28 -2
  24. package/main/utils/babel.spec.js +7 -15
  25. package/main/utils/index.js +13 -0
  26. package/main/utils/types.js +100 -78
  27. package/module/{wasm.js → client/client.js} +45 -35
  28. package/module/client/index.js +1 -0
  29. package/module/client/test/ts-client.arrays.spec.js +17 -0
  30. package/module/client/test/ts-client.cw-named-groups.test.js +17 -0
  31. package/module/client/test/ts-client.cw-proposal-single.test.js +21 -0
  32. package/module/client/test/ts-client.empty-enums.spec.js +11 -0
  33. package/module/client/test/ts-client.sg721.spec.js +17 -0
  34. package/module/client/test/ts-client.spec.js +85 -0
  35. package/module/client/test/ts-client.vectis.spec.js +44 -0
  36. package/module/context/context.js +25 -0
  37. package/module/context/index.js +1 -0
  38. package/module/{messages.js → from-partial/from-partial.js} +16 -57
  39. package/module/from-partial/from-partial.spec.js +12 -0
  40. package/module/from-partial/index.js +1 -0
  41. package/module/index.js +5 -3
  42. package/module/react-query/index.js +1 -0
  43. package/module/react-query/react-query.js +222 -0
  44. package/module/react-query/react-query.spec.js +53 -0
  45. package/module/recoil/index.js +1 -0
  46. package/module/{recoil.js → recoil/recoil.js} +6 -20
  47. package/module/recoil/recoil.spec.js +15 -0
  48. package/module/utils/babel.js +15 -0
  49. package/module/utils/babel.spec.js +1 -10
  50. package/module/utils/index.js +2 -1
  51. package/module/utils/types.js +83 -74
  52. package/package.json +26 -24
  53. package/types/src/client/client.d.ts +17 -0
  54. package/types/src/client/index.d.ts +1 -0
  55. package/types/src/client/test/ts-client.cw-named-groups.test.d.ts +1 -0
  56. package/types/src/client/test/ts-client.cw-proposal-single.test.d.ts +1 -0
  57. package/types/src/context/context.d.ts +20 -0
  58. package/types/src/context/index.d.ts +1 -0
  59. package/types/src/from-partial/from-partial.d.ts +4 -0
  60. package/types/src/from-partial/index.d.ts +1 -0
  61. package/types/src/index.d.ts +7 -0
  62. package/types/src/react-query/index.d.ts +1 -0
  63. package/types/src/react-query/react-query.d.ts +81 -0
  64. package/types/src/recoil/index.d.ts +1 -0
  65. package/types/{recoil.d.ts → src/recoil/recoil.d.ts} +5 -5
  66. package/types/{types.d.ts → src/types.d.ts} +17 -0
  67. package/types/{utils.d.ts → src/utils/babel.d.ts} +21 -1
  68. package/types/src/utils/index.d.ts +2 -0
  69. package/types/src/utils/types.d.ts +18 -0
  70. package/types/test-utils/index.d.ts +2 -0
  71. package/main/messages.spec.js +0 -24
  72. package/main/react-query.js +0 -155
  73. package/main/wasm.arrays.spec.js +0 -30
  74. package/main/wasm.cw-named-groups.test.js +0 -30
  75. package/main/wasm.cw-proposal-single.test.js +0 -32
  76. package/main/wasm.empty-enums.spec.js +0 -24
  77. package/main/wasm.sg721.spec.js +0 -30
  78. package/main/wasm.spec.js +0 -95
  79. package/main/wasm.vectis.spec.js +0 -53
  80. package/module/messages.spec.js +0 -18
  81. package/module/react-query.js +0 -120
  82. package/module/react-query.spec.js +0 -59
  83. package/module/recoil.spec.js +0 -22
  84. package/module/wasm.arrays.spec.js +0 -24
  85. package/module/wasm.cw-named-groups.test.js +0 -24
  86. package/module/wasm.cw-proposal-single.test.js +0 -25
  87. package/module/wasm.empty-enums.spec.js +0 -18
  88. package/module/wasm.sg721.spec.js +0 -24
  89. package/module/wasm.spec.js +0 -76
  90. package/module/wasm.vectis.spec.js +0 -43
  91. package/types/amino/amino-interfaces.d.ts +0 -6
  92. package/types/amino/from-amino.d.ts +0 -13
  93. package/types/amino/index.d.ts +0 -3
  94. package/types/amino/to-amino.d.ts +0 -11
  95. package/types/index.d.ts +0 -5
  96. package/types/messages.d.ts +0 -4
  97. package/types/react-query.d.ts +0 -32
  98. package/types/wasm.d.ts +0 -24
@@ -0,0 +1,222 @@
1
+ import * as t from '@babel/types';
2
+ import { camel, pascal } from 'case';
3
+ import { callExpression, getMessageProperties, identifier, tsObjectPattern, tsPropertySignature } from '../utils';
4
+ import { omitTypeReference, optionalConditionalExpression, propertySignature, shorthandProperty, typeRefOrOptionalUnion } from '../utils/babel';
5
+ import { getParamsTypeAnnotation, getPropertyType } from '../utils/types';
6
+ import { FIXED_EXECUTE_PARAMS } from '../client';
7
+ export const createReactQueryHooks = ({
8
+ context,
9
+ queryMsg,
10
+ contractName,
11
+ QueryClient
12
+ }) => {
13
+ const options = context.options.reactQuery;
14
+ return getMessageProperties(queryMsg).reduce((m, schema) => {
15
+ const underscoreName = Object.keys(schema.properties)[0];
16
+ const methodName = camel(underscoreName);
17
+ const hookName = `use${pascal(contractName)}${pascal(methodName)}Query`;
18
+ const hookParamsTypeName = `${pascal(contractName)}${pascal(methodName)}Query`;
19
+ const responseType = pascal(`${methodName}Response`);
20
+ const getterKey = camel(`${contractName}${pascal(methodName)}`);
21
+ const jsonschema = schema.properties[underscoreName];
22
+ return [createReactQueryHookInterface({
23
+ context,
24
+ hookParamsTypeName,
25
+ responseType,
26
+ QueryClient,
27
+ jsonschema,
28
+ options
29
+ }), createReactQueryHook({
30
+ context,
31
+ methodName,
32
+ hookName,
33
+ hookParamsTypeName,
34
+ responseType,
35
+ hookKeyName: getterKey,
36
+ jsonschema
37
+ }), ...m];
38
+ }, []);
39
+ };
40
+ export const createReactQueryHook = ({
41
+ context,
42
+ hookName,
43
+ hookParamsTypeName,
44
+ responseType,
45
+ hookKeyName,
46
+ methodName,
47
+ jsonschema
48
+ }) => {
49
+ const options = context.options.reactQuery;
50
+ const keys = Object.keys(jsonschema.properties ?? {});
51
+ let args = [];
52
+
53
+ if (keys.length) {
54
+ args = [t.objectExpression([...keys.map(prop => {
55
+ return t.objectProperty(t.identifier(camel(prop)), t.memberExpression(t.identifier('args'), t.identifier(camel(prop))));
56
+ })])];
57
+ }
58
+
59
+ let props = ['client', 'options'];
60
+
61
+ if (keys.length) {
62
+ props = ['client', 'args', 'options'];
63
+ }
64
+
65
+ return t.exportNamedDeclaration(t.functionDeclaration(t.identifier(hookName), [tsObjectPattern([...props.map(prop => {
66
+ return t.objectProperty(t.identifier(prop), t.identifier(prop), false, true);
67
+ })], 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
68
+ 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()])))])))])));
69
+ };
70
+
71
+ /**
72
+ * Example:
73
+ ```
74
+ export interface Cw4UpdateMembersMutation {
75
+ client: Cw4GroupClient
76
+ args: {
77
+ tokenId: string
78
+ remove: string[]
79
+ }
80
+ options?: Omit<
81
+ UseMutationOptions<ExecuteResult, Error, Pick<Cw4UpdateMembersMutation, 'args'>>,
82
+ 'mutationFn'
83
+ >
84
+ }
85
+ ```
86
+ */
87
+ export const createReactQueryMutationArgsInterface = ({
88
+ context,
89
+ ExecuteClient,
90
+ mutationHookParamsTypeName,
91
+ useMutationTypeParameter,
92
+ jsonschema
93
+ }) => {
94
+ const typedUseMutationOptions = t.tsTypeReference(t.identifier('UseMutationOptions'), useMutationTypeParameter);
95
+ const body = [tsPropertySignature(t.identifier('client'), t.tsTypeAnnotation(t.tsTypeReference(t.identifier(ExecuteClient))), false)];
96
+ const msgType = getParamsTypeAnnotation(context, jsonschema);
97
+
98
+ if (msgType) {
99
+ body.push(t.tsPropertySignature(t.identifier('msg'), msgType));
100
+ } // fee: number | StdFee | "auto" = "auto", memo?: string, funds?: Coin[]
101
+
102
+
103
+ const optionalArgs = t.tsPropertySignature(t.identifier('args'), t.tsTypeAnnotation(t.tsTypeLiteral(FIXED_EXECUTE_PARAMS.map(param => propertySignature(param.name, param.typeAnnotation, param.optional)))));
104
+ optionalArgs.optional = true;
105
+ body.push(optionalArgs);
106
+ return t.exportNamedDeclaration(t.tsInterfaceDeclaration(t.identifier(mutationHookParamsTypeName), null, [], t.tsInterfaceBody(body)));
107
+ };
108
+ export const createReactQueryMutationHooks = ({
109
+ context,
110
+ execMsg,
111
+ contractName,
112
+ ExecuteClient
113
+ }) => {
114
+ // merge the user options with the defaults
115
+ return getMessageProperties(execMsg).reduce((m, schema) => {
116
+ // update_members
117
+ const execMethodUnderscoreName = Object.keys(schema.properties)[0]; // updateMembers
118
+
119
+ const execMethodName = camel(execMethodUnderscoreName); // Cw20UpdateMembersMutation
120
+
121
+ const mutationHookParamsTypeName = `${pascal(contractName)}${pascal(execMethodName)}Mutation`; // useCw20UpdateMembersMutation
122
+
123
+ const mutationHookName = `use${mutationHookParamsTypeName}`;
124
+ const jsonschema = schema.properties[execMethodUnderscoreName];
125
+ const properties = jsonschema.properties ?? {}; // TODO: there should be a better way to do this
126
+
127
+ const hasMsg = !!(Object.keys(properties)?.length || jsonschema?.$ref); // <ExecuteResult, Error, Cw4UpdateMembersMutation>
128
+
129
+ const useMutationTypeParameter = generateMutationTypeParameter(mutationHookParamsTypeName, hasMsg);
130
+ return [createReactQueryMutationArgsInterface({
131
+ context,
132
+ mutationHookParamsTypeName,
133
+ ExecuteClient,
134
+ jsonschema,
135
+ useMutationTypeParameter
136
+ }), createReactQueryMutationHook({
137
+ context,
138
+ execMethodName,
139
+ mutationHookName,
140
+ mutationHookParamsTypeName,
141
+ hasMsg,
142
+ useMutationTypeParameter
143
+ }), ...m];
144
+ }, []);
145
+ };
146
+ /**
147
+ * Generates the mutation type parameter. If args exist, we use a pick. If not, we just return the params type.
148
+ */
149
+
150
+ function generateMutationTypeParameter(mutationHookParamsTypeName, hasArgs) {
151
+ return t.tsTypeParameterInstantiation([// Data
152
+ t.tSTypeReference(t.identifier('ExecuteResult')), // Error
153
+ t.tsTypeReference(t.identifier('Error')), // Variables
154
+ t.tsTypeReference(t.identifier(mutationHookParamsTypeName))]);
155
+ }
156
+
157
+ /**
158
+ *
159
+ * Example:
160
+ ```
161
+ export const useCw4UpdateMembersMutation = ({ client, options }: Omit<Cw4UpdateMembersMutation, 'args'>) =>
162
+ useMutation<ExecuteResult, Error, Pick<Cw4UpdateMembersMutation, 'args'>>(
163
+ ({ args }) => client.updateMembers(args),
164
+ options
165
+ )
166
+ ```
167
+ */
168
+ export const createReactQueryMutationHook = ({
169
+ context,
170
+ mutationHookName,
171
+ mutationHookParamsTypeName,
172
+ execMethodName,
173
+ useMutationTypeParameter,
174
+ hasMsg
175
+ }) => {
176
+ const useMutationFunctionArgs = [shorthandProperty('client')];
177
+ if (hasMsg) useMutationFunctionArgs.push(shorthandProperty('msg'));
178
+ useMutationFunctionArgs.push(t.objectProperty(t.identifier('args'), t.assignmentPattern(t.objectPattern(FIXED_EXECUTE_PARAMS.map(param => shorthandProperty(param.name))), t.objectExpression([]))));
179
+ return t.exportNamedDeclaration(t.functionDeclaration(t.identifier(mutationHookName), [identifier('options', t.tsTypeAnnotation(omitTypeReference(t.tsTypeReference(t.identifier('UseMutationOptions'), useMutationTypeParameter), 'mutationFn')), true)], t.blockStatement([t.returnStatement(callExpression(t.identifier('useMutation'), [t.arrowFunctionExpression([t.objectPattern(useMutationFunctionArgs)], t.callExpression(t.memberExpression(t.identifier('client'), t.identifier(execMethodName)), (hasMsg ? [t.identifier('msg')] : []).concat(FIXED_EXECUTE_PARAMS.map(param => t.identifier(param.name)))), false // not async
180
+ ), t.identifier('options')], useMutationTypeParameter))])));
181
+ };
182
+ export const createReactQueryHookInterface = ({
183
+ context,
184
+ QueryClient,
185
+ hookParamsTypeName,
186
+ responseType,
187
+ jsonschema
188
+ }) => {
189
+ // merge the user options with the defaults
190
+ const options = context.options.reactQuery;
191
+ 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()])))]));
192
+ 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([omitTypeReference(typedUseQueryOptions, "'queryKey' | 'queryFn' | 'initialData'"), t.tSTypeLiteral([t.tsPropertySignature(t.identifier('initialData?'), t.tsTypeAnnotation(t.tsUndefinedKeyword()))])]) : typedUseQueryOptions), true)];
193
+ const props = getProps(context, jsonschema);
194
+
195
+ if (props.length) {
196
+ body.push(t.tsPropertySignature(t.identifier('args'), t.tsTypeAnnotation(t.tsTypeLiteral(props))));
197
+ }
198
+
199
+ return t.exportNamedDeclaration(t.tsInterfaceDeclaration(t.identifier(hookParamsTypeName), null, [], t.tsInterfaceBody(body)));
200
+ };
201
+
202
+ const getProps = (context, jsonschema) => {
203
+ const keys = Object.keys(jsonschema.properties ?? {});
204
+ if (!keys.length) return [];
205
+ return keys.map(prop => {
206
+ const {
207
+ type,
208
+ optional
209
+ } = getPropertyType(context, jsonschema, prop);
210
+ return propertySignature(context.options.reactQuery.camelize ? camel(prop) : prop, t.tsTypeAnnotation(type), optional);
211
+ });
212
+ };
213
+
214
+ const generateUseQueryQueryKey = (hookKeyName, props, optionalClient) => {
215
+ const queryKey = [t.stringLiteral(hookKeyName), t.optionalMemberExpression(t.identifier('client'), t.identifier('contractAddress'), false, optionalClient)];
216
+
217
+ if (props.includes('args')) {
218
+ queryKey.push(t.callExpression(t.memberExpression(t.identifier('JSON'), t.identifier('stringify')), [t.identifier('args')]));
219
+ }
220
+
221
+ return queryKey;
222
+ };
@@ -0,0 +1,53 @@
1
+ import * as t from '@babel/types';
2
+ import query_msg from '../../../../__fixtures__/basic/query_msg.json';
3
+ import execute_msg from '../../../../__fixtures__/basic/execute_msg_for__empty.json';
4
+ import { RenderContext } from '../context';
5
+ import { createReactQueryHooks, createReactQueryMutationHooks } from './react-query';
6
+ import { expectCode } from '../../test-utils';
7
+ const execCtx = new RenderContext(execute_msg);
8
+ const queryCtx = new RenderContext(query_msg);
9
+ it('createReactQueryHooks', () => {
10
+ expectCode(t.program(createReactQueryHooks({
11
+ context: queryCtx,
12
+ queryMsg: query_msg,
13
+ contractName: 'Sg721',
14
+ QueryClient: 'Sg721QueryClient'
15
+ })));
16
+ expectCode(t.program(createReactQueryHooks({
17
+ context: new RenderContext(query_msg, {
18
+ reactQuery: {
19
+ optionalClient: true
20
+ }
21
+ }),
22
+ queryMsg: query_msg,
23
+ contractName: 'Sg721',
24
+ QueryClient: 'Sg721QueryClient'
25
+ })));
26
+ expectCode(t.program(createReactQueryHooks({
27
+ context: new RenderContext(query_msg, {
28
+ reactQuery: {
29
+ v4: true
30
+ }
31
+ }),
32
+ queryMsg: query_msg,
33
+ contractName: 'Sg721',
34
+ QueryClient: 'Sg721QueryClient'
35
+ })));
36
+ expectCode(t.program(createReactQueryHooks({
37
+ context: new RenderContext(query_msg, {
38
+ reactQuery: {
39
+ optionalClient: true,
40
+ v4: true
41
+ }
42
+ }),
43
+ queryMsg: query_msg,
44
+ contractName: 'Sg721',
45
+ QueryClient: 'Sg721QueryClient'
46
+ })));
47
+ expectCode(t.program(createReactQueryMutationHooks({
48
+ context: execCtx,
49
+ execMsg: execute_msg,
50
+ contractName: 'Sg721',
51
+ ExecuteClient: 'Sg721Client'
52
+ })));
53
+ });
@@ -0,0 +1 @@
1
+ export * from './recoil';
@@ -1,31 +1,17 @@
1
1
  import * as t from '@babel/types';
2
2
  import { camel, pascal } from 'case';
3
- import { callExpression, getMessageProperties } from './utils';
4
- export const createWasmRecoilMethod = jsonschema => {
5
- const underscoreName = Object.keys(jsonschema.properties)[0];
6
- const methodName = camel(underscoreName);
7
- const responseType = pascal(`${methodName}Response`);
8
- const properties = jsonschema.properties[underscoreName].properties ?? {};
9
-
10
- if (Object.keys(properties).length > 0) {} else {}
11
- };
12
- export const createRecoilSelector = (keyPrefix, QueryClient, methodName) => {
13
- // const propertyNames = getMessageProperties(queryMsg)
14
- // .map(method => Object.keys(method.properties)?.[0])
15
- // .filter(Boolean);
16
- // const methods = getMessageProperties(queryMsg)
17
- // .map(schema => createWasmRecoilMethod(schema))
3
+ import { callExpression, getMessageProperties } from '../utils';
4
+ export const createRecoilSelector = (context, keyPrefix, QueryClient, methodName) => {
18
5
  const selectorName = camel(`${methodName}Selector`);
19
6
  const responseType = pascal(`${methodName}Response`);
20
7
  const getterKey = camel(`${keyPrefix}${pascal(methodName)}`);
21
- return t.exportNamedDeclaration(t.variableDeclaration('const', [t.variableDeclarator(t.identifier(selectorName), callExpression(t.identifier('selectorFamily'), [t.objectExpression([t.objectProperty(t.identifier('key'), t.stringLiteral(getterKey)), t.objectProperty(t.identifier('get'), t.arrowFunctionExpression([t.objectPattern([t.objectProperty(t.identifier('params'), t.identifier('params'), false, true), t.restElement(t.identifier('queryClientParams'))])], t.arrowFunctionExpression([t.objectPattern([t.objectProperty(t.identifier('get'), t.identifier('get'), false, true)])], t.blockStatement([t.variableDeclaration('const', [t.variableDeclarator(t.identifier('client'), t.callExpression(t.identifier('get'), [t.callExpression(t.identifier('queryClient'), [t.identifier('queryClientParams')])]))]), t.returnStatement(t.awaitExpression(t.callExpression(t.memberExpression(t.identifier('client'), t.identifier(methodName)), [// t.identifier('params')
22
- t.spreadElement(t.identifier('params'))])))]), true)))])], t.tsTypeParameterInstantiation([t.tsTypeReference(t.identifier(responseType)), t.tsIntersectionType([t.tsTypeReference(t.identifier('QueryClientParams')), t.tsTypeLiteral([t.tsPropertySignature(t.identifier('params'), t.tsTypeAnnotation(t.tsTypeReference(t.identifier('Parameters'), t.tsTypeParameterInstantiation([t.tsIndexedAccessType(t.tsTypeReference(t.identifier(QueryClient)), t.tsLiteralType(t.stringLiteral(methodName)))]))))])])])))]));
8
+ return t.exportNamedDeclaration(t.variableDeclaration('const', [t.variableDeclarator(t.identifier(selectorName), callExpression(t.identifier('selectorFamily'), [t.objectExpression([t.objectProperty(t.identifier('key'), t.stringLiteral(getterKey)), t.objectProperty(t.identifier('get'), t.arrowFunctionExpression([t.objectPattern([t.objectProperty(t.identifier('params'), t.identifier('params'), false, true), t.restElement(t.identifier('queryClientParams'))])], t.arrowFunctionExpression([t.objectPattern([t.objectProperty(t.identifier('get'), t.identifier('get'), false, true)])], t.blockStatement([t.variableDeclaration('const', [t.variableDeclarator(t.identifier('client'), t.callExpression(t.identifier('get'), [t.callExpression(t.identifier('queryClient'), [t.identifier('queryClientParams')])]))]), t.returnStatement(t.awaitExpression(t.callExpression(t.memberExpression(t.identifier('client'), t.identifier(methodName)), [t.spreadElement(t.identifier('params'))])))]), true)))])], t.tsTypeParameterInstantiation([t.tsTypeReference(t.identifier(responseType)), t.tsIntersectionType([t.tsTypeReference(t.identifier('QueryClientParams')), t.tsTypeLiteral([t.tsPropertySignature(t.identifier('params'), t.tsTypeAnnotation(t.tsTypeReference(t.identifier('Parameters'), t.tsTypeParameterInstantiation([t.tsIndexedAccessType(t.tsTypeReference(t.identifier(QueryClient)), t.tsLiteralType(t.stringLiteral(methodName)))]))))])])])))]));
23
9
  };
24
- export const createRecoilSelectors = (keyPrefix, QueryClient, queryMsg) => {
10
+ export const createRecoilSelectors = (context, keyPrefix, QueryClient, queryMsg) => {
25
11
  return getMessageProperties(queryMsg).map(schema => {
26
12
  const underscoreName = Object.keys(schema.properties)[0];
27
13
  const methodName = camel(underscoreName);
28
- return createRecoilSelector(keyPrefix, QueryClient, methodName);
14
+ return createRecoilSelector(context, keyPrefix, QueryClient, methodName);
29
15
  });
30
16
  };
31
17
  export const createRecoilQueryClientType = () => ({
@@ -52,7 +38,7 @@ export const createRecoilQueryClientType = () => ({
52
38
  }]
53
39
  }
54
40
  });
55
- export const createRecoilQueryClient = (keyPrefix, QueryClient) => {
41
+ export const createRecoilQueryClient = (context, keyPrefix, QueryClient) => {
56
42
  const getterKey = camel(`${keyPrefix}${'QueryClient'}`);
57
43
  return t.exportNamedDeclaration(t.variableDeclaration('const', [t.variableDeclarator(t.identifier('queryClient'), callExpression(t.identifier('selectorFamily'), [t.objectExpression([t.objectProperty(t.identifier('key'), t.stringLiteral(getterKey)), t.objectProperty(t.identifier('get'), t.arrowFunctionExpression([t.objectPattern([t.objectProperty(t.identifier('contractAddress'), t.identifier('contractAddress'), false, true)])], t.arrowFunctionExpression([t.objectPattern([t.objectProperty(t.identifier('get'), t.identifier('get'), false, true)])], t.blockStatement([t.variableDeclaration('const', [t.variableDeclarator(t.identifier('client'), t.callExpression(t.identifier('get'), [t.identifier('cosmWasmClient')]))]), t.returnStatement(t.newExpression(t.identifier(QueryClient), [t.identifier('client'), t.identifier('contractAddress')]))]), false)))])], t.tsTypeParameterInstantiation([t.tsTypeReference(t.identifier(QueryClient)), t.tsTypeReference(t.identifier('QueryClientParams'))])))]));
58
44
  };
@@ -0,0 +1,15 @@
1
+ import * as t from '@babel/types';
2
+ import query_msg from '../../../../__fixtures__/basic/query_msg.json';
3
+ import { createRecoilSelector, createRecoilSelectors, createRecoilQueryClient } from './recoil';
4
+ import { RenderContext } from '../context';
5
+ import { expectCode } from '../../test-utils';
6
+ const ctx = new RenderContext(query_msg);
7
+ it('selector', () => {
8
+ expectCode(createRecoilSelector(ctx, 'SG721', 'SG721QueryClient', 'governanceModules'));
9
+ });
10
+ it('selectors', () => {
11
+ expectCode(t.program(createRecoilSelectors(ctx, 'SG721', 'SG721QueryClient', query_msg)));
12
+ });
13
+ it('client', () => {
14
+ expectCode(createRecoilQueryClient(ctx, 'SG721', 'SG721QueryClient'));
15
+ });
@@ -1,5 +1,6 @@
1
1
  import * as t from '@babel/types';
2
2
  import { snake } from "case";
3
+ // t.TSPropertySignature - kind?
3
4
  export const propertySignature = (name, typeAnnotation, optional = false) => {
4
5
  return {
5
6
  type: 'TSPropertySignature',
@@ -164,4 +165,18 @@ export const optionalConditionalExpression = (test, expression, alternate, optio
164
165
  export const typeRefOrOptionalUnion = (identifier, optional = false) => {
165
166
  const typeReference = t.tsTypeReference(identifier);
166
167
  return optional ? t.tsUnionType([typeReference, t.tsUndefinedKeyword()]) : typeReference;
168
+ };
169
+ export const parameterizedTypeReference = (identifier, from, omit) => {
170
+ return t.tsTypeReference(t.identifier(identifier), t.tsTypeParameterInstantiation([from, typeof omit === 'string' ? t.tsLiteralType(t.stringLiteral(omit)) : t.tsUnionType(omit.map(o => t.tsLiteralType(t.stringLiteral(o))))]));
171
+ };
172
+ /**
173
+ * omitTypeReference(t.tsTypeReference(t.identifier('Cw4UpdateMembersMutation'),),'args').....
174
+ * Omit<Cw4UpdateMembersMutation, 'args'>
175
+ */
176
+
177
+ export const omitTypeReference = (from, omit) => {
178
+ return parameterizedTypeReference('Omit', from, omit);
179
+ };
180
+ export const pickTypeReference = (from, pick) => {
181
+ return parameterizedTypeReference('Pick', from, pick);
167
182
  };
@@ -1,16 +1,7 @@
1
1
  import { importStmt } from './babel';
2
- import generate from '@babel/generator';
3
2
  import * as t from '@babel/types';
4
3
  import { bindMethod, typedIdentifier, promiseTypeAnnotation, classDeclaration, classProperty, arrowFunctionExpression } from './babel';
5
-
6
- const expectCode = ast => {
7
- expect(generate(ast).code).toMatchSnapshot();
8
- };
9
-
10
- const printCode = ast => {
11
- console.log(generate(ast).code);
12
- };
13
-
4
+ import { expectCode } from '../../test-utils';
14
5
  it('top import', async () => {
15
6
  expectCode(importStmt(['CosmWasmClient', 'ExecuteResult', 'SigningCosmWasmClient'], '@cosmjs/cosmwasm-stargate'));
16
7
  });
@@ -1 +1,2 @@
1
- export * from './babel';
1
+ export * from './babel';
2
+ export * from './types';
@@ -3,31 +3,15 @@ import { camel } from 'case';
3
3
  import { propertySignature } from './babel';
4
4
 
5
5
  const getTypeStrFromRef = $ref => {
6
- switch ($ref) {
7
- case '#/definitions/Binary':
8
- return 'Binary';
9
-
10
- default:
11
- if ($ref?.startsWith('#/definitions/')) {
12
- return $ref.replace('#/definitions/', '');
13
- }
14
-
15
- throw new Error('what is $ref: ' + $ref);
6
+ if ($ref?.startsWith('#/definitions/')) {
7
+ return $ref.replace('#/definitions/', '');
16
8
  }
17
- };
18
-
19
- const getTypeFromRef = $ref => {
20
- switch ($ref) {
21
- case '#/definitions/Binary':
22
- return t.tsTypeReference(t.identifier('Binary'));
23
9
 
24
- default:
25
- if ($ref?.startsWith('#/definitions/')) {
26
- return t.tsTypeReference(t.identifier($ref.replace('#/definitions/', '')));
27
- }
10
+ throw new Error('what is $ref: ' + $ref);
11
+ };
28
12
 
29
- throw new Error('what is $ref: ' + $ref);
30
- }
13
+ export const getTypeFromRef = $ref => {
14
+ return t.tsTypeReference(t.identifier(getTypeStrFromRef($ref)));
31
15
  };
32
16
 
33
17
  const getArrayTypeFromRef = $ref => {
@@ -61,7 +45,7 @@ export const getType = type => {
61
45
  throw new Error('contact maintainers [unknown type]: ' + type);
62
46
  }
63
47
  };
64
- export const getPropertyType = (schema, prop) => {
48
+ export const getPropertyType = (context, schema, prop) => {
65
49
  const props = schema.properties ?? {};
66
50
  let info = props[prop];
67
51
  let type = null;
@@ -150,65 +134,90 @@ export const getPropertyType = (schema, prop) => {
150
134
  optional
151
135
  };
152
136
  };
153
- export const createTypedObjectParams = (jsonschema, camelize = true) => {
154
- const keys = Object.keys(jsonschema.properties ?? {});
155
- if (!keys.length) return;
156
- const typedParams = keys.map(prop => {
157
- if (jsonschema.properties[prop].type === 'object') {
158
- if (jsonschema.properties[prop].title) {
159
- return propertySignature(camelize ? camel(prop) : prop, t.tsTypeAnnotation(t.tsTypeReference(t.identifier(jsonschema.properties[prop].title))));
160
- } else {
161
- throw new Error('createTypedObjectParams() contact maintainer');
162
- }
137
+ export function getPropertySignatureFromProp(context, jsonschema, prop, camelize) {
138
+ if (jsonschema.properties[prop].type === 'object') {
139
+ if (jsonschema.properties[prop].title) {
140
+ return propertySignature(camelize ? camel(prop) : prop, t.tsTypeAnnotation(t.tsTypeReference(t.identifier(jsonschema.properties[prop].title))));
141
+ } else {
142
+ throw new Error('createTypedObjectParams() contact maintainer');
163
143
  }
144
+ }
164
145
 
165
- if (Array.isArray(jsonschema.properties[prop].allOf)) {
166
- const isOptional = !jsonschema.required?.includes(prop);
167
- const unionTypes = jsonschema.properties[prop].allOf.map(el => {
168
- if (el.title) return el.title;
169
- if (el.$ref) return getTypeStrFromRef(el.$ref);
170
- return el.type;
171
- });
172
- const uniqUnionTypes = [...new Set(unionTypes)];
173
-
174
- if (uniqUnionTypes.length === 1) {
175
- return propertySignature(camelize ? camel(prop) : prop, t.tsTypeAnnotation(t.tsTypeReference(t.identifier(uniqUnionTypes[0]))), isOptional);
176
- } else {
177
- return propertySignature(camelize ? camel(prop) : prop, t.tsTypeAnnotation(t.tsUnionType(uniqUnionTypes.map(typ => t.tsTypeReference(t.identifier(typ))))), isOptional);
178
- }
179
- } else if (Array.isArray(jsonschema.properties[prop].oneOf)) {
180
- const isOptional = !jsonschema.required?.includes(prop);
181
- const unionTypes = jsonschema.properties[prop].oneOf.map(el => {
182
- if (el.title) return el.title;
183
- if (el.$ref) return getTypeStrFromRef(el.$ref);
184
- return el.type;
185
- });
186
- const uniqUnionTypes = [...new Set(unionTypes)];
187
-
188
- if (uniqUnionTypes.length === 1) {
189
- return propertySignature(camelize ? camel(prop) : prop, t.tsTypeAnnotation(t.tsTypeReference(t.identifier(uniqUnionTypes[0]))), isOptional);
190
- } else {
191
- return propertySignature(camelize ? camel(prop) : prop, t.tsTypeAnnotation(t.tsUnionType(uniqUnionTypes.map(typ => t.tsTypeReference(t.identifier(typ))))), isOptional);
192
- }
146
+ if (Array.isArray(jsonschema.properties[prop].allOf)) {
147
+ const isOptional = !jsonschema.required?.includes(prop);
148
+ const unionTypes = jsonschema.properties[prop].allOf.map(el => {
149
+ if (el.title) return el.title;
150
+ if (el.$ref) return getTypeStrFromRef(el.$ref);
151
+ return el.type;
152
+ });
153
+ const uniqUnionTypes = [...new Set(unionTypes)];
154
+
155
+ if (uniqUnionTypes.length === 1) {
156
+ return propertySignature(camelize ? camel(prop) : prop, t.tsTypeAnnotation(t.tsTypeReference(t.identifier(uniqUnionTypes[0]))), isOptional);
157
+ } else {
158
+ return propertySignature(camelize ? camel(prop) : prop, t.tsTypeAnnotation(t.tsUnionType(uniqUnionTypes.map(typ => t.tsTypeReference(t.identifier(typ))))), isOptional);
193
159
  }
194
-
195
- try {
196
- getPropertyType(jsonschema, prop);
197
- } catch (e) {
198
- console.log(e);
199
- console.log(jsonschema, prop);
160
+ } else if (Array.isArray(jsonschema.properties[prop].oneOf)) {
161
+ const isOptional = !jsonschema.required?.includes(prop);
162
+ const unionTypes = jsonschema.properties[prop].oneOf.map(el => {
163
+ if (el.title) return el.title;
164
+ if (el.$ref) return getTypeStrFromRef(el.$ref);
165
+ return el.type;
166
+ });
167
+ const uniqUnionTypes = [...new Set(unionTypes)];
168
+
169
+ if (uniqUnionTypes.length === 1) {
170
+ return propertySignature(camelize ? camel(prop) : prop, t.tsTypeAnnotation(t.tsTypeReference(t.identifier(uniqUnionTypes[0]))), isOptional);
171
+ } else {
172
+ return propertySignature(camelize ? camel(prop) : prop, t.tsTypeAnnotation(t.tsUnionType(uniqUnionTypes.map(typ => t.tsTypeReference(t.identifier(typ))))), isOptional);
200
173
  }
174
+ }
175
+
176
+ try {
177
+ getPropertyType(context, jsonschema, prop);
178
+ } catch (e) {
179
+ console.log(e);
180
+ console.log(jsonschema, prop);
181
+ }
182
+
183
+ const {
184
+ type,
185
+ optional
186
+ } = getPropertyType(context, jsonschema, prop);
187
+ return propertySignature(camelize ? camel(prop) : prop, t.tsTypeAnnotation(type), optional);
188
+ }
189
+ export const getParamsTypeAnnotation = (context, jsonschema, camelize = true) => {
190
+ const keys = Object.keys(jsonschema.properties ?? {});
191
+
192
+ if (!keys.length && jsonschema.$ref) {
193
+ return t.tsTypeAnnotation(getTypeFromRef(jsonschema.$ref));
194
+ }
195
+
196
+ if (!keys.length) return undefined;
197
+ const typedParams = keys.map(prop => getPropertySignatureFromProp(context, jsonschema, prop, camelize));
198
+ return t.tsTypeAnnotation(t.tsTypeLiteral([...typedParams]));
199
+ };
200
+ export const createTypedObjectParams = (context, jsonschema, camelize = true) => {
201
+ const keys = Object.keys(jsonschema.properties ?? {});
202
+
203
+ if (!keys.length) {
204
+ // is there a ref?
205
+ if (jsonschema.$ref) {
206
+ const obj = context.refLookup(jsonschema.$ref);
207
+
208
+ if (obj) {
209
+ return createTypedObjectParams(context, obj, camelize);
210
+ }
211
+ } // no results...
212
+
213
+
214
+ return;
215
+ }
201
216
 
202
- const {
203
- type,
204
- optional
205
- } = getPropertyType(jsonschema, prop);
206
- return propertySignature(camelize ? camel(prop) : prop, t.tsTypeAnnotation(type), optional);
207
- });
208
217
  const params = keys.map(prop => {
209
218
  return t.objectProperty(camelize ? t.identifier(camel(prop)) : t.identifier(prop), camelize ? t.identifier(camel(prop)) : t.identifier(prop), false, true);
210
219
  });
211
220
  const obj = t.objectPattern([...params]);
212
- obj.typeAnnotation = t.tsTypeAnnotation(t.tsTypeLiteral([...typedParams]));
221
+ obj.typeAnnotation = getParamsTypeAnnotation(context, jsonschema, camelize);
213
222
  return obj;
214
223
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wasm-ast-types",
3
- "version": "0.4.3",
3
+ "version": "0.6.0",
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",
@@ -48,41 +48,43 @@
48
48
  "url": "https://github.com/pyramation/cosmwasm-typescript-gen/issues"
49
49
  },
50
50
  "devDependencies": {
51
- "@babel/cli": "7.17.10",
52
- "@babel/core": "7.18.5",
53
- "@babel/eslint-parser": "^7.18.2",
54
- "@babel/generator": "7.18.2",
55
- "@babel/node": "^7.18.5",
56
- "@babel/parser": "^7.18.5",
57
- "@babel/plugin-proposal-class-properties": "7.17.12",
58
- "@babel/plugin-proposal-export-default-from": "7.17.12",
59
- "@babel/plugin-proposal-object-rest-spread": "7.18.0",
60
- "@babel/plugin-transform-runtime": "7.18.5",
61
- "@babel/preset-env": "7.18.2",
62
- "@babel/preset-typescript": "^7.17.12",
63
- "@babel/traverse": "7.18.5",
64
- "@types/jest": "^28.1.2",
51
+ "@babel/cli": "7.18.10",
52
+ "@babel/core": "7.18.10",
53
+ "@babel/eslint-parser": "^7.18.9",
54
+ "@babel/generator": "7.18.12",
55
+ "@babel/node": "^7.18.10",
56
+ "@babel/parser": "^7.18.11",
57
+ "@babel/plugin-proposal-class-properties": "7.18.6",
58
+ "@babel/plugin-proposal-export-default-from": "7.18.10",
59
+ "@babel/plugin-proposal-object-rest-spread": "7.18.9",
60
+ "@babel/plugin-transform-runtime": "7.18.10",
61
+ "@babel/preset-env": "7.18.10",
62
+ "@babel/preset-typescript": "^7.18.6",
63
+ "@babel/traverse": "7.18.11",
64
+ "@types/jest": "^28.1.6",
65
65
  "babel-core": "7.0.0-bridge.0",
66
- "babel-jest": "28.1.1",
66
+ "babel-jest": "^28.1.3",
67
67
  "babel-watch": "^7.0.0",
68
68
  "cross-env": "^7.0.2",
69
- "eslint": "8.17.0",
69
+ "eslint": "8.21.0",
70
70
  "eslint-config-prettier": "^8.5.0",
71
- "eslint-plugin-prettier": "^4.0.0",
71
+ "eslint-plugin-prettier": "^4.2.1",
72
72
  "glob": "8.0.3",
73
- "jest": "^28.1.1",
73
+ "jest": "28.1.3",
74
74
  "jest-in-case": "^1.0.2",
75
75
  "mkdirp": "1.0.4",
76
76
  "prettier": "^2.7.1",
77
77
  "regenerator-runtime": "^0.13.7",
78
- "ts-jest": "^28.0.5",
78
+ "ts-jest": "^28.0.7",
79
79
  "typescript": "^4.7.4"
80
80
  },
81
81
  "dependencies": {
82
- "@babel/runtime": "^7.18.3",
83
- "@babel/types": "7.18.4",
82
+ "@babel/runtime": "^7.18.9",
83
+ "@babel/types": "7.18.10",
84
+ "@jest/transform": "28.1.3",
84
85
  "ast-stringify": "0.1.0",
85
- "case": "1.6.3"
86
+ "case": "1.6.3",
87
+ "deepmerge": "4.2.2"
86
88
  },
87
- "gitHead": "c5fc4992d9da5f1c9da1f2c34469994dee7801df"
89
+ "gitHead": "51e6e530adc95d8118606379848080dd9bc3c25a"
88
90
  }