1
1
![ Node CI] ( https://github.com/serverlessworkflow/sdk-typescript/workflows/Node%20CI/badge.svg ) [ ![ Gitpod ready-to-code] ( https://img.shields.io/badge/Gitpod-ready--to--code-blue?logo=gitpod )] ( https://gitpod.io/#https://github.com/serverlessworkflow/sdk-typescript )
2
2
3
+ - [ Serverless Workflow Specification - TypeScript SDK] ( #serverless-workflow-specification---typescript-sdk )
4
+ - [ Status] ( #status )
5
+ - [ SDK Structure] ( #sdk-structure )
6
+ - [ Types and Interfaces] ( #types-and-interfaces )
7
+ - [ Classes] ( #classes )
8
+ - [ Fluent Builders] ( #fluent-builders )
9
+ - [ Validation Function] ( #validation-function )
10
+ - [ Other tools] ( #other-tools )
11
+ - [ Getting Started] ( #getting-started )
12
+ - [ Installation] ( #installation )
13
+ - [ Usage] ( #usage )
14
+ - [ Create a Workflow Definition from YAML or JSON] ( #create-a-workflow-definition-from-yaml-or-json )
15
+ - [ Create a Workflow Definition by Casting an Object] ( #create-a-workflow-definition-by-casting-an-object )
16
+ - [ Create a Workflow Definition Using a Class Constructor] ( #create-a-workflow-definition-using-a-class-constructor )
17
+ - [ Create a Workflow Definition Using the Builder API] ( #create-a-workflow-definition-using-the-builder-api )
18
+ - [ Serialize a Workflow Definition to YAML or JSON] ( #serialize-a-workflow-definition-to-yaml-or-json )
19
+ - [ Validate Workflow Definitions] ( #validate-workflow-definitions )
20
+ - [ Generate a directed graph] ( #generate-a-directed-graph )
21
+ - [ Generate a MermaidJS flowchart] ( #generate-a-mermaidjs-flowchart )
22
+ - [ Building Locally] ( #building-locally )
23
+
3
24
# Serverless Workflow Specification - TypeScript SDK
4
25
5
26
This SDK provides a TypeScript API for working with the [ Serverless Workflow Specification] ( https://github.com/serverlessworkflow/specification ) .
@@ -14,7 +35,7 @@ The npm [`@serverlessworkflow/sdk`](https://www.npmjs.com/package/@serverlesswor
14
35
15
36
| Latest Releases | Conformance to Spec Version |
16
37
| :---: | :---: |
17
- | [ v1.0.0. \* ] ( https://github.com/serverlessworkflow/sdk-typescript/releases/ ) | [ v1.0.0] ( https://github.com/serverlessworkflow/specification ) |
38
+ | [ v1.0.\* ] ( https://github.com/serverlessworkflow/sdk-typescript/releases/ ) | [ v1.0.0] ( https://github.com/serverlessworkflow/specification ) |
18
39
19
40
> [ !WARNING]
20
41
> Previous versions of the SDK were published with a typo in the scope:
@@ -56,6 +77,9 @@ The SDK includes a validation function to check if objects conform to the expect
56
77
57
78
The ` validate ` function is directly exported and can be used as ` validate('Workflow', workflowObject) ` .
58
79
80
+ ### Other Tools
81
+ The SDK also ships tools to build directed graph and MermaidJS flowcharts from a workflow.
82
+
59
83
## Getting Started
60
84
61
85
### Installation
@@ -223,7 +247,7 @@ Validation can be achieved in two ways: via the `validate` function or the insta
223
247
``` typescript
224
248
import { Classes , validate } from ' @serverlessworkflow/sdk' ;
225
249
226
- // const workflowDefinition = <Your preferred method>;
250
+ const workflowDefinition = /* <Your preferred method> */ ;
227
251
try {
228
252
if (workflowDefinition instanceof Classes .Workflow ) {
229
253
workflowDefinition .validate ();
@@ -237,6 +261,91 @@ catch (ex) {
237
261
}
238
262
```
239
263
264
+ #### Generate a directed graph
265
+ A [ directed graph] ( https://en.wikipedia.org/wiki/Directed_graph ) of a workflow can be generated using the ` buildGraph ` function:
266
+
267
+ ``` typescript
268
+ import { buildGraph } from ' @serverlessworkflow/sdk' ;
269
+
270
+ const workflowDefinition = {
271
+ document: {
272
+ dsl: ' 1.0.0' ,
273
+ name: ' using-plain-object' ,
274
+ version: ' 1.0.0' ,
275
+ namespace: ' default' ,
276
+ },
277
+ do: [
278
+ {
279
+ step1: {
280
+ set: {
281
+ variable: ' my first workflow' ,
282
+ },
283
+ },
284
+ },
285
+ ],
286
+ };
287
+ const graph = buildGraph (workflowDefinition );
288
+ /* {
289
+ id: 'root',
290
+ type: 'root',
291
+ label: undefined,
292
+ parent: null,
293
+ nodes: [...], // length 3 - root entry node, step1 node, root exit node
294
+ edges: [...], // length 2 - entry to step1, step1 to exit
295
+ entryNode: {...}, // root entry node
296
+ exitNode: {...} // root exit node
297
+ }*/
298
+ ```
299
+
300
+ #### Generate a MermaidJS flowchart
301
+ Generating a [ MermaidJS] ( https://mermaid.js.org/ ) flowchart can be achieved in two ways: using the ` convertToMermaidCode ` or the legacy ` MermaidDiagram ` class.
302
+
303
+ ``` typescript
304
+ import { convertToMermaidCode , MermaidDiagram } from ' @serverlessworkflow/sdk' ;
305
+
306
+ const workflowDefinition = {
307
+ document: {
308
+ dsl: ' 1.0.0' ,
309
+ name: ' using-plain-object' ,
310
+ version: ' 1.0.0' ,
311
+ namespace: ' default' ,
312
+ },
313
+ do: [
314
+ {
315
+ step1: {
316
+ set: {
317
+ variable: ' my first workflow' ,
318
+ },
319
+ },
320
+ },
321
+ ],
322
+ };
323
+ const mermaidCode = convertToMermaidCode (workflowDefinition ) /* or new MermaidDiagram(workflowDefinition).sourceCode() */ ;
324
+ /*
325
+ flowchart TD
326
+ root-entry-node(( ))
327
+ root-exit-node((( )))
328
+ /do/0/step1["step1"]
329
+ /do/0/step1 --> root-exit-node
330
+ root-entry-node --> /do/0/step1
331
+
332
+
333
+ classDef hidden display: none;
334
+ */
335
+ ```
336
+
337
+ ``` mermaid
338
+ flowchart TD
339
+ root-entry-node(( ))
340
+ root-exit-node((( )))
341
+ /do/0/step1["step1"]
342
+ /do/0/step1 --> root-exit-node
343
+ root-entry-node --> /do/0/step1
344
+
345
+
346
+ classDef hidden display: none;
347
+ ```
348
+
240
349
### Building Locally
241
350
242
351
To build the project and run tests locally, use the following commands:
0 commit comments