Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Typescript SDK for Serverless Workflow

License

NotificationsYou must be signed in to change notification settings

serverlessworkflow/sdk-typescript

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Node CIGitpod ready-to-code

Serverless Workflow Specification - TypeScript SDK

This SDK provides a TypeScript API for working with theServerless Workflow Specification.

With this SDK, you can:

  • Parse workflow definitions in JSON and YAML formats
  • Programmatically build workflow definitions
  • Validate workflow definitions

Status

The npm@serverlessworkflow/sdk package versioning aligns with the versioning of thespecification:

Latest ReleasesConformance to Spec Version
v1.0.*v1.0.0

Warning

Previous versions of the SDK were published with a typo in the scope:@severlessworkflow/sdk-typescript instead of@serverlessworkflow/sdk-typescript

Latest ReleasesConformance to Spec Version
v1.0.0v0.6
v2.0.0v0.7
v3.0.0v0.8

SDK Structure

This SDK includes the following key components:

Types and Interfaces

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 theSpecification object, e.g.,Specification.Workflow.

Classes

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 theClasses object, e.g.,Classes.Workflow.

Fluent Builders

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 thebuild() method to disable validation or normalization.

Validation Function

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.

Thevalidate function is directly exported and can be used asvalidate('Workflow', workflowObject).

Other Tools

The SDK also ships tools to build directed graph and MermaidJS flowcharts from a workflow.

Getting Started

Installation

Note

Version v1.0.0.* has not been released yet.

npm install @serverlessworkflow/sdk

Usage

Create a Workflow Definition from YAML or JSON

You can deserialize a YAML or JSON representation of the workflow using the static methodClasses.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');consttext=`document:  dsl: 1.0.0  name: test  version: 1.0.0  namespace: defaultdo:- step1:    set:      variable: 'my first workflow'`;constworkflow=Classes.Workflow.deserialize(text);

Create a Workflow Definition by Casting an Object

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:constworkflow={document:{dsl:'1.0.0',name:'test',version:'1.0.0',namespace:'default',},do:[{step1:{set:{variable:'my first workflow',},},},],}asSpecification.Workflow;// Validate ittry{validate('Workflow',workflow);// Serialize itconstdefinitionTxt=Classes.Workflow.serialize(workflow);}catch(ex){// Invalid workflow definition}

Create a Workflow Definition Using a Class Constructor

You can create a workflow definition by calling a constructor:

import{Classes,validate}from'@serverlessworkflow/sdk';// Simply use the constructorconstworkflow=newClasses.Workflow({document:{dsl:'1.0.0',name:'test',version:'1.0.0',namespace:'default',},do:[/*    {      step1: {        set: {          variable: 'my first workflow',        },      },    },  */],});workflow.do.push({step1:newClasses.SetTask({set:{variable:'my first workflow',}})});// Validate ittry{workflow.validate();// Serialize itconstdefinitionTxt=workflow.serialize();}catch(ex){// Invalid workflow definition}

Create a Workflow Definition Using the Builder API

You can use the fluent API to build a validated and normalized workflow definition:

import{documentBuilder,setTaskBuilder,taskListBuilder,workflowBuilder}from'@serverlessworkflow/sdk';constworkflow=workflowBuilder(/*workflowObject*/).document(documentBuilder().dsl('1.0.0').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  }*/);

Serialize a Workflow Definition to YAML or JSON

You can serialize a workflow definition either by using itsserialize method if it's an instance or the static method with the same name:

import{Classes}from'@serverlessworkflow/sdk';// const workflow = <Your preferred method>;if(workflowinstanceofClasses.Workflow){constyaml=workflow.serialize(/*'yaml' | 'json' */);}else{constjson=Classes.Workflow.serialize(workflow,'json');}

Note

The default serialization format is YAML.

Validate Workflow Definitions

Validation can be achieved in two ways: via thevalidate function or the instancevalidate method:

import{Classes,validate}from'@serverlessworkflow/sdk';constworkflow=/* <Your preferred method> */;try{if(workflowinstanceofClasses.Workflow){workflow.validate();}else{validate('Workflow',workflow);}}catch(ex){// Workflow definition is invalid}

Generate a directed graph

Adirected graph of a workflow can be generated using thebuildGraph function, or alternatives:

  • Workflow instance.toGraph();
  • StaticClasses.Workflow.toGraph(workflow)
import{buildGraph}from'@serverlessworkflow/sdk';constworkflow={document:{dsl:'1.0.0',name:'using-plain-object',version:'1.0.0',namespace:'default',},do:[{step1:{set:{variable:'my first workflow',},},},],};constgraph=buildGraph(workflow);// const workflow = new Classes.Workflow({...}); const graph = workflow.toGraph();// const graph = Classes.Workflow.toGraph(workflow);/*{  id: 'root',  type: 'root',  label: undefined,  parent: null,  nodes: [...], // length 3 - root entry node, step1 node, root exit node  edges: [...], // length 2 - entry to step1, step1 to exit  entryNode: {...}, // root entry node  exitNode: {...} // root exit node}*/

Generate a MermaidJS flowchart

Generating aMermaidJS flowchart can be achieved in two ways: using theconvertToMermaidCode, the legacyMermaidDiagram class, or alternatives:

  • Workflow instance.toMermaidCode();
  • StaticClasses.Workflow.toMermaidCode(workflow)
import{convertToMermaidCode,MermaidDiagram}from'@serverlessworkflow/sdk';constworkflow={document:{dsl:'1.0.0',name:'using-plain-object',version:'1.0.0',namespace:'default',},do:[{step1:{set:{variable:'my first workflow',},},},],};constmermaidCode=convertToMermaidCode(workflow)/* or  */;// const mermaidCode = new MermaidDiagram(workflow).sourceCode();// const workflow = new Classes.Workflow({...}); const mermaidCode = workflow.toMermaidCode();// const mermaidCode = Classes.Workflow.toMermaidCode(workflow);/*flowchart TD    root-entry-node(( ))    root-exit-node((( )))    /do/0/step1["step1"]    /do/0/step1 --> root-exit-node    root-entry-node --> /do/0/step1classDef hidden display: none;*/
flowchart TD    root-entry-node(( ))    root-exit-node((( )))    /do/0/step1["step1"]    /do/0/step1 --> root-exit-node    root-entry-node --> /do/0/step1classDef hidden display: none;
Loading

Building Locally

To build the project and run tests locally, use the following commands:

git clone https://github.com/serverlessworkflow/sdk-typescript.gitcd sdk-typescriptnpm install&& npm run build&& npm runtest

If you're interested in contributing, reading theTooling Architecture is a good place to start.

About

Typescript SDK for Serverless Workflow

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Contributors8

Languages


[8]ページ先頭

©2009-2025 Movatter.jp