@doeixd/machine 0.0.17 → 0.0.18

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.
@@ -27,6 +27,7 @@ __export(src_exports, {
27
27
  MachineBase: () => MachineBase,
28
28
  MiddlewareBuilder: () => MiddlewareBuilder,
29
29
  MultiMachineBase: () => MultiMachineBase,
30
+ _typeToJson: () => _typeToJson,
30
31
  action: () => action,
31
32
  bindTransitions: () => bindTransitions,
32
33
  branch: () => branch,
@@ -708,6 +709,43 @@ function createParallelMachine(m1, m2) {
708
709
 
709
710
  // src/extract.ts
710
711
  var import_ts_morph = require("ts-morph");
712
+ function _typeToJson(type, verbose = false) {
713
+ const symbol = type.getSymbol();
714
+ if (symbol && symbol.getDeclarations().some(import_ts_morph.Node.isClassDeclaration)) {
715
+ return symbol.getName();
716
+ }
717
+ if (type.isStringLiteral()) return type.getLiteralValue();
718
+ if (type.isNumberLiteral()) return type.getLiteralValue();
719
+ if (type.isBooleanLiteral()) return type.getLiteralValue();
720
+ if (type.isString()) return "string";
721
+ if (type.isNumber()) return "number";
722
+ if (type.isBoolean()) return "boolean";
723
+ if (type.isArray()) {
724
+ const elementType = type.getArrayElementTypeOrThrow();
725
+ return [_typeToJson(elementType, verbose)];
726
+ }
727
+ if (type.isObject() || type.isIntersection()) {
728
+ const obj = {};
729
+ const properties = type.getProperties();
730
+ for (const prop of properties) {
731
+ const propName = prop.getName();
732
+ if (propName.startsWith("__@")) continue;
733
+ const declaration = prop.getValueDeclaration();
734
+ if (!declaration) continue;
735
+ try {
736
+ obj[propName] = _typeToJson(declaration.getType(), verbose);
737
+ } catch (e) {
738
+ if (verbose) console.error(` Warning: Failed to serialize property ${propName}:`, e);
739
+ obj[propName] = "unknown";
740
+ }
741
+ }
742
+ return Object.keys(obj).length > 0 ? obj : null;
743
+ }
744
+ if (verbose) {
745
+ console.error(` Unhandled type: ${type.getText()}`);
746
+ }
747
+ return "unknown";
748
+ }
711
749
  function resolveClassName(node) {
712
750
  if (import_ts_morph.Node.isIdentifier(node)) {
713
751
  return node.getText();