npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@severlessworkflow/sdk-typescript

v4.0.0-rc1

Published

Typescript SDK for Serverless Workflow Specification

Downloads

6,585

Readme

[!CAUTION] This is a legacy branch. It is not officially supported by the ServerlessWorkflow organisation anymore but still accepts community contributions.

Node CI Gitpod ready-to-code

Serverless Workflow Specification - Typescript SDK

Provides the Typescript API/SPI for the Serverless Workflow Specification

With the SDK you can:

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

Status

| Latest Releases | Conformance to spec version | | :---: | :---: | | v1.0.0 | v0.6 | | v2.0.0 | v0.7 | | v3.0.0 | v0.8 | | v4.0.0 | v0.9 |

Getting Started

Building locally

To build the project and run tests locally:

git clone https://github.com/serverlessworkflow/sdk-typescript.git
cd sdk-typescript
npm install && npm run build && npm run test

How to use

Install

Version >= 4.0.0
npm i @serverlessworkflow/sdk-typescript
Version < 4.0.0
npm i @severlessworkflow/sdk-typescript

Create Workflow using builder API

import { workflowBuilder, injectstateBuilder, Specification } from '@serverlessworkflow/sdk-typescript';

const workflow: Specification.Workflow = workflowBuilder()
  .id("helloworld")
  .specVersion("0.9")
  .version("1.0")
  .name("Hello World Workflow")
  .description("Inject Hello World")
  .start("Hello State")
  .states([
    injectstateBuilder()
      .name("Hello State")
      .data({
          "result": "Hello World!"
      })
      .build()
  ])
  .build();

Create Workflow from JSON/YAML source

import { Specification, Workflow } from '@serverlessworkflow/sdk-typescript';

const source = `id: helloworld
version: '1.0'
specVerion: '0.8'
name: Hello World Workflow
description: Inject Hello World
start: Hello State
states:
  - type: inject
    name: Hello State
    data:
      result: Hello World!
    end: true`

const workflow: Specification.Workflow = Workflow.fromSource(source);

Where source can be in both JSON or YAML format.

Parse a Workflow instance to JSON/YAML

Having the following workflow instance:

import { workflowBuilder, injectstateBuilder, Specification } from '@serverlessworkflow/sdk-typescript';

const workflow: Specification.Workflow = workflowBuilder()
  .id("helloworld")
  .version("1.0")
  .specVersion("0.9")
  .name("Hello World Workflow")
  .description("Inject Hello World")
  .start("Hello State")
  .states([
    injectstateBuilder()
      .name("Hello State")
      .data({
        "result": "Hello World!"
      })
      .end(true)
      .build()
  ])
  .build();

You can convert it to its string representation in JSON or YAML format by using the static methods Workflow.toJson or Workflow.toYaml respectively:

import {Specification, workflowBuilder} from "@severlessworkflow/sdk-typescript";

const workflow: Specification.Workflow = workflowBuilder()
    //...
    .build()

const workflowAsJson: string = Specification.Workflow.toJson(workflow);
import {Specification, workflowBuilder} from "@severlessworkflow/sdk-typescript";

const workflow: Specification.Workflow = workflowBuilder()
    //...
    .build()

const workflowAsYaml: string = Specification.Workflow.toYaml(workflow);

Validate workflow definitions

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

WorkflowValidator class provides a validation method:

  • validate(): boolean
import {WorkflowValidator, Specification} from '@serverlessworkflow/sdk-typescript';
import {Workflow} from "./workflow";

const workflow = {
    id: 'helloworld',
    version: '1.0',
    specVersion: '0.9',
    name: 'Hello World Workflow',
    description: 'Inject Hello World',
    start: 'Hello State',
    states: [
        {
            type: 'inject',
            name: 'Hello State',
            end: true,
            data: {
                result: "Hello World!"
            }
        }
    ]
};

const workflowValidator: WorkflowValidator = new WorkflowValidator(Workflow.fromSource(JSON.stringify(workflow)));
if (!workflowValidator.isValid) {
    workflowValidator.errors.forEach(error => console.error((error as ValidationError).message));
}

You can also validate parts of a workflow using validators:

import { ValidateFunction } from 'ajv';
import { validators, Specification } from '@serverlessworkflow/sdk-typescript';

const injectionState: Specification.Injectstate = workflow.states[0];
const injectionStateValidator: ValidateFunction<Specification.Injectstate> = validators.get('Injectstate');
if (!injectionStateValidator(injectionState)) {
  injectionStateValidator.errors.forEach(error => console.error(error.message));
}

Generate workflow diagram

It is possible to generate the workflow diagram with Mermaid

const workflow = workflowBuilder()
    .id("helloworld")
    ....
    .build();

const mermaidSourceCode = new MermaidDiagram(workflow).sourceCode();

Here you can see a full example that uses mermaid in the browser to generate the workflow diagram.