wasm-ast-types 0.26.4 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- package/LICENSE-Apache +201 -0
- package/LICENSE-MIT +21 -0
- package/README.md +1 -1
- package/{types/client → client}/client.d.ts +2 -2
- package/client/client.js +275 -0
- package/client/index.js +17 -0
- package/{types/context → context}/context.d.ts +9 -22
- package/context/context.js +114 -0
- package/{types/context → context}/imports.d.ts +9 -8
- package/context/imports.js +166 -0
- package/context/index.js +18 -0
- package/esm/client/client.js +238 -0
- package/esm/client/index.js +1 -0
- package/esm/context/context.js +104 -0
- package/esm/context/imports.js +136 -0
- package/{module → esm}/context/index.js +1 -1
- package/{src/index.ts → esm/index.js} +0 -1
- package/esm/message-builder/message-builder.js +64 -0
- package/esm/message-composer/message-composer.js +105 -0
- package/esm/provider/provider.js +97 -0
- package/esm/react-query/react-query.js +458 -0
- package/esm/recoil/recoil.js +110 -0
- package/esm/utils/babel.js +231 -0
- package/esm/utils/constants.js +20 -0
- package/esm/utils/ref.js +4 -0
- package/esm/utils/types.js +343 -0
- package/{types/index.d.ts → index.d.ts} +0 -1
- package/index.js +24 -0
- package/message-builder/index.js +17 -0
- package/{types/message-builder → message-builder}/message-builder.d.ts +1 -1
- package/message-builder/message-builder.js +91 -0
- package/message-composer/index.d.ts +1 -0
- package/message-composer/index.js +17 -0
- package/{types/message-composer → message-composer}/message-composer.d.ts +1 -1
- package/message-composer/message-composer.js +133 -0
- package/package.json +19 -68
- package/provider/index.d.ts +1 -0
- package/provider/index.js +17 -0
- package/provider/provider.js +127 -0
- package/react-query/index.d.ts +1 -0
- package/react-query/index.js +17 -0
- package/{types/react-query → react-query}/react-query.d.ts +2 -3
- package/react-query/react-query.js +490 -0
- package/recoil/index.d.ts +1 -0
- package/recoil/index.js +17 -0
- package/{types/recoil → recoil}/recoil.d.ts +1 -1
- package/recoil/recoil.js +140 -0
- package/{types/utils → utils}/babel.d.ts +9 -8
- package/utils/babel.js +285 -0
- package/utils/constants.js +46 -0
- package/utils/index.js +26 -0
- package/{types/utils → utils}/ref.d.ts +1 -1
- package/utils/ref.js +8 -0
- package/{types/utils → utils}/types.d.ts +8 -8
- package/utils/types.js +379 -0
- package/main/client/client.js +0 -196
- package/main/client/index.js +0 -16
- package/main/client/test/ts-client.account-nfts.spec.js +0 -28
- package/main/client/test/ts-client.arrays-ref.spec.js +0 -19
- package/main/client/test/ts-client.arrays.spec.js +0 -26
- package/main/client/test/ts-client.cw-named-groups.test.js +0 -19
- package/main/client/test/ts-client.cw-proposal-single.test.js +0 -23
- package/main/client/test/ts-client.empty-enums.spec.js +0 -13
- package/main/client/test/ts-client.issue-101.spec.js +0 -23
- package/main/client/test/ts-client.issue-103.test.js +0 -44
- package/main/client/test/ts-client.issue-71.test.js +0 -81
- package/main/client/test/ts-client.issue-98.test.js +0 -28
- package/main/client/test/ts-client.issues.test.js +0 -81
- package/main/client/test/ts-client.overrides.spec.js +0 -33
- package/main/client/test/ts-client.sg721.spec.js +0 -19
- package/main/client/test/ts-client.spec.js +0 -87
- package/main/client/test/ts-client.vectis.spec.js +0 -46
- package/main/client/test/ts-client.wager.spec.js +0 -39
- package/main/context/context.js +0 -162
- package/main/context/imports.js +0 -159
- package/main/context/index.js +0 -27
- package/main/index.js +0 -104
- package/main/message-builder/index.js +0 -16
- package/main/message-builder/message-builder.js +0 -62
- package/main/message-builder/message-builder.spec.js +0 -20
- package/main/message-composer/index.js +0 -16
- package/main/message-composer/message-composer.js +0 -86
- package/main/message-composer/message-composer.spec.js +0 -23
- package/main/provider/index.js +0 -16
- package/main/provider/provider.js +0 -71
- package/main/provider/provider.spec.js +0 -60
- package/main/react-query/index.js +0 -16
- package/main/react-query/react-query.js +0 -413
- package/main/react-query/react-query.spec.js +0 -91
- package/main/recoil/index.js +0 -16
- package/main/recoil/recoil.js +0 -61
- package/main/recoil/recoil.spec.js +0 -20
- package/main/types.js +0 -4
- package/main/utils/babel.js +0 -282
- package/main/utils/babel.spec.js +0 -109
- package/main/utils/constants.js +0 -26
- package/main/utils/index.js +0 -65
- package/main/utils/ref.js +0 -12
- package/main/utils/types.js +0 -343
- package/module/client/client.js +0 -166
- package/module/client/index.js +0 -1
- package/module/client/test/ts-client.account-nfts.spec.js +0 -25
- package/module/client/test/ts-client.arrays-ref.spec.js +0 -16
- package/module/client/test/ts-client.arrays.spec.js +0 -23
- package/module/client/test/ts-client.cw-named-groups.test.js +0 -16
- package/module/client/test/ts-client.cw-proposal-single.test.js +0 -20
- package/module/client/test/ts-client.empty-enums.spec.js +0 -10
- package/module/client/test/ts-client.issue-101.spec.js +0 -22
- package/module/client/test/ts-client.issue-103.test.js +0 -41
- package/module/client/test/ts-client.issue-71.test.js +0 -21
- package/module/client/test/ts-client.issue-98.test.js +0 -25
- package/module/client/test/ts-client.issues.test.js +0 -21
- package/module/client/test/ts-client.overrides.spec.js +0 -30
- package/module/client/test/ts-client.sg721.spec.js +0 -16
- package/module/client/test/ts-client.spec.js +0 -84
- package/module/client/test/ts-client.vectis.spec.js +0 -43
- package/module/client/test/ts-client.wager.spec.js +0 -36
- package/module/context/context.js +0 -116
- package/module/context/imports.js +0 -127
- package/module/index.js +0 -9
- package/module/message-builder/index.js +0 -1
- package/module/message-builder/message-builder.js +0 -52
- package/module/message-builder/message-builder.spec.js +0 -17
- package/module/message-composer/index.js +0 -1
- package/module/message-composer/message-composer.js +0 -70
- package/module/message-composer/message-composer.spec.js +0 -20
- package/module/provider/index.js +0 -1
- package/module/provider/provider.js +0 -45
- package/module/provider/provider.spec.js +0 -58
- package/module/react-query/index.js +0 -1
- package/module/react-query/react-query.js +0 -403
- package/module/react-query/react-query.spec.js +0 -85
- package/module/recoil/index.js +0 -1
- package/module/recoil/recoil.js +0 -46
- package/module/recoil/recoil.spec.js +0 -14
- package/module/types.js +0 -3
- package/module/utils/babel.js +0 -203
- package/module/utils/babel.spec.js +0 -70
- package/module/utils/constants.js +0 -12
- package/module/utils/index.js +0 -6
- package/module/utils/ref.js +0 -4
- package/module/utils/types.js +0 -301
- package/src/client/client.ts +0 -584
- package/src/client/index.ts +0 -1
- package/src/client/test/__snapshots__/ts-client.account-nfts.spec.ts.snap +0 -497
- package/src/client/test/__snapshots__/ts-client.arrays-ref.spec.ts.snap +0 -452
- package/src/client/test/__snapshots__/ts-client.arrays.spec.ts.snap +0 -101
- package/src/client/test/__snapshots__/ts-client.cw-named-groups.test.ts.snap +0 -141
- package/src/client/test/__snapshots__/ts-client.cw-proposal-single.test.ts.snap +0 -341
- package/src/client/test/__snapshots__/ts-client.empty-enums.spec.ts.snap +0 -20
- package/src/client/test/__snapshots__/ts-client.issue-101.spec.ts.snap +0 -47
- package/src/client/test/__snapshots__/ts-client.issue-103.test.ts.snap +0 -79
- package/src/client/test/__snapshots__/ts-client.issue-71.test.ts.snap +0 -432
- package/src/client/test/__snapshots__/ts-client.issue-98.test.ts.snap +0 -117
- package/src/client/test/__snapshots__/ts-client.issues.test.ts.snap +0 -984
- package/src/client/test/__snapshots__/ts-client.overrides.spec.ts.snap +0 -709
- package/src/client/test/__snapshots__/ts-client.sg721.spec.ts.snap +0 -350
- package/src/client/test/__snapshots__/ts-client.spec.ts.snap +0 -723
- package/src/client/test/__snapshots__/ts-client.vectis.spec.ts.snap +0 -337
- package/src/client/test/__snapshots__/ts-client.wager.spec.ts.snap +0 -132
- package/src/client/test/ts-client.account-nfts.spec.ts +0 -55
- package/src/client/test/ts-client.arrays-ref.spec.ts +0 -48
- package/src/client/test/ts-client.arrays.spec.ts +0 -58
- package/src/client/test/ts-client.cw-named-groups.test.ts +0 -48
- package/src/client/test/ts-client.cw-proposal-single.test.ts +0 -50
- package/src/client/test/ts-client.empty-enums.spec.ts +0 -28
- package/src/client/test/ts-client.issue-101.spec.ts +0 -37
- package/src/client/test/ts-client.issue-103.test.ts +0 -106
- package/src/client/test/ts-client.issue-71.test.ts +0 -51
- package/src/client/test/ts-client.issue-98.test.ts +0 -55
- package/src/client/test/ts-client.issues.test.ts +0 -52
- package/src/client/test/ts-client.overrides.spec.ts +0 -74
- package/src/client/test/ts-client.sg721.spec.ts +0 -46
- package/src/client/test/ts-client.spec.ts +0 -166
- package/src/client/test/ts-client.vectis.spec.ts +0 -97
- package/src/client/test/ts-client.wager.spec.ts +0 -63
- package/src/context/context.ts +0 -229
- package/src/context/imports.ts +0 -195
- package/src/context/index.ts +0 -2
- package/src/message-builder/__snapshots__/message-builder.spec.ts.snap +0 -270
- package/src/message-builder/message-builder.spec.ts +0 -24
- package/src/message-builder/message-builder.ts +0 -119
- package/src/message-composer/__snapshots__/message-composer.spec.ts.snap +0 -331
- package/src/message-composer/index.ts +0 -1
- package/src/message-composer/message-composer.spec.ts +0 -46
- package/src/message-composer/message-composer.ts +0 -262
- package/src/provider/__snapshots__/provider.spec.ts.snap +0 -49
- package/src/provider/index.ts +0 -1
- package/src/provider/provider.spec.ts +0 -81
- package/src/provider/provider.ts +0 -237
- package/src/react-query/__snapshots__/react-query.spec.ts.snap +0 -1359
- package/src/react-query/index.ts +0 -1
- package/src/react-query/react-query.spec.ts +0 -116
- package/src/react-query/react-query.ts +0 -1072
- package/src/recoil/__snapshots__/recoil.spec.ts.snap +0 -203
- package/src/recoil/index.ts +0 -1
- package/src/recoil/recoil.spec.ts +0 -38
- package/src/recoil/recoil.ts +0 -307
- package/src/types.ts +0 -44
- package/src/utils/__snapshots__/babel.spec.ts.snap +0 -75
- package/src/utils/babel.spec.ts +0 -511
- package/src/utils/babel.ts +0 -352
- package/src/utils/constants.ts +0 -37
- package/src/utils/ref.ts +0 -6
- package/src/utils/types.ts +0 -478
- package/types/types.d.ts +0 -41
- /package/{types/client → client}/index.d.ts +0 -0
- /package/{types/context → context}/index.d.ts +0 -0
- /package/{src/message-builder/index.ts → esm/message-builder/index.js} +0 -0
- /package/{types/message-composer/index.d.ts → esm/message-composer/index.js} +0 -0
- /package/{types/provider/index.d.ts → esm/provider/index.js} +0 -0
- /package/{types/react-query/index.d.ts → esm/react-query/index.js} +0 -0
- /package/{types/recoil/index.d.ts → esm/recoil/index.js} +0 -0
- /package/{src/utils/index.ts → esm/utils/index.js} +0 -0
- /package/{types/message-builder → message-builder}/index.d.ts +0 -0
- /package/{types/provider → provider}/provider.d.ts +0 -0
- /package/{types/utils → utils}/constants.d.ts +0 -0
- /package/{types/utils → utils}/index.d.ts +0 -0
@@ -0,0 +1,458 @@
|
|
1
|
+
import * as t from '@babel/types';
|
2
|
+
import { camel, pascal } from 'case';
|
3
|
+
import { callExpression, createTypedObjectParams, getMessageProperties, identifier, OPTIONAL_FUNDS_PARAM, tsObjectPattern, tsPropertySignature } from '../utils';
|
4
|
+
import { omitTypeReference, optionalConditionalExpression, propertySignature, shorthandProperty } from '../utils/babel';
|
5
|
+
import { getPropertyType, getResponseType } from '../utils/types';
|
6
|
+
import { OPTIONAL_FEE_PARAM, OPTIONAL_MEMO_PARAM } from '../utils/constants';
|
7
|
+
export const createReactQueryHooks = ({ context, queryMsg, contractName, QueryClient }) => {
|
8
|
+
const options = context.options.reactQuery;
|
9
|
+
const genericQueryInterfaceName = `${pascal(contractName)}ReactQuery`;
|
10
|
+
const underscoreNames = getMessageProperties(queryMsg).map((schema) => Object.keys(schema.properties)[0]);
|
11
|
+
const body = [];
|
12
|
+
const queryKeysName = `${camel(contractName)}QueryKeys`;
|
13
|
+
if (options.queryKeys) {
|
14
|
+
body.push(createReactQueryKeys({
|
15
|
+
context,
|
16
|
+
queryKeysName,
|
17
|
+
camelContractName: camel(contractName),
|
18
|
+
underscoreNames
|
19
|
+
}));
|
20
|
+
}
|
21
|
+
const queryMsgs = getMessageProperties(queryMsg).map((schema) => {
|
22
|
+
// list_voters
|
23
|
+
const underscoreName = Object.keys(schema.properties)[0];
|
24
|
+
// listVoters
|
25
|
+
const methodName = camel(underscoreName);
|
26
|
+
// Cw3FlexMultisigListVotersQuery
|
27
|
+
const hookParamsTypeName = `${pascal(contractName)}${pascal(methodName)}Query`;
|
28
|
+
// useCw3FlexMultisigListVotersQuery
|
29
|
+
const hookName = `use${hookParamsTypeName}`;
|
30
|
+
// listVotersResponse
|
31
|
+
const responseType = getResponseType(context, underscoreName);
|
32
|
+
// cw3FlexMultisigListVoters
|
33
|
+
const getterKey = camel(`${contractName}${pascal(methodName)}`);
|
34
|
+
const jsonschema = schema.properties[underscoreName];
|
35
|
+
return {
|
36
|
+
underscoreName,
|
37
|
+
methodName,
|
38
|
+
hookParamsTypeName,
|
39
|
+
hookName,
|
40
|
+
responseType,
|
41
|
+
getterKey,
|
42
|
+
jsonschema
|
43
|
+
};
|
44
|
+
});
|
45
|
+
const queryFactoryName = `${camel(contractName)}Queries`;
|
46
|
+
if (options.queryFactory) {
|
47
|
+
body.push(createReactQueryFactory({
|
48
|
+
context,
|
49
|
+
queryFactoryName,
|
50
|
+
queryKeysName,
|
51
|
+
queryMsgs
|
52
|
+
}));
|
53
|
+
}
|
54
|
+
body.push(createReactQueryHookGenericInterface({
|
55
|
+
context,
|
56
|
+
QueryClient,
|
57
|
+
genericQueryInterfaceName
|
58
|
+
}));
|
59
|
+
body.push(...queryMsgs.reduce((m, { methodName, hookParamsTypeName, hookName, responseType, getterKey, jsonschema }) => {
|
60
|
+
return [
|
61
|
+
createReactQueryHookInterface({
|
62
|
+
context,
|
63
|
+
hookParamsTypeName,
|
64
|
+
responseType,
|
65
|
+
queryInterfaceName: genericQueryInterfaceName,
|
66
|
+
QueryClient,
|
67
|
+
jsonschema
|
68
|
+
}),
|
69
|
+
createReactQueryHook({
|
70
|
+
context,
|
71
|
+
methodName,
|
72
|
+
hookName,
|
73
|
+
hookParamsTypeName,
|
74
|
+
queryKeysName,
|
75
|
+
responseType,
|
76
|
+
hookKeyName: getterKey,
|
77
|
+
jsonschema
|
78
|
+
}),
|
79
|
+
...m
|
80
|
+
];
|
81
|
+
}, []));
|
82
|
+
return body;
|
83
|
+
};
|
84
|
+
function buildQueryFn(methodName, jsonschema, options) {
|
85
|
+
const keys = Object.keys(jsonschema.properties ?? {});
|
86
|
+
let args = [];
|
87
|
+
if (keys.length) {
|
88
|
+
args = [
|
89
|
+
t.objectExpression([
|
90
|
+
...keys.map((prop) => {
|
91
|
+
return t.objectProperty(t.identifier(camel(prop)), t.memberExpression(t.identifier('args'), t.identifier(camel(prop))));
|
92
|
+
})
|
93
|
+
])
|
94
|
+
];
|
95
|
+
}
|
96
|
+
const rejectInvalidClient = t.callExpression(t.memberExpression(t.identifier('Promise'), t.identifier('reject')), [
|
97
|
+
t.newExpression(t.identifier('Error'), [
|
98
|
+
t.stringLiteral('Invalid client')
|
99
|
+
])
|
100
|
+
]);
|
101
|
+
return t.arrowFunctionExpression([], optionalConditionalExpression(t.identifier('client'), t.callExpression(t.memberExpression(t.identifier('client'), t.identifier(methodName)), args), rejectInvalidClient, options.optionalClient), false);
|
102
|
+
}
|
103
|
+
const ENABLED_QUERY_OPTION = t.objectProperty(t.identifier('enabled'), t.logicalExpression('&&', t.unaryExpression('!', t.unaryExpression('!', t.identifier('client'))), t.conditionalExpression(
|
104
|
+
// explicitly check for undefined
|
105
|
+
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))));
|
106
|
+
function buildQueryOptions(options) {
|
107
|
+
return options.optionalClient
|
108
|
+
? t.objectExpression([
|
109
|
+
t.spreadElement(t.identifier('options')),
|
110
|
+
t.objectProperty(t.identifier('enabled'), t.logicalExpression('&&', t.unaryExpression('!', t.unaryExpression('!', t.identifier('client'))), t.conditionalExpression(
|
111
|
+
// explicitly check for undefined
|
112
|
+
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))))
|
113
|
+
])
|
114
|
+
: t.identifier('options');
|
115
|
+
}
|
116
|
+
export const createReactQueryHook = ({ context, hookName, hookParamsTypeName, responseType, hookKeyName, queryKeysName, methodName, jsonschema }) => {
|
117
|
+
context.addUtil('useQuery');
|
118
|
+
context.addUtil('UseQueryOptions');
|
119
|
+
const options = context.options.reactQuery;
|
120
|
+
const keys = Object.keys(jsonschema.properties ?? {});
|
121
|
+
let props = ['client', 'options'];
|
122
|
+
if (keys.length) {
|
123
|
+
props = ['client', 'args', 'options'];
|
124
|
+
}
|
125
|
+
const selectResponseGenericTypeName = GENERIC_SELECT_RESPONSE_NAME;
|
126
|
+
const queryFunctionDeclaration = t.functionDeclaration(t.identifier(hookName), [
|
127
|
+
tsObjectPattern([
|
128
|
+
...props.map((prop) => {
|
129
|
+
return t.objectProperty(t.identifier(prop), t.identifier(prop), false, true);
|
130
|
+
})
|
131
|
+
], t.tsTypeAnnotation(t.tsTypeReference(t.identifier(hookParamsTypeName), t.tsTypeParameterInstantiation([
|
132
|
+
t.tsTypeReference(t.identifier(selectResponseGenericTypeName))
|
133
|
+
]))))
|
134
|
+
], t.blockStatement([
|
135
|
+
t.returnStatement(callExpression(t.identifier('useQuery'), [
|
136
|
+
generateUseQueryQueryKey({
|
137
|
+
hookKeyName,
|
138
|
+
queryKeysName,
|
139
|
+
methodName,
|
140
|
+
props,
|
141
|
+
options
|
142
|
+
}),
|
143
|
+
buildQueryFn(methodName, jsonschema, options),
|
144
|
+
buildQueryOptions(options)
|
145
|
+
], t.tsTypeParameterInstantiation([
|
146
|
+
t.tsTypeReference(t.identifier(responseType)),
|
147
|
+
t.tsTypeReference(t.identifier('Error')),
|
148
|
+
t.tsTypeReference(t.identifier(selectResponseGenericTypeName))
|
149
|
+
])))
|
150
|
+
]));
|
151
|
+
// Add the TData type parameters
|
152
|
+
queryFunctionDeclaration.typeParameters = t.tsTypeParameterDeclaration([
|
153
|
+
t.tsTypeParameter(undefined, t.tSTypeReference(t.identifier(responseType)), selectResponseGenericTypeName)
|
154
|
+
]);
|
155
|
+
return t.exportNamedDeclaration(queryFunctionDeclaration);
|
156
|
+
};
|
157
|
+
/**
|
158
|
+
* Example:
|
159
|
+
```
|
160
|
+
export interface Cw721RevokeMutation {
|
161
|
+
client: Cw721Client;
|
162
|
+
msg: {
|
163
|
+
spender: string;
|
164
|
+
tokenId: string;
|
165
|
+
};
|
166
|
+
args?: {
|
167
|
+
fee?: number | StdFee | "auto";
|
168
|
+
memo?: string;
|
169
|
+
funds?: Coin[];
|
170
|
+
};
|
171
|
+
}
|
172
|
+
```
|
173
|
+
*/
|
174
|
+
export const createReactQueryMutationArgsInterface = ({ context, ExecuteClient, mutationHookParamsTypeName, useMutationTypeParameter, jsonschema }) => {
|
175
|
+
const typedUseMutationOptions = t.tsTypeReference(t.identifier('UseMutationOptions'), useMutationTypeParameter);
|
176
|
+
const body = [
|
177
|
+
tsPropertySignature(t.identifier('client'), t.tsTypeAnnotation(t.tsTypeReference(t.identifier(ExecuteClient))), false)
|
178
|
+
];
|
179
|
+
const msgType = createTypedObjectParams(context, jsonschema)?.typeAnnotation;
|
180
|
+
if (msgType) {
|
181
|
+
body.push(t.tsPropertySignature(t.identifier('msg'),
|
182
|
+
// @ts-ignore
|
183
|
+
msgType));
|
184
|
+
}
|
185
|
+
context.addUtil('StdFee');
|
186
|
+
context.addUtil('Coin');
|
187
|
+
const optionalArgs = t.tsPropertySignature(t.identifier('args'), t.tsTypeAnnotation(
|
188
|
+
// @ts-ignore:next-line
|
189
|
+
t.tsTypeLiteral([
|
190
|
+
propertySignature('fee', OPTIONAL_FEE_PARAM.typeAnnotation, true),
|
191
|
+
propertySignature('memo', OPTIONAL_MEMO_PARAM.typeAnnotation, true),
|
192
|
+
propertySignature('funds', OPTIONAL_FUNDS_PARAM.typeAnnotation, true)
|
193
|
+
])));
|
194
|
+
optionalArgs.optional = true;
|
195
|
+
body.push(optionalArgs);
|
196
|
+
return t.exportNamedDeclaration(t.tsInterfaceDeclaration(t.identifier(mutationHookParamsTypeName), null, [], t.tsInterfaceBody(body)));
|
197
|
+
};
|
198
|
+
export const createReactQueryMutationHooks = ({ context, execMsg, contractName, ExecuteClient }) => {
|
199
|
+
// merge the user options with the defaults
|
200
|
+
return getMessageProperties(execMsg).reduce((m, schema) => {
|
201
|
+
// update_members
|
202
|
+
const execMethodUnderscoreName = Object.keys(schema.properties)[0];
|
203
|
+
// updateMembers
|
204
|
+
const execMethodName = camel(execMethodUnderscoreName);
|
205
|
+
// Cw20UpdateMembersMutation
|
206
|
+
const mutationHookParamsTypeName = `${pascal(contractName)}${pascal(execMethodName)}Mutation`;
|
207
|
+
// useCw20UpdateMembersMutation
|
208
|
+
const mutationHookName = `use${mutationHookParamsTypeName}`;
|
209
|
+
const jsonschema = schema.properties[execMethodUnderscoreName];
|
210
|
+
const properties = jsonschema.properties ?? {};
|
211
|
+
// TODO: there should be a better way to do this
|
212
|
+
const hasMsg = !!(Object.keys(properties)?.length || jsonschema?.$ref);
|
213
|
+
// <ExecuteResult, Error, Cw4UpdateMembersMutation>
|
214
|
+
const useMutationTypeParameter = generateMutationTypeParameter(context, mutationHookParamsTypeName);
|
215
|
+
return [
|
216
|
+
createReactQueryMutationArgsInterface({
|
217
|
+
context,
|
218
|
+
mutationHookParamsTypeName,
|
219
|
+
ExecuteClient,
|
220
|
+
jsonschema,
|
221
|
+
useMutationTypeParameter
|
222
|
+
}),
|
223
|
+
createReactQueryMutationHook({
|
224
|
+
context,
|
225
|
+
execMethodName,
|
226
|
+
mutationHookName,
|
227
|
+
mutationHookParamsTypeName,
|
228
|
+
hasMsg,
|
229
|
+
useMutationTypeParameter
|
230
|
+
}),
|
231
|
+
...m
|
232
|
+
];
|
233
|
+
}, []);
|
234
|
+
};
|
235
|
+
/**
|
236
|
+
* Generates the mutation type parameter. If args exist, we use a pick. If not, we just return the params type.
|
237
|
+
*/
|
238
|
+
const generateMutationTypeParameter = (context, mutationHookParamsTypeName) => {
|
239
|
+
context.addUtil('ExecuteResult');
|
240
|
+
return t.tsTypeParameterInstantiation([
|
241
|
+
// Data
|
242
|
+
t.tSTypeReference(t.identifier('ExecuteResult')),
|
243
|
+
// Error
|
244
|
+
t.tsTypeReference(t.identifier('Error')),
|
245
|
+
// Variables
|
246
|
+
t.tsTypeReference(t.identifier(mutationHookParamsTypeName))
|
247
|
+
]);
|
248
|
+
};
|
249
|
+
/**
|
250
|
+
*
|
251
|
+
* Example:
|
252
|
+
```
|
253
|
+
export const useCw4UpdateMembersMutation = ({ client, options }: Omit<Cw4UpdateMembersMutation, 'args'>) =>
|
254
|
+
useMutation<ExecuteResult, Error, Pick<Cw4UpdateMembersMutation, 'args'>>(
|
255
|
+
({ args }) => client.updateMembers(args),
|
256
|
+
options
|
257
|
+
)
|
258
|
+
```
|
259
|
+
*/
|
260
|
+
export const createReactQueryMutationHook = ({ context, mutationHookName, mutationHookParamsTypeName, execMethodName, useMutationTypeParameter, hasMsg }) => {
|
261
|
+
context.addUtil('useMutation');
|
262
|
+
context.addUtil('UseMutationOptions');
|
263
|
+
const useMutationFunctionArgs = [shorthandProperty('client')];
|
264
|
+
if (hasMsg)
|
265
|
+
useMutationFunctionArgs.push(shorthandProperty('msg'));
|
266
|
+
useMutationFunctionArgs.push(t.objectProperty(t.identifier('args'), t.assignmentPattern(t.objectPattern([
|
267
|
+
shorthandProperty('fee'),
|
268
|
+
shorthandProperty('memo'),
|
269
|
+
shorthandProperty('funds')
|
270
|
+
]), t.objectExpression([]))));
|
271
|
+
return t.exportNamedDeclaration(t.functionDeclaration(t.identifier(mutationHookName), [
|
272
|
+
identifier('options', t.tsTypeAnnotation(omitTypeReference(t.tsTypeReference(t.identifier('UseMutationOptions'), useMutationTypeParameter), 'mutationFn')), true)
|
273
|
+
], t.blockStatement([
|
274
|
+
t.returnStatement(callExpression(t.identifier('useMutation'), [
|
275
|
+
t.arrowFunctionExpression([t.objectPattern(useMutationFunctionArgs)], t.callExpression(t.memberExpression(t.identifier('client'), t.identifier(execMethodName)), (hasMsg ? [t.identifier('msg')] : []).concat([
|
276
|
+
t.identifier('fee'),
|
277
|
+
t.identifier('memo'),
|
278
|
+
t.identifier('funds')
|
279
|
+
])), false // not async
|
280
|
+
),
|
281
|
+
t.identifier('options')
|
282
|
+
], useMutationTypeParameter))
|
283
|
+
])));
|
284
|
+
};
|
285
|
+
function createReactQueryKeys({ context, queryKeysName, camelContractName, underscoreNames }) {
|
286
|
+
const options = context.options.reactQuery;
|
287
|
+
const contractAddressTypeAnnotation = t.tsTypeAnnotation(options.optionalClient
|
288
|
+
? t.tsUnionType([t.tsStringKeyword(), t.tsUndefinedKeyword()])
|
289
|
+
: t.tSStringKeyword());
|
290
|
+
return t.exportNamedDeclaration(t.variableDeclaration('const', [
|
291
|
+
t.variableDeclarator(t.identifier(queryKeysName), t.objectExpression([
|
292
|
+
// 1: contract
|
293
|
+
t.objectProperty(t.identifier('contract'), t.tSAsExpression(t.arrayExpression([
|
294
|
+
t.objectExpression([
|
295
|
+
t.objectProperty(t.identifier('contract'), t.stringLiteral(camelContractName))
|
296
|
+
])
|
297
|
+
]), t.tSTypeReference(t.identifier('const')))),
|
298
|
+
// 2: address
|
299
|
+
t.objectProperty(t.identifier('address'), t.arrowFunctionExpression([identifier('contractAddress', contractAddressTypeAnnotation)], t.tSAsExpression(t.arrayExpression([
|
300
|
+
t.objectExpression([
|
301
|
+
// 1
|
302
|
+
t.spreadElement(t.memberExpression(t.memberExpression(t.identifier(queryKeysName), t.identifier('contract')), t.numericLiteral(0), true // computed
|
303
|
+
)),
|
304
|
+
t.objectProperty(t.identifier('address'), t.identifier('contractAddress'))
|
305
|
+
])
|
306
|
+
]), t.tSTypeReference(t.identifier('const'))))),
|
307
|
+
// 3: methods
|
308
|
+
...underscoreNames.map((underscoreMethodName) => t.objectProperty(
|
309
|
+
// key id is the camel method name
|
310
|
+
t.identifier(camel(underscoreMethodName)), t.arrowFunctionExpression([
|
311
|
+
identifier('contractAddress', contractAddressTypeAnnotation),
|
312
|
+
identifier('args',
|
313
|
+
// Record<string, unknown>
|
314
|
+
t.tSTypeAnnotation(t.tsTypeReference(t.identifier('Record'), t.tsTypeParameterInstantiation([
|
315
|
+
t.tsStringKeyword(),
|
316
|
+
t.tsUnknownKeyword()
|
317
|
+
]))), true // optional
|
318
|
+
)
|
319
|
+
], t.tSAsExpression(t.arrayExpression([
|
320
|
+
t.objectExpression([
|
321
|
+
//...cw3FlexMultisigQueryKeys.address(contractAddress)[0]
|
322
|
+
t.spreadElement(t.memberExpression(t.callExpression(t.memberExpression(t.identifier(queryKeysName), t.identifier('address')), [t.identifier('contractAddress')]), t.numericLiteral(0), true // computed
|
323
|
+
)),
|
324
|
+
// method: list_voters
|
325
|
+
t.objectProperty(t.identifier('method'), t.stringLiteral(underscoreMethodName)),
|
326
|
+
// args
|
327
|
+
shorthandProperty('args')
|
328
|
+
])
|
329
|
+
]), t.tSTypeReference(t.identifier('const'))))))
|
330
|
+
]))
|
331
|
+
]));
|
332
|
+
}
|
333
|
+
function createReactQueryFactory({ context, queryFactoryName, queryKeysName, queryMsgs }) {
|
334
|
+
const options = context.options.reactQuery;
|
335
|
+
return t.exportNamedDeclaration(t.variableDeclaration('const', [
|
336
|
+
t.variableDeclarator(t.identifier(queryFactoryName), t.objectExpression([
|
337
|
+
...queryMsgs.map(({ methodName, hookParamsTypeName, responseType, jsonschema }) => {
|
338
|
+
const hasArgs = Object.keys(jsonschema.properties ?? {}).length > 0;
|
339
|
+
const methodQueryOptionsFn = t.arrowFunctionExpression([
|
340
|
+
tsObjectPattern([
|
341
|
+
shorthandProperty('client'),
|
342
|
+
...(hasArgs ? [shorthandProperty('args')] : []),
|
343
|
+
shorthandProperty('options')
|
344
|
+
], t.tsTypeAnnotation(t.tsTypeReference(t.identifier(hookParamsTypeName), t.tsTypeParameterInstantiation([
|
345
|
+
t.tsTypeReference(t.identifier(GENERIC_SELECT_RESPONSE_NAME))
|
346
|
+
]))))
|
347
|
+
], t.objectExpression([
|
348
|
+
// 1: queryKey
|
349
|
+
t.objectProperty(t.identifier('queryKey'), t.callExpression(t.memberExpression(t.identifier(queryKeysName), t.identifier(methodName)), [
|
350
|
+
t.optionalMemberExpression(t.identifier('client'), t.identifier('contractAddress'), false, true),
|
351
|
+
...(hasArgs ? [t.identifier('args')] : [])
|
352
|
+
])),
|
353
|
+
// 2: queryFn
|
354
|
+
t.objectProperty(t.identifier('queryFn'), buildQueryFn(methodName, jsonschema, options)),
|
355
|
+
// 3: spread options
|
356
|
+
t.spreadElement(t.identifier('options')),
|
357
|
+
// 4. enabled
|
358
|
+
ENABLED_QUERY_OPTION
|
359
|
+
]));
|
360
|
+
methodQueryOptionsFn.typeParameters =
|
361
|
+
t.tsTypeParameterDeclaration([
|
362
|
+
t.tsTypeParameter(undefined, t.tsTypeReference(t.identifier(responseType)), GENERIC_SELECT_RESPONSE_NAME)
|
363
|
+
]);
|
364
|
+
methodQueryOptionsFn.returnType = t.tsTypeAnnotation(t.tsTypeReference(t.identifier('UseQueryOptions'), t.tsTypeParameterInstantiation([
|
365
|
+
t.tsTypeReference(t.identifier(responseType)),
|
366
|
+
t.tsTypeReference(t.identifier('Error')),
|
367
|
+
t.tsTypeReference(t.identifier(GENERIC_SELECT_RESPONSE_NAME))
|
368
|
+
])));
|
369
|
+
return t.objectProperty(
|
370
|
+
// key id is the camel method name
|
371
|
+
t.identifier(camel(methodName)), methodQueryOptionsFn);
|
372
|
+
})
|
373
|
+
]))
|
374
|
+
]));
|
375
|
+
}
|
376
|
+
const GENERIC_SELECT_RESPONSE_NAME = 'TData';
|
377
|
+
function createReactQueryHookGenericInterface({ context, QueryClient, genericQueryInterfaceName }) {
|
378
|
+
const options = context.options.reactQuery;
|
379
|
+
const genericResponseTypeName = 'TResponse';
|
380
|
+
context.addUtil('UseQueryOptions');
|
381
|
+
// UseQueryOptions<TResponse, Error, TData>,
|
382
|
+
const typedUseQueryOptions = t.tsTypeReference(t.identifier('UseQueryOptions'), t.tsTypeParameterInstantiation([
|
383
|
+
t.tsTypeReference(t.identifier(genericResponseTypeName)),
|
384
|
+
t.tsTypeReference(t.identifier('Error')),
|
385
|
+
t.tsTypeReference(t.identifier(GENERIC_SELECT_RESPONSE_NAME))
|
386
|
+
]));
|
387
|
+
const body = [
|
388
|
+
tsPropertySignature(t.identifier('client'), t.tsTypeAnnotation(options.optionalClient
|
389
|
+
? t.tsUnionType([
|
390
|
+
t.tsTypeReference(t.identifier(QueryClient)),
|
391
|
+
t.tsUndefinedKeyword()
|
392
|
+
])
|
393
|
+
: t.tsTypeReference(t.identifier(QueryClient))), false),
|
394
|
+
tsPropertySignature(t.identifier('options'), t.tsTypeAnnotation(options.version === 'v4'
|
395
|
+
? t.tSIntersectionType([
|
396
|
+
omitTypeReference(typedUseQueryOptions, "'queryKey' | 'queryFn' | 'initialData'"),
|
397
|
+
t.tSTypeLiteral([
|
398
|
+
t.tsPropertySignature(t.identifier('initialData?'), t.tsTypeAnnotation(t.tsUndefinedKeyword()))
|
399
|
+
])
|
400
|
+
])
|
401
|
+
: typedUseQueryOptions), true)
|
402
|
+
];
|
403
|
+
return t.exportNamedDeclaration(t.tsInterfaceDeclaration(t.identifier(genericQueryInterfaceName), t.tsTypeParameterDeclaration([
|
404
|
+
// 1: TResponse
|
405
|
+
t.tsTypeParameter(undefined, undefined, genericResponseTypeName),
|
406
|
+
// 2: TData
|
407
|
+
t.tsTypeParameter(undefined, t.tSTypeReference(t.identifier(genericResponseTypeName)), GENERIC_SELECT_RESPONSE_NAME)
|
408
|
+
]), [], t.tSInterfaceBody(body)));
|
409
|
+
}
|
410
|
+
export const createReactQueryHookInterface = ({ context, QueryClient, hookParamsTypeName, queryInterfaceName, responseType, jsonschema }) => {
|
411
|
+
// merge the user options with the defaults
|
412
|
+
const options = context.options.reactQuery;
|
413
|
+
const body = [];
|
414
|
+
const props = getProps(context, jsonschema);
|
415
|
+
if (props.length) {
|
416
|
+
body.push(t.tsPropertySignature(t.identifier('args'), t.tsTypeAnnotation(
|
417
|
+
// @ts-ignore:next-line
|
418
|
+
t.tsTypeLiteral(props))));
|
419
|
+
}
|
420
|
+
return t.exportNamedDeclaration(t.tsInterfaceDeclaration(t.identifier(hookParamsTypeName), t.tsTypeParameterDeclaration([
|
421
|
+
t.tSTypeParameter(undefined, undefined, GENERIC_SELECT_RESPONSE_NAME)
|
422
|
+
]), [
|
423
|
+
t.tSExpressionWithTypeArguments(t.identifier(queryInterfaceName), t.tsTypeParameterInstantiation([
|
424
|
+
// 1: response
|
425
|
+
t.tsTypeReference(t.identifier(responseType)),
|
426
|
+
// 2: select generic
|
427
|
+
t.tSTypeReference(t.identifier(GENERIC_SELECT_RESPONSE_NAME))
|
428
|
+
]))
|
429
|
+
], t.tsInterfaceBody(body)));
|
430
|
+
};
|
431
|
+
const getProps = (context, jsonschema) => {
|
432
|
+
const keys = Object.keys(jsonschema.properties ?? {});
|
433
|
+
if (!keys.length)
|
434
|
+
return [];
|
435
|
+
return keys.map((prop) => {
|
436
|
+
const { type, optional } = getPropertyType(context, jsonschema, prop);
|
437
|
+
return propertySignature(context.options.reactQuery.camelize ? camel(prop) : prop, t.tsTypeAnnotation(type), optional);
|
438
|
+
});
|
439
|
+
};
|
440
|
+
const generateUseQueryQueryKey = ({ hookKeyName, queryKeysName, methodName, props, options }) => {
|
441
|
+
const { optionalClient, queryKeys } = options;
|
442
|
+
const hasArgs = props.includes('args');
|
443
|
+
const contractAddressExpression = t.optionalMemberExpression(t.identifier('client'), t.identifier('contractAddress'), false, optionalClient);
|
444
|
+
if (queryKeys) {
|
445
|
+
const callArgs = [contractAddressExpression];
|
446
|
+
if (hasArgs)
|
447
|
+
callArgs.push(t.identifier('args'));
|
448
|
+
return t.callExpression(t.memberExpression(t.identifier(queryKeysName), t.identifier(camel(methodName))), callArgs);
|
449
|
+
}
|
450
|
+
const queryKey = [
|
451
|
+
t.stringLiteral(hookKeyName),
|
452
|
+
contractAddressExpression
|
453
|
+
];
|
454
|
+
if (hasArgs) {
|
455
|
+
queryKey.push(t.callExpression(t.memberExpression(t.identifier('JSON'), t.identifier('stringify')), [t.identifier('args')]));
|
456
|
+
}
|
457
|
+
return t.arrayExpression(queryKey);
|
458
|
+
};
|
@@ -0,0 +1,110 @@
|
|
1
|
+
import * as t from '@babel/types';
|
2
|
+
import { camel, pascal } from 'case';
|
3
|
+
import { callExpression, getMessageProperties, getResponseType } from '../utils';
|
4
|
+
export const createRecoilSelector = (context, keyPrefix, QueryClient, methodName, responseType) => {
|
5
|
+
context.addUtil('selectorFamily');
|
6
|
+
const selectorName = camel(`${methodName}Selector`);
|
7
|
+
const getterKey = camel(`${keyPrefix}${pascal(methodName)}`);
|
8
|
+
return t.exportNamedDeclaration(t.variableDeclaration('const', [t.variableDeclarator(t.identifier(selectorName), callExpression(t.identifier('selectorFamily'), [
|
9
|
+
t.objectExpression([
|
10
|
+
t.objectProperty(t.identifier('key'), t.stringLiteral(getterKey)),
|
11
|
+
t.objectProperty(t.identifier('get'), t.arrowFunctionExpression([
|
12
|
+
t.objectPattern([
|
13
|
+
t.objectProperty(t.identifier('params'), t.identifier('params'), false, true),
|
14
|
+
t.restElement(t.identifier('queryClientParams'))
|
15
|
+
])
|
16
|
+
], t.arrowFunctionExpression([
|
17
|
+
t.objectPattern([
|
18
|
+
t.objectProperty(t.identifier('get'), t.identifier('get'), false, true)
|
19
|
+
])
|
20
|
+
], t.blockStatement([
|
21
|
+
t.variableDeclaration('const', [
|
22
|
+
t.variableDeclarator(t.identifier('client'), t.callExpression(t.identifier('get'), [
|
23
|
+
t.callExpression(t.identifier('queryClient'), [
|
24
|
+
t.identifier('queryClientParams')
|
25
|
+
])
|
26
|
+
]))
|
27
|
+
]),
|
28
|
+
t.returnStatement(t.awaitExpression(t.callExpression(t.memberExpression(t.identifier('client'), t.identifier(methodName)), [
|
29
|
+
t.spreadElement(t.identifier('params'))
|
30
|
+
])))
|
31
|
+
]), true)))
|
32
|
+
])
|
33
|
+
], t.tsTypeParameterInstantiation([
|
34
|
+
t.tsTypeReference(t.identifier(responseType)),
|
35
|
+
t.tsIntersectionType([
|
36
|
+
t.tsTypeReference(t.identifier('QueryClientParams')),
|
37
|
+
t.tsTypeLiteral([
|
38
|
+
t.tsPropertySignature(t.identifier('params'), t.tsTypeAnnotation(t.tsTypeReference(t.identifier('Parameters'), t.tsTypeParameterInstantiation([
|
39
|
+
t.tsIndexedAccessType(t.tsTypeReference(t.identifier(QueryClient)), t.tsLiteralType(t.stringLiteral(methodName)))
|
40
|
+
]))))
|
41
|
+
])
|
42
|
+
])
|
43
|
+
])))]));
|
44
|
+
};
|
45
|
+
export const createRecoilSelectors = (context, keyPrefix, QueryClient, queryMsg) => {
|
46
|
+
return getMessageProperties(queryMsg)
|
47
|
+
.map((schema) => {
|
48
|
+
const underscoreName = Object.keys(schema.properties)[0];
|
49
|
+
const methodName = camel(underscoreName);
|
50
|
+
const responseType = getResponseType(context, underscoreName);
|
51
|
+
return createRecoilSelector(context, keyPrefix, QueryClient, methodName, responseType);
|
52
|
+
});
|
53
|
+
};
|
54
|
+
export const createRecoilQueryClientType = () => ({
|
55
|
+
"type": "TSTypeAliasDeclaration",
|
56
|
+
"id": {
|
57
|
+
"type": "Identifier",
|
58
|
+
"name": "QueryClientParams"
|
59
|
+
},
|
60
|
+
"typeAnnotation": {
|
61
|
+
"type": "TSTypeLiteral",
|
62
|
+
"members": [
|
63
|
+
{
|
64
|
+
"type": "TSPropertySignature",
|
65
|
+
"key": {
|
66
|
+
"type": "Identifier",
|
67
|
+
"name": "contractAddress"
|
68
|
+
},
|
69
|
+
"computed": false,
|
70
|
+
"typeAnnotation": {
|
71
|
+
"type": "TSTypeAnnotation",
|
72
|
+
"typeAnnotation": {
|
73
|
+
"type": "TSStringKeyword"
|
74
|
+
}
|
75
|
+
}
|
76
|
+
}
|
77
|
+
]
|
78
|
+
}
|
79
|
+
});
|
80
|
+
export const createRecoilQueryClient = (context, keyPrefix, QueryClient) => {
|
81
|
+
context.addUtil('selectorFamily');
|
82
|
+
const getterKey = camel(`${keyPrefix}${'QueryClient'}`);
|
83
|
+
return t.exportNamedDeclaration(t.variableDeclaration('const', [t.variableDeclarator(t.identifier('queryClient'), callExpression(t.identifier('selectorFamily'), [
|
84
|
+
t.objectExpression([
|
85
|
+
t.objectProperty(t.identifier('key'), t.stringLiteral(getterKey)),
|
86
|
+
t.objectProperty(t.identifier('get'), t.arrowFunctionExpression([
|
87
|
+
t.objectPattern([
|
88
|
+
t.objectProperty(t.identifier('contractAddress'), t.identifier('contractAddress'), false, true)
|
89
|
+
])
|
90
|
+
], t.arrowFunctionExpression([
|
91
|
+
t.objectPattern([
|
92
|
+
t.objectProperty(t.identifier('get'), t.identifier('get'), false, true)
|
93
|
+
])
|
94
|
+
], t.blockStatement([
|
95
|
+
t.variableDeclaration('const', [
|
96
|
+
t.variableDeclarator(t.identifier('client'), t.callExpression(t.identifier('get'), [
|
97
|
+
t.identifier('cosmWasmClient')
|
98
|
+
]))
|
99
|
+
]),
|
100
|
+
t.returnStatement(t.newExpression(t.identifier(QueryClient), [
|
101
|
+
t.identifier('client'),
|
102
|
+
t.identifier('contractAddress')
|
103
|
+
]))
|
104
|
+
]), false)))
|
105
|
+
])
|
106
|
+
], t.tsTypeParameterInstantiation([
|
107
|
+
t.tsTypeReference(t.identifier(QueryClient)),
|
108
|
+
t.tsTypeReference(t.identifier('QueryClientParams'))
|
109
|
+
])))]));
|
110
|
+
};
|