wasm-ast-types 0.4.1 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +129 -0
- package/main/react-query.js +178 -16
- package/main/react-query.spec.js +24 -0
- package/main/recoil.spec.js +1 -1
- package/main/utils/babel.js +28 -2
- package/main/utils/types.js +88 -62
- package/main/wasm.arrays.spec.js +30 -0
- package/main/wasm.js +7 -5
- package/module/react-query.js +156 -10
- package/module/react-query.spec.js +24 -1
- package/module/recoil.spec.js +1 -1
- package/module/utils/babel.js +15 -0
- package/module/utils/types.js +70 -57
- package/module/wasm.arrays.spec.js +24 -0
- package/module/wasm.js +4 -4
- package/package.json +2 -2
- package/types/react-query.d.ts +40 -1
package/module/utils/types.js
CHANGED
@@ -16,7 +16,7 @@ const getTypeStrFromRef = $ref => {
|
|
16
16
|
}
|
17
17
|
};
|
18
18
|
|
19
|
-
const getTypeFromRef = $ref => {
|
19
|
+
export const getTypeFromRef = $ref => {
|
20
20
|
switch ($ref) {
|
21
21
|
case '#/definitions/Binary':
|
22
22
|
return t.tsTypeReference(t.identifier('Binary'));
|
@@ -34,8 +34,16 @@ const getArrayTypeFromRef = $ref => {
|
|
34
34
|
return t.tsArrayType(getTypeFromRef($ref));
|
35
35
|
};
|
36
36
|
|
37
|
-
const
|
38
|
-
|
37
|
+
const getArrayTypeFromItems = items => {
|
38
|
+
if (items.type === 'array') {
|
39
|
+
if (Array.isArray(items.items)) {
|
40
|
+
return t.tsArrayType(t.tsArrayType(getType(items.items[0].type)));
|
41
|
+
} else {
|
42
|
+
return t.tsArrayType(getArrayTypeFromItems(items.items));
|
43
|
+
}
|
44
|
+
}
|
45
|
+
|
46
|
+
return t.tsArrayType(getType(items.type));
|
39
47
|
};
|
40
48
|
|
41
49
|
export const getType = type => {
|
@@ -48,8 +56,6 @@ export const getType = type => {
|
|
48
56
|
|
49
57
|
case 'integer':
|
50
58
|
return t.tsNumberKeyword();
|
51
|
-
// case 'object':
|
52
|
-
// return t.tsObjectKeyword();
|
53
59
|
|
54
60
|
default:
|
55
61
|
throw new Error('contact maintainers [unknown type]: ' + type);
|
@@ -101,7 +107,7 @@ export const getPropertyType = (schema, prop) => {
|
|
101
107
|
} else if (info.items.title) {
|
102
108
|
type = t.tsArrayType(t.tsTypeReference(t.identifier(info.items.title)));
|
103
109
|
} else if (info.items.type) {
|
104
|
-
type =
|
110
|
+
type = getArrayTypeFromItems(info.items);
|
105
111
|
} else {
|
106
112
|
throw new Error('[info.items] case not handled by transpiler. contact maintainers.');
|
107
113
|
}
|
@@ -144,65 +150,72 @@ export const getPropertyType = (schema, prop) => {
|
|
144
150
|
optional
|
145
151
|
};
|
146
152
|
};
|
147
|
-
export
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
return propertySignature(camelize ? camel(prop) : prop, t.tsTypeAnnotation(t.tsTypeReference(t.identifier(jsonschema.properties[prop].title))));
|
154
|
-
} else {
|
155
|
-
throw new Error('createTypedObjectParams() contact maintainer');
|
156
|
-
}
|
153
|
+
export function getPropertySignatureFromProp(jsonschema, prop, camelize) {
|
154
|
+
if (jsonschema.properties[prop].type === 'object') {
|
155
|
+
if (jsonschema.properties[prop].title) {
|
156
|
+
return propertySignature(camelize ? camel(prop) : prop, t.tsTypeAnnotation(t.tsTypeReference(t.identifier(jsonschema.properties[prop].title))));
|
157
|
+
} else {
|
158
|
+
throw new Error('createTypedObjectParams() contact maintainer');
|
157
159
|
}
|
160
|
+
}
|
158
161
|
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
}
|
173
|
-
} else if (Array.isArray(jsonschema.properties[prop].oneOf)) {
|
174
|
-
const isOptional = !jsonschema.required?.includes(prop);
|
175
|
-
const unionTypes = jsonschema.properties[prop].oneOf.map(el => {
|
176
|
-
if (el.title) return el.title;
|
177
|
-
if (el.$ref) return getTypeStrFromRef(el.$ref);
|
178
|
-
return el.type;
|
179
|
-
});
|
180
|
-
const uniqUnionTypes = [...new Set(unionTypes)];
|
181
|
-
|
182
|
-
if (uniqUnionTypes.length === 1) {
|
183
|
-
return propertySignature(camelize ? camel(prop) : prop, t.tsTypeAnnotation(t.tsTypeReference(t.identifier(uniqUnionTypes[0]))), isOptional);
|
184
|
-
} else {
|
185
|
-
return propertySignature(camelize ? camel(prop) : prop, t.tsTypeAnnotation(t.tsUnionType(uniqUnionTypes.map(typ => t.tsTypeReference(t.identifier(typ))))), isOptional);
|
186
|
-
}
|
162
|
+
if (Array.isArray(jsonschema.properties[prop].allOf)) {
|
163
|
+
const isOptional = !jsonschema.required?.includes(prop);
|
164
|
+
const unionTypes = jsonschema.properties[prop].allOf.map(el => {
|
165
|
+
if (el.title) return el.title;
|
166
|
+
if (el.$ref) return getTypeStrFromRef(el.$ref);
|
167
|
+
return el.type;
|
168
|
+
});
|
169
|
+
const uniqUnionTypes = [...new Set(unionTypes)];
|
170
|
+
|
171
|
+
if (uniqUnionTypes.length === 1) {
|
172
|
+
return propertySignature(camelize ? camel(prop) : prop, t.tsTypeAnnotation(t.tsTypeReference(t.identifier(uniqUnionTypes[0]))), isOptional);
|
173
|
+
} else {
|
174
|
+
return propertySignature(camelize ? camel(prop) : prop, t.tsTypeAnnotation(t.tsUnionType(uniqUnionTypes.map(typ => t.tsTypeReference(t.identifier(typ))))), isOptional);
|
187
175
|
}
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
176
|
+
} else if (Array.isArray(jsonschema.properties[prop].oneOf)) {
|
177
|
+
const isOptional = !jsonschema.required?.includes(prop);
|
178
|
+
const unionTypes = jsonschema.properties[prop].oneOf.map(el => {
|
179
|
+
if (el.title) return el.title;
|
180
|
+
if (el.$ref) return getTypeStrFromRef(el.$ref);
|
181
|
+
return el.type;
|
182
|
+
});
|
183
|
+
const uniqUnionTypes = [...new Set(unionTypes)];
|
184
|
+
|
185
|
+
if (uniqUnionTypes.length === 1) {
|
186
|
+
return propertySignature(camelize ? camel(prop) : prop, t.tsTypeAnnotation(t.tsTypeReference(t.identifier(uniqUnionTypes[0]))), isOptional);
|
187
|
+
} else {
|
188
|
+
return propertySignature(camelize ? camel(prop) : prop, t.tsTypeAnnotation(t.tsUnionType(uniqUnionTypes.map(typ => t.tsTypeReference(t.identifier(typ))))), isOptional);
|
194
189
|
}
|
190
|
+
}
|
191
|
+
|
192
|
+
try {
|
193
|
+
getPropertyType(jsonschema, prop);
|
194
|
+
} catch (e) {
|
195
|
+
console.log(e);
|
196
|
+
console.log(jsonschema, prop);
|
197
|
+
}
|
198
|
+
|
199
|
+
const {
|
200
|
+
type,
|
201
|
+
optional
|
202
|
+
} = getPropertyType(jsonschema, prop);
|
203
|
+
return propertySignature(camelize ? camel(prop) : prop, t.tsTypeAnnotation(type), optional);
|
204
|
+
}
|
205
|
+
export const getParamsTypeAnnotation = (jsonschema, camelize = true) => {
|
206
|
+
const keys = Object.keys(jsonschema.properties ?? {});
|
207
|
+
if (!keys.length) return undefined;
|
208
|
+
const typedParams = keys.map(prop => getPropertySignatureFromProp(jsonschema, prop, camelize));
|
209
|
+
return t.tsTypeAnnotation(t.tsTypeLiteral([...typedParams]));
|
210
|
+
};
|
211
|
+
export const createTypedObjectParams = (jsonschema, camelize = true) => {
|
212
|
+
const keys = Object.keys(jsonschema.properties ?? {});
|
213
|
+
if (!keys.length) return; // const typedParams = keys.map(prop => getPropertySignatureFromProp(jsonschema, prop, camelize));
|
195
214
|
|
196
|
-
const {
|
197
|
-
type,
|
198
|
-
optional
|
199
|
-
} = getPropertyType(jsonschema, prop);
|
200
|
-
return propertySignature(camelize ? camel(prop) : prop, t.tsTypeAnnotation(type), optional);
|
201
|
-
});
|
202
215
|
const params = keys.map(prop => {
|
203
216
|
return t.objectProperty(camelize ? t.identifier(camel(prop)) : t.identifier(prop), camelize ? t.identifier(camel(prop)) : t.identifier(prop), false, true);
|
204
217
|
});
|
205
218
|
const obj = t.objectPattern([...params]);
|
206
|
-
obj.typeAnnotation =
|
219
|
+
obj.typeAnnotation = getParamsTypeAnnotation(jsonschema, camelize);
|
207
220
|
return obj;
|
208
221
|
};
|
@@ -0,0 +1,24 @@
|
|
1
|
+
import generate from '@babel/generator';
|
2
|
+
import message from './../../../__fixtures__/arrays/schema/schema.json';
|
3
|
+
import { createQueryClass, createExecuteClass, createExecuteInterface, createTypeInterface } from './wasm';
|
4
|
+
|
5
|
+
const expectCode = ast => {
|
6
|
+
expect(generate(ast).code).toMatchSnapshot();
|
7
|
+
};
|
8
|
+
|
9
|
+
const printCode = ast => {
|
10
|
+
console.log(generate(ast).code);
|
11
|
+
};
|
12
|
+
|
13
|
+
it('execute_msg_for__empty', () => {
|
14
|
+
expectCode(createTypeInterface(message));
|
15
|
+
});
|
16
|
+
it('query classes', () => {
|
17
|
+
expectCode(createQueryClass('SG721QueryClient', 'SG721ReadOnlyInstance', message));
|
18
|
+
});
|
19
|
+
it('execute classes array types', () => {
|
20
|
+
expectCode(createExecuteClass('SG721Client', 'SG721Instance', null, message));
|
21
|
+
});
|
22
|
+
it('execute interfaces no extends', () => {
|
23
|
+
expectCode(createExecuteInterface('SG721Instance', null, message));
|
24
|
+
});
|
package/module/wasm.js
CHANGED
@@ -27,6 +27,7 @@ export const createQueryClass = (className, implementsClassName, queryMsg) => {
|
|
27
27
|
t.classMethod('constructor', t.identifier('constructor'), [typedIdentifier('client', t.tsTypeAnnotation(t.tsTypeReference(t.identifier('CosmWasmClient')))), typedIdentifier('contractAddress', t.tsTypeAnnotation(t.tsStringKeyword()))], t.blockStatement([// client/contract set
|
28
28
|
t.expressionStatement(t.assignmentExpression('=', t.memberExpression(t.thisExpression(), t.identifier('client')), t.identifier('client'))), t.expressionStatement(t.assignmentExpression('=', t.memberExpression(t.thisExpression(), t.identifier('contractAddress')), t.identifier('contractAddress'))), ...bindings])), ...methods], [t.tSExpressionWithTypeArguments(t.identifier(implementsClassName))]));
|
29
29
|
};
|
30
|
+
export const CONSTANT_EXEC_PARAMS = [t.assignmentPattern(identifier('fee', t.tsTypeAnnotation(t.tsUnionType([t.tSNumberKeyword(), t.tsTypeReference(t.identifier('StdFee')), t.tsLiteralType(t.stringLiteral('auto'))])), false), t.stringLiteral('auto')), identifier('memo', t.tsTypeAnnotation(t.tsStringKeyword()), true), identifier('funds', t.tsTypeAnnotation(tsTypeOperator(t.tsArrayType(t.tsTypeReference(t.identifier('Coin'))), 'readonly')), true)];
|
30
31
|
export const createWasmExecMethod = jsonschema => {
|
31
32
|
const underscoreName = Object.keys(jsonschema.properties)[0];
|
32
33
|
const methodName = camel(underscoreName);
|
@@ -35,9 +36,8 @@ export const createWasmExecMethod = jsonschema => {
|
|
35
36
|
const args = Object.keys(properties).map(prop => {
|
36
37
|
return t.objectProperty(t.identifier(prop), t.identifier(camel(prop)), false, prop === camel(prop));
|
37
38
|
});
|
38
|
-
const constantParams = [t.assignmentPattern(identifier('fee', t.tsTypeAnnotation(t.tsUnionType([t.tSNumberKeyword(), t.tsTypeReference(t.identifier('StdFee')), t.tsLiteralType(t.stringLiteral('auto'))])), false), t.stringLiteral('auto')), identifier('memo', t.tsTypeAnnotation(t.tsStringKeyword()), true), identifier('funds', t.tsTypeAnnotation(tsTypeOperator(t.tsArrayType(t.tsTypeReference(t.identifier('Coin'))), 'readonly')), true)];
|
39
39
|
return t.classProperty(t.identifier(methodName), arrowFunctionExpression(obj ? [// props
|
40
|
-
obj, ...
|
40
|
+
obj, ...CONSTANT_EXEC_PARAMS] : CONSTANT_EXEC_PARAMS, t.blockStatement([t.returnStatement(t.awaitExpression(t.callExpression(t.memberExpression(t.memberExpression(t.thisExpression(), t.identifier('client')), t.identifier('execute')), [t.memberExpression(t.thisExpression(), t.identifier('sender')), t.memberExpression(t.thisExpression(), t.identifier('contractAddress')), t.objectExpression([t.objectProperty(t.identifier(underscoreName), t.objectExpression([...args]))]), t.identifier('fee'), t.identifier('memo'), t.identifier('funds')])))]), // return type
|
41
41
|
t.tsTypeAnnotation(t.tsTypeReference(t.identifier('Promise'), t.tsTypeParameterInstantiation([t.tSTypeReference(t.identifier('ExecuteResult'))]))), true));
|
42
42
|
};
|
43
43
|
export const createExecuteClass = (className, implementsClassName, extendsClassName, execMsg) => {
|
@@ -81,13 +81,13 @@ export const createPropertyFunctionWithObjectParams = (methodName, responseType,
|
|
81
81
|
};
|
82
82
|
return t.tSPropertySignature(t.identifier(methodName), t.tsTypeAnnotation(func));
|
83
83
|
};
|
84
|
+
export const FIXED_EXECUTE_PARAMS = [identifier('fee', t.tsTypeAnnotation(t.tsUnionType([t.tsNumberKeyword(), t.tsTypeReference(t.identifier('StdFee')), t.tsLiteralType(t.stringLiteral('auto'))])), true), identifier('memo', t.tsTypeAnnotation(t.tsStringKeyword()), true), identifier('funds', t.tsTypeAnnotation(tsTypeOperator(t.tsArrayType(t.tsTypeReference(t.identifier('Coin'))), 'readonly')), true)];
|
84
85
|
export const createPropertyFunctionWithObjectParamsForExec = (methodName, responseType, jsonschema) => {
|
85
86
|
const obj = createTypedObjectParams(jsonschema);
|
86
|
-
const fixedParams = [identifier('fee', t.tsTypeAnnotation(t.tsUnionType([t.tsNumberKeyword(), t.tsTypeReference(t.identifier('StdFee')), t.tsLiteralType(t.stringLiteral('auto'))])), true), identifier('memo', t.tsTypeAnnotation(t.tsStringKeyword()), true), identifier('funds', t.tsTypeAnnotation(tsTypeOperator(t.tsArrayType(t.tsTypeReference(t.identifier('Coin'))), 'readonly')), true)];
|
87
87
|
const func = {
|
88
88
|
type: 'TSFunctionType',
|
89
89
|
typeAnnotation: promiseTypeAnnotation(responseType),
|
90
|
-
parameters: obj ? [obj, ...
|
90
|
+
parameters: obj ? [obj, ...FIXED_EXECUTE_PARAMS] : FIXED_EXECUTE_PARAMS
|
91
91
|
};
|
92
92
|
return t.tSPropertySignature(t.identifier(methodName), t.tsTypeAnnotation(func));
|
93
93
|
};
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "wasm-ast-types",
|
3
|
-
"version": "0.
|
3
|
+
"version": "0.5.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",
|
@@ -84,5 +84,5 @@
|
|
84
84
|
"ast-stringify": "0.1.0",
|
85
85
|
"case": "1.6.3"
|
86
86
|
},
|
87
|
-
"gitHead": "
|
87
|
+
"gitHead": "871479982969e89f40b75ca5c85e8bed34a824da"
|
88
88
|
}
|
package/types/react-query.d.ts
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
import * as t from '@babel/types';
|
2
|
-
import { QueryMsg } from './types';
|
2
|
+
import { QueryMsg, ExecuteMsg } from './types';
|
3
3
|
|
4
4
|
interface ReactQueryHooks {
|
5
5
|
queryMsg: QueryMsg
|
@@ -18,6 +18,8 @@ interface ReactQueryHookQuery {
|
|
18
18
|
|
19
19
|
export declare interface ReactQueryOptions {
|
20
20
|
optionalClient?: boolean
|
21
|
+
v4?: boolean
|
22
|
+
mutations?: boolean
|
21
23
|
}
|
22
24
|
export declare const createReactQueryHooks: ({ queryMsg, contractName, QueryClient, options }: ReactQueryHooks) => any;
|
23
25
|
export declare const createReactQueryHook: ({ hookName, hookParamsTypeName, responseType, hookKeyName, methodName, jsonschema }: ReactQueryHookQuery) => t.ExportNamedDeclaration;
|
@@ -28,4 +30,41 @@ interface ReactQueryHookQueryInterface {
|
|
28
30
|
jsonschema: any;
|
29
31
|
}
|
30
32
|
export declare const createReactQueryHookInterface: ({ QueryClient, hookParamsTypeName, responseType, jsonschema }: ReactQueryHookQueryInterface) => t.ExportNamedDeclaration;
|
33
|
+
|
34
|
+
interface ReactQueryMutationHooks {
|
35
|
+
execMsg: ExecuteMsg
|
36
|
+
contractName: string
|
37
|
+
ExecuteClient: string
|
38
|
+
options?: ReactQueryOptions
|
39
|
+
}
|
40
|
+
export declare const createReactQueryMutationHooks: ({ execMsg, contractName, ExecuteClient, options }: ReactQueryMutationHooks) => any
|
41
|
+
|
42
|
+
interface ReactQueryMutationHook {
|
43
|
+
mutationHookName: string;
|
44
|
+
mutationHookParamsTypeName: string;
|
45
|
+
execMethodName: string;
|
46
|
+
execArgs: t.ObjectProperty[]
|
47
|
+
options?: ReactQueryOptions
|
48
|
+
}
|
49
|
+
|
50
|
+
export declare const createReactQueryMutationHook: ({
|
51
|
+
mutationHookName,
|
52
|
+
mutationHookParamsTypeName,
|
53
|
+
execMethodName,
|
54
|
+
execArgs,
|
55
|
+
options,
|
56
|
+
}: ReactQueryMutationHook) => any
|
57
|
+
|
58
|
+
interface ReactQueryMutationHookInterface {
|
59
|
+
ExecuteClient: string
|
60
|
+
mutationHookParamsTypeName: string;
|
61
|
+
jsonschema: any
|
62
|
+
}
|
63
|
+
|
64
|
+
export declare const createReactQueryMutationArgsInterface: ({
|
65
|
+
ExecuteClient,
|
66
|
+
mutationHookParamsTypeName,
|
67
|
+
jsonschema
|
68
|
+
}: ReactQueryMutationHookInterface) => t.ExportNamedDeclaration;
|
69
|
+
|
31
70
|
export { };
|