Options
All
  • Public
  • Public/Protected
  • All
Menu

styled with prettier GitHub Workflow Status Coveralls Dev Dependencies Donate

workflow is a Node JS library designed to facilitate the execution of simple workflows with rollback support. This allows you to take small units of code and combine them together into a larger workflow to accomplish a single large task.

See the documentation for more details.

Get Started

npm install @sammarks/workflow
yarn add @sammarks/workflow
import { execute } from '@sammarks/workflow'

interface WorkflowContext {
  firstStepRun: boolean
  secondStepRun: boolean
  test: string
}

const workflow = [
  {
    name: 'first',
    run: async (context: WorkflowContext): Promise<void> => {
      context.firstStepRun = true
    },
    revert: async (context: WorkflowContext): Promise<void> => {
      context.firstStepRun = false
    }
  },
  {
    name: 'second',
    run: async (context: WorkflowContext): Promise<void> => {
      context.secondStepRun = true
    },
    revert: async (context: WorkflowContext): Promise<void> => {
      context.secondStepRun = false
    }
  }
]

const result = await execute<WorkflowContext>('test-workflow', workflow, { test: 'foo' })
console.log(result)

// {
//   firstStepRun: true,
//   secondStepRun: true,
//   test: 'foo'
// }

Features

  • Define steps in a simple, typed, array-object format.
  • Steps and workflows are given names for debugging support.
  • Rollback support in case a step fails.
  • Predictable and documented error handling in case something goes wrong.

Why use this?

Breaking the large task into smaller units of code allows a less complicated end result, and greater unit testing abilities since each step in the workflow is broken into its own function.

Rollback support allows a transactional nature for executing workflows. If one step in the process fails to execute, all previously-executed steps with a configured rollback are executed.

workflow provides TypeScript hinting and a predictable error interface to handle issues when they arise.

Index

Type aliases

StepContext

StepContext: object

The context passed to all steps and their filter functions.

StepFilter

StepFilter<Context>: (context: Context) => Promise<boolean>

Determines whether or not a step should be run.

param

the current step context

returns

a promise resolving to a boolean indicating if the step should run

Type parameters

Type declaration

    • (context: Context): Promise<boolean>
    • Parameters

      • context: Context

      Returns Promise<boolean>

StepFunction

StepFunction<Context>: (context: Context) => Promise<void>

A callback executed whenever running a step, or reverting it.

param

the current step context

returns

a promise

Type parameters

Type declaration

    • (context: Context): Promise<void>
    • Parameters

      • context: Context

      Returns Promise<void>

Variables

Const debug

debug: Debugger = Debug('workflow')

Functions

Const execute

  • execute<T>(name: string, steps: Array<Step<T>>, context: T): Promise<T>
  • Executes a workflow.

    Type parameters

    Parameters

    • name: string

      the name of the workflow to execute

    • steps: Array<Step<T>>

      an array of steps used to define the workflow

    • context: T

      the default context to pass to all steps

    Returns Promise<T>

    a promise resolving to the final context

Legend

  • Constructor
  • Property
  • Property
  • Inherited property
  • Static property

Generated using TypeDoc