Skip to content

Commit

Permalink
Added examples
Browse files Browse the repository at this point in the history
- Readded browser and node examples
- Added call async api/grpc/function tests

Signed-off-by: Jean-Baptiste Bianchi <[email protected]>
  • Loading branch information
JBBianchi committed Jan 10, 2025
1 parent 3c9501c commit fc5defd
Show file tree
Hide file tree
Showing 13 changed files with 364 additions and 30 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ The npm [`@serverlessworkflow/sdk`](https://www.npmjs.com/package/@serverlesswor

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

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

### Installation
> [!NOTE]
> Version v1.0.0-alpha2.\* has not been released yet.
> Version v1.0.0-alpha5.\* has not been released yet.
```sh
npm install @serverlessworkflow/sdk
Expand All @@ -75,7 +75,7 @@ import { Classes } from '@serverlessworkflow/sdk';
// const text = await fetch('https://myserver.com/my-workflow-definition.json');
const text = `
document:
dsl: 1.0.0-alpha2
dsl: 1.0.0-alpha5
name: test
version: 1.0.0
namespace: default
Expand All @@ -96,7 +96,7 @@ import { Classes, Specification, validate } from '@serverlessworkflow/sdk';
// Simply cast an object:
const workflowDefinition = {
document: {
dsl: '1.0.0-alpha2',
dsl: '1.0.0-alpha5',
name: 'test',
version: '1.0.0',
namespace: 'default',
Expand Down Expand Up @@ -132,7 +132,7 @@ import { Classes, validate } from '@serverlessworkflow/sdk';
// Simply use the constructor
const workflowDefinition = new Classes.Workflow({
document: {
dsl: '1.0.0-alpha2',
dsl: '1.0.0-alpha5',
name: 'test',
version: '1.0.0',
namespace: 'default',
Expand Down Expand Up @@ -175,7 +175,7 @@ import { documentBuilder, setTaskBuilder, taskListBuilder, workflowBuilder } fro
const workflowDefinition = workflowBuilder(/*workflowDefinitionObject*/)
.document(
documentBuilder()
.dsl('1.0.0-alpha2')
.dsl('1.0.0-alpha5')
.name('test')
.version('1.0.0')
.namespace('default')
Expand Down
44 changes: 44 additions & 0 deletions examples/browser/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<!doctype html>
<html lang="en">

<head>
<meta charset="utf-8">
<title>Serveless Workflow</title>
<base href="/">
<meta content="width=device-width, initial-scale=1" name="viewport">
</head>

<body>
<div id="output"></div>
<script src="../../dist/umd/index.umd.js"></script>
<script type="text/javascript">
(() => {
const { Classes: { Workflow } } = serverWorkflowSdk;
const workflow = new Workflow({
document: {
dsl: '1.0.0-alpha5',
name: 'test',
version: '1.0.0',
namespace: 'default',
},
do: [
{
step1: {
set: {
variable: 'my first workflow',
},
},
},
],
});
try {
workflow.validate();
document.getElementById('output').innerHTML = workflow.serialize('json');
} catch (ex) {
console.error('Invalid workflow', ex);
}
})();
</script>
</body>

</html>
42 changes: 42 additions & 0 deletions examples/node/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 2021-Present The Serverless Workflow Specification Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Classes } from '../../dist';
const { Workflow } = Classes;

const workflow = new Workflow({
document: {
dsl: '1.0.0-alpha5',
name: 'test',
version: '1.0.0',
namespace: 'default',
},
do: [
{
step1: {
set: {
variable: 'my first workflow',
},
},
},
],
});

try {
workflow.validate();
console.log(workflow.serialize('json'));
} catch (ex) {
console.error('Invalid workflow', ex);
}
3 changes: 3 additions & 0 deletions examples/node/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "../../tsconfig.base.json"
}
4 changes: 2 additions & 2 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
/** @type {import('ts-jest/dist/types').JestConfigWithTsJest} */
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
testPathIgnorePatterns: [".d.ts", ".js"],
modulePathIgnorePatterns: ['<rootDir>/dist/'],
modulePathIgnorePatterns: ['<rootDir>/dist/', '<rootDir>/out-tsc/'],
};
25 changes: 14 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"verify-publish-directory": "node -e 'if (!process.cwd().endsWith(\"dist\")) { console.error(\"Packaging/Publishing should be done from ./dist/\"); process.exitCode = 1; } process.exit();'",
"prepack": "npm run verify-publish-directory",
"prepublishOnly": "npm run verify-publish-directory",
"prepublish": "husky install"
"prepublish": "husky"
},
"dependencies": {
"ajv": "^8.17.1",
Expand Down
11 changes: 6 additions & 5 deletions src/lib/generated/classes/workflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,16 +99,17 @@ export class Workflow extends ObjectHydrator<Specification.Workflow> {
}

static serialize(
workflow: WorkflowIntersection,
model: Partial<WorkflowIntersection>,
format: 'yaml' | 'json' = 'yaml',
normalize: boolean = true,
): string {
const workflow = new Workflow(model);
workflow.validate();
const model = normalize ? workflow.normalize() : workflow;
const normalized = normalize ? workflow.normalize() : workflow;
if (format === 'json') {
return JSON.stringify(model);
return JSON.stringify(normalized);
}
return yaml.dump(model);
return yaml.dump(normalized);
}

/**
Expand Down Expand Up @@ -137,5 +138,5 @@ export const _Workflow = Workflow as WorkflowConstructor & {
* @param normalize If the workflow should be normalized before serialization, default true
* @returns A string representation of the workflow
*/
serialize(workflow: WorkflowIntersection, format?: 'yaml' | 'json', normalize?: boolean): string;
serialize(workflow: Partial<WorkflowIntersection>, format?: 'yaml' | 'json', normalize?: boolean): string;
};
70 changes: 70 additions & 0 deletions tests/builders/call-async-api-builder.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright 2021-Present The Serverless Workflow Specification Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* oUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

import { callAsyncAPIBuilder } from '../../src/lib/generated/builders';
import { Classes } from '../../src/lib/generated/classes';

const document = { endpoint: 'https://example.com', name: 'example' };
const operationRef = 'operationRef';

describe('CallAsyncAPI builder', () => {
it('should build with fluent api', () => {
const callAsyncAPI = callAsyncAPIBuilder()
.with({
document,
operationRef,
})
.build();
expect(callAsyncAPI).toBeDefined();
expect(callAsyncAPI).toBeInstanceOf(Classes.CallAsyncAPI);
expect(callAsyncAPI.call).toBe('asyncapi');
expect(callAsyncAPI.with).toBeDefined();
expect(callAsyncAPI.with!.document).toEqual(document);
expect(callAsyncAPI.with!.operationRef).toBe(operationRef);
});

it('should build with input', () => {
const data = {
with: {
document,
operationRef,
},
};
const callAsyncAPI = callAsyncAPIBuilder(data).build();
expect(callAsyncAPI).toBeDefined();
expect(callAsyncAPI).toBeInstanceOf(Classes.CallAsyncAPI);
expect(callAsyncAPI.call).toBe('asyncapi');
expect(callAsyncAPI.with).toBeDefined();
expect(callAsyncAPI.with!.document).toEqual(document);
expect(callAsyncAPI.with!.operationRef).toBe(operationRef);
});

it('should throw when invalid', () => {
const test = () => {
callAsyncAPIBuilder().build();
};
expect(test).toThrow(Error);
expect(test).toThrow(/'CallAsyncAPI' is invalid/);
});

it('should not throw when validation is disabled', () => {
const test = () => {
callAsyncAPIBuilder().build({ validate: false });
};
expect(test).not.toThrow();
});
});
Loading

0 comments on commit fc5defd

Please sign in to comment.