@serverlessworkflow/sdk 1.0.0 → 1.0.1

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.
package/esm/index.esm.js CHANGED
@@ -133,6 +133,17 @@ function __extends(d, b) {
133
133
  d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
134
134
  }
135
135
 
136
+ var __assign = function() {
137
+ __assign = Object.assign || function __assign(t) {
138
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
139
+ s = arguments[i];
140
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
141
+ }
142
+ return t;
143
+ };
144
+ return __assign.apply(this, arguments);
145
+ };
146
+
136
147
  typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
137
148
  var e = new Error(message);
138
149
  return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
@@ -18089,7 +18100,7 @@ var _ExternalScript = ExternalScript;
18089
18100
  * Represents a FlowDirective with methods for validation and normalization.
18090
18101
  * Inherits from ObjectHydrator which provides functionality for hydrating the state based on a model.
18091
18102
  */
18092
- var FlowDirective = /** @class */ (function (_super) {
18103
+ var FlowDirective$1 = /** @class */ (function (_super) {
18093
18104
  __extends(FlowDirective, _super);
18094
18105
  /**
18095
18106
  * Instanciates a new instance of the FlowDirective class.
@@ -18124,7 +18135,7 @@ var FlowDirective = /** @class */ (function (_super) {
18124
18135
  };
18125
18136
  return FlowDirective;
18126
18137
  }(ObjectHydrator));
18127
- var _FlowDirective = FlowDirective;
18138
+ var _FlowDirective = FlowDirective$1;
18128
18139
 
18129
18140
  /*
18130
18141
  * Copyright 2021-Present The Serverless Workflow Specification Authors
@@ -24814,6 +24825,541 @@ var dumper = {
24814
24825
  var load = loader.load;
24815
24826
  var dump = dumper.dump;
24816
24827
 
24828
+ var entrySuffix = '-entry-node';
24829
+ var exitSuffix = '-exit-node';
24830
+ var rooId = 'root';
24831
+ var doReference = '/do';
24832
+ var forReference = '/for';
24833
+ var catchReference = '/catch';
24834
+ var branchReference = '/fork/branches';
24835
+ var tryReference = '/try';
24836
+ /**
24837
+ * Enumeration of possible node types in a graph.
24838
+ */
24839
+ var GraphNodeType;
24840
+ (function (GraphNodeType) {
24841
+ GraphNodeType["Root"] = "root";
24842
+ GraphNodeType["Start"] = "start";
24843
+ GraphNodeType["End"] = "end";
24844
+ GraphNodeType["Entry"] = "entry";
24845
+ GraphNodeType["Exit"] = "exit";
24846
+ GraphNodeType["Call"] = "call";
24847
+ GraphNodeType["Catch"] = "catch";
24848
+ GraphNodeType["Do"] = "do";
24849
+ GraphNodeType["Emit"] = "emit";
24850
+ GraphNodeType["For"] = "for";
24851
+ GraphNodeType["Fork"] = "fork";
24852
+ GraphNodeType["Listen"] = "listen";
24853
+ GraphNodeType["Raise"] = "raise";
24854
+ GraphNodeType["Run"] = "run";
24855
+ GraphNodeType["Set"] = "set";
24856
+ GraphNodeType["Switch"] = "switch";
24857
+ GraphNodeType["Try"] = "try";
24858
+ GraphNodeType["TryCatch"] = "try-catch";
24859
+ GraphNodeType["Wait"] = "wait";
24860
+ })(GraphNodeType || (GraphNodeType = {}));
24861
+ /**
24862
+ * Enumeration of possible workflow flow directives.
24863
+ */
24864
+ var FlowDirective;
24865
+ (function (FlowDirective) {
24866
+ FlowDirective["Exit"] = "exit";
24867
+ FlowDirective["End"] = "end";
24868
+ FlowDirective["Continue"] = "continue";
24869
+ })(FlowDirective || (FlowDirective = {}));
24870
+ /**
24871
+ * Converts an array of TaskItem objects into a Map for easy lookup.
24872
+ *
24873
+ * @param tasksList An array of TaskItem objects.
24874
+ * @returns A map where keys are task names and values are Task objects.
24875
+ */
24876
+ function mapTasks(tasksList) {
24877
+ return (tasksList || []).reduce(function (acc, item) {
24878
+ var _a = Object.entries(item)[0], key = _a[0], task = _a[1];
24879
+ acc.set(key, task);
24880
+ return acc;
24881
+ }, new Map());
24882
+ }
24883
+ /**
24884
+ * Initializes a graph with default entry and exit nodes.
24885
+ *
24886
+ * @param type The type of the graph node.
24887
+ * @param id Unique identifier for the graph.
24888
+ * @param label Optional label for the graph.
24889
+ * @param parent Optional parent graph if this is a subgraph.
24890
+ * @returns A newly created Graph instance.
24891
+ */
24892
+ function initGraph(type, id, label, parent) {
24893
+ if (id === void 0) { id = rooId; }
24894
+ if (label === void 0) { label = undefined; }
24895
+ if (parent === void 0) { parent = undefined; }
24896
+ var entryNode = {
24897
+ type: id === rooId ? GraphNodeType.Start : GraphNodeType.Entry,
24898
+ id: "".concat(id).concat(entrySuffix),
24899
+ };
24900
+ var exitNode = {
24901
+ type: id === rooId ? GraphNodeType.End : GraphNodeType.Exit,
24902
+ id: "".concat(id).concat(exitSuffix),
24903
+ };
24904
+ var graph = {
24905
+ id: id,
24906
+ label: label,
24907
+ type: type,
24908
+ parent: parent,
24909
+ entryNode: entryNode,
24910
+ exitNode: exitNode,
24911
+ nodes: [entryNode, exitNode],
24912
+ edges: [],
24913
+ };
24914
+ if (parent)
24915
+ parent.nodes.push(graph);
24916
+ return graph;
24917
+ }
24918
+ /**
24919
+ * Constructs a graph representation based on the given workflow.
24920
+ *
24921
+ * @param workflow The workflow to be converted into a graph structure.
24922
+ * @returns A graph representation of the workflow.
24923
+ */
24924
+ function buildGraph(workflow) {
24925
+ var graph = initGraph(GraphNodeType.Root);
24926
+ buildTransitions(graph.entryNode, {
24927
+ graph: graph,
24928
+ reference: doReference,
24929
+ taskList: mapTasks(workflow.do),
24930
+ taskReference: doReference,
24931
+ });
24932
+ return graph;
24933
+ }
24934
+ /**
24935
+ * Gets the next task to be executed in the workflow
24936
+ * @param tasksList The list of task to resolve the next task from
24937
+ * @param taskName The current task name, if any
24938
+ * @param transition A specific transition, if any
24939
+ * @returns
24940
+ */
24941
+ function getNextTask(tasksList, taskName, transition) {
24942
+ if (taskName === void 0) { taskName = undefined; }
24943
+ if (transition === void 0) { transition = undefined; }
24944
+ if (!(tasksList === null || tasksList === void 0 ? void 0 : tasksList.size))
24945
+ throw new Error('The task list cannot be empty. No tasks list to get the next task from.');
24946
+ var currentTask = tasksList.get(taskName || '');
24947
+ transition = transition || (currentTask === null || currentTask === void 0 ? void 0 : currentTask.then) || '';
24948
+ if (transition == FlowDirective.End || transition == FlowDirective.Exit) {
24949
+ return {
24950
+ name: transition,
24951
+ index: -1,
24952
+ };
24953
+ }
24954
+ var index = 0;
24955
+ if (transition && transition != FlowDirective.Continue) {
24956
+ index = Array.from(tasksList.keys()).indexOf(transition);
24957
+ }
24958
+ else if (currentTask) {
24959
+ index = Array.from(tasksList.values()).indexOf(currentTask) + 1;
24960
+ if (index >= tasksList.size) {
24961
+ return {
24962
+ name: FlowDirective.End,
24963
+ index: -1,
24964
+ };
24965
+ }
24966
+ }
24967
+ var taskEntry = Array.from(tasksList.entries())[index];
24968
+ return {
24969
+ index: index,
24970
+ name: taskEntry[0],
24971
+ task: taskEntry[1],
24972
+ };
24973
+ }
24974
+ /**
24975
+ * Builds the provided transition from the source node
24976
+ * @param sourceNode The node to build the transition from
24977
+ * @param transition The transition to follow
24978
+ * @param context The context in which the transition is built
24979
+ */
24980
+ function buildTransition(sourceNode, transition, context) {
24981
+ var exitAnchor = sourceNode.exitNode || sourceNode;
24982
+ if (transition.index != -1) {
24983
+ var destinationNode = buildTaskNode(__assign(__assign({}, context), { taskReference: "".concat(context.reference, "/").concat(transition.index, "/").concat(transition.name), taskName: transition.name }));
24984
+ buildEdge(context.graph, exitAnchor, destinationNode.entryNode || destinationNode, transition.label);
24985
+ }
24986
+ else if (transition.name === FlowDirective.Exit) {
24987
+ buildEdge(context.graph, exitAnchor, context.graph.exitNode, transition.label);
24988
+ }
24989
+ else if (transition.name === FlowDirective.End) {
24990
+ buildEdge(context.graph, exitAnchor, context.graph.exitNode, transition.label);
24991
+ }
24992
+ else
24993
+ throw new Error('Invalid transition');
24994
+ }
24995
+ /**
24996
+ * Builds all the possible transitions from the provided node in the provided context
24997
+ * @param sourceNode The node to build the transitions from
24998
+ * @param context The context in which the transitions are built
24999
+ */
25000
+ function buildTransitions(sourceNode, context) {
25001
+ var _a, _b;
25002
+ var transitions = [];
25003
+ var nextTransition = getNextTask(context.taskList, context.taskName);
25004
+ transitions.push(nextTransition);
25005
+ while ((_a = nextTransition === null || nextTransition === void 0 ? void 0 : nextTransition.task) === null || _a === void 0 ? void 0 : _a.if) {
25006
+ nextTransition.label = (_b = nextTransition === null || nextTransition === void 0 ? void 0 : nextTransition.task) === null || _b === void 0 ? void 0 : _b.if;
25007
+ nextTransition = getNextTask(context.taskList, nextTransition.name, FlowDirective.Continue);
25008
+ transitions.push(nextTransition);
25009
+ }
25010
+ transitions
25011
+ .filter(function (transition, index) {
25012
+ return transitions.findIndex(function (t) { return t.index === transition.index && t.name === transition.name && t.task === transition.task; }) === index;
25013
+ })
25014
+ .forEach(function (transition) { return buildTransition(sourceNode, transition, context); });
25015
+ }
25016
+ /**
25017
+ * Builds a graph representation of a task
25018
+ * @param context The context to build the graph/node for
25019
+ * @returns A graph or node for the provided context
25020
+ */
25021
+ function buildTaskNode(context) {
25022
+ var task = context.taskList.get(context.taskName);
25023
+ if (!task)
25024
+ throw new Error("Unabled to find the task '".concat(context.taskName, "' in the current context"));
25025
+ if (task.call)
25026
+ return buildCallTaskNode(task, context);
25027
+ if (task.catch)
25028
+ return buildTryCatchTaskNode(task, context);
25029
+ if (task.emit)
25030
+ return buildEmitTaskNode(task, context);
25031
+ if (task.for)
25032
+ return buildForTaskNode(task, context);
25033
+ if (task.fork)
25034
+ return buildForkTaskNode(task, context);
25035
+ if (task.listen)
25036
+ return buildListenTaskNode(task, context);
25037
+ if (task.raise)
25038
+ return buildRaiseTaskNode(task, context);
25039
+ if (task.run)
25040
+ return buildRunTaskNode(task, context);
25041
+ if (task.set)
25042
+ return buildSetTaskNode(task, context);
25043
+ if (task.switch)
25044
+ return buildSwitchTaskNode(task, context);
25045
+ if (task.wait)
25046
+ return buildWaitTaskNode(task, context);
25047
+ if (task.do)
25048
+ return buildDoTaskNode(task, context);
25049
+ throw new Error("Unable to defined task type of task named '".concat(context.taskName, "'"));
25050
+ }
25051
+ /**
25052
+ * Builds a graph node with the provided type and context
25053
+ * @param type The type of the node
25054
+ * @param context The context to build the graph node for
25055
+ * @returns A graph node for the provided context
25056
+ */
25057
+ function buildGenericTaskNode(type, context) {
25058
+ var node = {
25059
+ type: type,
25060
+ id: context.taskReference,
25061
+ label: context.taskName,
25062
+ };
25063
+ context.graph.nodes.push(node);
25064
+ buildTransitions(node, context);
25065
+ return node;
25066
+ }
25067
+ /**
25068
+ * Builds a graph node for the provided call task
25069
+ * @param task The task to build the graph node for
25070
+ * @param context The context to build the graph node for
25071
+ * @returns A graph node for the provided task
25072
+ */
25073
+ function buildCallTaskNode(task, context) {
25074
+ var node = buildGenericTaskNode(GraphNodeType.Call, context);
25075
+ // TODO: add some details about the task?
25076
+ return node;
25077
+ }
25078
+ /**
25079
+ * Builds a graph for the provided do task
25080
+ * @param task The task to build the graph for
25081
+ * @param context The context to build the graph for
25082
+ * @returns A graph for the provided task
25083
+ */
25084
+ function buildDoTaskNode(task, context) {
25085
+ var subgraph = initGraph(GraphNodeType.Do, context.taskReference, context.taskName, context.graph);
25086
+ var doContext = __assign(__assign({}, context), { graph: subgraph, reference: context.taskReference + doReference, taskList: mapTasks(task.do), taskName: null });
25087
+ buildTransitions(subgraph.entryNode, doContext);
25088
+ buildTransitions(subgraph, context);
25089
+ return subgraph;
25090
+ }
25091
+ /**
25092
+ * Builds a graph node for the provided emit task
25093
+ * @param task The task to build the graph node for
25094
+ * @param context The context to build the graph node for
25095
+ * @returns A graph node for the provided task
25096
+ */
25097
+ function buildEmitTaskNode(task, context) {
25098
+ var node = buildGenericTaskNode(GraphNodeType.Emit, context);
25099
+ // TODO: add some details about the task?
25100
+ return node;
25101
+ }
25102
+ /**
25103
+ * Builds a graph for the provided for task
25104
+ * @param task The task to build the graph for
25105
+ * @param context The context to build the graph for
25106
+ * @returns A graph for the provided task
25107
+ */
25108
+ function buildForTaskNode(task, context) {
25109
+ var subgraph = initGraph(GraphNodeType.For, context.taskReference, context.taskName, context.graph);
25110
+ var forContext = __assign(__assign({}, context), { graph: subgraph, reference: subgraph.id + forReference + doReference, taskList: mapTasks(task.do), taskName: null });
25111
+ buildTransitions(subgraph.entryNode, forContext);
25112
+ buildTransitions(subgraph, context);
25113
+ return subgraph;
25114
+ }
25115
+ /**
25116
+ * Builds a graph for the provided fork task
25117
+ * @param task The task to build the graph for
25118
+ * @param context The context to build the graph for
25119
+ * @returns A graph for the provided task
25120
+ */
25121
+ function buildForkTaskNode(task, context) {
25122
+ var _a, _b;
25123
+ var subgraph = initGraph(GraphNodeType.Fork, context.taskReference, context.taskName, context.graph);
25124
+ for (var i = 0, c = ((_a = task.fork) === null || _a === void 0 ? void 0 : _a.branches.length) || 0; i < c; i++) {
25125
+ var branchItem = (_b = task.fork) === null || _b === void 0 ? void 0 : _b.branches[i];
25126
+ if (!branchItem)
25127
+ continue;
25128
+ var branchName = Object.entries(branchItem)[0][0];
25129
+ var branchContext = __assign(__assign({}, context), { graph: subgraph, reference: "".concat(context.taskReference).concat(branchReference), taskList: mapTasks([branchItem]), taskReference: "".concat(context.taskReference).concat(branchReference, "/").concat(i, "/").concat(branchName), taskName: branchName });
25130
+ var branchNode = buildTaskNode(branchContext);
25131
+ buildEdge(subgraph, subgraph.entryNode, branchNode.entryNode || branchNode);
25132
+ buildEdge(subgraph, branchNode.exitNode || branchNode, subgraph.exitNode);
25133
+ }
25134
+ buildTransitions(subgraph, context);
25135
+ return subgraph;
25136
+ }
25137
+ /**
25138
+ * Builds a graph node for the provided listen task
25139
+ * @param task The task to build the graph node for
25140
+ * @param context The context to build the graph node for
25141
+ * @returns A graph node for the provided task
25142
+ */
25143
+ function buildListenTaskNode(task, context) {
25144
+ var node = buildGenericTaskNode(GraphNodeType.Listen, context);
25145
+ // TODO: add some details about the task?
25146
+ return node;
25147
+ }
25148
+ /**
25149
+ * Builds a graph node for the provided rasie task
25150
+ * @param task The task to build the graph node for
25151
+ * @param context The context to build the graph node for
25152
+ * @returns A graph node for the provided task
25153
+ */
25154
+ function buildRaiseTaskNode(task, context) {
25155
+ var node = buildGenericTaskNode(GraphNodeType.Raise, context);
25156
+ // TODO: add some details about the task?
25157
+ return node;
25158
+ }
25159
+ /**
25160
+ * Builds a graph node for the provided run task
25161
+ * @param task The task to build the graph node for
25162
+ * @param context The context to build the graph node for
25163
+ * @returns A graph node for the provided task
25164
+ */
25165
+ function buildRunTaskNode(task, context) {
25166
+ var node = buildGenericTaskNode(GraphNodeType.Run, context);
25167
+ // TODO: add some details about the task?
25168
+ return node;
25169
+ }
25170
+ /**
25171
+ * Builds a graph node for the provided set task
25172
+ * @param task The task to build the graph node for
25173
+ * @param context The context to build the graph node for
25174
+ * @returns A graph node for the provided task
25175
+ */
25176
+ function buildSetTaskNode(task, context) {
25177
+ var node = buildGenericTaskNode(GraphNodeType.Set, context);
25178
+ // TODO: add some details about the task?
25179
+ return node;
25180
+ }
25181
+ /**
25182
+ * Builds a graph node for the provided switch task
25183
+ * @param task The task to build the graph node for
25184
+ * @param context The context to build the graph node for
25185
+ * @returns A graph node for the provided task
25186
+ */
25187
+ function buildSwitchTaskNode(task, context) {
25188
+ var _a;
25189
+ var node = buildGenericTaskNode(GraphNodeType.Switch, context);
25190
+ var hasDefaultCase = false;
25191
+ (_a = task.switch) === null || _a === void 0 ? void 0 : _a.forEach(function (switchItem) {
25192
+ var _a = Object.entries(switchItem)[0], caseName = _a[0], switchCase = _a[1];
25193
+ if (!switchCase.when)
25194
+ hasDefaultCase = true;
25195
+ var transition = getNextTask(context.taskList, context.taskName, switchCase.then);
25196
+ transition.label = caseName;
25197
+ buildTransition(node, transition, context);
25198
+ });
25199
+ if (!hasDefaultCase) {
25200
+ buildTransitions(node, context);
25201
+ }
25202
+ return node;
25203
+ }
25204
+ /**
25205
+ * Builds a graph for the provided try/catch task
25206
+ * @param task The task to build the graph for
25207
+ * @param context The context to build the graph for
25208
+ * @returns A graph for the provided task
25209
+ */
25210
+ function buildTryCatchTaskNode(task, context) {
25211
+ var _a, _b;
25212
+ var containerSubgraph = initGraph(GraphNodeType.TryCatch, context.taskReference, context.taskName, context.graph);
25213
+ var trySubgraph = initGraph(GraphNodeType.Try, context.taskReference + tryReference, context.taskName + ' (try)', containerSubgraph);
25214
+ buildEdge(containerSubgraph, containerSubgraph.entryNode, trySubgraph.entryNode);
25215
+ var tryContext = __assign(__assign({}, context), { graph: trySubgraph, reference: trySubgraph.id, taskList: mapTasks(task.try), taskName: null });
25216
+ buildTransitions(trySubgraph.entryNode, tryContext);
25217
+ if (!((_b = (_a = task.catch) === null || _a === void 0 ? void 0 : _a.do) === null || _b === void 0 ? void 0 : _b.length)) {
25218
+ var catchNode = {
25219
+ type: GraphNodeType.Catch,
25220
+ id: context.taskReference + catchReference,
25221
+ label: context.taskName + ' (catch)',
25222
+ };
25223
+ containerSubgraph.nodes.push(catchNode);
25224
+ buildEdge(containerSubgraph, trySubgraph.exitNode, catchNode);
25225
+ buildEdge(containerSubgraph, catchNode, containerSubgraph.exitNode);
25226
+ }
25227
+ else {
25228
+ var catchSubgraph = initGraph(GraphNodeType.Catch, context.taskReference + catchReference + doReference, context.taskName + ' (catch)', containerSubgraph);
25229
+ buildEdge(containerSubgraph, trySubgraph.exitNode, catchSubgraph.entryNode);
25230
+ var catchContext = __assign(__assign({}, context), { graph: catchSubgraph, reference: catchSubgraph.id, taskList: mapTasks(task.catch.do), taskName: null });
25231
+ buildTransitions(catchSubgraph.entryNode, catchContext);
25232
+ buildEdge(containerSubgraph, catchSubgraph.exitNode, containerSubgraph.exitNode);
25233
+ }
25234
+ buildTransitions(containerSubgraph, context);
25235
+ return containerSubgraph;
25236
+ }
25237
+ /**
25238
+ * Builds a graph node for the provided wait task
25239
+ * @param task The task to build the graph node for
25240
+ * @param context The context to build the graph node for
25241
+ * @returns A graph node for the provided task
25242
+ */
25243
+ function buildWaitTaskNode(task, context) {
25244
+ var node = buildGenericTaskNode(GraphNodeType.Wait, context);
25245
+ // TODO: add some details about the task?
25246
+ return node;
25247
+ }
25248
+ /**
25249
+ * Builds an edge between two elements
25250
+ * @param graph The graph element containing the nodes
25251
+ * @param source The origin node
25252
+ * @param destination The destination node
25253
+ * @param label The edge label, if any
25254
+ */
25255
+ function buildEdge(graph, source, destination, label) {
25256
+ if (label === void 0) { label = ''; }
25257
+ var edge = graph.edges.find(function (e) { return e.sourceId === source.id && e.destinationId === destination.id; });
25258
+ if (edge) {
25259
+ if (label) {
25260
+ edge.label = edge.label + (edge.label ? ' / ' : '') + label;
25261
+ }
25262
+ return edge;
25263
+ }
25264
+ edge = {
25265
+ label: label,
25266
+ id: "".concat(source.id, "-").concat(destination.id).concat(label ? "-".concat(label) : ''),
25267
+ sourceId: source.id,
25268
+ destinationId: destination.id,
25269
+ };
25270
+ graph.edges.push(edge);
25271
+ }
25272
+
25273
+ /**
25274
+ * Adds indentation to each line of the provided code
25275
+ * @param code The code to indent
25276
+ * @returns The indented code
25277
+ */
25278
+ var indent = function (code) {
25279
+ return code
25280
+ .split('\n')
25281
+ .map(function (line) { return " ".concat(line); })
25282
+ .join('\n');
25283
+ };
25284
+ /**
25285
+ * Converts a graph to Mermaid code
25286
+ * @param graph The graph to convert
25287
+ * @returns The converted graph
25288
+ */
25289
+ function convertGraphToCode(graph) {
25290
+ var isRoot = graph.id === 'root';
25291
+ var code = "".concat(isRoot ? 'flowchart TD' : "subgraph ".concat(graph.id, " [\"").concat(graph.label || graph.id, "\"]"), "\n").concat(indent(graph.nodes.map(function (node) { return convertNodeToCode(node); }).join('\n')), "\n").concat(indent(graph.edges.map(function (edge) { return convertEdgeToCode(edge); }).join('\n')), "\n").concat(isRoot ? '' : 'end');
25292
+ return code;
25293
+ }
25294
+ /**
25295
+ * Converts a node to Mermaid code
25296
+ * @param node The node to convert
25297
+ * @returns The converted node
25298
+ */
25299
+ function convertNodeToCode(node) {
25300
+ var _a;
25301
+ var code = '';
25302
+ if ((_a = node.nodes) === null || _a === void 0 ? void 0 : _a.length) {
25303
+ code = convertGraphToCode(node);
25304
+ }
25305
+ else {
25306
+ code = node.id;
25307
+ switch (node.type) {
25308
+ case GraphNodeType.Entry:
25309
+ case GraphNodeType.Exit:
25310
+ code += ':::hidden';
25311
+ break;
25312
+ case GraphNodeType.Start:
25313
+ code += '(( ))'; // alt '@{ shape: circle, label: " "}';
25314
+ break;
25315
+ case GraphNodeType.End:
25316
+ code += '((( )))'; // alt '@{ shape: dbl-circ, label: " "}';
25317
+ break;
25318
+ default:
25319
+ code += "[\"".concat(node.label, "\"]"); // alt `@{ label: "${node.label}" }`
25320
+ }
25321
+ }
25322
+ return code;
25323
+ }
25324
+ /**
25325
+ * Converts an edge to Mermaid code
25326
+ * @param edge The edge to convert
25327
+ * @returns The converted edge
25328
+ */
25329
+ function convertEdgeToCode(edge) {
25330
+ var ignoreEndArrow = !edge.destinationId.startsWith('root') &&
25331
+ (edge.destinationId.endsWith('-entry-node') || edge.destinationId.endsWith('-exit-node'));
25332
+ var code = "".concat(edge.sourceId, " ").concat(edge.label ? "--\"".concat(edge.label, "\"") : '', "--").concat(ignoreEndArrow ? '-' : '>', " ").concat(edge.destinationId);
25333
+ return code;
25334
+ }
25335
+ /**
25336
+ * Converts the provided workflow to Mermaid code
25337
+ * @param workflow The workflow to convert
25338
+ * @returns The Mermaid diagram
25339
+ */
25340
+ function convertToMermaidCode(workflow) {
25341
+ var graph = buildGraph(workflow);
25342
+ return (convertGraphToCode(graph) +
25343
+ "\n\nclassDef hidden display: none;");
25344
+ }
25345
+ /**
25346
+ * Represents a Mermaid diagram generator for a given workflow.
25347
+ * This class takes a workflow definition and converts it into a Mermaid.js-compatible diagram.
25348
+ */
25349
+ var MermaidDiagram = /** @class */ (function () {
25350
+ function MermaidDiagram(workflow) {
25351
+ this.workflow = workflow;
25352
+ }
25353
+ /**
25354
+ * Generates the Mermaid code representation of the workflow.
25355
+ * @returns The Mermaid diagram source code as a string.
25356
+ */
25357
+ MermaidDiagram.prototype.sourceCode = function () {
25358
+ return convertToMermaidCode(this.workflow);
25359
+ };
25360
+ return MermaidDiagram;
25361
+ }());
25362
+
24817
25363
  /*
24818
25364
  * Copyright 2021-Present The Serverless Workflow Specification Authors
24819
25365
  *
@@ -24899,6 +25445,12 @@ var Workflow = /** @class */ (function (_super) {
24899
25445
  }
24900
25446
  return dump(normalized);
24901
25447
  };
25448
+ Workflow.toGraph = function (model) {
25449
+ return buildGraph(model);
25450
+ };
25451
+ Workflow.toMermaidCode = function (model) {
25452
+ return convertToMermaidCode(model);
25453
+ };
24902
25454
  /**
24903
25455
  * Serializes the workflow to YAML or JSON
24904
25456
  * @param format The format, 'yaml' or 'json', default is 'yaml'
@@ -24910,6 +25462,20 @@ var Workflow = /** @class */ (function (_super) {
24910
25462
  if (normalize === void 0) { normalize = true; }
24911
25463
  return Workflow.serialize(this, format, normalize);
24912
25464
  };
25465
+ /**
25466
+ * Creates a directed graph representation of the workflow
25467
+ * @returns A directed graph of the workflow
25468
+ */
25469
+ Workflow.prototype.toGraph = function () {
25470
+ return Workflow.toGraph(this);
25471
+ };
25472
+ /**
25473
+ * Generates the MermaidJS code corresponding to the workflow
25474
+ * @returns The MermaidJS code
25475
+ */
25476
+ Workflow.prototype.toMermaidCode = function () {
25477
+ return Workflow.toMermaidCode(this);
25478
+ };
24913
25479
  return Workflow;
24914
25480
  }(ObjectHydrator));
24915
25481
  var _Workflow = Workflow;
@@ -30718,5 +31284,5 @@ var specification = /*#__PURE__*/Object.freeze({
30718
31284
  __proto__: null
30719
31285
  });
30720
31286
 
30721
- export { Classes, specification as Specification, allEventConsumptionStrategyBuilder, allEventConsumptionStrategyConfigurationBuilder, anyEventConsumptionStrategyBuilder, anyEventConsumptionStrategyConfigurationBuilder, anyEventConsumptionStrategyUntilBuilder, anyEventUntilConsumedBuilder, asyncApiArgumentsBuilder, authenticationPolicyBuilder, authenticationPolicyReferenceBuilder, basicAuthenticationPolicyBuilder, basicAuthenticationPolicyConfigurationBuilder, basicAuthenticationPropertiesBuilder, bearerAuthenticationPolicyBuilder, bearerAuthenticationPolicyConfigurationBuilder, bearerAuthenticationPropertiesBuilder, callAsyncAPIBuilder, callFunctionBuilder, callGRPCBuilder, callHTTPBuilder, callOpenAPIBuilder, callTaskBuilder, catalogBuilder, catchErrorsBuilder, constantBackoffBuilder, containerBuilder, containerEnvironmentBuilder, containerLifetimeBuilder, containerPortsBuilder, containerVolumesBuilder, digestAuthenticationPolicyBuilder, digestAuthenticationPolicyConfigurationBuilder, digestAuthenticationPropertiesBuilder, doTaskBuilder, documentBuilder, durationBuilder, durationInlineBuilder, emitEventDefinitionBuilder, emitEventWithBuilder, emitTaskBuilder, emitTaskConfigurationBuilder, endpointBuilder, endpointConfigurationBuilder, endpointUriBuilder, errorBuilder, errorFilterBuilder, errorInstanceBuilder, errorTypeBuilder, eventConsumptionStrategyBuilder, eventDataBuilder, eventDataschemaBuilder, eventFilterBuilder, eventFilterCorrelateBuilder, eventSourceBuilder, eventTimeBuilder, exponentialBackOffBuilder, exportAsBuilder, exportBuilder, extensionBuilder, extensionItemBuilder, externalResourceBuilder, externalScriptBuilder, flowDirectiveBuilder, forTaskBuilder, forTaskConfigurationBuilder, forkTaskBuilder, forkTaskConfigurationBuilder, functionArgumentsBuilder, gRPCArgumentsBuilder, hTTPArgumentsBuilder, hTTPBodyBuilder, hTTPHeadersBuilder, hTTPQueryBuilder, inlineScriptBuilder, inputBuilder, inputFromBuilder, linearBackoffBuilder, listenTaskBuilder, listenTaskConfigurationBuilder, oAuth2AutenthicationDataAudiencesBuilder, oAuth2AutenthicationDataBuilder, oAuth2AutenthicationDataClientBuilder, oAuth2AutenthicationDataScopesBuilder, oAuth2AuthenticationPolicyBuilder, oAuth2AuthenticationPolicyConfigurationBuilder, oAuth2AuthenticationPropertiesEndpointsBuilder, oAuth2ConnectAuthenticationPropertiesBuilder, oAuth2IssuersBuilder, oAuth2TokenDefinitionBuilder, oAuth2TokenRequestBuilder, oneEventConsumptionStrategyBuilder, openAPIArgumentsBuilder, openIdConnectAuthenticationPolicyBuilder, openIdConnectAuthenticationPolicyConfigurationBuilder, openIdConnectAuthenticationPropertiesBuilder, outputAsBuilder, outputBuilder, raiseTaskBuilder, raiseTaskConfigurationBuilder, raiseTaskErrorBuilder, referenceableAuthenticationPolicyBuilder, retryBackoffBuilder, retryLimitAttemptBuilder, retryLimitBuilder, retryPolicyBuilder, retryPolicyJitterBuilder, runContainerBuilder, runScriptBuilder, runShellBuilder, runTaskBuilder, runTaskConfigurationBuilder, runWorkflowBuilder, runtimeExpressionBuilder, scheduleBuilder, schemaBuilder, schemaExternalBuilder, schemaInlineBuilder, scriptBuilder, secretBasedAuthenticationPolicyBuilder, setTaskBuilder, setTaskConfigurationBuilder, shellArgumentsBuilder, shellBuilder, shellEnvironmentBuilder, subflowConfigurationBuilder, subflowInputBuilder, subscriptionIteratorBuilder, switchCaseBuilder, switchItemBuilder, switchTaskBuilder, switchTaskConfigurationBuilder, taskBaseBuilder, taskBaseIfBuilder, taskBuilder, taskItemBuilder, taskListBuilder, taskMetadataBuilder, taskTimeoutBuilder, timeoutBuilder, tryTaskBuilder, tryTaskCatchBuilder, tryTaskCatchRetryBuilder, uriTemplateBuilder, useAuthenticationsBuilder, useBuilder, useCatalogsBuilder, useErrorsBuilder, useExtensionsBuilder, useFunctionsBuilder, useRetriesBuilder, useSecretsBuilder, useTimeoutsBuilder, validate, waitTaskBuilder, withEventBuilder, withGRPCArgumentsBuilder, withGRPCServiceBuilder, withOpenAPIParametersBuilder, workflowBuilder, workflowMetadataBuilder, workflowTagsBuilder, workflowTimeoutBuilder };
31287
+ export { Classes, GraphNodeType, MermaidDiagram, specification as Specification, allEventConsumptionStrategyBuilder, allEventConsumptionStrategyConfigurationBuilder, anyEventConsumptionStrategyBuilder, anyEventConsumptionStrategyConfigurationBuilder, anyEventConsumptionStrategyUntilBuilder, anyEventUntilConsumedBuilder, asyncApiArgumentsBuilder, authenticationPolicyBuilder, authenticationPolicyReferenceBuilder, basicAuthenticationPolicyBuilder, basicAuthenticationPolicyConfigurationBuilder, basicAuthenticationPropertiesBuilder, bearerAuthenticationPolicyBuilder, bearerAuthenticationPolicyConfigurationBuilder, bearerAuthenticationPropertiesBuilder, buildGraph, callAsyncAPIBuilder, callFunctionBuilder, callGRPCBuilder, callHTTPBuilder, callOpenAPIBuilder, callTaskBuilder, catalogBuilder, catchErrorsBuilder, constantBackoffBuilder, containerBuilder, containerEnvironmentBuilder, containerLifetimeBuilder, containerPortsBuilder, containerVolumesBuilder, convertToMermaidCode, digestAuthenticationPolicyBuilder, digestAuthenticationPolicyConfigurationBuilder, digestAuthenticationPropertiesBuilder, doTaskBuilder, documentBuilder, durationBuilder, durationInlineBuilder, emitEventDefinitionBuilder, emitEventWithBuilder, emitTaskBuilder, emitTaskConfigurationBuilder, endpointBuilder, endpointConfigurationBuilder, endpointUriBuilder, errorBuilder, errorFilterBuilder, errorInstanceBuilder, errorTypeBuilder, eventConsumptionStrategyBuilder, eventDataBuilder, eventDataschemaBuilder, eventFilterBuilder, eventFilterCorrelateBuilder, eventSourceBuilder, eventTimeBuilder, exponentialBackOffBuilder, exportAsBuilder, exportBuilder, extensionBuilder, extensionItemBuilder, externalResourceBuilder, externalScriptBuilder, flowDirectiveBuilder, forTaskBuilder, forTaskConfigurationBuilder, forkTaskBuilder, forkTaskConfigurationBuilder, functionArgumentsBuilder, gRPCArgumentsBuilder, hTTPArgumentsBuilder, hTTPBodyBuilder, hTTPHeadersBuilder, hTTPQueryBuilder, inlineScriptBuilder, inputBuilder, inputFromBuilder, linearBackoffBuilder, listenTaskBuilder, listenTaskConfigurationBuilder, oAuth2AutenthicationDataAudiencesBuilder, oAuth2AutenthicationDataBuilder, oAuth2AutenthicationDataClientBuilder, oAuth2AutenthicationDataScopesBuilder, oAuth2AuthenticationPolicyBuilder, oAuth2AuthenticationPolicyConfigurationBuilder, oAuth2AuthenticationPropertiesEndpointsBuilder, oAuth2ConnectAuthenticationPropertiesBuilder, oAuth2IssuersBuilder, oAuth2TokenDefinitionBuilder, oAuth2TokenRequestBuilder, oneEventConsumptionStrategyBuilder, openAPIArgumentsBuilder, openIdConnectAuthenticationPolicyBuilder, openIdConnectAuthenticationPolicyConfigurationBuilder, openIdConnectAuthenticationPropertiesBuilder, outputAsBuilder, outputBuilder, raiseTaskBuilder, raiseTaskConfigurationBuilder, raiseTaskErrorBuilder, referenceableAuthenticationPolicyBuilder, retryBackoffBuilder, retryLimitAttemptBuilder, retryLimitBuilder, retryPolicyBuilder, retryPolicyJitterBuilder, runContainerBuilder, runScriptBuilder, runShellBuilder, runTaskBuilder, runTaskConfigurationBuilder, runWorkflowBuilder, runtimeExpressionBuilder, scheduleBuilder, schemaBuilder, schemaExternalBuilder, schemaInlineBuilder, scriptBuilder, secretBasedAuthenticationPolicyBuilder, setTaskBuilder, setTaskConfigurationBuilder, shellArgumentsBuilder, shellBuilder, shellEnvironmentBuilder, subflowConfigurationBuilder, subflowInputBuilder, subscriptionIteratorBuilder, switchCaseBuilder, switchItemBuilder, switchTaskBuilder, switchTaskConfigurationBuilder, taskBaseBuilder, taskBaseIfBuilder, taskBuilder, taskItemBuilder, taskListBuilder, taskMetadataBuilder, taskTimeoutBuilder, timeoutBuilder, tryTaskBuilder, tryTaskCatchBuilder, tryTaskCatchRetryBuilder, uriTemplateBuilder, useAuthenticationsBuilder, useBuilder, useCatalogsBuilder, useErrorsBuilder, useExtensionsBuilder, useFunctionsBuilder, useRetriesBuilder, useSecretsBuilder, useTimeoutsBuilder, validate, waitTaskBuilder, withEventBuilder, withGRPCArgumentsBuilder, withGRPCServiceBuilder, withOpenAPIParametersBuilder, workflowBuilder, workflowMetadataBuilder, workflowTagsBuilder, workflowTimeoutBuilder };
30722
31288
  //# sourceMappingURL=index.esm.js.map