wasm-ast-types 0.18.2 → 0.20.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.
Files changed (66) hide show
  1. package/main/client/client.js +16 -7
  2. package/main/client/test/ts-client.issue-101.spec.js +27 -0
  3. package/main/client/test/ts-client.issue-98.test.js +30 -0
  4. package/main/context/context.js +59 -13
  5. package/main/context/imports.js +37 -15
  6. package/main/index.js +13 -0
  7. package/main/message-composer/message-composer.js +13 -4
  8. package/main/message-composer/message-composer.spec.js +14 -4
  9. package/main/msg-builder/index.js +18 -0
  10. package/main/msg-builder/msg-builder.js +68 -0
  11. package/main/msg-builder/msg-builder.spec.js +26 -0
  12. package/main/react-query/react-query.spec.js +11 -0
  13. package/main/utils/babel.js +56 -5
  14. package/main/utils/index.js +13 -0
  15. package/main/utils/ref.js +15 -0
  16. package/main/utils/types.js +10 -2
  17. package/module/client/client.js +18 -9
  18. package/module/client/test/ts-client.issue-101.spec.js +20 -0
  19. package/module/client/test/ts-client.issue-98.test.js +23 -0
  20. package/module/context/context.js +29 -6
  21. package/module/context/imports.js +33 -14
  22. package/module/index.js +2 -1
  23. package/module/message-composer/message-composer.js +13 -4
  24. package/module/message-composer/message-composer.spec.js +16 -7
  25. package/module/msg-builder/index.js +1 -0
  26. package/module/msg-builder/msg-builder.js +47 -0
  27. package/module/msg-builder/msg-builder.spec.js +17 -0
  28. package/module/react-query/react-query.spec.js +10 -0
  29. package/module/utils/babel.js +31 -5
  30. package/module/utils/index.js +2 -1
  31. package/module/utils/ref.js +4 -0
  32. package/module/utils/types.js +10 -3
  33. package/package.json +2 -2
  34. package/src/client/client.ts +29 -24
  35. package/src/client/test/__snapshots__/ts-client.issue-101.spec.ts.snap +47 -0
  36. package/src/client/test/__snapshots__/ts-client.issue-98.test.ts.snap +117 -0
  37. package/src/client/test/__snapshots__/ts-client.vectis.spec.ts.snap +8 -8
  38. package/src/client/test/ts-client.issue-101.spec.ts +37 -0
  39. package/src/client/test/ts-client.issue-98.test.ts +55 -0
  40. package/src/context/context.ts +45 -10
  41. package/src/context/imports.ts +49 -15
  42. package/src/index.ts +2 -1
  43. package/src/message-composer/__snapshots__/message-composer.spec.ts.snap +60 -0
  44. package/src/message-composer/message-composer.spec.ts +41 -20
  45. package/src/message-composer/message-composer.ts +13 -5
  46. package/src/msg-builder/__snapshots__/msg-builder.spec.ts.snap +270 -0
  47. package/src/msg-builder/index.ts +1 -0
  48. package/src/msg-builder/msg-builder.spec.ts +25 -0
  49. package/src/msg-builder/msg-builder.ts +119 -0
  50. package/src/react-query/__snapshots__/react-query.spec.ts.snap +45 -0
  51. package/src/react-query/react-query.spec.ts +17 -1
  52. package/src/react-query/react-query.ts +1 -1
  53. package/src/utils/babel.ts +34 -5
  54. package/src/utils/index.ts +1 -0
  55. package/src/utils/ref.ts +6 -0
  56. package/src/utils/types.ts +11 -4
  57. package/types/context/context.d.ts +30 -5
  58. package/types/context/imports.d.ts +7 -2
  59. package/types/index.d.ts +1 -0
  60. package/types/msg-builder/index.d.ts +1 -0
  61. package/types/msg-builder/msg-builder.d.ts +4 -0
  62. package/types/react-query/react-query.d.ts +11 -10
  63. package/types/recoil/recoil.d.ts +1 -1
  64. package/types/utils/babel.d.ts +3 -2
  65. package/types/utils/index.d.ts +1 -0
  66. package/types/utils/ref.d.ts +2 -0
@@ -1,23 +1,19 @@
1
1
  import * as t from '@babel/types';
2
- import { camel, pascal } from 'case';
2
+ import { camel } from 'case';
3
3
  import {
4
+ arrowFunctionExpression,
4
5
  bindMethod,
5
- typedIdentifier,
6
- promiseTypeAnnotation,
7
6
  classDeclaration,
8
7
  classProperty,
9
- arrowFunctionExpression,
10
- getMessageProperties
11
- } from '../utils'
8
+ getMessageProperties,
9
+ promiseTypeAnnotation,
10
+ typedIdentifier
11
+ } from '../utils';
12
12
 
13
- import {
14
- QueryMsg,
15
- ExecuteMsg
16
- } from '../types';
13
+ import { ExecuteMsg, JSONSchema, QueryMsg } from '../types';
17
14
 
18
- import { getPropertyType, getType, createTypedObjectParams, getResponseType } from '../utils/types';
15
+ import { createTypedObjectParams, getPropertyType, getResponseType, getType } from '../utils/types';
19
16
  import { RenderContext } from '../context';
20
- import { JSONSchema } from '../types';
21
17
  import { identifier, propertySignature } from '../utils/babel';
22
18
 
23
19
  export const CONSTANT_EXEC_PARAMS = [
@@ -89,7 +85,7 @@ export const createWasmQueryMethod = (
89
85
  const methodName = camel(underscoreName);
90
86
  const responseType = getResponseType(context, underscoreName);
91
87
 
92
- const obj = createTypedObjectParams(
88
+ const param = createTypedObjectParams(
93
89
  context,
94
90
  jsonschema.properties[underscoreName]
95
91
  );
@@ -99,13 +95,14 @@ export const createWasmQueryMethod = (
99
95
  jsonschema.properties[underscoreName]
100
96
  );
101
97
 
102
- const actionArg =
103
- t.objectProperty(t.identifier(underscoreName), t.objectExpression(args));
98
+ const msgAction = t.identifier(underscoreName);
99
+ // If the param is an identifier, we can just use it as is
100
+ const msgActionValue = param?.type === 'Identifier' ? t.identifier(param.name) : t.objectExpression(args)
104
101
 
105
102
  return t.classProperty(
106
103
  t.identifier(methodName),
107
104
  arrowFunctionExpression(
108
- obj ? [obj] : [],
105
+ param ? [param] : [],
109
106
  t.blockStatement(
110
107
  [
111
108
  t.returnStatement(
@@ -120,7 +117,7 @@ export const createWasmQueryMethod = (
120
117
  [
121
118
  t.memberExpression(t.thisExpression(), t.identifier('contractAddress')),
122
119
  t.objectExpression([
123
- actionArg
120
+ t.objectProperty(msgAction, msgActionValue)
124
121
  ])
125
122
  ]
126
123
  )
@@ -237,9 +234,15 @@ export const getWasmMethodArgs = (
237
234
  // only 1 degree $ref-lookup
238
235
  if (!keys.length && jsonschema.$ref) {
239
236
  const obj = context.refLookup(jsonschema.$ref);
237
+ // properties
240
238
  if (obj) {
241
239
  keys = Object.keys(obj.properties ?? {})
242
240
  }
241
+
242
+ // tuple struct or otherwise, use the name of the reference
243
+ if (!keys.length && obj?.oneOf) {
244
+ // TODO????? ADAIR
245
+ }
243
246
  }
244
247
 
245
248
  const args = keys.map(prop => {
@@ -265,7 +268,7 @@ export const createWasmExecMethod = (
265
268
 
266
269
  const underscoreName = Object.keys(jsonschema.properties)[0];
267
270
  const methodName = camel(underscoreName);
268
- const obj = createTypedObjectParams(
271
+ const param = createTypedObjectParams(
269
272
  context,
270
273
  jsonschema.properties[underscoreName]
271
274
  );
@@ -274,12 +277,16 @@ export const createWasmExecMethod = (
274
277
  jsonschema.properties[underscoreName]
275
278
  );
276
279
 
280
+ const msgAction = t.identifier(underscoreName);
281
+ // If the param is an identifier, we can just use it as is
282
+ const msgActionValue = param?.type === 'Identifier' ? t.identifier(param.name) : t.objectExpression(args)
283
+
277
284
  return t.classProperty(
278
285
  t.identifier(methodName),
279
286
  arrowFunctionExpression(
280
- obj ? [
287
+ param ? [
281
288
  // props
282
- obj,
289
+ param,
283
290
  ...CONSTANT_EXEC_PARAMS
284
291
  ] : CONSTANT_EXEC_PARAMS,
285
292
  t.blockStatement(
@@ -306,10 +313,8 @@ export const createWasmExecMethod = (
306
313
  t.objectExpression(
307
314
  [
308
315
  t.objectProperty(
309
- t.identifier(underscoreName),
310
- t.objectExpression([
311
- ...args
312
- ])
316
+ msgAction,
317
+ msgActionValue
313
318
  )
314
319
 
315
320
  ]
@@ -0,0 +1,47 @@
1
+ // Jest Snapshot v1, https://goo.gl/fbAQLP
2
+
3
+ exports[`execute interfaces no extends 1`] = `
4
+ "export interface OwnershipInstance {
5
+ contractAddress: string;
6
+ sender: string;
7
+ setFactory: ({
8
+ newFactory
9
+ }: {
10
+ newFactory: string;
11
+ }, fee?: number | StdFee | \\"auto\\", memo?: string, funds?: Coin[]) => Promise<ExecuteResult>;
12
+ updateOwnership: (action: Action, fee?: number | StdFee | \\"auto\\", memo?: string, funds?: Coin[]) => Promise<ExecuteResult>;
13
+ }"
14
+ `;
15
+
16
+ exports[`ownership client with tuple 1`] = `
17
+ "export class OwnershipClient implements OwnershipInstance {
18
+ client: SigningCosmWasmClient;
19
+ sender: string;
20
+ contractAddress: string;
21
+
22
+ constructor(client: SigningCosmWasmClient, sender: string, contractAddress: string) {
23
+ this.client = client;
24
+ this.sender = sender;
25
+ this.contractAddress = contractAddress;
26
+ this.setFactory = this.setFactory.bind(this);
27
+ this.updateOwnership = this.updateOwnership.bind(this);
28
+ }
29
+
30
+ setFactory = async ({
31
+ newFactory
32
+ }: {
33
+ newFactory: string;
34
+ }, fee: number | StdFee | \\"auto\\" = \\"auto\\", memo?: string, funds?: Coin[]): Promise<ExecuteResult> => {
35
+ return await this.client.execute(this.sender, this.contractAddress, {
36
+ set_factory: {
37
+ new_factory: newFactory
38
+ }
39
+ }, fee, memo, funds);
40
+ };
41
+ updateOwnership = async (action: Action, fee: number | StdFee | \\"auto\\" = \\"auto\\", memo?: string, funds?: Coin[]): Promise<ExecuteResult> => {
42
+ return await this.client.execute(this.sender, this.contractAddress, {
43
+ update_ownership: action
44
+ }, fee, memo, funds);
45
+ };
46
+ }"
47
+ `;
@@ -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
+ `;
@@ -193,9 +193,9 @@ exports[`query classes 1`] = `
193
193
  this.wasm = this.wasm.bind(this);
194
194
  }
195
195
 
196
- bank = async (): Promise<BankResponse> => {
196
+ bank = async (bankMsg: BankMsg): Promise<BankResponse> => {
197
197
  return this.client.queryContractSmart(this.contractAddress, {
198
- bank: {}
198
+ bank: bankMsg
199
199
  });
200
200
  };
201
201
  custom = async (): Promise<CustomResponse> => {
@@ -203,19 +203,19 @@ exports[`query classes 1`] = `
203
203
  custom: {}
204
204
  });
205
205
  };
206
- staking = async (): Promise<StakingResponse> => {
206
+ staking = async (stakingMsg: StakingMsg): Promise<StakingResponse> => {
207
207
  return this.client.queryContractSmart(this.contractAddress, {
208
- staking: {}
208
+ staking: stakingMsg
209
209
  });
210
210
  };
211
- distribution = async (): Promise<DistributionResponse> => {
211
+ distribution = async (distributionMsg: DistributionMsg): Promise<DistributionResponse> => {
212
212
  return this.client.queryContractSmart(this.contractAddress, {
213
- distribution: {}
213
+ distribution: distributionMsg
214
214
  });
215
215
  };
216
- wasm = async (): Promise<WasmResponse> => {
216
+ wasm = async (wasmMsg: WasmMsg): Promise<WasmResponse> => {
217
217
  return this.client.queryContractSmart(this.contractAddress, {
218
- wasm: {}
218
+ wasm: wasmMsg
219
219
  });
220
220
  };
221
221
  }"
@@ -0,0 +1,37 @@
1
+ import { createExecuteClass, createExecuteInterface } from '../client';
2
+ import { expectCode, makeContext } from '../../../test-utils';
3
+ import ownership from '../../../../../__fixtures__/basic/ownership.json';
4
+
5
+
6
+ // it('query classes', () => {
7
+ // const ctx = makeContext(cosmos_msg_for__empty);
8
+ // expectCode(createQueryClass(
9
+ // ctx,
10
+ // 'SG721QueryClient',
11
+ // 'SG721ReadOnlyInstance',
12
+ // cosmos_msg_for__empty
13
+ // ))
14
+ // });
15
+
16
+ it('execute interfaces no extends', () => {
17
+ const ctx = makeContext(ownership);
18
+ expectCode(createExecuteInterface(
19
+ ctx,
20
+ 'OwnershipInstance',
21
+ null,
22
+ ownership
23
+ ))
24
+ });
25
+
26
+ it('ownership client with tuple', () => {
27
+ const ctx = makeContext(ownership);
28
+ expectCode(createExecuteClass(
29
+ ctx,
30
+ 'OwnershipClient',
31
+ 'OwnershipInstance',
32
+ null,
33
+ ownership
34
+ ))
35
+ });
36
+
37
+
@@ -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
+ });
@@ -1,5 +1,6 @@
1
1
  import { JSONSchema } from "../types";
2
- import { convertUtilsToImportList, getImportStatements } from "./imports";
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
- export interface RenderContext {
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: RenderOptions;
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
- export class RenderContext implements RenderContext {
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?: RenderOptions
140
+ options?: TOpt
117
141
  ) {
118
142
  this.contract = contract;
119
143
  this.schema = getDefinitionSchema(contract.schemas);
120
- this.options = deepmerge(defaultOptions, 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
- const refName = $ref.replace('#/definitions/', '')
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
+ }
@@ -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
- 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];
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
@@ -4,4 +4,5 @@ export * from './context';
4
4
  export * from './recoil';
5
5
  export * from './message-composer';
6
6
  export * from './react-query';
7
- export * from './types';
7
+ export * from './types';
8
+ export * from './msg-builder';