UNPKG

4.88 kBMarkdownView Raw
1
2![Statebox Logo](/images/statebox-logo.png)
3
4[![Tymly Package](https://img.shields.io/badge/tymly-package-blue.svg)](https://tymly.io/)
5[![npm (scoped)](https://img.shields.io/npm/v/@wmfs/statebox.svg)](https://www.npmjs.com/package/@wmfs/statebox)
6[![CircleCI](https://circleci.com/gh/wmfs/statebox.svg?style=svg)](https://circleci.com/gh/wmfs/statebox)
7[![codecov](https://codecov.io/gh/wmfs/statebox/branch/master/graph/badge.svg)](https://codecov.io/gh/wmfs/statebox)
8[![CodeFactor](https://www.codefactor.io/repository/github/wmfs/statebox/badge)](https://www.codefactor.io/repository/github/wmfs/statebox)
9[![Dependabot badge](https://img.shields.io/badge/Dependabot-active-brightgreen.svg)](https://dependabot.com/)
10[![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg)](http://commitizen.github.io/cz-cli/)
11[![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)
12[![license](https://img.shields.io/github/license/mashape/apistatus.svg)](https://github.com/wmfs/tymly/blob/master/packages/pg-concat/LICENSE)
13
14
15> Orchestrate Node functions using [Amazon States Language](https://states-language.net/spec.html)
16
17## Useful links
18
19* [AWS Step Functions](https://aws.amazon.com/step-functions/) - overview of Amazon States Language, use cases, etc
20* [statelint](https://npmjs.com/package/@wmfs/statelint) - a validator for Amazon States Language JSON files
21* https://docs.aws.amazon.com/step-functions/latest/dg/concepts-amazon-states-language.html
22* https://docs.aws.amazon.com/step-functions/latest/apireference/API_SendTaskSuccess.html
23
24## <a name='install'></a>Install
25```bash
26$ npm install @wmfs/statebox --save
27```
28
29## <a name='usage'></a>Usage
30
31```javascript
32const Statebox = require('@wmfs/statebox')
33const statebox = new Statebox({})
34
35const main = async
36
37function() {
38
39 // STEP 1:
40 // Create some 'module' resources (i.e. Javascript
41 // classes with 'run' and optional 'init' methods)
42 // that state machines can then refer to...
43 // -------------------------------------------------
44 await statebox.ready
45 statebox.createModuleResources({
46 // Simple module to add two numbers together
47 add: class Add {
48 run(event, context) {
49 context.sendTaskSuccess(event.number1 + event.number2)
50 }
51 },
52 // Simple module to subtract one number from another
53 subtract: class Subtract {
54 // Init methods are optional, but all allow
55 // resource-instances to be configured...
56 init(resourceConfig, env, callback) {
57 callback(null)
58 }
59 run(event, context) {
60 context.sendTaskSuccess(event.number1 - event.number2)
61 }
62 }
63 })
64
65 // STEP 2:
66 // Next create a new 'calculator' state
67 // machine using Amazon States Language...
68 // ---------------------------------------
69 await statebox.createStateMachines({
70 'calculator': {
71 Comment: 'A simple calculator',
72 StartAt: 'OperatorChoice',
73 States: {
74 OperatorChoice: {
75 Type: 'Choice',
76 Choices: [{
77 Variable: '$.operator',
78 StringEquals: '+',
79 Next: 'Add'
80 }, {
81 Variable: '$.operator',
82 StringEquals: '-',
83 Next: 'Subtract'
84 }]
85 },
86 Add: {
87 Type: 'Task',
88 InputPath: '$.numbers',
89 Resource: 'module:add', // See createModuleResources()
90 ResultPath: '$.result',
91 End: true
92 },
93 Subtract: {
94 Type: 'Task',
95 InputPath: '$.numbers',
96 Resource: 'module:subtract',
97 ResultPath: '$.result',
98 End: true
99 }
100 }
101 }
102 }, {}, // 'env': An environment/context/sandbox
103 )
104
105 // STEP 3:
106 // Start a new execution on a state machine
107 // ----------------------------------------
108 const executionDescription = await statebox.startExecution({
109 numbers: {
110 number1: 3,
111 number2: 2
112 },
113 operator: '-'
114 }, // input
115 'calculator', // state machine name
116 {} // options
117 )
118
119 // STEP 4:
120 // Look at the results...
121 // ----------------------
122 console.log(executionDescription)
123 // Result object
124 // -------------
125 // {
126 // executionName: '...',
127 // ctx: {
128 // numbers': {
129 // number1: 3,
130 // number2: 2
131 // },
132 // operator: '-',
133 // result: 1 <--- The important bit :-)
134 // },
135 // currentStateName:'Subtract',
136 // currentResource:'module:subtract',
137 // stateMachineName:'calculator',
138 // startDate: '2018-09-03T21:58:04.287Z'
139 // }
140}
141
142if (require.main === module) {
143 main();
144}
145```
146
147## <a name='test'></a>Testing
148
149```bash
150$ npm test
151```
152
153## <a name='license'></a>License
154[MIT](https://github.com/wmfs/tymly/packages/statebox/blob/master/LICENSE)