wasm-ast-types 0.11.3 → 0.12.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/main/client/test/ts-client.issue-71.test.js +103 -0
- package/main/utils/types.js +20 -7
- package/module/client/test/ts-client.issue-71.test.js +21 -0
- package/module/utils/types.js +20 -7
- package/package.json +5 -3
- package/src/client/client.ts +665 -0
- package/src/client/index.ts +1 -0
- package/src/client/test/__snapshots__/ts-client.account-nfts.spec.ts.snap +497 -0
- package/src/client/test/__snapshots__/ts-client.arrays-ref.spec.ts.snap +452 -0
- package/src/client/test/__snapshots__/ts-client.arrays.spec.ts.snap +101 -0
- package/src/client/test/__snapshots__/ts-client.cw-named-groups.test.ts.snap +141 -0
- package/src/client/test/__snapshots__/ts-client.cw-proposal-single.test.ts.snap +341 -0
- package/src/client/test/__snapshots__/ts-client.empty-enums.spec.ts.snap +20 -0
- package/src/client/test/__snapshots__/ts-client.issue-71.test.ts.snap +432 -0
- package/src/client/test/__snapshots__/ts-client.issues.test.ts.snap +984 -0
- package/src/client/test/__snapshots__/ts-client.sg721.spec.ts.snap +350 -0
- package/src/client/test/__snapshots__/ts-client.spec.ts.snap +723 -0
- package/src/client/test/__snapshots__/ts-client.vectis.spec.ts.snap +337 -0
- package/src/client/test/ts-client.account-nfts.spec.ts +55 -0
- package/src/client/test/ts-client.arrays-ref.spec.ts +48 -0
- package/src/client/test/ts-client.arrays.spec.ts +58 -0
- package/src/client/test/ts-client.cw-named-groups.test.ts +48 -0
- package/src/client/test/ts-client.cw-proposal-single.test.ts +50 -0
- package/src/client/test/ts-client.empty-enums.spec.ts +28 -0
- package/src/client/test/ts-client.issue-71.test.ts +51 -0
- package/src/client/test/ts-client.issues.test.ts +52 -0
- package/src/client/test/ts-client.sg721.spec.ts +46 -0
- package/src/client/test/ts-client.spec.ts +166 -0
- package/src/client/test/ts-client.vectis.spec.ts +97 -0
- package/src/context/context.ts +132 -0
- package/src/context/imports.ts +126 -0
- package/src/context/index.ts +2 -0
- package/src/index.ts +7 -0
- package/src/message-composer/__snapshots__/message-composer.spec.ts.snap +271 -0
- package/src/message-composer/index.ts +1 -0
- package/src/message-composer/message-composer.spec.ts +25 -0
- package/src/message-composer/message-composer.ts +305 -0
- package/src/react-query/__snapshots__/react-query.spec.ts.snap +913 -0
- package/src/react-query/index.ts +1 -0
- package/src/react-query/react-query.spec.ts +75 -0
- package/src/react-query/react-query.ts +913 -0
- package/src/recoil/__snapshots__/recoil.spec.ts.snap +203 -0
- package/src/recoil/index.ts +1 -0
- package/src/recoil/recoil.spec.ts +38 -0
- package/src/recoil/recoil.ts +307 -0
- package/src/types.ts +44 -0
- package/src/utils/__snapshots__/babel.spec.ts.snap +75 -0
- package/src/utils/babel.spec.ts +511 -0
- package/src/utils/babel.ts +315 -0
- package/src/utils/index.ts +2 -0
- package/src/utils/types.ts +459 -0
@@ -0,0 +1,52 @@
|
|
1
|
+
import { globContracts, makeContext } from '../../../test-utils'
|
2
|
+
import {
|
3
|
+
createQueryClass,
|
4
|
+
createExecuteClass,
|
5
|
+
createExecuteInterface,
|
6
|
+
createTypeInterface
|
7
|
+
} from '../client'
|
8
|
+
import { expectCode } from '../../../test-utils';
|
9
|
+
import cases from 'jest-in-case';
|
10
|
+
|
11
|
+
const contracts = globContracts('issues/55');
|
12
|
+
|
13
|
+
|
14
|
+
cases('execute_msg_for__empty', async opts => {
|
15
|
+
const ctx = makeContext(opts.content);
|
16
|
+
expectCode(createTypeInterface(
|
17
|
+
ctx,
|
18
|
+
opts.content
|
19
|
+
));
|
20
|
+
}, contracts);
|
21
|
+
|
22
|
+
cases('query classes', async opts => {
|
23
|
+
const ctx = makeContext(opts.content);
|
24
|
+
expectCode(createQueryClass(
|
25
|
+
ctx,
|
26
|
+
'SG721QueryClient',
|
27
|
+
'SG721ReadOnlyInstance',
|
28
|
+
opts.content
|
29
|
+
))
|
30
|
+
}, contracts);
|
31
|
+
|
32
|
+
cases('execute class', async opts => {
|
33
|
+
const ctx = makeContext(opts.content);
|
34
|
+
expectCode(createExecuteClass(
|
35
|
+
ctx,
|
36
|
+
'SG721Client',
|
37
|
+
'SG721Instance',
|
38
|
+
null,
|
39
|
+
opts.content
|
40
|
+
))
|
41
|
+
}, contracts);
|
42
|
+
|
43
|
+
cases('execute interface', async opts => {
|
44
|
+
const ctx = makeContext(opts.content);
|
45
|
+
expectCode(createExecuteInterface(
|
46
|
+
ctx,
|
47
|
+
'SG721Instance',
|
48
|
+
null,
|
49
|
+
opts.content
|
50
|
+
))
|
51
|
+
}, contracts);
|
52
|
+
|
@@ -0,0 +1,46 @@
|
|
1
|
+
import execute_msg_for__empty from '../../../../../__fixtures__/sg721/execute_msg_for__empty.json';
|
2
|
+
import {
|
3
|
+
createQueryClass,
|
4
|
+
createExecuteClass,
|
5
|
+
createExecuteInterface,
|
6
|
+
createTypeInterface
|
7
|
+
} from '../client'
|
8
|
+
import { expectCode, makeContext } from '../../../test-utils';
|
9
|
+
|
10
|
+
const ctx = makeContext(execute_msg_for__empty);
|
11
|
+
|
12
|
+
it('execute_msg_for__empty', () => {
|
13
|
+
expectCode(createTypeInterface(
|
14
|
+
ctx,
|
15
|
+
execute_msg_for__empty
|
16
|
+
))
|
17
|
+
})
|
18
|
+
|
19
|
+
|
20
|
+
it('query classes', () => {
|
21
|
+
expectCode(createQueryClass(
|
22
|
+
ctx,
|
23
|
+
'SG721QueryClient',
|
24
|
+
'SG721ReadOnlyInstance',
|
25
|
+
execute_msg_for__empty
|
26
|
+
))
|
27
|
+
});
|
28
|
+
|
29
|
+
it('execute classes array types', () => {
|
30
|
+
expectCode(createExecuteClass(
|
31
|
+
ctx,
|
32
|
+
'SG721Client',
|
33
|
+
'SG721Instance',
|
34
|
+
null,
|
35
|
+
execute_msg_for__empty
|
36
|
+
))
|
37
|
+
});
|
38
|
+
|
39
|
+
it('execute interfaces no extends', () => {
|
40
|
+
expectCode(createExecuteInterface(
|
41
|
+
ctx,
|
42
|
+
'SG721Instance',
|
43
|
+
null,
|
44
|
+
execute_msg_for__empty
|
45
|
+
))
|
46
|
+
});
|
@@ -0,0 +1,166 @@
|
|
1
|
+
import execute_msg_named_groups from '../../../../../__fixtures__/daodao/cw-named-groups/execute_msg.json';
|
2
|
+
|
3
|
+
import query_msg from '../../../../../__fixtures__/basic/query_msg.json';
|
4
|
+
import execute_msg from '../../../../../__fixtures__/basic/execute_msg_for__empty.json';
|
5
|
+
import approval_response from '../../../../../__fixtures__/basic/approval_response.json';
|
6
|
+
import all_nft_info_response from '../../../../../__fixtures__/basic/all_nft_info_response.json';
|
7
|
+
import approvals_response from '../../../../../__fixtures__/basic/approvals_response.json';
|
8
|
+
import collection_info_response from '../../../../../__fixtures__/basic/collection_info_response.json';
|
9
|
+
import contract_info_response from '../../../../../__fixtures__/basic/contract_info_response.json';
|
10
|
+
import instantiate_msg from '../../../../../__fixtures__/basic/instantiate_msg.json';
|
11
|
+
import nft_info_response from '../../../../../__fixtures__/basic/nft_info_response.json';
|
12
|
+
import num_tokens_response from '../../../../../__fixtures__/basic/num_tokens_response.json';
|
13
|
+
import operators_response from '../../../../../__fixtures__/basic/operators_response.json';
|
14
|
+
import owner_of_response from '../../../../../__fixtures__/basic/owner_of_response.json';
|
15
|
+
import tokens_response from '../../../../../__fixtures__/basic/tokens_response.json';
|
16
|
+
|
17
|
+
import {
|
18
|
+
createQueryClass,
|
19
|
+
createQueryInterface,
|
20
|
+
createExecuteClass,
|
21
|
+
createExecuteInterface,
|
22
|
+
createTypeInterface
|
23
|
+
} from '../client';
|
24
|
+
|
25
|
+
import { expectCode, makeContext } from '../../../test-utils';
|
26
|
+
|
27
|
+
it('approval_response', () => {
|
28
|
+
const ctx = makeContext(approval_response);
|
29
|
+
expectCode(createTypeInterface(
|
30
|
+
ctx,
|
31
|
+
approval_response
|
32
|
+
))
|
33
|
+
});
|
34
|
+
|
35
|
+
it('all_nft_info_response', () => {
|
36
|
+
const ctx = makeContext(all_nft_info_response);
|
37
|
+
expectCode(createTypeInterface(
|
38
|
+
ctx,
|
39
|
+
all_nft_info_response
|
40
|
+
))
|
41
|
+
})
|
42
|
+
it('approvals_response', () => {
|
43
|
+
const ctx = makeContext(approvals_response);
|
44
|
+
expectCode(createTypeInterface(
|
45
|
+
ctx,
|
46
|
+
approvals_response
|
47
|
+
))
|
48
|
+
})
|
49
|
+
it('collection_info_response', () => {
|
50
|
+
const ctx = makeContext(collection_info_response);
|
51
|
+
expectCode(createTypeInterface(
|
52
|
+
ctx,
|
53
|
+
collection_info_response
|
54
|
+
))
|
55
|
+
})
|
56
|
+
it('contract_info_response', () => {
|
57
|
+
const ctx = makeContext(contract_info_response);
|
58
|
+
expectCode(createTypeInterface(
|
59
|
+
ctx,
|
60
|
+
contract_info_response
|
61
|
+
))
|
62
|
+
})
|
63
|
+
it('instantiate_msg', () => {
|
64
|
+
const ctx = makeContext(instantiate_msg);
|
65
|
+
expectCode(createTypeInterface(
|
66
|
+
ctx,
|
67
|
+
instantiate_msg
|
68
|
+
))
|
69
|
+
})
|
70
|
+
it('nft_info_response', () => {
|
71
|
+
const ctx = makeContext(nft_info_response);
|
72
|
+
expectCode(createTypeInterface(
|
73
|
+
ctx,
|
74
|
+
nft_info_response
|
75
|
+
))
|
76
|
+
})
|
77
|
+
it('num_tokens_response', () => {
|
78
|
+
const ctx = makeContext(num_tokens_response);
|
79
|
+
expectCode(createTypeInterface(
|
80
|
+
ctx,
|
81
|
+
num_tokens_response
|
82
|
+
))
|
83
|
+
})
|
84
|
+
it('operators_response', () => {
|
85
|
+
const ctx = makeContext(operators_response);
|
86
|
+
expectCode(createTypeInterface(
|
87
|
+
ctx,
|
88
|
+
operators_response
|
89
|
+
))
|
90
|
+
})
|
91
|
+
it('owner_of_response', () => {
|
92
|
+
const ctx = makeContext(owner_of_response);
|
93
|
+
expectCode(createTypeInterface(
|
94
|
+
ctx,
|
95
|
+
owner_of_response
|
96
|
+
))
|
97
|
+
})
|
98
|
+
it('tokens_response', () => {
|
99
|
+
const ctx = makeContext(tokens_response);
|
100
|
+
expectCode(createTypeInterface(
|
101
|
+
ctx,
|
102
|
+
tokens_response
|
103
|
+
))
|
104
|
+
})
|
105
|
+
|
106
|
+
it('query classes', () => {
|
107
|
+
const ctx = makeContext(query_msg);
|
108
|
+
expectCode(createQueryClass(
|
109
|
+
ctx,
|
110
|
+
'SG721QueryClient',
|
111
|
+
'SG721ReadOnlyInstance',
|
112
|
+
query_msg
|
113
|
+
))
|
114
|
+
});
|
115
|
+
|
116
|
+
it('execute classes', () => {
|
117
|
+
const ctx = makeContext(execute_msg);
|
118
|
+
expectCode(createExecuteClass(
|
119
|
+
ctx,
|
120
|
+
'SG721Client',
|
121
|
+
'SG721Instance',
|
122
|
+
'SG721QueryClient',
|
123
|
+
execute_msg
|
124
|
+
))
|
125
|
+
});
|
126
|
+
|
127
|
+
it('execute classes no extends', () => {
|
128
|
+
const ctx = makeContext(execute_msg);
|
129
|
+
expectCode(createExecuteClass(
|
130
|
+
ctx,
|
131
|
+
'SG721Client',
|
132
|
+
'SG721Instance',
|
133
|
+
null,
|
134
|
+
execute_msg
|
135
|
+
))
|
136
|
+
});
|
137
|
+
|
138
|
+
it('execute classes array types', () => {
|
139
|
+
const ctx = makeContext(execute_msg_named_groups);
|
140
|
+
expectCode(createExecuteClass(
|
141
|
+
ctx,
|
142
|
+
'SG721Client',
|
143
|
+
'SG721Instance',
|
144
|
+
null,
|
145
|
+
execute_msg_named_groups
|
146
|
+
))
|
147
|
+
});
|
148
|
+
|
149
|
+
it('execute interfaces no extends', () => {
|
150
|
+
const ctx = makeContext(execute_msg);
|
151
|
+
expectCode(createExecuteInterface(
|
152
|
+
ctx,
|
153
|
+
'SG721Instance',
|
154
|
+
null,
|
155
|
+
execute_msg
|
156
|
+
))
|
157
|
+
});
|
158
|
+
|
159
|
+
it('query interfaces', () => {
|
160
|
+
const ctx = makeContext(query_msg);
|
161
|
+
expectCode(createQueryInterface(
|
162
|
+
ctx,
|
163
|
+
'SG721ReadOnlyInstance',
|
164
|
+
query_msg
|
165
|
+
))
|
166
|
+
});
|
@@ -0,0 +1,97 @@
|
|
1
|
+
import cosmos_msg_for__empty from '../../../../../__fixtures__/vectis/govec/cosmos_msg_for__empty.json';
|
2
|
+
import execute_msg_for__empty from '../../../../../__fixtures__/vectis/govec/execute_msg_for__empty.json';
|
3
|
+
import can_execute_relay_response from '../../../../../__fixtures__/vectis/govec/can_execute_relay_response.json';
|
4
|
+
import info_response from '../../../../../__fixtures__/vectis/govec/info_response.json';
|
5
|
+
import relay_transaction from '../../../../../__fixtures__/vectis/govec/relay_transaction.json';
|
6
|
+
|
7
|
+
import {
|
8
|
+
createQueryClass,
|
9
|
+
createExecuteClass,
|
10
|
+
createExecuteInterface,
|
11
|
+
createTypeInterface
|
12
|
+
} from '../client';
|
13
|
+
|
14
|
+
import { RenderContext } from '../../context';
|
15
|
+
import { expectCode, makeContext } from '../../../test-utils';
|
16
|
+
|
17
|
+
it('cosmos_msg_for__empty', () => {
|
18
|
+
const ctx = makeContext(cosmos_msg_for__empty);
|
19
|
+
expectCode(createTypeInterface(
|
20
|
+
ctx,
|
21
|
+
cosmos_msg_for__empty
|
22
|
+
))
|
23
|
+
});
|
24
|
+
|
25
|
+
it('execute_msg_for__empty', () => {
|
26
|
+
const ctx = makeContext(execute_msg_for__empty);
|
27
|
+
expectCode(createTypeInterface(
|
28
|
+
ctx,
|
29
|
+
execute_msg_for__empty
|
30
|
+
))
|
31
|
+
})
|
32
|
+
|
33
|
+
it('can_execute_relay_response', () => {
|
34
|
+
const ctx = makeContext(can_execute_relay_response);
|
35
|
+
expectCode(createTypeInterface(
|
36
|
+
ctx,
|
37
|
+
can_execute_relay_response
|
38
|
+
))
|
39
|
+
})
|
40
|
+
|
41
|
+
it('info_response', () => {
|
42
|
+
const ctx = makeContext(info_response);
|
43
|
+
expectCode(createTypeInterface(
|
44
|
+
ctx,
|
45
|
+
info_response
|
46
|
+
))
|
47
|
+
})
|
48
|
+
|
49
|
+
it('relay_transaction', () => {
|
50
|
+
const ctx = makeContext(relay_transaction);
|
51
|
+
expectCode(createTypeInterface(
|
52
|
+
ctx,
|
53
|
+
relay_transaction
|
54
|
+
))
|
55
|
+
})
|
56
|
+
|
57
|
+
|
58
|
+
it('query classes', () => {
|
59
|
+
const ctx = makeContext(cosmos_msg_for__empty);
|
60
|
+
expectCode(createQueryClass(
|
61
|
+
ctx,
|
62
|
+
'SG721QueryClient',
|
63
|
+
'SG721ReadOnlyInstance',
|
64
|
+
cosmos_msg_for__empty
|
65
|
+
))
|
66
|
+
});
|
67
|
+
|
68
|
+
it('query classes', () => {
|
69
|
+
const ctx = makeContext(execute_msg_for__empty);
|
70
|
+
expectCode(createQueryClass(
|
71
|
+
ctx,
|
72
|
+
'SG721QueryClient',
|
73
|
+
'SG721ReadOnlyInstance',
|
74
|
+
execute_msg_for__empty
|
75
|
+
))
|
76
|
+
});
|
77
|
+
|
78
|
+
it('execute classes array types', () => {
|
79
|
+
const ctx = makeContext(execute_msg_for__empty);
|
80
|
+
expectCode(createExecuteClass(
|
81
|
+
ctx,
|
82
|
+
'SG721Client',
|
83
|
+
'SG721Instance',
|
84
|
+
null,
|
85
|
+
execute_msg_for__empty
|
86
|
+
))
|
87
|
+
});
|
88
|
+
|
89
|
+
it('execute interfaces no extends', () => {
|
90
|
+
const ctx = makeContext(execute_msg_for__empty);
|
91
|
+
expectCode(createExecuteInterface(
|
92
|
+
ctx,
|
93
|
+
'SG721Instance',
|
94
|
+
null,
|
95
|
+
execute_msg_for__empty
|
96
|
+
))
|
97
|
+
});
|
@@ -0,0 +1,132 @@
|
|
1
|
+
import { JSONSchema } from "../types";
|
2
|
+
import { convertUtilsToImportList, getImportStatements } from "./imports";
|
3
|
+
import deepmerge from "deepmerge";
|
4
|
+
|
5
|
+
/// Plugin Types
|
6
|
+
export interface ReactQueryOptions {
|
7
|
+
enabled?: boolean;
|
8
|
+
optionalClient?: boolean;
|
9
|
+
version?: 'v3' | 'v4';
|
10
|
+
mutations?: boolean;
|
11
|
+
camelize?: boolean;
|
12
|
+
queryKeys?: boolean
|
13
|
+
}
|
14
|
+
|
15
|
+
export interface TSClientOptions {
|
16
|
+
enabled?: boolean;
|
17
|
+
}
|
18
|
+
export interface MessageComposerOptions {
|
19
|
+
enabled?: boolean;
|
20
|
+
}
|
21
|
+
export interface RecoilOptions {
|
22
|
+
enabled?: boolean;
|
23
|
+
}
|
24
|
+
export interface TSTypesOptions {
|
25
|
+
enabled?: boolean;
|
26
|
+
aliasExecuteMsg?: boolean;
|
27
|
+
}
|
28
|
+
|
29
|
+
/// END Plugin Types
|
30
|
+
|
31
|
+
interface KeyedSchema {
|
32
|
+
[key: string]: JSONSchema;
|
33
|
+
}
|
34
|
+
export interface IDLObject {
|
35
|
+
contract_name: string;
|
36
|
+
contract_version: string;
|
37
|
+
idl_version: string;
|
38
|
+
instantiate: JSONSchema;
|
39
|
+
execute: JSONSchema;
|
40
|
+
query: JSONSchema;
|
41
|
+
migrate: JSONSchema;
|
42
|
+
sudo: JSONSchema;
|
43
|
+
responses: KeyedSchema;
|
44
|
+
}
|
45
|
+
|
46
|
+
export interface ContractInfo {
|
47
|
+
schemas: JSONSchema[];
|
48
|
+
responses?: Record<string, JSONSchema>;
|
49
|
+
idlObject?: IDLObject;
|
50
|
+
};
|
51
|
+
export interface RenderOptions {
|
52
|
+
types?: TSTypesOptions;
|
53
|
+
recoil?: RecoilOptions;
|
54
|
+
messageComposer?: MessageComposerOptions;
|
55
|
+
client?: TSClientOptions;
|
56
|
+
reactQuery?: ReactQueryOptions;
|
57
|
+
}
|
58
|
+
|
59
|
+
export interface RenderContext {
|
60
|
+
contract: ContractInfo;
|
61
|
+
options: RenderOptions;
|
62
|
+
}
|
63
|
+
|
64
|
+
export const defaultOptions: RenderOptions = {
|
65
|
+
types: {
|
66
|
+
enabled: true,
|
67
|
+
aliasExecuteMsg: false
|
68
|
+
},
|
69
|
+
client: {
|
70
|
+
enabled: true
|
71
|
+
},
|
72
|
+
recoil: {
|
73
|
+
enabled: false
|
74
|
+
},
|
75
|
+
messageComposer: {
|
76
|
+
enabled: false
|
77
|
+
},
|
78
|
+
reactQuery: {
|
79
|
+
enabled: false,
|
80
|
+
optionalClient: false,
|
81
|
+
version: 'v3',
|
82
|
+
mutations: false,
|
83
|
+
camelize: true,
|
84
|
+
queryKeys: false
|
85
|
+
}
|
86
|
+
};
|
87
|
+
|
88
|
+
export const getDefinitionSchema = (schemas: JSONSchema[]): JSONSchema => {
|
89
|
+
const aggregateSchema = {
|
90
|
+
definitions: {
|
91
|
+
//
|
92
|
+
}
|
93
|
+
};
|
94
|
+
|
95
|
+
schemas.forEach(schema => {
|
96
|
+
schema.definitions = schema.definitions || {};
|
97
|
+
aggregateSchema.definitions = {
|
98
|
+
...aggregateSchema.definitions,
|
99
|
+
...schema.definitions
|
100
|
+
};
|
101
|
+
});
|
102
|
+
|
103
|
+
return aggregateSchema;
|
104
|
+
};
|
105
|
+
export class RenderContext implements RenderContext {
|
106
|
+
contract: ContractInfo;
|
107
|
+
utils: string[] = [];
|
108
|
+
schema: JSONSchema;
|
109
|
+
constructor(
|
110
|
+
contract: ContractInfo,
|
111
|
+
options?: RenderOptions
|
112
|
+
) {
|
113
|
+
this.contract = contract;
|
114
|
+
this.schema = getDefinitionSchema(contract.schemas);
|
115
|
+
this.options = deepmerge(defaultOptions, options ?? {});
|
116
|
+
}
|
117
|
+
refLookup($ref: string) {
|
118
|
+
const refName = $ref.replace('#/definitions/', '')
|
119
|
+
return this.schema.definitions?.[refName];
|
120
|
+
}
|
121
|
+
addUtil(util: string) {
|
122
|
+
this.utils[util] = true;
|
123
|
+
}
|
124
|
+
getImports() {
|
125
|
+
return getImportStatements(
|
126
|
+
convertUtilsToImportList(
|
127
|
+
this,
|
128
|
+
Object.keys(this.utils)
|
129
|
+
)
|
130
|
+
);
|
131
|
+
}
|
132
|
+
}
|
@@ -0,0 +1,126 @@
|
|
1
|
+
import * as t from '@babel/types';
|
2
|
+
import { importAs, importStmt } from "../utils";
|
3
|
+
import { RenderContext } from './context';
|
4
|
+
|
5
|
+
export interface ImportObj {
|
6
|
+
type: 'import' | 'default' | 'namespace';
|
7
|
+
name: string;
|
8
|
+
path: string;
|
9
|
+
importAs?: string;
|
10
|
+
}
|
11
|
+
|
12
|
+
const makeReactQuerySwitch = (varName) => {
|
13
|
+
return (context: RenderContext) => {
|
14
|
+
switch (context.options.reactQuery.version) {
|
15
|
+
case 'v4':
|
16
|
+
return {
|
17
|
+
type: 'import',
|
18
|
+
path: '@tanstack/react-query',
|
19
|
+
name: varName
|
20
|
+
}
|
21
|
+
case 'v3':
|
22
|
+
default:
|
23
|
+
return {
|
24
|
+
type: 'import',
|
25
|
+
path: 'react-query',
|
26
|
+
name: varName
|
27
|
+
}
|
28
|
+
}
|
29
|
+
};
|
30
|
+
}
|
31
|
+
|
32
|
+
export const UTILS = {
|
33
|
+
MsgExecuteContract: 'cosmjs-types/cosmwasm/wasm/v1/tx',
|
34
|
+
MsgExecuteContractEncodeObject: 'cosmwasm',
|
35
|
+
Coin: '@cosmjs/amino',
|
36
|
+
toUtf8: '@cosmjs/encoding',
|
37
|
+
selectorFamily: 'recoil',
|
38
|
+
StdFee: '@cosmjs/amino',
|
39
|
+
CosmWasmClient: '@cosmjs/cosmwasm-stargate',
|
40
|
+
ExecuteResult: '@cosmjs/cosmwasm-stargate',
|
41
|
+
SigningCosmWasmClient: '@cosmjs/cosmwasm-stargate',
|
42
|
+
|
43
|
+
// react-query
|
44
|
+
useQuery: makeReactQuerySwitch('useQuery'),
|
45
|
+
UseQueryOptions: makeReactQuerySwitch('UseQueryOptions'),
|
46
|
+
useMutation: makeReactQuerySwitch('useMutation'),
|
47
|
+
UseMutationOptions: makeReactQuerySwitch('UseMutationOptions')
|
48
|
+
|
49
|
+
};
|
50
|
+
|
51
|
+
export const convertUtilsToImportList = (
|
52
|
+
context: RenderContext,
|
53
|
+
utils: string[]
|
54
|
+
): ImportObj[] => {
|
55
|
+
return utils.map(util => {
|
56
|
+
if (!UTILS.hasOwnProperty(util)) throw new Error(`missing Util! ::[${util}]`);
|
57
|
+
if (typeof UTILS[util] === 'string') {
|
58
|
+
return {
|
59
|
+
type: 'import',
|
60
|
+
path: UTILS[util],
|
61
|
+
name: util
|
62
|
+
};
|
63
|
+
} else if (typeof UTILS[util] === 'function') {
|
64
|
+
return UTILS[util](context);
|
65
|
+
} else {
|
66
|
+
UTILS[util];
|
67
|
+
}
|
68
|
+
});
|
69
|
+
}
|
70
|
+
|
71
|
+
export const getImportStatements = (list: ImportObj[]) => {
|
72
|
+
const imports = list.reduce((m, obj) => {
|
73
|
+
m[obj.path] = m[obj.path] || [];
|
74
|
+
const exists = m[obj.path].find(el => el.type === obj.type && el.path === obj.path && el.name === obj.name);
|
75
|
+
|
76
|
+
// TODO some have google.protobuf.Any shows up... figure out the better way to handle this
|
77
|
+
if (/\./.test(obj.name)) {
|
78
|
+
obj.name = obj.name.split('.')[obj.name.split('.').length - 1];
|
79
|
+
}
|
80
|
+
|
81
|
+
if (!exists) m[obj.path].push(obj);
|
82
|
+
return m;
|
83
|
+
}, {})
|
84
|
+
|
85
|
+
return Object.entries(imports)
|
86
|
+
.reduce((m, [importPath, imports]: [string, ImportObj[]]) => {
|
87
|
+
const defaultImports = imports.filter(a => a.type === 'default');
|
88
|
+
if (defaultImports.length) {
|
89
|
+
if (defaultImports.length > 1) throw new Error('more than one default name NOT allowed.')
|
90
|
+
m.push(
|
91
|
+
t.importDeclaration(
|
92
|
+
[
|
93
|
+
t.importDefaultSpecifier(
|
94
|
+
t.identifier(defaultImports[0].name)
|
95
|
+
)
|
96
|
+
],
|
97
|
+
t.stringLiteral(defaultImports[0].path)
|
98
|
+
)
|
99
|
+
)
|
100
|
+
}
|
101
|
+
const namedImports = imports.filter(a => a.type === 'import' && (!a.importAs || (a.name === a.importAs)));
|
102
|
+
if (namedImports.length) {
|
103
|
+
m.push(importStmt(namedImports.map(i => i.name), namedImports[0].path));
|
104
|
+
}
|
105
|
+
const aliasNamedImports = imports.filter(a => a.type === 'import' && (a.importAs && (a.name !== a.importAs)));
|
106
|
+
aliasNamedImports.forEach(imp => {
|
107
|
+
m.push(importAs(imp.name, imp.importAs, imp.path));
|
108
|
+
});
|
109
|
+
|
110
|
+
const namespaced = imports.filter(a => a.type === 'namespace');
|
111
|
+
if (namespaced.length) {
|
112
|
+
if (namespaced.length > 1) throw new Error('more than one namespaced name NOT allowed.')
|
113
|
+
m.push(
|
114
|
+
t.importDeclaration(
|
115
|
+
[
|
116
|
+
t.importNamespaceSpecifier(
|
117
|
+
t.identifier(namespaced[0].name)
|
118
|
+
)
|
119
|
+
],
|
120
|
+
t.stringLiteral(namespaced[0].path)
|
121
|
+
)
|
122
|
+
)
|
123
|
+
}
|
124
|
+
return m;
|
125
|
+
}, [])
|
126
|
+
};
|