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
@@ -2,6 +2,7 @@ import * as t from '@babel/types';
2
2
  import { snake } from "case";
3
3
  import { Field, QueryMsg, ExecuteMsg } from '../types';
4
4
  import { TSTypeAnnotation, TSExpressionWithTypeArguments } from '@babel/types';
5
+ import { refLookup } from './ref';
5
6
 
6
7
  // t.TSPropertySignature - kind?
7
8
  export const propertySignature = (
@@ -30,11 +31,28 @@ export const tsTypeOperator = (typeAnnotation: t.TSType, operator: string) => {
30
31
  return obj;
31
32
  };
32
33
 
33
- export const getMessageProperties = (msg: QueryMsg | ExecuteMsg) => {
34
- if (msg.anyOf) return msg.anyOf;
35
- if (msg.oneOf) return msg.oneOf;
36
- if (msg.allOf) return msg.allOf;
37
- return [];
34
+ export const getMessageProperties = (msg) => {
35
+ let results = [];
36
+ let objs = [];
37
+ if (msg.anyOf) { objs = msg.anyOf; }
38
+ else if (msg.oneOf) { objs = msg.oneOf; }
39
+ else if (msg.allOf) { objs = msg.allOf; }
40
+
41
+ for (const obj of objs) {
42
+ if(obj.properties){
43
+ results.push(obj);
44
+ } else{
45
+ if(obj.$ref){
46
+ const ref = refLookup(obj.$ref, msg)
47
+
48
+ const refProps = getMessageProperties(ref);
49
+
50
+ results = [...results, ...refProps];
51
+ }
52
+ }
53
+ }
54
+
55
+ return results;
38
56
  }
39
57
 
40
58
  export const tsPropertySignature = (
@@ -112,6 +130,17 @@ export const promiseTypeAnnotation = (name) => {
112
130
  );
113
131
  }
114
132
 
133
+ export const abstractClassDeclaration = (name: string, body: any[], implementsExressions: TSExpressionWithTypeArguments[] = [], superClass: t.Identifier = null) => {
134
+ const declaration = classDeclaration(
135
+ name,
136
+ body,
137
+ implementsExressions,
138
+ superClass
139
+ );
140
+ declaration.abstract = true
141
+ return declaration;
142
+ };
143
+
115
144
  export const classDeclaration = (name: string, body: any[], implementsExressions: TSExpressionWithTypeArguments[] = [], superClass: t.Identifier = null) => {
116
145
  const declaration = t.classDeclaration(
117
146
  t.identifier(name),
@@ -1,2 +1,3 @@
1
1
  export * from './babel';
2
2
  export * from './types';
3
+ export * from './ref';
@@ -0,0 +1,6 @@
1
+ import { JSONSchema } from "../types";
2
+
3
+ export const refLookup = ($ref: string, schema: JSONSchema) => {
4
+ const refName = $ref.replace('#/definitions/', '')
5
+ return schema.definitions?.[refName];
6
+ }
@@ -1,5 +1,5 @@
1
1
  import * as t from '@babel/types';
2
- import { camel, pascal } from 'case';
2
+ import { camel, pascal, snake } from 'case';
3
3
  import { propertySignature } from './babel';
4
4
  import { TSTypeAnnotation } from '@babel/types';
5
5
  import { RenderContext } from '../context';
@@ -425,15 +425,22 @@ export const createTypedObjectParams = (
425
425
  context: RenderContext,
426
426
  jsonschema: JSONSchema,
427
427
  camelize: boolean = true
428
- ): t.ObjectPattern => {
428
+ ): (t.Identifier | t.Pattern | t.RestElement) => {
429
429
 
430
430
  const keys = Object.keys(jsonschema.properties ?? {});
431
431
  if (!keys.length) {
432
-
433
432
  // is there a ref?
434
433
  if (jsonschema.$ref) {
435
434
  const obj = context.refLookup(jsonschema.$ref);
436
- if (obj) {
435
+ // If there is a oneOf, then we need to create a type for it
436
+ if (obj?.oneOf) {
437
+ // the actual type of the ref
438
+ const refType = jsonschema.$ref.split('/').pop();
439
+ const refName = camel(refType);
440
+ const id = t.identifier(refName);
441
+ id.typeAnnotation = t.tsTypeAnnotation(t.tsTypeReference(t.identifier(refType)));
442
+ return id
443
+ } else if (obj) {
437
444
  return createTypedObjectParams(
438
445
  context,
439
446
  obj,
@@ -1,4 +1,5 @@
1
1
  import { JSONSchema } from "../types";
2
+ import { UtilMapping } from "./imports";
2
3
  export interface ReactQueryOptions {
3
4
  enabled?: boolean;
4
5
  optionalClient?: boolean;
@@ -16,6 +17,9 @@ export interface TSClientOptions {
16
17
  export interface MessageComposerOptions {
17
18
  enabled?: boolean;
18
19
  }
20
+ export interface MsgBuilderOptions {
21
+ enabled?: boolean;
22
+ }
19
23
  export interface RecoilOptions {
20
24
  enabled?: boolean;
21
25
  }
@@ -43,25 +47,46 @@ export interface ContractInfo {
43
47
  idlObject?: IDLObject;
44
48
  }
45
49
  export interface RenderOptions {
50
+ enabled?: boolean;
46
51
  types?: TSTypesOptions;
47
52
  recoil?: RecoilOptions;
48
53
  messageComposer?: MessageComposerOptions;
54
+ msgBuilder?: MsgBuilderOptions;
49
55
  client?: TSClientOptions;
50
56
  reactQuery?: ReactQueryOptions;
51
57
  }
52
- export interface RenderContext {
58
+ export interface IContext {
59
+ refLookup($ref: string): any;
60
+ addUtil(util: string): any;
61
+ getImports(registeredUtils?: UtilMapping): any;
62
+ }
63
+ export interface IRenderContext<TOpt = RenderOptions> extends IContext {
53
64
  contract: ContractInfo;
54
- options: RenderOptions;
65
+ options: TOpt;
55
66
  }
56
67
  export declare const defaultOptions: RenderOptions;
57
68
  export declare const getDefinitionSchema: (schemas: JSONSchema[]) => JSONSchema;
58
- export declare class RenderContext implements RenderContext {
69
+ /**
70
+ * context object for generating code.
71
+ * only mergeDefaultOpt needs to implementing for combine options and default options.
72
+ * @param TOpt option type
73
+ */
74
+ export declare abstract class RenderContextBase<TOpt = RenderOptions> implements IRenderContext<TOpt> {
59
75
  contract: ContractInfo;
60
76
  utils: string[];
61
77
  schema: JSONSchema;
62
- constructor(contract: ContractInfo, options?: RenderOptions);
78
+ options: TOpt;
79
+ constructor(contract: ContractInfo, options?: TOpt);
80
+ /**
81
+ * merge options and default options
82
+ * @param options
83
+ */
84
+ abstract mergeDefaultOpt(options: TOpt): TOpt;
63
85
  refLookup($ref: string): JSONSchema;
64
86
  addUtil(util: string): void;
65
- getImports(): any[];
87
+ getImports(registeredUtils?: UtilMapping): any;
88
+ }
89
+ export declare class RenderContext extends RenderContextBase {
90
+ mergeDefaultOpt(options: RenderOptions): RenderOptions;
66
91
  }
67
92
  export {};
@@ -5,12 +5,16 @@ export interface ImportObj {
5
5
  path: string;
6
6
  importAs?: string;
7
7
  }
8
+ export type GetUtilFn = (<TContext = RenderContext>(...args: any[]) => (context: TContext) => ImportObj);
9
+ export type UtilMapping = {
10
+ [key: string]: ImportObj | string | GetUtilFn;
11
+ };
8
12
  export declare const UTILS: {
13
+ selectorFamily: string;
9
14
  MsgExecuteContract: string;
10
15
  MsgExecuteContractEncodeObject: string;
11
16
  Coin: string;
12
17
  toUtf8: string;
13
- selectorFamily: string;
14
18
  StdFee: string;
15
19
  CosmWasmClient: string;
16
20
  ExecuteResult: string;
@@ -36,5 +40,6 @@ export declare const UTILS: {
36
40
  name: any;
37
41
  };
38
42
  };
39
- export declare const convertUtilsToImportList: (context: RenderContext, utils: string[]) => ImportObj[];
43
+ export declare const convertUtilsToImportList: (context: RenderContext, utils: string[], registeredUtils?: UtilMapping) => ImportObj[];
44
+ export declare const convertUtil: (context: RenderContext, util: string, registeredUtils: object) => ImportObj;
40
45
  export declare const getImportStatements: (list: ImportObj[]) => any[];
package/types/index.d.ts CHANGED
@@ -5,3 +5,4 @@ export * from './recoil';
5
5
  export * from './message-composer';
6
6
  export * from './react-query';
7
7
  export * from './types';
8
+ export * from './msg-builder';
@@ -0,0 +1 @@
1
+ export * from './msg-builder';
@@ -0,0 +1,4 @@
1
+ import * as t from "@babel/types";
2
+ import { ExecuteMsg, QueryMsg } from "../types";
3
+ import { RenderContext } from "../context";
4
+ export declare const createMsgBuilderClass: (context: RenderContext, className: string, msg: ExecuteMsg | QueryMsg) => t.ExportNamedDeclaration;
@@ -30,16 +30,17 @@ interface ReactQueryMutationHookInterface {
30
30
  /**
31
31
  * Example:
32
32
  ```
33
- export interface Cw4UpdateMembersMutation {
34
- client: Cw4GroupClient
35
- args: {
36
- tokenId: string
37
- remove: string[]
38
- }
39
- options?: Omit<
40
- UseMutationOptions<ExecuteResult, Error, Pick<Cw4UpdateMembersMutation, 'args'>>,
41
- 'mutationFn'
42
- >
33
+ export interface Cw721RevokeMutation {
34
+ client: Cw721Client;
35
+ msg: {
36
+ spender: string;
37
+ tokenId: string;
38
+ };
39
+ args?: {
40
+ fee?: number | StdFee | "auto";
41
+ memo?: string;
42
+ funds?: Coin[];
43
+ };
43
44
  }
44
45
  ```
45
46
  */
@@ -2,7 +2,7 @@ import * as t from '@babel/types';
2
2
  import { QueryMsg } from '../types';
3
3
  import { RenderContext } from '../context';
4
4
  export declare const createRecoilSelector: (context: RenderContext, keyPrefix: string, QueryClient: string, methodName: string, responseType: string) => t.ExportNamedDeclaration;
5
- export declare const createRecoilSelectors: (context: RenderContext, keyPrefix: string, QueryClient: string, queryMsg: QueryMsg) => any;
5
+ export declare const createRecoilSelectors: (context: RenderContext, keyPrefix: string, QueryClient: string, queryMsg: QueryMsg) => t.ExportNamedDeclaration[];
6
6
  export declare const createRecoilQueryClientType: () => {
7
7
  type: string;
8
8
  id: {
@@ -1,5 +1,5 @@
1
1
  import * as t from '@babel/types';
2
- import { Field, QueryMsg, ExecuteMsg } from '../types';
2
+ import { Field } from '../types';
3
3
  import { TSTypeAnnotation, TSExpressionWithTypeArguments } from '@babel/types';
4
4
  export declare const propertySignature: (name: string, typeAnnotation: t.TSTypeAnnotation, optional?: boolean) => {
5
5
  type: string;
@@ -9,13 +9,14 @@ export declare const propertySignature: (name: string, typeAnnotation: t.TSTypeA
9
9
  };
10
10
  export declare const identifier: (name: string, typeAnnotation: t.TSTypeAnnotation, optional?: boolean) => t.Identifier;
11
11
  export declare const tsTypeOperator: (typeAnnotation: t.TSType, operator: string) => t.TSTypeOperator;
12
- export declare const getMessageProperties: (msg: QueryMsg | ExecuteMsg) => any;
12
+ export declare const getMessageProperties: (msg: any) => any[];
13
13
  export declare const tsPropertySignature: (key: t.Expression, typeAnnotation: t.TSTypeAnnotation, optional: boolean) => t.TSPropertySignature;
14
14
  export declare const tsObjectPattern: (properties: (t.RestElement | t.ObjectProperty)[], typeAnnotation: t.TSTypeAnnotation) => t.ObjectPattern;
15
15
  export declare const callExpression: (callee: t.Expression | t.V8IntrinsicIdentifier, _arguments: (t.Expression | t.SpreadElement | t.ArgumentPlaceholder)[], typeParameters: t.TSTypeParameterInstantiation) => t.CallExpression;
16
16
  export declare const bindMethod: (name: string) => t.ExpressionStatement;
17
17
  export declare const typedIdentifier: (name: string, typeAnnotation: TSTypeAnnotation, optional?: boolean) => t.Identifier;
18
18
  export declare const promiseTypeAnnotation: (name: any) => t.TSTypeAnnotation;
19
+ export declare const abstractClassDeclaration: (name: string, body: any[], implementsExressions?: TSExpressionWithTypeArguments[], superClass?: t.Identifier) => t.ClassDeclaration;
19
20
  export declare const classDeclaration: (name: string, body: any[], implementsExressions?: TSExpressionWithTypeArguments[], superClass?: t.Identifier) => t.ClassDeclaration;
20
21
  export declare const classProperty: (name: string, typeAnnotation?: TSTypeAnnotation, isReadonly?: boolean, isStatic?: boolean, noImplicitOverride?: boolean) => t.ClassProperty;
21
22
  export declare const arrowFunctionExpression: (params: (t.Identifier | t.Pattern | t.RestElement)[], body: t.BlockStatement, returnType: t.TSTypeAnnotation, isAsync?: boolean) => t.ArrowFunctionExpression;
@@ -1,2 +1,3 @@
1
1
  export * from './babel';
2
2
  export * from './types';
3
+ export * from './ref';
@@ -0,0 +1,2 @@
1
+ import { JSONSchema } from "../types";
2
+ export declare const refLookup: ($ref: string, schema: JSONSchema) => JSONSchema;