0.5.4 • Published 4 years ago

sabar v0.5.4

Weekly downloads
12
License
MIT
Repository
-
Last release
4 years ago

sabar

A tiny middleware combiner has back, next, abort and resume actions

Motivation

A action could split into minor action, and every minor action has a connection. such as trigger next one if success, retry from last step, or restart from beginning..

For this purpose, every minor task should comply with a semantically pattern. In sabar, the last two params will be ctx and actions

Install

npm i sabar

Simple example

import Sabar from 'sabar'

const payment = new Sabar()

const validateIdentity = (user, ctx, actions) => {
  const falsy = validate(use.identity)
  if (falsy) actions.next()
  else actions.resume()
}

const validateAccount = (user, ctx, actions) => {
  const falsy = isAfford(use.account)
  if (falsy) actions.next()
  else action.resume()
}

payment.use(validateIdentity)
payment.use(validateAccount)

const user = {
  identify: { name: 'charlie' },
  account: 100,
}
payment.start(user)

Usage

Sabar({ ctx: object, onError?: Function, onSuccess?: Function, onFinish?: Function })

PropertyDescriptionTypeRequired
ctxInitial value of ctx and default as {}. It will be shared between middlewareobjectno
onErrorTriggered when abort function is invokedFunctionno
onSuccessTriggered when there is no nextSibling of current running middlewareFunctionno
onFinishTrigger when onError or onSuccess is invokedFunctionno

Provide initial ctx value

const payment = new Sabar({ ctx: { paymentMethod: 'visa' }})

use(...args: Sabar | <...T>(...args: ...T, ctx, actions)[] => void)

use is to register fn to sabar instance. fn is an variadic function with ctx and actions tailing params.

const payment = new Sabar({ ctx: { paymentMethod: 'visa' }})

const validateAddress = (args, ctx, actions) => {
  const { location, name } = args
  if (!isValidAddress({ location, name })) {
    return actions.abort()
  }

  actions.next()
}

const validateCard = (args, ctx, actions) => {
  const { cardNumber } = args
  if (!isValidCard({ cardNumber })) {
    return actions.abort()
  }

  actions.next()
}

const applyPayment = payment.use(
  validateAddress,
  validateCard,
)

arg could be a Sabar object. In this condition, Sabar will copy middleware from arg object.

const job = new Sabar()
job.use(fn)

const nextJob = new Sabar()
nextJob.use(job)

job.start()
nextJob.start()

start(...args: array[])

start will make actions begin running. It args will be passing between middleware as heading params.

const job = new Sabar()
job.use(fn)

job.start()

actions

PropertyDescription
nextTrigger next middleware
backRerun from last middleware
abortStop middleware running
resumeRerun from beginning
0.5.4

4 years ago

0.5.3

4 years ago

0.5.2

4 years ago

0.5.1

4 years ago

0.5.0

4 years ago

0.4.1

4 years ago

0.4.0

4 years ago

0.3.3

4 years ago

0.3.2

4 years ago

0.3.1

4 years ago

0.3.0

4 years ago

0.2.1

4 years ago

0.2.0

4 years ago

0.1.0

4 years ago