Skip to content

Commit fc5defd

Browse files
committed
Added examples
- Readded browser and node examples - Added call async api/grpc/function tests Signed-off-by: Jean-Baptiste Bianchi <[email protected]>
1 parent 3c9501c commit fc5defd

13 files changed

+364
-30
lines changed

README.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ The npm [`@serverlessworkflow/sdk`](https://www.npmjs.com/package/@serverlesswor
1414

1515
| Latest Releases | Conformance to Spec Version |
1616
| :---: | :---: |
17-
| [v1.0.0-alpha2.\*](https://github.com/serverlessworkflow/sdk-typescript/releases/) | [v1.0.0-alpha2](https://github.com/serverlessworkflow/specification) |
17+
| [v1.0.0-alpha5.\*](https://github.com/serverlessworkflow/sdk-typescript/releases/) | [v1.0.0-alpha5](https://github.com/serverlessworkflow/specification) |
1818

1919
> [!WARNING]
2020
> Previous versions of the SDK were published with a typo in the scope:
@@ -58,7 +58,7 @@ The `validate` function is directly exported and can be used as `validate('Workf
5858

5959
### Installation
6060
> [!NOTE]
61-
> Version v1.0.0-alpha2.\* has not been released yet.
61+
> Version v1.0.0-alpha5.\* has not been released yet.
6262
6363
```sh
6464
npm install @serverlessworkflow/sdk
@@ -75,7 +75,7 @@ import { Classes } from '@serverlessworkflow/sdk';
7575
// const text = await fetch('https://myserver.com/my-workflow-definition.json');
7676
const text = `
7777
document:
78-
dsl: 1.0.0-alpha2
78+
dsl: 1.0.0-alpha5
7979
name: test
8080
version: 1.0.0
8181
namespace: default
@@ -96,7 +96,7 @@ import { Classes, Specification, validate } from '@serverlessworkflow/sdk';
9696
// Simply cast an object:
9797
const workflowDefinition = {
9898
document: {
99-
dsl: '1.0.0-alpha2',
99+
dsl: '1.0.0-alpha5',
100100
name: 'test',
101101
version: '1.0.0',
102102
namespace: 'default',
@@ -132,7 +132,7 @@ import { Classes, validate } from '@serverlessworkflow/sdk';
132132
// Simply use the constructor
133133
const workflowDefinition = new Classes.Workflow({
134134
document: {
135-
dsl: '1.0.0-alpha2',
135+
dsl: '1.0.0-alpha5',
136136
name: 'test',
137137
version: '1.0.0',
138138
namespace: 'default',
@@ -175,7 +175,7 @@ import { documentBuilder, setTaskBuilder, taskListBuilder, workflowBuilder } fro
175175
const workflowDefinition = workflowBuilder(/*workflowDefinitionObject*/)
176176
.document(
177177
documentBuilder()
178-
.dsl('1.0.0-alpha2')
178+
.dsl('1.0.0-alpha5')
179179
.name('test')
180180
.version('1.0.0')
181181
.namespace('default')

examples/browser/index.html

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<!doctype html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="utf-8">
6+
<title>Serveless Workflow</title>
7+
<base href="/">
8+
<meta content="width=device-width, initial-scale=1" name="viewport">
9+
</head>
10+
11+
<body>
12+
<div id="output"></div>
13+
<script src="../../dist/umd/index.umd.js"></script>
14+
<script type="text/javascript">
15+
(() => {
16+
const { Classes: { Workflow } } = serverWorkflowSdk;
17+
const workflow = new Workflow({
18+
document: {
19+
dsl: '1.0.0-alpha5',
20+
name: 'test',
21+
version: '1.0.0',
22+
namespace: 'default',
23+
},
24+
do: [
25+
{
26+
step1: {
27+
set: {
28+
variable: 'my first workflow',
29+
},
30+
},
31+
},
32+
],
33+
});
34+
try {
35+
workflow.validate();
36+
document.getElementById('output').innerHTML = workflow.serialize('json');
37+
} catch (ex) {
38+
console.error('Invalid workflow', ex);
39+
}
40+
})();
41+
</script>
42+
</body>
43+
44+
</html>

examples/node/index.ts

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2021-Present The Serverless Workflow Specification Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
import { Classes } from '../../dist';
17+
const { Workflow } = Classes;
18+
19+
const workflow = new Workflow({
20+
document: {
21+
dsl: '1.0.0-alpha5',
22+
name: 'test',
23+
version: '1.0.0',
24+
namespace: 'default',
25+
},
26+
do: [
27+
{
28+
step1: {
29+
set: {
30+
variable: 'my first workflow',
31+
},
32+
},
33+
},
34+
],
35+
});
36+
37+
try {
38+
workflow.validate();
39+
console.log(workflow.serialize('json'));
40+
} catch (ex) {
41+
console.error('Invalid workflow', ex);
42+
}

examples/node/tsconfig.json

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

jest.config.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
1+
/** @type {import('ts-jest/dist/types').JestConfigWithTsJest} */
22
module.exports = {
33
preset: 'ts-jest',
44
testEnvironment: 'node',
55
testPathIgnorePatterns: [".d.ts", ".js"],
6-
modulePathIgnorePatterns: ['<rootDir>/dist/'],
6+
modulePathIgnorePatterns: ['<rootDir>/dist/', '<rootDir>/out-tsc/'],
77
};

package-lock.json

+14-11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"verify-publish-directory": "node -e 'if (!process.cwd().endsWith(\"dist\")) { console.error(\"Packaging/Publishing should be done from ./dist/\"); process.exitCode = 1; } process.exit();'",
3333
"prepack": "npm run verify-publish-directory",
3434
"prepublishOnly": "npm run verify-publish-directory",
35-
"prepublish": "husky install"
35+
"prepublish": "husky"
3636
},
3737
"dependencies": {
3838
"ajv": "^8.17.1",

src/lib/generated/classes/workflow.ts

+6-5
Original file line numberDiff line numberDiff line change
@@ -99,16 +99,17 @@ export class Workflow extends ObjectHydrator<Specification.Workflow> {
9999
}
100100

101101
static serialize(
102-
workflow: WorkflowIntersection,
102+
model: Partial<WorkflowIntersection>,
103103
format: 'yaml' | 'json' = 'yaml',
104104
normalize: boolean = true,
105105
): string {
106+
const workflow = new Workflow(model);
106107
workflow.validate();
107-
const model = normalize ? workflow.normalize() : workflow;
108+
const normalized = normalize ? workflow.normalize() : workflow;
108109
if (format === 'json') {
109-
return JSON.stringify(model);
110+
return JSON.stringify(normalized);
110111
}
111-
return yaml.dump(model);
112+
return yaml.dump(normalized);
112113
}
113114

114115
/**
@@ -137,5 +138,5 @@ export const _Workflow = Workflow as WorkflowConstructor & {
137138
* @param normalize If the workflow should be normalized before serialization, default true
138139
* @returns A string representation of the workflow
139140
*/
140-
serialize(workflow: WorkflowIntersection, format?: 'yaml' | 'json', normalize?: boolean): string;
141+
serialize(workflow: Partial<WorkflowIntersection>, format?: 'yaml' | 'json', normalize?: boolean): string;
141142
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright 2021-Present The Serverless Workflow Specification Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* oUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
import { callAsyncAPIBuilder } from '../../src/lib/generated/builders';
19+
import { Classes } from '../../src/lib/generated/classes';
20+
21+
const document = { endpoint: 'https://example.com', name: 'example' };
22+
const operationRef = 'operationRef';
23+
24+
describe('CallAsyncAPI builder', () => {
25+
it('should build with fluent api', () => {
26+
const callAsyncAPI = callAsyncAPIBuilder()
27+
.with({
28+
document,
29+
operationRef,
30+
})
31+
.build();
32+
expect(callAsyncAPI).toBeDefined();
33+
expect(callAsyncAPI).toBeInstanceOf(Classes.CallAsyncAPI);
34+
expect(callAsyncAPI.call).toBe('asyncapi');
35+
expect(callAsyncAPI.with).toBeDefined();
36+
expect(callAsyncAPI.with!.document).toEqual(document);
37+
expect(callAsyncAPI.with!.operationRef).toBe(operationRef);
38+
});
39+
40+
it('should build with input', () => {
41+
const data = {
42+
with: {
43+
document,
44+
operationRef,
45+
},
46+
};
47+
const callAsyncAPI = callAsyncAPIBuilder(data).build();
48+
expect(callAsyncAPI).toBeDefined();
49+
expect(callAsyncAPI).toBeInstanceOf(Classes.CallAsyncAPI);
50+
expect(callAsyncAPI.call).toBe('asyncapi');
51+
expect(callAsyncAPI.with).toBeDefined();
52+
expect(callAsyncAPI.with!.document).toEqual(document);
53+
expect(callAsyncAPI.with!.operationRef).toBe(operationRef);
54+
});
55+
56+
it('should throw when invalid', () => {
57+
const test = () => {
58+
callAsyncAPIBuilder().build();
59+
};
60+
expect(test).toThrow(Error);
61+
expect(test).toThrow(/'CallAsyncAPI' is invalid/);
62+
});
63+
64+
it('should not throw when validation is disabled', () => {
65+
const test = () => {
66+
callAsyncAPIBuilder().build({ validate: false });
67+
};
68+
expect(test).not.toThrow();
69+
});
70+
});

0 commit comments

Comments
 (0)