Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

step-flow

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

step-flow

step flow

  • 1.1.0
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

step-flow

step-flow is a lightweight(without any libraries and less than 200 lines) business processes control library that allows to easily manage business logic by step. step-flow using the syntax of middleware which is similar to express. It provides process control, step jumps, and unified error handling.

中文版文档

Build Status Build status codecov js-semistandard-style Node.js version license

Features

  • Simple and lightweight
  • Code coverage 100%
  • Support step name
  • Support step jump
  • Support asynchronous step flow
  • Supports error handling
  • Support context

Install

npm install --save step-flow

Usage

1. require step-flow

var Flow = require('step-flow');

2. create a flow

var flow = new Flow();

3. add step function

one step with multiple functions:

flow.use(
  'step1',
  function fn1 (ctx, next) {
    ctx.fn1 = true;
    next();
  },
  function fn11 (ctx, next) {
    ctx.fn11 = true;
    next();
  }
);

one step with one function:

flow
  .use('step2', function fn2 (ctx, next) {
    ctx.fn2 = true;
    // next();
  })
  .use(function fn3 (ctx, next) {
    ctx.fn3 = true;
  });

4. error handling

flow.catch(function (err) {
  console.log('flow error:', err);
});

5. run

var context = {};

flow.run(context)

API

StepFlow()


stepFlow.use([stepName]) ⇒ StepFlow

Add the steps and the corresponding function. If the specified steps already exist, these functions will be appended to this step. If it does not exist, create a new one.

Each function added here will receive the parameters (context, next, nextTo, data):

  • context: context object.
  • next(err[,data]): Execute the next function in step, and if it is not called, the next function will not be executed.
  • nextTo(step[,data]): Call this method and pass the step name, you can jump to the corresponding steps.
  • data: the data that the next(null, data) pass.

Only call next() will continue to execute the next function in the step. If a non-empty parameter err is passed, and the subsequent functions will not be executed. The error handling function set with catch(fn) will be executed. If you call next()/nextTo(), and passing the parameter data, the next function will receive this data. However, the functions that after the 'next function' will not receive this data, unless the 'next function' call next()/nextTo() and pass the data.

ParamTypeDefaultDescription
[stepName]String'default'The step name, if you omit this parameter, the default use is default


stepFlow.catch(fn) ⇒ StepFlow

Add error handling functions that will be executed when next (err) is called and a non-null err arguments are passed.

In addition, if an error occurs while running the method specified by the use() method, fn will also be executed and the error object will be passed to fn.

The fn will accept the parameter(err),err for the error message.

ParamTypeDescription
fnfunctionError handling function


stepFlow.run(context, stepName) ⇒ StepFlow

Start to run the step functions. If the step name is specified, it will be executed from the corresponding step. If it is not specified, it will be executed from the first step.

ParamTypeDescription
contextAnyContext object, the function of each step will accept this parameter
stepNameStringStart step name, starting from the first step by default

Running tests

npm test

Contributing

Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.

Authors

  • zdying - HTML/JavaScript/CSS/Node.js developer zdying

See also the list of contributors who participated in this project.

License

This project is licensed under the MIT License - see the LICENSE file for details

Keywords

FAQs

Package last updated on 08 Apr 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc