Skip to content

Commit 296865f

Browse files
committed
Examples
- updated definitions generation, exporting types from .ts and without namespace, accessed via 'Specification' instead of 'ServerlessWorkflow', adapted rest of the code base - added browser & node examples - added 'Create Workflow using object literals' in README Signed-off-by: JBBianchi <[email protected]>
1 parent 72cf8ac commit 296865f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+307
-200
lines changed

README.md

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,13 @@
44

55
Provides the Typescript API/SPI for the [Serverless Workflow Specification](https://github.com/serverlessworkflow/specification)
66

7-
87
With the SDK you can:
98
* Parse workflow JSON and YAML definitions
109
* Programmatically build workflow definitions
1110
* Validate workflow definitions
1211

13-
## Getting Started
1412

13+
## Getting Started
1514

1615
### Building locally
1716

@@ -53,7 +52,7 @@ It will create a symbolic link from globally-installed `sdk-typescript` to `node
5352
#### Create Workflow using builder API
5453

5554
```typescript
56-
const workflow = workflowBuilder()
55+
const workflow: ServerlessWorkflow.Workflow = workflowBuilder()
5756
.id("helloworld")
5857
.version("1.0")
5958
.name("Hello World Workflow")
@@ -70,6 +69,30 @@ const workflow = workflowBuilder()
7069
.build();
7170
```
7271

72+
73+
#### Create Workflow using object literals
74+
75+
```typescript
76+
const workflow: ServerlessWorkflow.Workflow = {
77+
id: 'helloworld',
78+
version: '1.0',
79+
name: 'Hello World Workflow',
80+
description: 'Inject Hello World',
81+
start: 'Hello State',
82+
states: [
83+
{
84+
type: 'inject',
85+
name: 'Hello State',
86+
end: true,
87+
data: {
88+
result: "Hello World!"
89+
}
90+
} as ServerlessWorkflow.Injectstate
91+
]
92+
};
93+
```
94+
95+
7396
#### Load a file JSON/YAML to a Workflow instance
7497

7598
```typescript
@@ -78,7 +101,6 @@ const workflow = workflowBuilder()
78101
Where `source` is a JSON or a YAML string.
79102

80103

81-
82104
#### Parse a Workflow instance to JSON/YAML
83105

84106
Having the following workflow instance:
@@ -112,6 +134,7 @@ by using the static methods `toJson` or `toYaml` respectively:
112134
const workflowAsYaml = WorkflowConverter.toYaml(workflow);
113135
```
114136

137+
115138
#### Validate workflow definitions
116139

117140
The sdk provides a way to validate if a workflow object is compliant with the serverlessworkflow specification.

examples/browser/index.html

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Serveless Workflow JS SDK</title>
6+
<base href="/">
7+
<meta name="viewport" content="width=device-width, initial-scale=1">
8+
</head>
9+
<body>
10+
<!--
11+
Run http-server from the project root then navigate to http://localhost:8080/examples/browser/index.html
12+
-->
13+
<div id="output"></div>
14+
<script src="../../dist/umd/index.umd.js"></script>
15+
<script type="text/javascript">
16+
(() => {
17+
const { workflowBuilder, injectstateBuilder } = serverWorkflowSdk;
18+
const outputDiv = document.getElementById('output');
19+
const workflow = workflowBuilder()
20+
.id("helloworld")
21+
.version("1.0")
22+
.name("Hello World Workflow")
23+
.description("Inject Hello World")
24+
.start("Hello State")
25+
.states([
26+
injectstateBuilder()
27+
.name("Hello State")
28+
.data({
29+
"result": "Hello World!"
30+
})
31+
.end(true)
32+
.build()
33+
])
34+
.build();
35+
outputDiv.innerHTML = workflow.id;
36+
})();
37+
</script>
38+
</body>
39+
</html>

examples/node/index.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const { workflowBuilder, injectstateBuilder } = require('../../dist');
2+
const workflow = workflowBuilder()
3+
.id("helloworld")
4+
.version("1.0")
5+
.name("Hello World Workflow")
6+
.description("Inject Hello World")
7+
.start("Hello State")
8+
.states([
9+
injectstateBuilder()
10+
.name("Hello State")
11+
.data({
12+
"result": "Hello World!"
13+
})
14+
.end(true)
15+
.build()
16+
])
17+
.build();
18+
console.log(workflow.id);

examples/node/index.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { workflowBuilder, injectstateBuilder, Specification } from '../../dist';
2+
const workflow: Specification.Workflow = workflowBuilder()
3+
.id("helloworld")
4+
.version("1.0")
5+
.name("Hello World Workflow")
6+
.description("Inject Hello World")
7+
.start("Hello State")
8+
.states([
9+
injectstateBuilder()
10+
.name("Hello State")
11+
.data({
12+
"result": "Hello World!"
13+
})
14+
.end(true)
15+
.build()
16+
])
17+
.build();
18+
console.log(workflow.id);

examples/node/tsconfig.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "../../tsconfig.base.json"
3+
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"test": "npx tsc --project ./tests/tsconfig.json && npx jasmine --config=./tests/support/jasmine.json",
2121
"prebuild": "npx rimraf dist",
2222
"build": "npx rollup -c rollup.config.ts",
23-
"postbuild":"npx shx cp ./package.json ./README.md ./LICENSE ./dist/",
23+
"postbuild": "npx shx cp ./package.json ./README.md ./LICENSE ./dist/",
2424
"verify-publish-directory": "node -e 'if (!process.cwd().endsWith(\"dist\")) { console.error(\"Packaging/Publishing should be done from ./dist/\"); process.exitCode = 1; } process.exit();'",
2525
"prepack": "npm run verify-publish-directory",
2626
"prepublish": "npm run verify-publish-directory"

rollup.config.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ export default {
1717
],
1818
plugins: [
1919
json(),
20-
typescript({ useTsconfigDeclarationDir: true }),
20+
typescript({
21+
useTsconfigDeclarationDir: true,
22+
exclude: []
23+
}),
2124
commonjs(),
2225
resolve(),
2326
sourceMaps(),

src/lib/builders/action-builder.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { DefinedError } from 'ajv';
22
import { Builder, builder } from '../builder';
3+
import { Specification } from '../definitions';
34
import { validators } from '../validators';
4-
import Action = ServerlessWorkflow.Action;
55

6-
export function actionValidator(data: Action): (() => Action) {
6+
export function actionValidator(data: Specification.Action): (() => Specification.Action) {
77
return () => {
88
const validate = validators.get('Action');
99
// TODO: ignore validation if no validator or throw ?
@@ -17,6 +17,6 @@ export function actionValidator(data: Action): (() => Action) {
1717
};
1818
}
1919

20-
export function actionBuilder(): Builder<Action> {
21-
return builder<Action>(actionValidator);
20+
export function actionBuilder(): Builder<Specification.Action> {
21+
return builder<Specification.Action>(actionValidator);
2222
}

src/lib/builders/actiondatafilter-builder.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { DefinedError } from 'ajv';
22
import { Builder, builder } from '../builder';
3+
import { Specification } from '../definitions';
34
import { validators } from '../validators';
4-
import Actiondatafilter = ServerlessWorkflow.Actiondatafilter;
55

6-
export function actiondatafilterValidator(data: Actiondatafilter): (() => Actiondatafilter) {
6+
export function actiondatafilterValidator(data: Specification.Actiondatafilter): (() => Specification.Actiondatafilter) {
77
return () => {
88
const validate = validators.get('Actiondatafilter');
99
// TODO: ignore validation if no validator or throw ?
@@ -17,6 +17,6 @@ export function actiondatafilterValidator(data: Actiondatafilter): (() => Action
1717
};
1818
}
1919

20-
export function actiondatafilterBuilder(): Builder<Actiondatafilter> {
21-
return builder<Actiondatafilter>(actiondatafilterValidator);
20+
export function actiondatafilterBuilder(): Builder<Specification.Actiondatafilter> {
21+
return builder<Specification.Actiondatafilter>(actiondatafilterValidator);
2222
}

src/lib/builders/branch-builder.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { DefinedError } from 'ajv';
22
import { Builder, builder } from '../builder';
3+
import { Specification } from '../definitions';
34
import { validators } from '../validators';
4-
import Branch = ServerlessWorkflow.Branch;
55

6-
export function branchValidator(data: Branch): (() => Branch) {
6+
export function branchValidator(data: Specification.Branch): (() => Specification.Branch) {
77
return () => {
88
const validate = validators.get('Branch');
99
// TODO: ignore validation if no validator or throw ?
@@ -17,6 +17,6 @@ export function branchValidator(data: Branch): (() => Branch) {
1717
};
1818
}
1919

20-
export function branchBuilder(): Builder<Branch> {
21-
return builder<Branch>(branchValidator);
20+
export function branchBuilder(): Builder<Specification.Branch> {
21+
return builder<Specification.Branch>(branchValidator);
2222
}

0 commit comments

Comments
 (0)