0.0.9 • Published 5 years ago

@cladikzone/sm-connector v0.0.9

Weekly downloads
11
License
ISC
Repository
-
Last release
5 years ago

State Machine Connector

Installation
 npm i @cladikzone/sm-connector --save
Usage
  • import the connector init and smValidationResult 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 the itemId
    • stateToKey Which request property that contains the stateTo 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"
    }
}
0.0.9

5 years ago

0.0.8

5 years ago

0.0.7

5 years ago

0.0.6

5 years ago

0.0.5

5 years ago

0.0.4

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago