wasm-ast-types 0.11.3 → 0.13.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 (55) hide show
  1. package/main/client/test/ts-client.issue-71.test.js +103 -0
  2. package/main/context/context.js +2 -1
  3. package/main/utils/types.js +20 -7
  4. package/module/client/test/ts-client.issue-71.test.js +21 -0
  5. package/module/context/context.js +2 -1
  6. package/module/utils/types.js +20 -7
  7. package/package.json +5 -3
  8. package/src/client/client.ts +665 -0
  9. package/src/client/index.ts +1 -0
  10. package/src/client/test/__snapshots__/ts-client.account-nfts.spec.ts.snap +497 -0
  11. package/src/client/test/__snapshots__/ts-client.arrays-ref.spec.ts.snap +452 -0
  12. package/src/client/test/__snapshots__/ts-client.arrays.spec.ts.snap +101 -0
  13. package/src/client/test/__snapshots__/ts-client.cw-named-groups.test.ts.snap +141 -0
  14. package/src/client/test/__snapshots__/ts-client.cw-proposal-single.test.ts.snap +341 -0
  15. package/src/client/test/__snapshots__/ts-client.empty-enums.spec.ts.snap +20 -0
  16. package/src/client/test/__snapshots__/ts-client.issue-71.test.ts.snap +432 -0
  17. package/src/client/test/__snapshots__/ts-client.issues.test.ts.snap +984 -0
  18. package/src/client/test/__snapshots__/ts-client.sg721.spec.ts.snap +350 -0
  19. package/src/client/test/__snapshots__/ts-client.spec.ts.snap +723 -0
  20. package/src/client/test/__snapshots__/ts-client.vectis.spec.ts.snap +337 -0
  21. package/src/client/test/ts-client.account-nfts.spec.ts +55 -0
  22. package/src/client/test/ts-client.arrays-ref.spec.ts +48 -0
  23. package/src/client/test/ts-client.arrays.spec.ts +58 -0
  24. package/src/client/test/ts-client.cw-named-groups.test.ts +48 -0
  25. package/src/client/test/ts-client.cw-proposal-single.test.ts +50 -0
  26. package/src/client/test/ts-client.empty-enums.spec.ts +28 -0
  27. package/src/client/test/ts-client.issue-71.test.ts +51 -0
  28. package/src/client/test/ts-client.issues.test.ts +52 -0
  29. package/src/client/test/ts-client.sg721.spec.ts +46 -0
  30. package/src/client/test/ts-client.spec.ts +166 -0
  31. package/src/client/test/ts-client.vectis.spec.ts +97 -0
  32. package/src/context/context.ts +134 -0
  33. package/src/context/imports.ts +126 -0
  34. package/src/context/index.ts +2 -0
  35. package/src/index.ts +7 -0
  36. package/src/message-composer/__snapshots__/message-composer.spec.ts.snap +271 -0
  37. package/src/message-composer/index.ts +1 -0
  38. package/src/message-composer/message-composer.spec.ts +25 -0
  39. package/src/message-composer/message-composer.ts +305 -0
  40. package/src/react-query/__snapshots__/react-query.spec.ts.snap +913 -0
  41. package/src/react-query/index.ts +1 -0
  42. package/src/react-query/react-query.spec.ts +75 -0
  43. package/src/react-query/react-query.ts +913 -0
  44. package/src/recoil/__snapshots__/recoil.spec.ts.snap +203 -0
  45. package/src/recoil/index.ts +1 -0
  46. package/src/recoil/recoil.spec.ts +38 -0
  47. package/src/recoil/recoil.ts +307 -0
  48. package/src/types.ts +44 -0
  49. package/src/utils/__snapshots__/babel.spec.ts.snap +75 -0
  50. package/src/utils/babel.spec.ts +511 -0
  51. package/src/utils/babel.ts +315 -0
  52. package/src/utils/index.ts +2 -0
  53. package/src/utils/types.ts +459 -0
  54. package/types/client/client.d.ts +1 -1
  55. package/types/context/context.d.ts +1 -0
@@ -0,0 +1,459 @@
1
+ import * as t from '@babel/types';
2
+ import { camel, pascal } from 'case';
3
+ import { propertySignature } from './babel';
4
+ import { TSTypeAnnotation } from '@babel/types';
5
+ import { RenderContext } from '../context';
6
+ import { JSONSchema } from '../types';
7
+
8
+ export function getResponseType(
9
+ context: RenderContext,
10
+ underscoreName: string
11
+ ) {
12
+ const methodName = camel(underscoreName);
13
+ return pascal(
14
+ context.contract?.responses?.[underscoreName]?.title
15
+ ??
16
+ // after v1.1 is adopted, we can deprecate this and require the above response
17
+ `${methodName}Response`
18
+ );
19
+ };
20
+
21
+ const getTypeStrFromRef = ($ref) => {
22
+ if ($ref?.startsWith('#/definitions/')) {
23
+ return $ref.replace('#/definitions/', '');
24
+ }
25
+ throw new Error('what is $ref: ' + $ref);
26
+ }
27
+
28
+ export const getTypeFromRef = ($ref) => {
29
+ return t.tsTypeReference(t.identifier(getTypeStrFromRef($ref)))
30
+ }
31
+
32
+ const getArrayTypeFromRef = ($ref) => {
33
+ return t.tsArrayType(
34
+ getTypeFromRef($ref)
35
+ );
36
+ }
37
+
38
+ const getTypeOrRef = (obj) => {
39
+ if (obj.type) {
40
+ return getType(obj.type)
41
+ }
42
+ if (obj.$ref) {
43
+ return getTypeFromRef(obj.$ref);
44
+ }
45
+ throw new Error('contact maintainers cannot find type for ' + obj)
46
+ }
47
+
48
+ const getArrayTypeFromItems = (items) => {
49
+ // passing in [{"type":"string"}]
50
+ if (Array.isArray(items)) {
51
+ return t.tsArrayType(
52
+ t.tsArrayType(
53
+ getTypeOrRef(items[0])
54
+ )
55
+ );
56
+ }
57
+
58
+ // passing in {"items": [{"type":"string"}]}
59
+ const detect = detectType(items.type);
60
+
61
+ if (detect.type === 'array') {
62
+ if (Array.isArray(items.items)) {
63
+ return t.tsArrayType(
64
+ t.tsArrayType(
65
+ getTypeOrRef(items.items[0])
66
+ )
67
+ );
68
+ } else {
69
+ return t.tsArrayType(
70
+ getArrayTypeFromItems(
71
+ items.items
72
+ )
73
+ );
74
+ }
75
+ }
76
+
77
+
78
+ return t.tsArrayType(
79
+ getType(detect.type)
80
+ );
81
+ }
82
+
83
+
84
+ export const detectType = (type: string | string[]) => {
85
+ let optional = false;
86
+ let theType = '';
87
+ if (Array.isArray(type)) {
88
+ if (type.length !== 2) {
89
+ throw new Error('[getType(array length)] case not handled by transpiler. contact maintainers.')
90
+ }
91
+ const [nullableType, nullType] = type;
92
+ if (nullType !== 'null') {
93
+ throw new Error('[getType(null)] case not handled by transpiler. contact maintainers.')
94
+ }
95
+ theType = nullableType;
96
+ optional = true;
97
+ } else {
98
+ theType = type;
99
+ }
100
+
101
+ return {
102
+ type: theType,
103
+ optional
104
+ };
105
+ }
106
+
107
+ export const getTypeInfo = (info: JSONSchema) => {
108
+ let type = undefined;
109
+ let optional = undefined;
110
+
111
+ if (Array.isArray(info.anyOf)) {
112
+ // assuming 2nd is null, but let's check to ensure
113
+ if (info.anyOf.length !== 2) {
114
+ throw new Error('case not handled by transpiler. contact maintainers.')
115
+ }
116
+ const [nullableType, nullType] = info.anyOf;
117
+ if (nullType?.type !== 'null') {
118
+ throw new Error('[nullableType.type]: case not handled by transpiler. contact maintainers.')
119
+ }
120
+ if (!nullableType?.$ref) {
121
+ if (nullableType.title) {
122
+ type = t.tsTypeReference(t.identifier(nullableType.title));
123
+ } else {
124
+ throw new Error('[nullableType.title] case not handled by transpiler. contact maintainers.')
125
+ }
126
+ } else {
127
+ type = getTypeFromRef(nullableType?.$ref);
128
+ }
129
+ optional = true;
130
+ }
131
+
132
+
133
+ if (typeof info.type === 'string') {
134
+ if (info.type === 'array') {
135
+ if (typeof info.items === 'object' && !Array.isArray(info.items)) {
136
+ if (info.items.$ref) {
137
+ type = getArrayTypeFromRef(info.items.$ref);
138
+ } else if (info.items.title) {
139
+ type = t.tsArrayType(
140
+ t.tsTypeReference(
141
+ t.identifier(info.items.title)
142
+ )
143
+ );
144
+ } else if (info.items.type) {
145
+ type = getArrayTypeFromItems(info.items);
146
+ } else {
147
+ throw new Error('[info.items] case not handled by transpiler. contact maintainers.')
148
+ }
149
+ } else {
150
+ throw new Error('[info.items] case not handled by transpiler. contact maintainers.')
151
+ }
152
+ } else {
153
+ const detect = detectType(info.type);
154
+ type = getType(detect.type);
155
+ optional = detect.optional;
156
+ }
157
+ }
158
+
159
+ if (Array.isArray(info.type)) {
160
+ // assuming 2nd is null, but let's check to ensure
161
+ if (info.type.length !== 2) {
162
+ throw new Error('please report this to maintainers (field type): ' + JSON.stringify(info, null, 2))
163
+ }
164
+ const [nullableType, nullType] = info.type;
165
+ if (nullType !== 'null') {
166
+ throw new Error('please report this to maintainers (field type): ' + JSON.stringify(info, null, 2))
167
+ }
168
+
169
+ if (nullableType === 'array' && typeof info.items === 'object' && !Array.isArray(info.items)) {
170
+
171
+ if (info.items.type) {
172
+ const detect = detectType(info.items.type);
173
+ if (detect.type === 'array') {
174
+ // wen recursion?
175
+ type = t.tsArrayType(
176
+ getArrayTypeFromItems(info.items)
177
+ );
178
+ } else {
179
+ type = t.tsArrayType(
180
+ getType(detect.type)
181
+ );
182
+ }
183
+ optional = detect.optional;
184
+ } else if (info.items.$ref) {
185
+ type = getArrayTypeFromRef(info.items.$ref);
186
+ // } else if (info.items.title) {
187
+ // type = t.tsArrayType(
188
+ // t.tsTypeReference(
189
+ // t.identifier(info.items.title)
190
+ // )
191
+ // );
192
+ } else if (info.items.type) {
193
+ type = getArrayTypeFromItems(info.items);
194
+ } else {
195
+ throw new Error('[info.items] case not handled by transpiler. contact maintainers.')
196
+ }
197
+
198
+ } else {
199
+ const detect = detectType(nullableType);
200
+ optional = detect.optional;
201
+ if (detect.type === 'array') {
202
+ type = getArrayTypeFromItems(
203
+ info.items
204
+ );
205
+ } else {
206
+ type = getType(detect.type);
207
+ }
208
+
209
+ }
210
+
211
+ optional = true;
212
+ }
213
+
214
+ return {
215
+ type,
216
+ optional
217
+ };
218
+
219
+ }
220
+
221
+
222
+ export const getType = (type: string) => {
223
+ switch (type) {
224
+ case 'string':
225
+ return t.tsStringKeyword();
226
+ case 'boolean':
227
+ return t.tSBooleanKeyword();
228
+ case 'integer':
229
+ return t.tsNumberKeyword();
230
+ default:
231
+ throw new Error('contact maintainers [unknown type]: ' + type);
232
+ }
233
+ }
234
+
235
+ export const getPropertyType = (
236
+ context: RenderContext,
237
+ schema: JSONSchema,
238
+ prop: string
239
+ ) => {
240
+ const props = schema.properties ?? {};
241
+ let info = props[prop];
242
+
243
+ let type = null;
244
+ let optional = !schema.required?.includes(prop);
245
+
246
+ if (info.allOf && info.allOf.length === 1) {
247
+ info = info.allOf[0];
248
+ }
249
+
250
+ if (typeof info.$ref === 'string') {
251
+ type = getTypeFromRef(info.$ref)
252
+ }
253
+
254
+ const typeInfo = getTypeInfo(info);
255
+ if (typeof typeInfo.optional !== 'undefined') {
256
+ optional = typeInfo.optional;
257
+ }
258
+ if (typeof typeInfo.type !== 'undefined') {
259
+ type = typeInfo.type;
260
+ }
261
+
262
+ if (!type) {
263
+ throw new Error('cannot find type for ' + JSON.stringify(info))
264
+ }
265
+
266
+ if (schema.required?.includes(prop)) {
267
+ optional = false;
268
+ }
269
+
270
+ return { type, optional };
271
+ };
272
+
273
+
274
+ export function getPropertySignatureFromProp(
275
+ context: RenderContext,
276
+ jsonschema: JSONSchema,
277
+ prop: string,
278
+ camelize: boolean
279
+ ) {
280
+ if (jsonschema.properties[prop].type === 'object') {
281
+ if (jsonschema.properties[prop].title) {
282
+ return propertySignature(
283
+ camelize ? camel(prop) : prop,
284
+ t.tsTypeAnnotation(
285
+ t.tsTypeReference(t.identifier(jsonschema.properties[prop].title))
286
+ )
287
+ );
288
+ } else {
289
+ throw new Error('getPropertySignatureFromProp() contact maintainer');
290
+ }
291
+ }
292
+
293
+ if (Array.isArray(jsonschema.properties[prop].allOf)) {
294
+ const isOptional = !jsonschema.required?.includes(prop);
295
+ const unionTypes = jsonschema.properties[prop].allOf.map(el => {
296
+ if (el.title) return el.title;
297
+ if (el.$ref) return getTypeStrFromRef(el.$ref);
298
+ return el.type;
299
+ });
300
+ // @ts-ignore:next-line
301
+ const uniqUnionTypes = [...new Set(unionTypes)];
302
+
303
+ if (uniqUnionTypes.length === 1) {
304
+ return propertySignature(
305
+ camelize ? camel(prop) : prop,
306
+ t.tsTypeAnnotation(
307
+ t.tsTypeReference(
308
+ t.identifier(uniqUnionTypes[0])
309
+ )
310
+ ),
311
+ isOptional
312
+ );
313
+ } else {
314
+ return propertySignature(
315
+ camelize ? camel(prop) : prop,
316
+ t.tsTypeAnnotation(
317
+ t.tsUnionType(
318
+ uniqUnionTypes.map(typ =>
319
+ t.tsTypeReference(
320
+ t.identifier(typ)
321
+ )
322
+ )
323
+ )
324
+ ),
325
+ isOptional
326
+ );
327
+ }
328
+ } else if (Array.isArray(jsonschema.properties[prop].oneOf)) {
329
+ const isOptional = !jsonschema.required?.includes(prop);
330
+ const unionTypes = jsonschema.properties[prop].oneOf.map(el => {
331
+ if (el.title) return el.title;
332
+ if (el.$ref) return getTypeStrFromRef(el.$ref);
333
+ return el.type;
334
+ });
335
+ // @ts-ignore:next-line
336
+ const uniqUnionTypes = [...new Set(unionTypes)];
337
+ if (uniqUnionTypes.length === 1) {
338
+ return propertySignature(
339
+ camelize ? camel(prop) : prop,
340
+ t.tsTypeAnnotation(
341
+ t.tsTypeReference(
342
+ t.identifier(uniqUnionTypes[0])
343
+ )
344
+ ),
345
+ isOptional
346
+ );
347
+ } else {
348
+ return propertySignature(
349
+ camelize ? camel(prop) : prop,
350
+ t.tsTypeAnnotation(
351
+ t.tsUnionType(
352
+ uniqUnionTypes.map(typ =>
353
+ t.tsTypeReference(
354
+ t.identifier(typ)
355
+ )
356
+ )
357
+ )
358
+ ),
359
+ isOptional
360
+ );
361
+ }
362
+
363
+ }
364
+
365
+ try {
366
+ getPropertyType(context, jsonschema, prop);
367
+ } catch (e) {
368
+ console.log(e);
369
+ console.log(JSON.stringify(jsonschema, null, 2), prop);
370
+ }
371
+
372
+ const { type, optional } = getPropertyType(context, jsonschema, prop);
373
+ return propertySignature(
374
+ camelize ? camel(prop) : prop,
375
+ t.tsTypeAnnotation(
376
+ type
377
+ ),
378
+ optional
379
+ );
380
+ }
381
+
382
+ export const getParamsTypeAnnotation = (
383
+ context: RenderContext,
384
+ jsonschema: any,
385
+ camelize: boolean = true
386
+ ): t.TSTypeAnnotation => {
387
+ const keys = Object.keys(jsonschema.properties ?? {});
388
+
389
+ if (!keys.length && jsonschema.$ref) {
390
+ return t.tsTypeAnnotation(getTypeFromRef(jsonschema.$ref))
391
+ }
392
+
393
+ if (!keys.length) return undefined;
394
+
395
+ const typedParams = keys.map(prop => getPropertySignatureFromProp(
396
+ context,
397
+ jsonschema,
398
+ prop,
399
+ camelize
400
+ ));
401
+
402
+ return t.tsTypeAnnotation(
403
+ t.tsTypeLiteral(
404
+ // @ts-ignore:next-line
405
+ [
406
+ ...typedParams
407
+ ]
408
+ )
409
+ )
410
+ }
411
+
412
+ export const createTypedObjectParams = (
413
+ context: RenderContext,
414
+ jsonschema: JSONSchema,
415
+ camelize: boolean = true
416
+ ): t.ObjectPattern => {
417
+
418
+ const keys = Object.keys(jsonschema.properties ?? {});
419
+ if (!keys.length) {
420
+
421
+ // is there a ref?
422
+ if (jsonschema.$ref) {
423
+ const obj = context.refLookup(jsonschema.$ref);
424
+ if (obj) {
425
+ return createTypedObjectParams(
426
+ context,
427
+ obj,
428
+ camelize
429
+ );
430
+ }
431
+ }
432
+
433
+ // no results...
434
+ return;
435
+ }
436
+
437
+ const params = keys.map(prop => {
438
+ return t.objectProperty(
439
+ camelize ? t.identifier(camel(prop)) : t.identifier(prop),
440
+ camelize ? t.identifier(camel(prop)) : t.identifier(prop),
441
+ false,
442
+ true
443
+ );
444
+ });
445
+
446
+ const obj = t.objectPattern(
447
+ [
448
+ ...params
449
+ ]
450
+ );
451
+
452
+ obj.typeAnnotation = getParamsTypeAnnotation(
453
+ context,
454
+ jsonschema,
455
+ camelize
456
+ );
457
+
458
+ return obj;
459
+ };
@@ -8,7 +8,7 @@ export declare const createWasmQueryMethod: (context: RenderContext, jsonschema:
8
8
  export declare const createQueryClass: (context: RenderContext, className: string, implementsClassName: string, queryMsg: QueryMsg) => t.ExportNamedDeclaration;
9
9
  export declare const getWasmMethodArgs: (context: RenderContext, jsonschema: JSONSchema) => t.ObjectProperty[];
10
10
  export declare const createWasmExecMethod: (context: RenderContext, jsonschema: JSONSchema) => t.ClassProperty;
11
- export declare const createExecuteClass: (context: RenderContext, className: string, implementsClassName: string, extendsClassName: string, execMsg: ExecuteMsg) => t.ExportNamedDeclaration;
11
+ export declare const createExecuteClass: (context: RenderContext, className: string, implementsClassName: string, extendsClassName: string | null, execMsg: ExecuteMsg) => t.ExportNamedDeclaration;
12
12
  export declare const createExecuteInterface: (context: RenderContext, className: string, extendsClassName: string | null, execMsg: ExecuteMsg) => t.ExportNamedDeclaration;
13
13
  export declare const createPropertyFunctionWithObjectParams: (context: RenderContext, methodName: string, responseType: string, jsonschema: JSONSchema) => t.TSPropertySignature;
14
14
  export declare const createPropertyFunctionWithObjectParamsForExec: (context: RenderContext, methodName: string, responseType: string, jsonschema: JSONSchema) => t.TSPropertySignature;
@@ -9,6 +9,7 @@ export interface ReactQueryOptions {
9
9
  }
10
10
  export interface TSClientOptions {
11
11
  enabled?: boolean;
12
+ execExtendsQuery?: boolean;
12
13
  }
13
14
  export interface MessageComposerOptions {
14
15
  enabled?: boolean;