GithubHelp home page GithubHelp logo

wittech / sdk-typescript-1 Goto Github PK

View Code? Open in Web Editor NEW

This project forked from serverlessworkflow/sdk-typescript

0.0 0.0 0.0 759 KB

Typescript SDK for Serverless Workflow

Home Page: https://serverlessworkflow.io/

License: Apache License 2.0

Shell 0.03% JavaScript 0.15% TypeScript 99.82%

sdk-typescript-1's Introduction

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

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

For the latest stable version:

npm i @severlessworkflow/sdk-typescript

Create Workflow using builder API

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

const workflow: Specification.Workflow = workflowBuilder()
  .id("helloworld")
  .specVersion("0.8")
  .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 '@severlessworkflow/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 '@severlessworkflow/sdk-typescript';

const workflow: Specification.Workflow = workflowBuilder()
  .id("helloworld")
  .version("1.0")
  .specVersion("0.8")
  .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 { Workflow } from '../src/lib/definitions/workflow';

const workflowAsJson: string = Workflow.toJson(workflow);
import { Workflow } from '../src/lib/definitions/workflow';

const workflowAsYaml: string = 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 '@severlessworkflow/sdk-typescript';
import {Workflow} from "./workflow";

const workflow = {
    id: 'helloworld',
    version: '1.0',
    specVersion: '0.3',
    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 '@severlessworkflow/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.

sdk-typescript-1's People

Contributors

antmendoza avatar ghuntley avatar jbbianchi avatar mend-bolt-for-github[bot] avatar tsurdilo avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.