wasm-ast-types 0.18.1 → 0.19.0
Sign up to get free protection for your applications and to get access to all the features.
- package/main/client/test/ts-client.issue-98.test.js +30 -0
- package/main/context/context.js +59 -13
- package/main/context/imports.js +37 -15
- package/main/index.js +13 -0
- package/main/msg-builder/index.js +18 -0
- package/main/msg-builder/msg-builder.js +54 -0
- package/main/msg-builder/msg-builder.spec.js +20 -0
- package/main/utils/babel.js +56 -5
- package/main/utils/index.js +13 -0
- package/main/utils/ref.js +15 -0
- package/main/utils/types.js +1 -1
- package/module/client/test/ts-client.issue-98.test.js +23 -0
- package/module/context/context.js +29 -6
- package/module/context/imports.js +33 -14
- package/module/index.js +2 -1
- package/module/msg-builder/index.js +1 -0
- package/module/msg-builder/msg-builder.js +33 -0
- package/module/msg-builder/msg-builder.spec.js +12 -0
- package/module/utils/babel.js +31 -5
- package/module/utils/index.js +2 -1
- package/module/utils/ref.js +4 -0
- package/module/utils/types.js +1 -1
- package/package.json +2 -2
- package/src/client/test/__snapshots__/ts-client.issue-98.test.ts.snap +117 -0
- package/src/client/test/__snapshots__/ts-client.wager.spec.ts.snap +1 -1
- package/src/client/test/ts-client.issue-98.test.ts +55 -0
- package/src/context/context.ts +45 -10
- package/src/context/imports.ts +49 -15
- package/src/index.ts +2 -1
- package/src/msg-builder/__snapshots__/msg-builder.spec.ts.snap +249 -0
- package/src/msg-builder/index.ts +1 -0
- package/src/msg-builder/msg-builder.spec.ts +17 -0
- package/src/msg-builder/msg-builder.ts +107 -0
- package/src/react-query/react-query.ts +1 -1
- package/src/utils/babel.ts +34 -5
- package/src/utils/index.ts +1 -0
- package/src/utils/ref.ts +6 -0
- package/src/utils/types.ts +2 -6
- package/types/context/context.d.ts +30 -5
- package/types/context/imports.d.ts +7 -2
- package/types/index.d.ts +1 -0
- package/types/msg-builder/index.d.ts +1 -0
- package/types/msg-builder/msg-builder.d.ts +4 -0
- package/types/react-query/react-query.d.ts +11 -10
- package/types/recoil/recoil.d.ts +1 -1
- package/types/utils/babel.d.ts +3 -2
- package/types/utils/index.d.ts +1 -0
- package/types/utils/ref.d.ts +2 -0
@@ -23,11 +23,11 @@ const makeReactQuerySwitch = varName => {
|
|
23
23
|
};
|
24
24
|
|
25
25
|
export const UTILS = {
|
26
|
+
selectorFamily: 'recoil',
|
26
27
|
MsgExecuteContract: 'cosmjs-types/cosmwasm/wasm/v1/tx',
|
27
28
|
MsgExecuteContractEncodeObject: 'cosmwasm',
|
28
29
|
Coin: '@cosmjs/amino',
|
29
30
|
toUtf8: '@cosmjs/encoding',
|
30
|
-
selectorFamily: 'recoil',
|
31
31
|
StdFee: '@cosmjs/amino',
|
32
32
|
CosmWasmClient: '@cosmjs/cosmwasm-stargate',
|
33
33
|
ExecuteResult: '@cosmjs/cosmwasm-stargate',
|
@@ -38,23 +38,42 @@ export const UTILS = {
|
|
38
38
|
useMutation: makeReactQuerySwitch('useMutation'),
|
39
39
|
UseMutationOptions: makeReactQuerySwitch('UseMutationOptions')
|
40
40
|
};
|
41
|
-
export const convertUtilsToImportList = (context, utils) => {
|
41
|
+
export const convertUtilsToImportList = (context, utils, registeredUtils) => {
|
42
42
|
return utils.map(util => {
|
43
|
-
|
44
|
-
|
45
|
-
if (
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
}
|
51
|
-
}
|
52
|
-
|
53
|
-
|
54
|
-
|
43
|
+
let result = null;
|
44
|
+
|
45
|
+
if (registeredUtils) {
|
46
|
+
result = convertUtil(context, util, registeredUtils);
|
47
|
+
|
48
|
+
if (result) {
|
49
|
+
return result;
|
50
|
+
}
|
51
|
+
}
|
52
|
+
|
53
|
+
result = convertUtil(context, util, UTILS);
|
54
|
+
|
55
|
+
if (result) {
|
56
|
+
return result;
|
55
57
|
}
|
58
|
+
|
59
|
+
throw new Error(`missing Util! ::[${util}]`);
|
56
60
|
});
|
57
61
|
};
|
62
|
+
export const convertUtil = (context, util, registeredUtils) => {
|
63
|
+
if (!registeredUtils.hasOwnProperty(util)) return null;
|
64
|
+
|
65
|
+
if (typeof registeredUtils[util] === 'string') {
|
66
|
+
return {
|
67
|
+
type: 'import',
|
68
|
+
path: registeredUtils[util],
|
69
|
+
name: util
|
70
|
+
};
|
71
|
+
} else if (typeof registeredUtils[util] === 'function') {
|
72
|
+
return registeredUtils[util](context);
|
73
|
+
} else {
|
74
|
+
return registeredUtils[util];
|
75
|
+
}
|
76
|
+
};
|
58
77
|
export const getImportStatements = list => {
|
59
78
|
const imports = list.reduce((m, obj) => {
|
60
79
|
m[obj.path] = m[obj.path] || [];
|
package/module/index.js
CHANGED
@@ -0,0 +1 @@
|
|
1
|
+
export * from './msg-builder';
|
@@ -0,0 +1,33 @@
|
|
1
|
+
import * as t from "@babel/types";
|
2
|
+
import { camel } from "case";
|
3
|
+
import { abstractClassDeclaration, arrowFunctionExpression, getMessageProperties } from "../utils";
|
4
|
+
import { createTypedObjectParams } from "../utils/types";
|
5
|
+
import { getWasmMethodArgs } from "../client/client";
|
6
|
+
export const createMsgBuilderClass = (context, className, msg) => {
|
7
|
+
const staticMethods = getMessageProperties(msg).map(schema => {
|
8
|
+
return createStaticExecMethodMsgBuilder(context, schema, msg.title);
|
9
|
+
}); // const blockStmt = bindings;
|
10
|
+
|
11
|
+
return t.exportNamedDeclaration(abstractClassDeclaration(className, staticMethods, [], null));
|
12
|
+
};
|
13
|
+
/**
|
14
|
+
* CamelCasedProperties<Extract<ExecuteMsg, { exec_on_module: unknown }>['exec_on_module']>
|
15
|
+
*/
|
16
|
+
|
17
|
+
function createExtractTypeAnnotation(underscoreName, msgTitle) {
|
18
|
+
return t.tsTypeAnnotation(t.tsTypeReference(t.identifier("CamelCasedProperties"), t.tsTypeParameterInstantiation([t.tsIndexedAccessType(t.tsTypeReference(t.identifier("Extract"), t.tsTypeParameterInstantiation([t.tsTypeReference(t.identifier(msgTitle)), t.tsTypeLiteral([t.tsPropertySignature(t.identifier(underscoreName), t.tsTypeAnnotation(t.tsUnknownKeyword()))])])), t.tsLiteralType(t.stringLiteral(underscoreName)))])));
|
19
|
+
}
|
20
|
+
|
21
|
+
const createStaticExecMethodMsgBuilder = (context, jsonschema, msgTitle) => {
|
22
|
+
const underscoreName = Object.keys(jsonschema.properties)[0];
|
23
|
+
const methodName = camel(underscoreName);
|
24
|
+
const obj = createTypedObjectParams(context, jsonschema.properties[underscoreName]);
|
25
|
+
const args = getWasmMethodArgs(context, jsonschema.properties[underscoreName]);
|
26
|
+
if (obj) obj.typeAnnotation = createExtractTypeAnnotation(underscoreName, msgTitle);
|
27
|
+
return t.classProperty(t.identifier(methodName), arrowFunctionExpression( // params
|
28
|
+
obj ? [// props
|
29
|
+
obj] : [], // body
|
30
|
+
t.blockStatement([t.returnStatement(t.objectExpression([t.objectProperty(t.identifier(underscoreName), t.tsAsExpression(t.objectExpression(args), t.tsTypeReference(t.identifier('const'))))]))]), // return type
|
31
|
+
t.tsTypeAnnotation(t.tsTypeReference(t.identifier(msgTitle))), false), null, null, false, // static
|
32
|
+
true);
|
33
|
+
};
|
@@ -0,0 +1,12 @@
|
|
1
|
+
import execute_msg from '../../../../__fixtures__/basic/execute_msg_for__empty.json';
|
2
|
+
import query_msg from '../../../../__fixtures__/basic/query_msg.json';
|
3
|
+
import { createMsgBuilderClass } from './msg-builder';
|
4
|
+
import { expectCode, makeContext } from '../../test-utils';
|
5
|
+
it('execute class', () => {
|
6
|
+
const ctx = makeContext(execute_msg);
|
7
|
+
expectCode(createMsgBuilderClass(ctx, 'SG721MsgBuilder', execute_msg));
|
8
|
+
});
|
9
|
+
it('query class', () => {
|
10
|
+
const ctx = makeContext(execute_msg);
|
11
|
+
expectCode(createMsgBuilderClass(ctx, 'SG721MsgBuilder', query_msg));
|
12
|
+
});
|
package/module/utils/babel.js
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
import * as t from '@babel/types';
|
2
2
|
import { snake } from "case";
|
3
|
-
// t.TSPropertySignature - kind?
|
3
|
+
import { refLookup } from './ref'; // t.TSPropertySignature - kind?
|
4
|
+
|
4
5
|
export const propertySignature = (name, typeAnnotation, optional = false) => {
|
5
6
|
return {
|
6
7
|
type: 'TSPropertySignature',
|
@@ -21,10 +22,30 @@ export const tsTypeOperator = (typeAnnotation, operator) => {
|
|
21
22
|
return obj;
|
22
23
|
};
|
23
24
|
export const getMessageProperties = msg => {
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
25
|
+
let results = [];
|
26
|
+
let objs = [];
|
27
|
+
|
28
|
+
if (msg.anyOf) {
|
29
|
+
objs = msg.anyOf;
|
30
|
+
} else if (msg.oneOf) {
|
31
|
+
objs = msg.oneOf;
|
32
|
+
} else if (msg.allOf) {
|
33
|
+
objs = msg.allOf;
|
34
|
+
}
|
35
|
+
|
36
|
+
for (const obj of objs) {
|
37
|
+
if (obj.properties) {
|
38
|
+
results.push(obj);
|
39
|
+
} else {
|
40
|
+
if (obj.$ref) {
|
41
|
+
const ref = refLookup(obj.$ref, msg);
|
42
|
+
const refProps = getMessageProperties(ref);
|
43
|
+
results = [...results, ...refProps];
|
44
|
+
}
|
45
|
+
}
|
46
|
+
}
|
47
|
+
|
48
|
+
return results;
|
28
49
|
};
|
29
50
|
export const tsPropertySignature = (key, typeAnnotation, optional) => {
|
30
51
|
const obj = t.tsPropertySignature(key, typeAnnotation);
|
@@ -53,6 +74,11 @@ export const typedIdentifier = (name, typeAnnotation, optional = false) => {
|
|
53
74
|
export const promiseTypeAnnotation = name => {
|
54
75
|
return t.tsTypeAnnotation(t.tsTypeReference(t.identifier('Promise'), t.tsTypeParameterInstantiation([t.tsTypeReference(t.identifier(name))])));
|
55
76
|
};
|
77
|
+
export const abstractClassDeclaration = (name, body, implementsExressions = [], superClass = null) => {
|
78
|
+
const declaration = classDeclaration(name, body, implementsExressions, superClass);
|
79
|
+
declaration.abstract = true;
|
80
|
+
return declaration;
|
81
|
+
};
|
56
82
|
export const classDeclaration = (name, body, implementsExressions = [], superClass = null) => {
|
57
83
|
const declaration = t.classDeclaration(t.identifier(name), superClass, t.classBody(body));
|
58
84
|
|
package/module/utils/index.js
CHANGED
package/module/utils/types.js
CHANGED
@@ -40,7 +40,7 @@ const getArrayTypeFromItems = items => {
|
|
40
40
|
// passing in [{"type":"string"}]
|
41
41
|
if (Array.isArray(items)) {
|
42
42
|
if (items[0]?.type === 'array') {
|
43
|
-
return
|
43
|
+
return getArrayTypeFromItems(items[0]);
|
44
44
|
}
|
45
45
|
|
46
46
|
return t.tsArrayType(t.tsArrayType(getTypeOrRef(items[0])));
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "wasm-ast-types",
|
3
|
-
"version": "0.
|
3
|
+
"version": "0.19.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",
|
@@ -88,5 +88,5 @@
|
|
88
88
|
"case": "1.6.3",
|
89
89
|
"deepmerge": "4.2.2"
|
90
90
|
},
|
91
|
-
"gitHead": "
|
91
|
+
"gitHead": "dccd0edd182dc4d5e58b30e7f46732e0be483bc1"
|
92
92
|
}
|
@@ -0,0 +1,117 @@
|
|
1
|
+
// Jest Snapshot v1, https://goo.gl/fbAQLP
|
2
|
+
|
3
|
+
exports[`execute classes array types 1`] = `
|
4
|
+
"export class SG721Client implements SG721Instance {
|
5
|
+
client: SigningCosmWasmClient;
|
6
|
+
sender: string;
|
7
|
+
contractAddress: string;
|
8
|
+
|
9
|
+
constructor(client: SigningCosmWasmClient, sender: string, contractAddress: string) {
|
10
|
+
this.client = client;
|
11
|
+
this.sender = sender;
|
12
|
+
this.contractAddress = contractAddress;
|
13
|
+
this.getConfig = this.getConfig.bind(this);
|
14
|
+
this.getPlugins = this.getPlugins.bind(this);
|
15
|
+
this.getPluginById = this.getPluginById.bind(this);
|
16
|
+
}
|
17
|
+
|
18
|
+
getConfig = async (fee: number | StdFee | \\"auto\\" = \\"auto\\", memo?: string, funds?: Coin[]): Promise<ExecuteResult> => {
|
19
|
+
return await this.client.execute(this.sender, this.contractAddress, {
|
20
|
+
get_config: {}
|
21
|
+
}, fee, memo, funds);
|
22
|
+
};
|
23
|
+
getPlugins = async ({
|
24
|
+
limit,
|
25
|
+
startAfter
|
26
|
+
}: {
|
27
|
+
limit?: number;
|
28
|
+
startAfter?: number;
|
29
|
+
}, fee: number | StdFee | \\"auto\\" = \\"auto\\", memo?: string, funds?: Coin[]): Promise<ExecuteResult> => {
|
30
|
+
return await this.client.execute(this.sender, this.contractAddress, {
|
31
|
+
get_plugins: {
|
32
|
+
limit,
|
33
|
+
start_after: startAfter
|
34
|
+
}
|
35
|
+
}, fee, memo, funds);
|
36
|
+
};
|
37
|
+
getPluginById = async ({
|
38
|
+
id
|
39
|
+
}: {
|
40
|
+
id: number;
|
41
|
+
}, fee: number | StdFee | \\"auto\\" = \\"auto\\", memo?: string, funds?: Coin[]): Promise<ExecuteResult> => {
|
42
|
+
return await this.client.execute(this.sender, this.contractAddress, {
|
43
|
+
get_plugin_by_id: {
|
44
|
+
id
|
45
|
+
}
|
46
|
+
}, fee, memo, funds);
|
47
|
+
};
|
48
|
+
}"
|
49
|
+
`;
|
50
|
+
|
51
|
+
exports[`execute interfaces no extends 1`] = `
|
52
|
+
"export interface SG721Instance {
|
53
|
+
contractAddress: string;
|
54
|
+
sender: string;
|
55
|
+
getConfig: (fee?: number | StdFee | \\"auto\\", memo?: string, funds?: Coin[]) => Promise<ExecuteResult>;
|
56
|
+
getPlugins: ({
|
57
|
+
limit,
|
58
|
+
startAfter
|
59
|
+
}: {
|
60
|
+
limit?: number;
|
61
|
+
startAfter?: number;
|
62
|
+
}, fee?: number | StdFee | \\"auto\\", memo?: string, funds?: Coin[]) => Promise<ExecuteResult>;
|
63
|
+
getPluginById: ({
|
64
|
+
id
|
65
|
+
}: {
|
66
|
+
id: number;
|
67
|
+
}, fee?: number | StdFee | \\"auto\\", memo?: string, funds?: Coin[]) => Promise<ExecuteResult>;
|
68
|
+
}"
|
69
|
+
`;
|
70
|
+
|
71
|
+
exports[`execute_msg_for__empty 1`] = `"export type QueryMsg = QueryMsg;"`;
|
72
|
+
|
73
|
+
exports[`query classes 1`] = `
|
74
|
+
"export class SG721QueryClient implements SG721ReadOnlyInstance {
|
75
|
+
client: CosmWasmClient;
|
76
|
+
contractAddress: string;
|
77
|
+
|
78
|
+
constructor(client: CosmWasmClient, contractAddress: string) {
|
79
|
+
this.client = client;
|
80
|
+
this.contractAddress = contractAddress;
|
81
|
+
this.getConfig = this.getConfig.bind(this);
|
82
|
+
this.getPlugins = this.getPlugins.bind(this);
|
83
|
+
this.getPluginById = this.getPluginById.bind(this);
|
84
|
+
}
|
85
|
+
|
86
|
+
getConfig = async (): Promise<GetConfigResponse> => {
|
87
|
+
return this.client.queryContractSmart(this.contractAddress, {
|
88
|
+
get_config: {}
|
89
|
+
});
|
90
|
+
};
|
91
|
+
getPlugins = async ({
|
92
|
+
limit,
|
93
|
+
startAfter
|
94
|
+
}: {
|
95
|
+
limit?: number;
|
96
|
+
startAfter?: number;
|
97
|
+
}): Promise<GetPluginsResponse> => {
|
98
|
+
return this.client.queryContractSmart(this.contractAddress, {
|
99
|
+
get_plugins: {
|
100
|
+
limit,
|
101
|
+
start_after: startAfter
|
102
|
+
}
|
103
|
+
});
|
104
|
+
};
|
105
|
+
getPluginById = async ({
|
106
|
+
id
|
107
|
+
}: {
|
108
|
+
id: number;
|
109
|
+
}): Promise<GetPluginByIdResponse> => {
|
110
|
+
return this.client.queryContractSmart(this.contractAddress, {
|
111
|
+
get_plugin_by_id: {
|
112
|
+
id
|
113
|
+
}
|
114
|
+
});
|
115
|
+
};
|
116
|
+
}"
|
117
|
+
`;
|
@@ -36,7 +36,7 @@ exports[`execute classes 1`] = `
|
|
36
36
|
}: {
|
37
37
|
currentPrices: number[][];
|
38
38
|
prevPrices: number[][];
|
39
|
-
wagerKey: Addr[][]
|
39
|
+
wagerKey: Addr[][];
|
40
40
|
}, fee: number | StdFee | \\"auto\\" = \\"auto\\", memo?: string, funds?: Coin[]): Promise<ExecuteResult> => {
|
41
41
|
return await this.client.execute(this.sender, this.contractAddress, {
|
42
42
|
set_winner: {
|
@@ -0,0 +1,55 @@
|
|
1
|
+
import contract from '../../../../../__fixtures__/issues/98/schema.json';
|
2
|
+
|
3
|
+
import {
|
4
|
+
createQueryClass,
|
5
|
+
createExecuteClass,
|
6
|
+
createExecuteInterface,
|
7
|
+
createTypeInterface
|
8
|
+
} from '../client'
|
9
|
+
import { expectCode, printCode, makeContext } from '../../../test-utils';
|
10
|
+
|
11
|
+
const message = contract.query
|
12
|
+
const ctx = makeContext(message);
|
13
|
+
|
14
|
+
it('execute_msg_for__empty', () => {
|
15
|
+
expectCode(createTypeInterface(
|
16
|
+
ctx,
|
17
|
+
message
|
18
|
+
))
|
19
|
+
})
|
20
|
+
|
21
|
+
|
22
|
+
it('query classes', () => {
|
23
|
+
expectCode(createQueryClass(
|
24
|
+
ctx,
|
25
|
+
'SG721QueryClient',
|
26
|
+
'SG721ReadOnlyInstance',
|
27
|
+
message
|
28
|
+
))
|
29
|
+
});
|
30
|
+
|
31
|
+
// it('query classes response', () => {
|
32
|
+
// expectCode(createTypeInterface(
|
33
|
+
// ctx,
|
34
|
+
// contract.responses.all_debt_shares
|
35
|
+
// ))
|
36
|
+
// });
|
37
|
+
|
38
|
+
it('execute classes array types', () => {
|
39
|
+
expectCode(createExecuteClass(
|
40
|
+
ctx,
|
41
|
+
'SG721Client',
|
42
|
+
'SG721Instance',
|
43
|
+
null,
|
44
|
+
message
|
45
|
+
))
|
46
|
+
});
|
47
|
+
|
48
|
+
it('execute interfaces no extends', () => {
|
49
|
+
expectCode(createExecuteInterface(
|
50
|
+
ctx,
|
51
|
+
'SG721Instance',
|
52
|
+
null,
|
53
|
+
message
|
54
|
+
))
|
55
|
+
});
|
package/src/context/context.ts
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
import { JSONSchema } from "../types";
|
2
|
-
import {
|
2
|
+
import { refLookup } from "../utils";
|
3
|
+
import { convertUtilsToImportList, getImportStatements, UtilMapping } from "./imports";
|
3
4
|
import deepmerge from "deepmerge";
|
4
5
|
|
5
6
|
/// Plugin Types
|
@@ -21,6 +22,9 @@ export interface TSClientOptions {
|
|
21
22
|
export interface MessageComposerOptions {
|
22
23
|
enabled?: boolean;
|
23
24
|
}
|
25
|
+
export interface MsgBuilderOptions {
|
26
|
+
enabled?: boolean;
|
27
|
+
}
|
24
28
|
export interface RecoilOptions {
|
25
29
|
enabled?: boolean;
|
26
30
|
}
|
@@ -52,19 +56,29 @@ export interface ContractInfo {
|
|
52
56
|
idlObject?: IDLObject;
|
53
57
|
};
|
54
58
|
export interface RenderOptions {
|
59
|
+
enabled?: boolean;
|
55
60
|
types?: TSTypesOptions;
|
56
61
|
recoil?: RecoilOptions;
|
57
62
|
messageComposer?: MessageComposerOptions;
|
63
|
+
msgBuilder?: MsgBuilderOptions;
|
58
64
|
client?: TSClientOptions;
|
59
65
|
reactQuery?: ReactQueryOptions;
|
60
66
|
}
|
61
67
|
|
62
|
-
|
68
|
+
|
69
|
+
export interface IContext {
|
70
|
+
refLookup($ref: string);
|
71
|
+
addUtil(util: string);
|
72
|
+
getImports(registeredUtils?: UtilMapping);
|
73
|
+
}
|
74
|
+
|
75
|
+
export interface IRenderContext<TOpt = RenderOptions> extends IContext {
|
63
76
|
contract: ContractInfo;
|
64
|
-
options:
|
77
|
+
options: TOpt;
|
65
78
|
}
|
66
79
|
|
67
80
|
export const defaultOptions: RenderOptions = {
|
81
|
+
enabled: true,
|
68
82
|
types: {
|
69
83
|
enabled: true,
|
70
84
|
aliasExecuteMsg: false
|
@@ -80,6 +94,9 @@ export const defaultOptions: RenderOptions = {
|
|
80
94
|
messageComposer: {
|
81
95
|
enabled: false
|
82
96
|
},
|
97
|
+
msgBuilder: {
|
98
|
+
enabled: false,
|
99
|
+
},
|
83
100
|
reactQuery: {
|
84
101
|
enabled: false,
|
85
102
|
optionalClient: false,
|
@@ -107,31 +124,49 @@ export const getDefinitionSchema = (schemas: JSONSchema[]): JSONSchema => {
|
|
107
124
|
|
108
125
|
return aggregateSchema;
|
109
126
|
};
|
110
|
-
|
127
|
+
|
128
|
+
/**
|
129
|
+
* context object for generating code.
|
130
|
+
* only mergeDefaultOpt needs to implementing for combine options and default options.
|
131
|
+
* @param TOpt option type
|
132
|
+
*/
|
133
|
+
export abstract class RenderContextBase<TOpt = RenderOptions> implements IRenderContext<TOpt> {
|
111
134
|
contract: ContractInfo;
|
112
135
|
utils: string[] = [];
|
113
136
|
schema: JSONSchema;
|
137
|
+
options: TOpt;
|
114
138
|
constructor(
|
115
139
|
contract: ContractInfo,
|
116
|
-
options?:
|
140
|
+
options?: TOpt
|
117
141
|
) {
|
118
142
|
this.contract = contract;
|
119
143
|
this.schema = getDefinitionSchema(contract.schemas);
|
120
|
-
this.options =
|
144
|
+
this.options = this.mergeDefaultOpt(options);
|
121
145
|
}
|
146
|
+
/**
|
147
|
+
* merge options and default options
|
148
|
+
* @param options
|
149
|
+
*/
|
150
|
+
abstract mergeDefaultOpt(options: TOpt): TOpt;
|
122
151
|
refLookup($ref: string) {
|
123
|
-
|
124
|
-
return this.schema.definitions?.[refName];
|
152
|
+
return refLookup($ref, this.schema)
|
125
153
|
}
|
126
154
|
addUtil(util: string) {
|
127
155
|
this.utils[util] = true;
|
128
156
|
}
|
129
|
-
getImports() {
|
157
|
+
getImports(registeredUtils?: UtilMapping) {
|
130
158
|
return getImportStatements(
|
131
159
|
convertUtilsToImportList(
|
132
160
|
this,
|
133
|
-
Object.keys(this.utils)
|
161
|
+
Object.keys(this.utils),
|
162
|
+
registeredUtils,
|
134
163
|
)
|
135
164
|
);
|
136
165
|
}
|
137
166
|
}
|
167
|
+
|
168
|
+
export class RenderContext extends RenderContextBase{
|
169
|
+
mergeDefaultOpt(options: RenderOptions): RenderOptions {
|
170
|
+
return deepmerge(defaultOptions, options ?? {});
|
171
|
+
}
|
172
|
+
}
|
package/src/context/imports.ts
CHANGED
@@ -2,6 +2,7 @@ import * as t from '@babel/types';
|
|
2
2
|
import { importAs, importStmt } from "../utils";
|
3
3
|
import { RenderContext } from './context';
|
4
4
|
|
5
|
+
|
5
6
|
export interface ImportObj {
|
6
7
|
type: 'import' | 'default' | 'namespace';
|
7
8
|
name: string;
|
@@ -9,6 +10,14 @@ export interface ImportObj {
|
|
9
10
|
importAs?: string;
|
10
11
|
}
|
11
12
|
|
13
|
+
export type GetUtilFn = (<TContext = RenderContext>(...args: any[]) => (context: TContext) => ImportObj);
|
14
|
+
export type UtilMapping = {
|
15
|
+
[key: string]:
|
16
|
+
| ImportObj
|
17
|
+
| string
|
18
|
+
| GetUtilFn
|
19
|
+
};
|
20
|
+
|
12
21
|
const makeReactQuerySwitch = (varName) => {
|
13
22
|
return (context: RenderContext) => {
|
14
23
|
switch (context.options.reactQuery.version) {
|
@@ -30,11 +39,11 @@ const makeReactQuerySwitch = (varName) => {
|
|
30
39
|
}
|
31
40
|
|
32
41
|
export const UTILS = {
|
42
|
+
selectorFamily: 'recoil',
|
33
43
|
MsgExecuteContract: 'cosmjs-types/cosmwasm/wasm/v1/tx',
|
34
44
|
MsgExecuteContractEncodeObject: 'cosmwasm',
|
35
45
|
Coin: '@cosmjs/amino',
|
36
46
|
toUtf8: '@cosmjs/encoding',
|
37
|
-
selectorFamily: 'recoil',
|
38
47
|
StdFee: '@cosmjs/amino',
|
39
48
|
CosmWasmClient: '@cosmjs/cosmwasm-stargate',
|
40
49
|
ExecuteResult: '@cosmjs/cosmwasm-stargate',
|
@@ -50,23 +59,48 @@ export const UTILS = {
|
|
50
59
|
|
51
60
|
export const convertUtilsToImportList = (
|
52
61
|
context: RenderContext,
|
53
|
-
utils: string[]
|
62
|
+
utils: string[],
|
63
|
+
registeredUtils?: UtilMapping
|
54
64
|
): ImportObj[] => {
|
55
|
-
return utils.map(util => {
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
return UTILS[util](context);
|
65
|
-
} else {
|
66
|
-
UTILS[util];
|
65
|
+
return utils.map((util) => {
|
66
|
+
let result = null;
|
67
|
+
|
68
|
+
if(registeredUtils){
|
69
|
+
result = convertUtil(context, util, registeredUtils);
|
70
|
+
|
71
|
+
if (result) {
|
72
|
+
return result;
|
73
|
+
}
|
67
74
|
}
|
75
|
+
|
76
|
+
result = convertUtil(context, util, UTILS);
|
77
|
+
|
78
|
+
if (result) {
|
79
|
+
return result;
|
80
|
+
}
|
81
|
+
|
82
|
+
throw new Error(`missing Util! ::[${util}]`);
|
68
83
|
});
|
69
|
-
}
|
84
|
+
};
|
85
|
+
|
86
|
+
export const convertUtil = (
|
87
|
+
context: RenderContext,
|
88
|
+
util: string,
|
89
|
+
registeredUtils: object
|
90
|
+
): ImportObj => {
|
91
|
+
if (!registeredUtils.hasOwnProperty(util)) return null;
|
92
|
+
if (typeof registeredUtils[util] === 'string') {
|
93
|
+
return {
|
94
|
+
type: 'import',
|
95
|
+
path: registeredUtils[util],
|
96
|
+
name: util
|
97
|
+
};
|
98
|
+
} else if (typeof registeredUtils[util] === 'function') {
|
99
|
+
return registeredUtils[util](context);
|
100
|
+
} else {
|
101
|
+
return registeredUtils[util];
|
102
|
+
}
|
103
|
+
};
|
70
104
|
|
71
105
|
export const getImportStatements = (list: ImportObj[]) => {
|
72
106
|
const imports = list.reduce((m, obj) => {
|
package/src/index.ts
CHANGED