@@ -7,8 +7,8 @@ Provides the Typescript API/SPI for the [Serverless Workflow Specification](http
7
7
8
8
With the SDK you can:
9
9
* Parse workflow JSON and YAML definitions
10
- * ( _ WIP _ ) Programmatically build workflow definitions
11
- * ( _ WIP _ ) Validate workflow definitions
10
+ * Programmatically build workflow definitions
11
+ * Validate workflow definitions
12
12
13
13
## Getting Started
14
14
@@ -20,7 +20,7 @@ To build the project and run tests locally:
20
20
``` sh
21
21
git clone https://github.com/serverlessworkflow/sdk-typescript.git
22
22
cd sdk-typescript
23
- npm install && npm run test
23
+ npm install && npm run update-code-base && npm run test
24
24
```
25
25
26
26
@@ -53,28 +53,28 @@ It will create a symbolic link from globally-installed `sdk-typescript` to `node
53
53
#### Create Workflow using builder API
54
54
55
55
``` typescript
56
-
57
- const workflow = new WorkflowBuilder ( )
58
- .withId ( " helloworld " )
59
- .withVersion ( " 1.0 " )
60
- .withName ( " Hello World Workflow " )
61
- .withDescription ( " Inject Hello World " )
62
- .withStart ( " Hello State " )
63
- . withStates ([ new InjectStateBuilder ( )
64
- .withName (" Hello State" )
65
- .withData ({
56
+ const workflow = workflowJsonBuilder ()
57
+ . id ( " helloworld " )
58
+ .version ( " 1.0 " )
59
+ .name ( " Hello World Workflow " )
60
+ .description ( " Inject Hello World" )
61
+ .start ( " Hello State " )
62
+ .states ([ injectstateBuilder ( )
63
+ . type ( " inject " )
64
+ .name (" Hello State" )
65
+ .data ({
66
66
" result" : " Hello World!"
67
67
})
68
- .withEnd (true ).build ()])
69
- .build ();
68
+ .end (true ).build ()])
69
+ .build ()) ;
70
70
```
71
71
72
72
#### Load a file JSON/YAML to a Workflow instance
73
73
74
74
``` typescript
75
- const workflow = BaseWorkflow . fromSource (source )
75
+ const workflow = WorkflowConverter . fromString (source )
76
76
```
77
- Where ` source ` is the file location .
77
+ Where ` source ` is a JSON or a YAML string .
78
78
79
79
80
80
@@ -83,52 +83,43 @@ Where `source` is the file location.
83
83
Having the following workflow instance:
84
84
85
85
``` typescript
86
- const workflow = new WorkflowBuilder ()
87
- .withId (" helloworld" )
88
- .withVersion (" 1.0" )
89
- .withName (" Hello World Workflow" )
90
- .withDescription (" Inject Hello World" )
91
- .withStart (" Hello State" )
92
- .withStates ([new InjectStateBuilder ()
93
- .withName (" Hello State" )
94
- .withData ({
86
+ const workflow = workflowJsonBuilder ()
87
+ .id (" helloworld" )
88
+ .version (" 1.0" )
89
+ .name (" Hello World Workflow" )
90
+ .description (" Inject Hello World" )
91
+ .start (" Hello State" )
92
+ .states ([injectstateBuilder ()
93
+ .type (" inject" )
94
+ .name (" Hello State" )
95
+ .data ({
95
96
" result" : " Hello World!"
96
97
})
97
- .withEnd (true ).build ()])
98
- .build ();
98
+ .end (true ).build ()])
99
+ .build ()) ;
99
100
```
100
101
101
102
You can convert it to its string representation in JSON or YAML format
102
- by using the static methods ` toJSON ` or ` toYAML ` respectively:
103
+ by using the static methods ` toJson ` or ` toYaml ` respectively:
103
104
104
105
``` typescript
105
- const workflowAsJSON = BaseWorkflow . toJSON (workflow );
106
+ const workflowAsJson = WorkflowConverter . toJson (workflow );
106
107
```
107
108
108
109
``` typescript
109
- const workflowAsYAML = BaseWorkflow . toYAML (workflow );
110
+ const workflowAsYaml = WorkflowConverter . toYaml (workflow );
110
111
```
111
112
112
113
#### Validate workflow definitions
113
114
114
115
The sdk provides a way to validate if a workflow object is compliant with the serverlessworkflow specification.
115
116
116
- ` WorkflowValidator ` class provides two methods:
117
-
118
- - ` isValid(): boolean `
119
-
120
- ``` typescript
121
-
122
- const isValid = new WorkflowValidator (workflow ).isValid ();
123
-
124
- ```
125
-
126
- - ` validate(): ValidationErrors `
117
+ ` validators ` provides a map of validation functions:
127
118
128
119
``` typescript
129
-
130
- const validationErrors = new WorkflowValidator ( workflow ). validate ();
131
- validationErrors . errors (). forEach ( error => console . error ( error . message ()))
132
-
133
-
134
- ```
120
+ const validate = validators . get ( ' WorkflowJson ' );
121
+ const isValid = validate (workflow );
122
+ if ( ! isValid ) {
123
+ validate . errors . forEach ( error => console . error ( error . message ));
124
+ }
125
+ ```
0 commit comments