README
State Machine Connector
Installation
npm i @cladikzone/sm-connector --save
Usage
- import the connector
init
andsmValidationResult
methods.
import { smValidationResult, init } from '@cladikzone/sm-connector';
Keywords/Properties
locations
where your parameters locate in the request. accepted values are['params', 'body', 'query']
stateType
The type of the state. Ex:-ORDERS
PAYMENT
etc.model
The DB model to find the requested item's details. ( Currently only support for Sequalize )itemIdKey
Which request property that contains theitemId
stateToKey
Which request property that contains thestateTo
parameter.
Initialize the connector
const smValidator = init(
{
baseURL: 'http://localhost:3000/api/v1',
timeout: 1000,
headers: { 'X-Custom-Header': 'foobar' },
},
{ locations: ['params', 'body'], stateType: 'ORDERS', model: OxStreetDb.Order },
);
Validation
- This can be used as a NodeJs middleware function.
{
path: '/updateOrder',
method: 'put',
handler: {
v1: [
smValidator({ itemIdKey: 'orderId', stateToKey: 'stateTo' }),
async (req: Request, res: Response): Promise<void> => {
try {
smValidationResult(req).throw();
const result = await updateOrder(req.body);
res.status(200).send(result);
} catch (e) {
res.status(500).json(e);
}
},
],
},
}
- If the state machine accepts the request you can continue the process under
try
block - If the state machine reject the request following error will e thrown ad can be catch under
catch
block
{
"message": "Request blocked by state machine.",
"stateMachineError": {
"message": "State can not change from placed to release_payment",
"stateFrom": "placed",
"stateTo": "release_payment"
}
}