This SDK provides a TypeScript API for working with the Serverless Workflow Specification.
With this SDK, you can:
- Parse workflow definitions in JSON and YAML formats
- Programmatically build workflow definitions
- Validate workflow definitions
The npm @serverlessworkflow/sdk
package versioning aligns with the versioning of the specification:
Latest Releases | Conformance to Spec Version |
---|---|
v1.0.0-alpha5.* | v1.0.0-alpha5 |
Warning
Previous versions of the SDK were published with a typo in the scope: @severlessworkflow/sdk-typescript instead of @serverlessworkflow/sdk-typescript
Latest Releases | Conformance to Spec Version |
---|---|
v1.0.0 | v0.6 |
v2.0.0 | v0.7 |
v3.0.0 | v0.8 |
This SDK includes the following key components:
The SDK provides various TypeScript types and interfaces that ensure type safety and enhance the development experience by catching type errors during compile time.
To avoid confusion with classes, these types and interfaces are exported under the Specification
object, e.g., Specification.Workflow
.
The SDK includes classes that correspond to the aforementioned types and interfaces. These classes offer:
- Instance Checking: Easily verify if an object is an instance of a specific class.
- Self-Validation: Validate the internal state of an object to ensure it adheres to the expected structure.
- Normalization: Methods to normalize object data, ensuring consistent formatting and values.
To avoid confusion with type definitions, these classes are exported under the Classes
object, e.g., Classes.Workflow
.
The fluent builders wrap the core classes and provide a fluent API for constructing objects. This API allows you to chain method calls and configure objects in a more readable and convenient manner.
The fluent builders are directly exported as *<desired-type>*Builder
, e.g., workflowBuilder
.
By default, built objects are self-validated and self-normalized. BuildOptions
can be passed to the build()
method to disable validation or normalization.
The SDK includes a validation function to check if objects conform to the expected schema. This function ensures that your workflow objects are correctly structured and meet the required specifications.
The validate
function is directly exported and can be used as validate('Workflow', workflowObject)
.
Note
Version v1.0.0-alpha5.* has not been released yet.
npm install @serverlessworkflow/sdk
You can deserialize a YAML or JSON representation of the workflow using the static method Classes.Workflow.deserialize
:
import { Classes } from '@serverlessworkflow/sdk';
// const text = await readFile('/some/path/my-workflow-definition.yaml', { encoding: 'utf8' });
// const text = await fetch('https://myserver.com/my-workflow-definition.json');
const text = `
document:
dsl: 1.0.0-alpha5
name: test
version: 1.0.0
namespace: default
do:
- step1:
set:
variable: 'my first workflow'
`;
const workflowDefinition = Classes.Workflow.deserialize(text);
You can type-cast an object to match the structure of a workflow definition:
import { Classes, Specification, validate } from '@serverlessworkflow/sdk';
// Simply cast an object:
const workflowDefinition = {
document: {
dsl: '1.0.0-alpha5',
name: 'test',
version: '1.0.0',
namespace: 'default',
},
do: [
{
step1: {
set: {
variable: 'my first workflow',
},
},
},
],
} as Specification.Workflow;
// Validate it
try {
validate('Workflow', workflowDefinition);
// Serialize it
const definitionTxt = Classes.Workflow.serialize(workflowDefinition);
}
catch (ex) {
// Invalid workflow definition
}
You can create a workflow definition by calling a constructor:
import { Classes, validate } from '@serverlessworkflow/sdk';
// Simply use the constructor
const workflowDefinition = new Classes.Workflow({
document: {
dsl: '1.0.0-alpha5',
name: 'test',
version: '1.0.0',
namespace: 'default',
},
do: [/*
{
step1: {
set: {
variable: 'my first workflow',
},
},
},
*/],
});
workflowDefinition.do.push({
step1: new Classes.SetTask({
set: {
variable: 'my first workflow',
}
})
});
// Validate it
try {
workflowDefinition.validate();
// Serialize it
const definitionTxt = workflowDefinition.serialize();
}
catch (ex) {
// Invalid workflow definition
}
You can use the fluent API to build a validated and normalized workflow definition:
import { documentBuilder, setTaskBuilder, taskListBuilder, workflowBuilder } from '@serverlessworkflow/sdk';
const workflowDefinition = workflowBuilder(/*workflowDefinitionObject*/)
.document(
documentBuilder()
.dsl('1.0.0-alpha5')
.name('test')
.version('1.0.0')
.namespace('default')
.build()
)
.do(
taskListBuilder()
.push({
step1: setTaskBuilder()
.set({
variable: 'my first workflow'
})
.build()
})
.build()
)
.build(/*{
validate: false,
normalize: false
}*/);
You can serialize a workflow definition either by using its serialize
method if it's an instance or the static method with the same name:
import { Classes } from '@serverlessworkflow/sdk';
// const workflowDefinition = <Your preferred method>;
if (workflowDefinition instanceof Classes.Workflow) {
const yaml = workflowDefinition.serialize(/*'yaml' | 'json' */);
}
else {
const json = Classes.Workflow.serialize(workflowDefinition, 'json');
}
Note
The default serialization format is YAML.
Validation can be achieved in two ways: via the validate
function or the instance validate
method:
import { Classes, validate } from '@serverlessworkflow/sdk';
// const workflowDefinition = <Your preferred method>;
try {
if (workflowDefinition instanceof Classes.Workflow) {
workflowDefinition.validate();
}
else {
validate('Workflow', workflowDefinition);
}
}
catch (ex) {
// Workflow definition is invalid
}
To build the project and run tests locally, use the following commands:
git clone https://github.com/serverlessworkflow/sdk-typescript.git
cd sdk-typescript
npm install && npm run build && npm run test
If you're interested in contributing, reading the Tooling Architecture is a good place to start.