@taqueria/plugin-contract-types 0.8.2 → 0.10.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 (42) hide show
  1. package/chunk-GC2KSB5D.js +226 -0
  2. package/chunk-GC2KSB5D.js.map +1 -0
  3. package/chunk-T4SGVAIL.js +604 -0
  4. package/chunk-T4SGVAIL.js.map +1 -0
  5. package/example/test-generation/example-contract-0.jest-demo.ts +78 -0
  6. package/example/test-generation/example-contract-1.jest-demo.ts +196 -0
  7. package/example/test-generation/example-contract-10.jest-demo.ts +47 -0
  8. package/example/test-generation/example-contract-11.jest-demo.ts +48 -0
  9. package/example/test-generation/example-contract-2.jest-demo.ts +277 -0
  10. package/example/test-generation/example-contract-4.jest-demo.ts +203 -0
  11. package/example/test-generation/example-contract-5.jest-demo.ts +235 -0
  12. package/example/test-generation/example-contract-6.jest-demo.ts +277 -0
  13. package/example/test-generation/example-contract-7.jest-demo.ts +170 -0
  14. package/example/test-generation/example-contract-8.jest-demo.ts +227 -0
  15. package/example/test-generation/example-contract-9.jest-demo.ts +42 -0
  16. package/example/types-file/example-contract-10.types.ts +2 -2
  17. package/example/types-file/example-contract-11.types.ts +2 -2
  18. package/example/types-file/type-aliases.ts +4 -0
  19. package/index.cjs +920 -0
  20. package/index.cjs.map +1 -0
  21. package/index.d.ts +1 -0
  22. package/index.js +78 -882
  23. package/index.js.map +1 -1
  24. package/index.ts +1 -0
  25. package/package.json +27 -12
  26. package/src/cli-process.cjs +848 -0
  27. package/src/cli-process.cjs.map +1 -0
  28. package/src/cli-process.d.ts +9 -0
  29. package/src/cli-process.js +8 -0
  30. package/src/cli-process.js.map +1 -0
  31. package/src/generator/process.ts +37 -13
  32. package/src/generator/testing-code-generator-jest-demo.ts +75 -0
  33. package/src/generator/testing-code-generator.cjs +613 -0
  34. package/src/generator/testing-code-generator.cjs.map +1 -0
  35. package/src/generator/testing-code-generator.d.ts +79 -0
  36. package/src/generator/testing-code-generator.js +131 -0
  37. package/src/generator/testing-code-generator.js.map +1 -0
  38. package/src/generator/testing-code-generator.ts +282 -0
  39. package/src/generator/typescript-output.ts +136 -14
  40. package/src/type-aliases.ts +1 -0
  41. package/tsconfig.json +4 -1
  42. package/lib/tsconfig.tsbuildinfo +0 -1
@@ -0,0 +1,604 @@
1
+ // src/generator/contract-name.ts
2
+ var normalizeContractName = (text) => text.replace(/[^A-Za-z0-9]/g, "_").split("_").filter((x) => x).map((x) => x[0].toUpperCase() + x.substring(1)).join("");
3
+
4
+ // src/generator/common.ts
5
+ var GenerateApiError = class {
6
+ constructor(message, data) {
7
+ this.message = message;
8
+ this.data = data;
9
+ this.name = `GenerateApiError`;
10
+ console.error(`\u274C GenerateApiError: ${message}`, data);
11
+ }
12
+ };
13
+ var assertExhaustive = (value, message) => {
14
+ console.error(message, { value });
15
+ };
16
+ var reduceFlatMap = (out, x) => {
17
+ out.push(...x);
18
+ return out;
19
+ };
20
+
21
+ // src/generator/typescript-output.ts
22
+ var createTypescriptCodeGenerator = (options) => {
23
+ const usedStrictTypes = [];
24
+ const addTypeAlias = (strictType) => {
25
+ if (!usedStrictTypes.some((x) => x.aliasType === strictType.aliasType)) {
26
+ usedStrictTypes.push(strictType);
27
+ }
28
+ };
29
+ const tabs = (indent) => Array(indent).fill(` `).join(``);
30
+ const toIndentedItems = (indent, delimeters, items) => {
31
+ return `
32
+ ${tabs(indent + 1)}${items.join(`${delimeters.afterItem ?? ``}
33
+ ${tabs(indent + 1)}${delimeters.beforeItem ?? ``}`)}
34
+ ${tabs(indent)}`;
35
+ };
36
+ const typeToCode = (t, indent) => {
37
+ if ((options == null ? void 0 : options.mode) === "defaultValue") {
38
+ return typeToCode_defaultValue(t, indent);
39
+ }
40
+ if (t.kind === `value`) {
41
+ const prim = `prim` in t.raw ? t.raw.prim : `unknown`;
42
+ if (t.typescriptType === `boolean` || t.typescriptType === `string` && prim === `string`) {
43
+ return `${t.typescriptType}`;
44
+ }
45
+ if (t.typescriptType === "number") {
46
+ const simpleBaseType2 = `string | BigNumber | number`;
47
+ const typeAlias2 = {
48
+ aliasType: prim,
49
+ simpleTypeDefinition: `type ${prim} = ${simpleBaseType2};`,
50
+ simpleTypeImports: [{ name: "BigNumber", isDefault: true, from: "bignumber.js" }]
51
+ };
52
+ addTypeAlias(typeAlias2);
53
+ return typeAlias2.aliasType;
54
+ }
55
+ const simpleBaseType = t.typescriptType === "Date" ? "Date | string" : t.typescriptType;
56
+ const typeAlias = { aliasType: prim, simpleTypeDefinition: `type ${prim} = ${simpleBaseType};` };
57
+ addTypeAlias(typeAlias);
58
+ return typeAlias.aliasType;
59
+ }
60
+ if (t.kind === `array`) {
61
+ return `Array<${typeToCode(t.array.item, indent)}>`;
62
+ }
63
+ if (t.kind === `map`) {
64
+ const typeAlias = t.map.isBigMap ? {
65
+ aliasType: `BigMap`,
66
+ simpleTypeDefinition: "type BigMap<K, T> = MichelsonMap<K, T>;",
67
+ simpleTypeImports: [{ name: "MichelsonMap", from: "@taquito/taquito" }]
68
+ } : {
69
+ aliasType: `MMap`,
70
+ simpleTypeDefinition: "type MMap<K, T> = MichelsonMap<K, T>;",
71
+ simpleTypeImports: [{ name: "MichelsonMap", from: "@taquito/taquito" }]
72
+ };
73
+ addTypeAlias(typeAlias);
74
+ return `${typeAlias.aliasType}<${typeToCode(t.map.key, indent)}, ${typeToCode(t.map.value, indent)}>`;
75
+ }
76
+ if (t.kind === `object`) {
77
+ return `{${toIndentedItems(indent, {}, t.fields.map((a, i) => varToCode(a, i, indent + 1) + `;`))}}`;
78
+ }
79
+ if (t.kind === `union`) {
80
+ const getUnionItem = (a, i) => {
81
+ const itemCode = `${varToCode(a, i, indent + 1)}`;
82
+ if (!itemCode.includes(`
83
+ `)) {
84
+ return `{ ${itemCode} }`;
85
+ }
86
+ return `{${toIndentedItems(indent + 1, {}, [`${varToCode(a, i, indent + 2)}`])}}`;
87
+ };
88
+ return `(${toIndentedItems(indent, { beforeItem: `| ` }, t.union.map(getUnionItem))})`;
89
+ }
90
+ if (t.kind === `unit`) {
91
+ const typeAlias = {
92
+ aliasType: `unit`,
93
+ simpleTypeDefinition: `type unit = (true | undefined);`
94
+ };
95
+ addTypeAlias(typeAlias);
96
+ return typeAlias.aliasType;
97
+ }
98
+ if (t.kind === `never`) {
99
+ return `never`;
100
+ }
101
+ if (t.kind === `unknown`) {
102
+ return `unknown`;
103
+ }
104
+ assertExhaustive(t, `Unknown type`);
105
+ throw new GenerateApiError(`Unknown type node`, { t });
106
+ };
107
+ const typeToCode_defaultValue = (t, indent) => {
108
+ if (t.kind === `value`) {
109
+ const prim = `prim` in t.raw ? t.raw.prim : `unknown`;
110
+ if (t.typescriptType === "boolean") {
111
+ return `true`;
112
+ }
113
+ if (t.typescriptType === `string` && prim === `string`) {
114
+ return `'VALUE'`;
115
+ }
116
+ if (t.typescriptType === "number") {
117
+ return `tas.${prim}('42')`;
118
+ }
119
+ if (t.typescriptType === "Date") {
120
+ return `tas.timestamp(new Date())`;
121
+ }
122
+ if (prim === "address" || prim === "contract") {
123
+ return `tas.${prim}('tz1ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456')`;
124
+ }
125
+ if (prim === "bytes") {
126
+ return `tas.${prim}(char2Bytes('DATA'))`;
127
+ }
128
+ if (t.typescriptType === "string") {
129
+ return `tas.${prim}('VALUE')`;
130
+ }
131
+ assertExhaustive(t.typescriptType, `Unknown value type`);
132
+ return prim;
133
+ }
134
+ if (t.kind === `array`) {
135
+ return `[${typeToCode(t.array.item, indent)}]`;
136
+ }
137
+ if (t.kind === `map`) {
138
+ const keyPrim = `prim` in t.map.key.raw ? t.map.key.raw.prim : `unknown`;
139
+ const isStringBasedKey = t.map.key.kind === "value" && keyPrim === "string";
140
+ if (isStringBasedKey) {
141
+ return `tas.${t.map.isBigMap ? "bigMap" : "map"}({
142
+ ${tabs(indent + 1)}${typeToCode(t.map.key, indent)}: ${typeToCode(t.map.value, indent)},
143
+ ${tabs(indent)}})`;
144
+ }
145
+ return `tas.${t.map.isBigMap ? "bigMap" : "map"}([{
146
+ ${tabs(indent + 1)}key: ${typeToCode(t.map.key, indent)},
147
+ ${tabs(indent + 1)}value: ${typeToCode(t.map.value, indent)},
148
+ ${tabs(indent)}}])`;
149
+ }
150
+ if (t.kind === `object`) {
151
+ const delimeter = (options == null ? void 0 : options.mode) === "defaultValue" ? "," : `;`;
152
+ return `{${toIndentedItems(indent, {}, t.fields.map((a, i) => varToCode(a, i, indent + 1) + delimeter))}}`;
153
+ }
154
+ if (t.kind === `union`) {
155
+ const getUnionItem = (a, i) => {
156
+ const itemCode = `${varToCode(a, i, indent + 1)}`;
157
+ if (!itemCode.includes(`
158
+ `)) {
159
+ return `{ ${itemCode} }`;
160
+ }
161
+ return `{${toIndentedItems(indent + 1, {}, [`${varToCode(a, i, indent + 2)}`])}}`;
162
+ };
163
+ return `(${toIndentedItems(indent, { beforeItem: `| ` }, t.union.map(getUnionItem))})`;
164
+ }
165
+ if (t.kind === `unit`) {
166
+ return `tas.unit()`;
167
+ }
168
+ if (t.kind === `never`) {
169
+ return `never`;
170
+ }
171
+ if (t.kind === `unknown`) {
172
+ return `unknown`;
173
+ }
174
+ assertExhaustive(t, `Unknown type`);
175
+ throw new GenerateApiError(`Unknown type node`, { t });
176
+ };
177
+ const varToCode = (t, i, indent, numberVarNamePrefix = "") => {
178
+ return `${t.name ?? `${numberVarNamePrefix}${i}`}${t.type.optional && (options == null ? void 0 : options.mode) !== "defaultValue" ? `?` : ``}: ${typeToCode(t.type, indent)}`;
179
+ };
180
+ const argsToCode = (args, indent, asObject) => {
181
+ if (args.length === 1) {
182
+ if (args[0].type.kind === `unit`)
183
+ return ``;
184
+ if ((options == null ? void 0 : options.mode) === "defaultValue") {
185
+ return typeToCode(args[0].type, indent + 1);
186
+ }
187
+ return `${args[0].name ?? `param`}: ${typeToCode(args[0].type, indent + 1)}`;
188
+ }
189
+ const result = `${toIndentedItems(
190
+ indent,
191
+ {},
192
+ args.filter((x) => x.name || x.type.kind !== `unit`).map((a, i) => {
193
+ if (!asObject && (options == null ? void 0 : options.mode) === "defaultValue") {
194
+ return typeToCode(a.type, indent + 1) + `,`;
195
+ }
196
+ return varToCode(a, i, indent + 1, asObject ? "" : "_") + `,`;
197
+ })
198
+ )}`;
199
+ if (asObject) {
200
+ if ((options == null ? void 0 : options.mode) === "defaultValue") {
201
+ return `{${result}}`;
202
+ }
203
+ return `params: {${result}}`;
204
+ }
205
+ return result;
206
+ };
207
+ return {
208
+ usedStrictTypes,
209
+ tabs,
210
+ toIndentedItems,
211
+ typeToCode,
212
+ argsToCode
213
+ };
214
+ };
215
+ var toTypescriptCode = (storage, methods, contractName, parsedContract, protocol, typeAliasData, typeUtilsData) => {
216
+ const {
217
+ usedStrictTypes,
218
+ toIndentedItems,
219
+ typeToCode,
220
+ argsToCode
221
+ } = createTypescriptCodeGenerator();
222
+ const methodsToCode = (indent) => {
223
+ const methodFields = methods.map((x) => {
224
+ const methodCode = `${x.name}: (${argsToCode(x.args, indent + 1, false)}) => Promise<void>;`;
225
+ return methodCode;
226
+ });
227
+ const methodsTypeCode = `type Methods = {${toIndentedItems(indent, {}, methodFields)}};`;
228
+ return methodsTypeCode;
229
+ };
230
+ const methodsObjectToCode = (indent) => {
231
+ const methodFields = methods.map((x) => {
232
+ const methodCode = `${x.name}: (${argsToCode(x.args, indent + 1, true)}) => Promise<void>;`;
233
+ return methodCode;
234
+ });
235
+ const methodsTypeCode = `type MethodsObject = {${toIndentedItems(indent, {}, methodFields)}};`;
236
+ return methodsTypeCode;
237
+ };
238
+ const storageToCode = (indent) => {
239
+ const storageTypeCode = `type Storage = ${typeToCode(storage.storage, indent)};`;
240
+ return storageTypeCode;
241
+ };
242
+ const methodsCode = methodsToCode(0);
243
+ const methodsObjectCode = methodsObjectToCode(0);
244
+ const storageCode = storageToCode(0);
245
+ const simpleTypeMappingImportsAll = new Map(
246
+ usedStrictTypes.map((x) => x.simpleTypeImports ?? []).reduce(reduceFlatMap, []).map(
247
+ (x) => [`${x == null ? void 0 : x.from}:${x == null ? void 0 : x.name}:${x == null ? void 0 : x.isDefault}`, x]
248
+ )
249
+ );
250
+ const simpleTypeMappingImportsFrom = [...simpleTypeMappingImportsAll.values()].reduce((out, x) => {
251
+ const entry = out[x.from] ?? (out[x.from] = { names: [] });
252
+ if (x.isDefault) {
253
+ entry.default = x.name;
254
+ } else {
255
+ entry.names.push(x.name);
256
+ }
257
+ entry.names.sort((a, b) => a.localeCompare(b));
258
+ return out;
259
+ }, {});
260
+ const simpleTypeMappingImportsText = Object.keys(simpleTypeMappingImportsFrom).map((k) => {
261
+ const entry = simpleTypeMappingImportsFrom[k];
262
+ const items = [entry.default, entry.names.length ? `{ ${entry.names.join(", ")} }` : ""].filter((x) => x);
263
+ return `import ${items.join(", ")} from '${k}';
264
+ `;
265
+ }).join("");
266
+ const simpleTypeMapping = usedStrictTypes.sort((a, b) => a.aliasType.localeCompare(b.aliasType)).map((x) => x.simpleTypeDefinition).join(`
267
+ `);
268
+ const typeUtilsDefinitions = `import { ContractAbstractionFromContractType, WalletContractAbstractionFromContractType } from '${typeUtilsData.importPath}';`;
269
+ const typeAliasesDefinitions = typeAliasData.mode === "simple" ? `${simpleTypeMappingImportsText}${simpleTypeMapping}` : typeAliasData.mode === "local" ? typeAliasData.fileContent : `import { ${usedStrictTypes.map((x) => x.aliasType).join(`, `)} } from '${typeAliasData.importPath}';`;
270
+ const contractTypeName = `${contractName}ContractType`;
271
+ const walletTypeName = `${contractName}WalletType`;
272
+ const codeName = `${contractName}Code`;
273
+ const typesFileContent = `
274
+ ${typeUtilsDefinitions}
275
+ ${typeAliasesDefinitions}
276
+
277
+ ${storageCode}
278
+
279
+ ${methodsCode}
280
+
281
+ ${methodsObjectCode}
282
+
283
+ type contractTypes = { methods: Methods, methodsObject: MethodsObject, storage: Storage, code: { __type: '${codeName}', protocol: string, code: object[] } };
284
+ export type ${contractTypeName} = ContractAbstractionFromContractType<contractTypes>;
285
+ export type ${walletTypeName} = WalletContractAbstractionFromContractType<contractTypes>;
286
+ `;
287
+ const contractCodeFileContent = `
288
+ export const ${codeName}: { __type: '${codeName}', protocol: string, code: object[] } = {
289
+ __type: '${codeName}',
290
+ protocol: '${protocol.key}',
291
+ code: JSON.parse(\`${JSON.stringify(parsedContract)}\`)
292
+ };
293
+ `;
294
+ return {
295
+ typesFileContent,
296
+ contractCodeFileContent,
297
+ storage: storageCode,
298
+ methods: methodsCode,
299
+ methodsObject: methodsObjectCode
300
+ };
301
+ };
302
+
303
+ // src/generator/process.ts
304
+ import * as M from "@taquito/michel-codec";
305
+
306
+ // src/generator/contract-parser.ts
307
+ var parseContractStorage = (storage) => {
308
+ const fields = storage.args.map((x) => visitVar(x)).reduce(reduceFlatMap, []);
309
+ if (fields.length === 1 && !fields[0].name) {
310
+ return {
311
+ storage: fields[0].type
312
+ };
313
+ }
314
+ return {
315
+ storage: {
316
+ kind: `object`,
317
+ raw: storage,
318
+ fields
319
+ }
320
+ };
321
+ };
322
+ var parseContractParameter = (parameter) => {
323
+ return {
324
+ methods: parameter.args.map((x) => visitContractParameterEndpoint(x)).reduce(reduceFlatMap, [])
325
+ };
326
+ };
327
+ var visitContractParameterEndpoint = (node) => {
328
+ var _a, _b;
329
+ if (node.prim === `or`) {
330
+ return node.args.map((x) => visitContractParameterEndpoint(x)).reduce(reduceFlatMap, []);
331
+ }
332
+ if (node.prim === `list` && node.args.length === 1 && ((_a = node.args[0]) == null ? void 0 : _a.prim) === `or`) {
333
+ return node.args.map((x) => visitContractParameterEndpoint(x)).reduce(reduceFlatMap, []);
334
+ }
335
+ const nameRaw = (_b = node.annots) == null ? void 0 : _b[0];
336
+ const name = (nameRaw == null ? void 0 : nameRaw.startsWith("%")) ? nameRaw.substr(1) : null;
337
+ if (!name) {
338
+ console.warn(`Unknown method: ${node.prim}`, { node, args: node.args });
339
+ return [];
340
+ }
341
+ const nodeType = visitType(node, { ignorePairName: node.prim === "pair" });
342
+ if (nodeType.kind === "object") {
343
+ return [{ name, args: nodeType.fields }];
344
+ }
345
+ return [{
346
+ name,
347
+ args: [{ type: nodeType }]
348
+ }];
349
+ };
350
+ var visitVar = (node) => {
351
+ var _a;
352
+ const name = `annots` in node && ((_a = node.annots) == null ? void 0 : _a.length) === 1 ? node.annots[0].substr(1) : void 0;
353
+ const type = visitType(node);
354
+ return [{
355
+ name,
356
+ type
357
+ }];
358
+ };
359
+ var visitType = (node, options) => {
360
+ if (!(`prim` in node)) {
361
+ console.error(`visitType no prim`, { node });
362
+ return { kind: `unknown`, raw: node };
363
+ }
364
+ if (node.prim === `or`) {
365
+ const unionVars = node.args.map((x) => visitVar(x)).reduce(reduceFlatMap, []).map((x) => x);
366
+ const union = unionVars.map((x) => !x.name && x.type.kind === "union" ? x.type.union : [x]).reduce(reduceFlatMap, []);
367
+ if (union.some((x) => !x)) {
368
+ throw new GenerateApiError(`or: Some fields are null`, { node });
369
+ }
370
+ return {
371
+ kind: `union`,
372
+ raw: node,
373
+ union
374
+ };
375
+ }
376
+ if (node.prim === `pair`) {
377
+ const fields = node.args.map((x) => visitVar(x)).reduce(reduceFlatMap, []);
378
+ if (fields.some((x) => !x)) {
379
+ throw new GenerateApiError(`pair: Some fields are null`, { node, args: node.args, fields });
380
+ }
381
+ const fieldsFlat = fields.map(
382
+ (x) => (!x.name || (options == null ? void 0 : options.ignorePairName)) && x.type.kind === "object" ? x.type.fields : [x]
383
+ ).reduce(reduceFlatMap, []);
384
+ return {
385
+ kind: `object`,
386
+ raw: node,
387
+ fields: fieldsFlat
388
+ };
389
+ }
390
+ if (node.prim === `list` || node.prim === `set`) {
391
+ if (node.args.length !== 1) {
392
+ throw new GenerateApiError(`list does not have 1 arg`, { node, args: node.args });
393
+ }
394
+ const arrayItem = visitType(node.args[0]);
395
+ if (!arrayItem) {
396
+ throw new GenerateApiError(`arrayItem are null`, { node, args: node.args, arrayItem });
397
+ }
398
+ return {
399
+ kind: `array`,
400
+ raw: node,
401
+ array: { item: arrayItem }
402
+ };
403
+ }
404
+ if (node.prim === `map` || node.prim === `big_map`) {
405
+ if (node.args.length !== 2) {
406
+ throw new GenerateApiError(`map does not have 2 args`, { node, args: node.args });
407
+ }
408
+ const mapKey = visitType(node.args[0]);
409
+ const mapValue = visitType(node.args[1]);
410
+ if (!mapKey || !mapValue) {
411
+ throw new GenerateApiError(`map is missing key or value`, { node, args: node.args, mapKey, mapValue });
412
+ }
413
+ return {
414
+ kind: `map`,
415
+ raw: node,
416
+ map: {
417
+ key: mapKey,
418
+ value: mapValue,
419
+ isBigMap: node.prim === `big_map`
420
+ }
421
+ };
422
+ }
423
+ if (node.prim === `option`) {
424
+ return {
425
+ ...visitType(node.args[0]),
426
+ optional: true
427
+ };
428
+ }
429
+ if (node.prim === `bool`) {
430
+ return {
431
+ kind: `value`,
432
+ raw: node,
433
+ value: node.prim,
434
+ typescriptType: `boolean`
435
+ };
436
+ }
437
+ if (node.prim === `nat` || node.prim === `int` || node.prim === `mutez`) {
438
+ return {
439
+ kind: `value`,
440
+ raw: node,
441
+ value: node.prim,
442
+ typescriptType: `number`
443
+ };
444
+ }
445
+ if (node.prim === `timestamp`) {
446
+ return {
447
+ kind: `value`,
448
+ raw: node,
449
+ value: node.prim,
450
+ typescriptType: `Date`
451
+ };
452
+ }
453
+ if (node.prim === `address` || node.prim === `key` || node.prim === `key_hash` || node.prim === `chain_id` || node.prim === `string` || node.prim === `signature` || node.prim === `ticket` || node.prim === `bls12_381_fr` || node.prim === `bls12_381_g1` || node.prim === `bls12_381_g2` || node.prim === `sapling_state` || node.prim === `sapling_transaction` || node.prim === `contract`) {
454
+ return {
455
+ kind: `value`,
456
+ raw: node,
457
+ value: node.prim,
458
+ typescriptType: `string`
459
+ };
460
+ }
461
+ if (node.prim === `unit`) {
462
+ return {
463
+ kind: `unit`,
464
+ raw: node
465
+ };
466
+ }
467
+ if (node.prim === `bytes`) {
468
+ return {
469
+ kind: `value`,
470
+ raw: node,
471
+ value: node.prim,
472
+ typescriptType: `string`
473
+ };
474
+ }
475
+ if (node.prim === `lambda` || node.prim === `operation`) {
476
+ return {
477
+ kind: `value`,
478
+ raw: node,
479
+ value: node.prim,
480
+ typescriptType: `string`
481
+ };
482
+ }
483
+ if (node.prim === "chest" || node.prim === "chest_key") {
484
+ return {
485
+ kind: `value`,
486
+ raw: node,
487
+ value: node.prim,
488
+ typescriptType: `string`
489
+ };
490
+ }
491
+ if (node.prim === `never`) {
492
+ return {
493
+ kind: `never`,
494
+ raw: node
495
+ };
496
+ }
497
+ assertExhaustive(node, `Unknown type`);
498
+ throw new GenerateApiError(`Unknown type`, { node });
499
+ };
500
+
501
+ // src/generator/schema-output.ts
502
+ var toSchema = (methods, storage) => {
503
+ const getSchemaObjectType = (vars) => {
504
+ if (vars.some((x) => !x)) {
505
+ throw new GenerateApiError(`getSchemaObjectType has null vars`, { vars });
506
+ }
507
+ return vars.reduce((out, x, i) => {
508
+ out[x.name ?? i] = getSchemaType(x.type);
509
+ return out;
510
+ }, {});
511
+ };
512
+ const getSchemaType = (t) => {
513
+ return (t.kind === `value` && t.value ? t.value : null) ?? (t.kind === `array` && t.array ? [getSchemaType(t.array.item)] : null) ?? (t.kind === `map` && t.map ? [`map`, getSchemaType(t.map.key), getSchemaType(t.map.value)] : null) ?? (t.kind === `object` && t.fields ? getSchemaObjectType(t.fields) : null) ?? (t.kind === `unit` ? `unit` : null) ?? (t.kind === `never` ? `never` : null) ?? `${t.raw}`;
514
+ };
515
+ const schemaMethods = methods.reduce((out, x) => {
516
+ out[x.name] = {
517
+ params: x.args.length === 1 && !x.args[0].name ? getSchemaType(x.args[0].type) : getSchemaObjectType(x.args ?? [])
518
+ };
519
+ return out;
520
+ }, {});
521
+ const schemaStorage = getSchemaType(storage.storage);
522
+ return {
523
+ methods: schemaMethods,
524
+ storage: schemaStorage
525
+ };
526
+ };
527
+
528
+ // src/generator/process.ts
529
+ var parseContractWithMinimalProtocolLevel = (contractScript, format, contractLevelIndex) => {
530
+ const contractLevels = [
531
+ { name: "PsDELPH1", key: M.Protocol.PsDELPH1 },
532
+ { name: "PtEdo2Zk", key: M.Protocol.PtEdo2Zk },
533
+ { name: "PsFLorena", key: M.Protocol.PsFLorena }
534
+ ];
535
+ const protocol = contractLevels[contractLevelIndex];
536
+ if (!protocol) {
537
+ throw new GenerateApiError(`Could not parse contract script`, contractScript);
538
+ }
539
+ const p = new M.Parser({ protocol: protocol.key });
540
+ try {
541
+ const contract = format === "tz" ? p.parseScript(contractScript) : p.parseJSON(JSON.parse(contractScript));
542
+ if (contract) {
543
+ return {
544
+ contract,
545
+ protocol
546
+ };
547
+ }
548
+ } catch {
549
+ }
550
+ return parseContractWithMinimalProtocolLevel(contractScript, format, contractLevelIndex + 1);
551
+ };
552
+ var parseContractInterface = (contractScript, format) => {
553
+ const p = new M.Parser({ protocol: M.Protocol.PsFLorena });
554
+ const { contract, protocol } = parseContractWithMinimalProtocolLevel(contractScript, format, 0);
555
+ const contractStorage = contract.find((x) => x.prim === `storage`);
556
+ const contractParameter = contract.find((x) => x.prim === `parameter`);
557
+ const storageResult = contractStorage && parseContractStorage(contractStorage);
558
+ const storage = storageResult ?? { storage: { kind: `object`, raw: { prim: `never` }, fields: [] } };
559
+ const parameterResult = contractParameter && parseContractParameter(contractParameter);
560
+ const methods = (parameterResult == null ? void 0 : parameterResult.methods) ?? [];
561
+ return {
562
+ storage,
563
+ methods,
564
+ contract,
565
+ protocol
566
+ };
567
+ };
568
+ var generateContractTypesFromMichelsonCode = (contractScript, contractName, format, typeAliasData, typeUtilsData) => {
569
+ const {
570
+ storage,
571
+ methods,
572
+ contract,
573
+ protocol
574
+ } = parseContractInterface(
575
+ contractScript,
576
+ format
577
+ );
578
+ if (methods.length === 1)
579
+ methods[0].name = `default`;
580
+ const schemaOutput = toSchema(methods, storage);
581
+ const typescriptCode = toTypescriptCode(
582
+ storage,
583
+ methods,
584
+ contractName,
585
+ contract,
586
+ protocol,
587
+ typeAliasData,
588
+ typeUtilsData
589
+ );
590
+ return {
591
+ schemaOutput,
592
+ typescriptCodeOutput: typescriptCode,
593
+ parsedContract: contract,
594
+ minimalProtocol: protocol.key
595
+ };
596
+ };
597
+
598
+ export {
599
+ normalizeContractName,
600
+ createTypescriptCodeGenerator,
601
+ parseContractInterface,
602
+ generateContractTypesFromMichelsonCode
603
+ };
604
+ //# sourceMappingURL=chunk-T4SGVAIL.js.map