adaptorExpress

Adaptor to adapt (hooked) action function to express standard middleware/handler function signature.

adaptorExpress
Parameters
options (Object = {}) Config
Name Description
options.param Function? (default req=>req.body) Config on what variables passed to param
options.meta Function? (default req=>req.meta) Config on what variables passed to meta
options.context Function? (default ()=>{}) Config on what variables passed to context, besides the default req, res, next
Returns
any: the original result of the action function

errorCounter

A decorator to use context.metrics to count error thrown from action.

errorCounter
Parameters
options (object = {}) Decorator config.
Name Description
options.parseLabel ErrorHookMethod<object>? (default ()=>{}) Use to add extra labels to error metrics.
options.value ErrorHookMethod<number>? (default ()=>1) Use to set counter values, e.g. In case that retries are used.
Static Members
bypassHook(p, m, c)
errorHook(e, p, m, c, a)

errorHandler

A decorator to add conditional side-effect before error being thrown e.g. It can be used together with error-metrics to create conditional error-metrics e.g. It can also be used to handle specific error by throw error in the handler.

errorHandler
Parameters
options (object = {}) Config.
Name Description
options.condition ErrorHookMethod<boolean>? (default ()=>false) Condition to call the handler.
options.handler ErrorHookMethod<void>? (default ()=>{}) What to do when the condition met.
Static Members
errorHook(e, p, m, c, a)

errorMute

A decorator to mute errors when conditions are met.

errorMute
Parameters
options (object = {}) Config.
Name Description
options.condition ErrorHookMethod<boolean>? (default ()=>true) Condition to mute the error.
Returns
(Error | object | undefined): If the error is muted(not thrown to upper level) it is accessible in the return value.
Static Members
errorHook(e, p, m, c, a)

errorRetry

A decorator to retry action until condition met or reach maxRetries.

errorRetry
Parameters
options (object = {}) Config.
Name Description
options.condition ErrorHookMethod<boolean>? Condition to retry.
options.maxRetries number? Max times of retry.
options.delay number? Time to wait between retry.
Returns
(object | Array): The data returned from the original action call.
Static Members
errorHook(e, param, meta, context, action)

errorTag

A decorator used on actions when they are not chained with a logged upper-level call this decorator attaches action name to the error then being thrown to a logged level.

errorTag
Parameters
options (object = {}) Config.
Name Description
options.tag ErrorHookMethod<object>? (default (e,p,m,c,a)=>({action:a.name,message:e.message,constructor:e.constructor.name,stack:e.stack.split('\n').map(s=>s.trim())})) Function to append what to tag to the error.
Static Members
errorHook(e, p, m, c, a)

eventLogger

A decorator to attach standard log behaviour to action bypass: when logger instance is not available in context augment: parse action name and added to action meta to be chained in sub-actions after: log success event, with options to include param and result error: log error event, with option to parse error.

eventLogger
Parameters
options (object = {}) Config.
Name Description
options.logParam boolean? If include param in the log.
options.logResult boolean? If log the result.
options.errorParser any (default e=>e)
Static Members
bypassHook(p, m, c)
storeHook(p, meta, c, action)
actionHook(p, meta, c, a, $4)
afterHook(result, param, meta, $3, a, $5)
errorHook(e, p, meta, $3, a, $5)

eventPoller

A decorator used to poll remote endpoint with action.

eventPoller
Parameters
config (Object = {}) Config.
Name Description
config.until Function? The function to set conditions to stop the polling and return the data.
config.mapping Function? (default res=>res) The mapping function to transform response to the data format needed.
config.interval number? (default 1000) Time to wait between each polling call.
config.timeout number? (default 30*1000) The max time to wait for the polling before abort it.
Returns
Function: The decorated function returns the polling result.
Static Members
bypassHook()
storeHook(p, m, context)
afterHook(r, p, m, c, action, $5)

eventTimer

A decorator to timing action execution time in both success/error cases and send metrics using the client attached in context.

eventTimer
Parameters
options (object = {}) Config.
Name Description
options.parseLabel StorageHookMethod<object>? (default ()=>{}) Function use to include labels that are not directly presented in meta.
Static Members
bypassHook(p, m, $2)
storeHook(p, m, c, a)
afterHook(r, p, m, c, a, $5)
errorHook(e, p, m, c, a, $5)

addHooks

An opinionated decorator creator to ensure predictable behaviour with light test.

addHooks
Parameters
options (Object = {}) Options.
Name Description
options.bypassHook BypassHook? (default ()=>false) Define a condition to bypass the decorator.
options.storeHook StoreHook? (default ()=>{}) Define a function to prepare values to be accessed by other hooks.
options.beforeHook BeforeHook? (default ()=>{}) Define a function to be executed before calling action.
options.actionHook ActionHook? (default (param,meta,context,action)=>action(param,meta,context)) Define a function to return an augumented action with updated args.
options.afterHook AfterHook? (default ()=>{}) Define a function to be executed after the action call succeeds.
options.errorHook ErrorHook? (default ()=>{}) Define a function to be executed if error is thrown from action call.
Returns
Decorator: The decorator with behaviour defined by the hooks and returns the expected result of the action.

sleep

sleep for n ms before resolving the Promise

sleep
Parameters
time (Number) duration in ms
Returns
Promise: a Promise for async/await control flow