arazzo-runner 0.0.5 → 0.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "arazzo-runner",
3
- "version": "0.0.5",
3
+ "version": "0.0.6",
4
4
  "description": "A runner to run through Arazzo Document workflows",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/src/Arazzo.js CHANGED
@@ -112,16 +112,18 @@ class Arazzo extends Document {
112
112
 
113
113
  this.expression.addToContext("inputs", this.inputs);
114
114
 
115
- this.workflow.rules = rules;
116
-
117
115
  if (this.workflow.onSuccess) {
118
- this.workflow.rules.setWorkflowSuccess(this.workflow.onSuccess);
116
+ // this.workflow.rules.set(this.workflow.onSuccess);
117
+ rules.setSuccessRules(this.workflow.onSuccess);
119
118
  }
120
119
 
121
120
  if (this.workflow.onFailure) {
122
- this.workflow.rules.setWorkflowFailures(this.workflow.onFailure);
121
+ // this.workflow.rules.setWorkflowFailures(this.workflow.onFailure);
122
+ rules.setFailureRules(this.workflow.onFailure);
123
123
  }
124
124
 
125
+ this.workflow.rules = rules;
126
+
125
127
  await this.runSteps();
126
128
 
127
129
  if (this.workflow.outputs) {
@@ -203,6 +205,7 @@ class Arazzo extends Document {
203
205
 
204
206
  if (step) {
205
207
  this.step = step;
208
+ const rules = new Rules(this.expression, { logger: this.logger });
206
209
  // if (!this.stepContext[step.stepId])
207
210
  // Object.assign(this.stepContext, { [step.stepId]: {} });
208
211
 
@@ -211,13 +214,19 @@ class Arazzo extends Document {
211
214
  // need to deal with reloading the rules when in a retry state or a goto state
212
215
  // if (this.stepContext?.[step.stepId]?.hasLoadedRules === false) {
213
216
  if (this.step.onSuccess) {
214
- this.workflow.rules.setStepSuccesses(this.step.onSuccess);
217
+ rules.setSuccessRules(this.step.onSuccess);
218
+ // this.workflow.rules.setStepSuccesses(this.step.onSuccess);
215
219
  }
216
220
 
217
221
  if (this.step.onFailure) {
218
- this.workflow.rules.setStepFailures(this.step.onFailure);
222
+ rules.setFailureRules(this.step.onFailure);
223
+ // this.workflow.rules.setStepFailures(this.step.onFailure);
219
224
  }
220
225
 
226
+ rules.combineRules(this.workflow.rules);
227
+ this.step.rules = rules;
228
+ // this.step.rules.combineRules(this.workflow.rules);
229
+
221
230
  // this.stepContext[step.stepId].hasLoadedRules = true;
222
231
  // }
223
232
 
@@ -434,7 +443,7 @@ class Arazzo extends Document {
434
443
  await this.dealWithStepOutputs(response);
435
444
  }
436
445
 
437
- const whatNext = this.workflow.rules.runRules(true);
446
+ const whatNext = this.step.rules.runRules(true);
438
447
 
439
448
  if (whatNext.endWorkflow) {
440
449
  this.workflowIndex += 1;
@@ -496,7 +505,7 @@ class Arazzo extends Document {
496
505
  // await this.dealWithStepOutputs(response);
497
506
  // }
498
507
 
499
- const whatNext = this.workflow.rules.runRules();
508
+ const whatNext = this.step.rules.runRules();
500
509
  if (whatNext.endWorkflow) {
501
510
  this.workflowIndex += 1;
502
511
  this.logger.notice(
package/src/Rules.js CHANGED
@@ -13,24 +13,65 @@ class Rules {
13
13
  };
14
14
  }
15
15
 
16
- setWorkflowFailures(failureActions) {
17
- this.workflowFailures = failureActions;
16
+ /**
17
+ * @typedef {Object} criteriaType
18
+ * @property {string} type - REQUIRED. The type of condition to be applied. The options allowed are jsonpath or xpath.
19
+ * @property {string=} version - REQUIRED. A short hand string representing the version of the expression type being used. The allowed values for JSONPath are draft-goessner-dispatch-jsonpath-00. The allowed values for XPath are xpath-30, xpath-20, or xpath-10.
20
+ */
21
+
22
+ /**
23
+ * @typedef {Object} criteria
24
+ * @property {string} context - A Runtime Expression used to set the context for the condition to be applied on. If type is specified, then the context MUST be provided (e.g. $response.body would set the context that a JSONPath query expression could be applied to).
25
+ * @property {string} condition - REQUIRED. The condition to apply. Conditions can be simple (e.g. $statusCode == 200 which applies an operator on a value obtained from a runtime expression), or a regex, or a JSONPath expression. For regex or JSONPath, the type and context MUST be specified.
26
+ * @property {criteriaType} type - The type of condition to be applied. If specified, the options allowed are simple, regex, jsonpath or xpath. If omitted, then the condition is assumed to be simple, which at most combines literals, operators and Runtime Expressions. If jsonpath, then the expression MUST conform to JSONPath. If xpath the expression MUST conform to XML Path Language 3.1. Should other variants of JSONPath or XPath be required, then a Criterion Expression Type Object MUST be specified.
27
+ */
28
+
29
+ /**
30
+ * @typedef {Object} failureActionsObject
31
+ * @property {string} name - REQUIRED. The name of the failure action. Names are case sensitive.
32
+ * @property {string} type - REQUIRED. The type of action to take. Possible values are "end", "retry", or "goto".
33
+ * @property {string=} workflowId - The workflowId referencing an existing workflow within the Arazzo Description to transfer to upon failure of the step. This field is only relevant when the type field value is "goto" or "retry". If the referenced workflow is contained within an arazzo type sourceDescription, then the workflowId MUST be specified using a Runtime Expression (e.g., $sourceDescriptions.<name>.<workflowId>) to avoid ambiguity or potential clashes. This field is mutually exclusive to stepId. When used with "retry", context transfers back upon completion of the specified workflow.
34
+ * @property {string=} stepId - The stepId to transfer to upon failure of the step. This field is only relevant when the type field value is "goto" or "retry". The referenced stepId MUST be within the current workflow. This field is mutually exclusive to workflowId. When used with "retry", context transfers back upon completion of the specified step.
35
+ * @property {number=} retryAfter - A non-negative decimal indicating the seconds to delay after the step failure before another attempt SHALL be made. Note: if an HTTP Retry-After response header was returned to a step from a targeted operation, then it SHOULD overrule this particular field value. This field only applies when the type field value is "retry".
36
+ * @property {number=} retryLimit - A non-negative integer indicating how many attempts to retry the step MAY be attempted before failing the overall step. If not specified then a single retry SHALL be attempted. This field only applies when the type field value is "retry". The retryLimit MUST be exhausted prior to executing subsequent failure actions.
37
+ * @property {criteria[]} criteria - A list of assertions to determine if this action SHALL be executed. Each assertion is described using a Criterion Object.
38
+ */
39
+
40
+ /**
41
+ * @typedef {Object} successActionsObject
42
+ * @property {string} name - REQUIRED. The name of the failure action. Names are case sensitive.
43
+ * @property {string} type - REQUIRED. The type of action to take. Possible values are "end" or "goto".
44
+ * @property {string=} workflowId - The workflowId referencing an existing workflow within the Arazzo Description to transfer to upon success of the step. This field is only relevant when the type field value is "goto". If the referenced workflow is contained within an arazzo type sourceDescription, then the workflowId MUST be specified using a Runtime Expression (e.g., $sourceDescriptions.<name>.<workflowId>) to avoid ambiguity or potential clashes. This field is mutually exclusive to stepId.
45
+ * @property {string=} stepId - The stepId to transfer to upon success of the step. This field is only relevant when the type field value is "goto". The referenced stepId MUST be within the current workflow. This field is mutually exclusive to workflowId.
46
+ * @property {criteria[]} criteria - A list of assertions to determine if this action SHALL be executed. Each assertion is described using a Criterion Object. All criteria assertions MUST be satisfied for the action to be executed.
47
+ */
48
+
49
+ /**
50
+ * Sets the Failure Action Rules to be run
51
+ * @function setFailureRules
52
+ * @param {failureActionsObject[]} failureActions
53
+ */
54
+ setFailureRules(failureActions) {
18
55
  this.failureRules.push(...failureActions);
19
56
  }
20
57
 
21
- setStepFailures(failureActions) {
22
- this.stepFailures = failureActions;
23
- this.failureRules.push(...failureActions);
24
- }
25
-
26
- setWorkflowSuccess(successActions) {
27
- this.workflowSuccesses = successActions;
58
+ /**
59
+ * Sets the Success Action Rules to be run
60
+ * @function setSuccessRules
61
+ * @param {successActionsObject[]} successActions
62
+ */
63
+ setSuccessRules(successActions) {
28
64
  this.successRules.push(...successActions);
29
65
  }
30
66
 
31
- setStepSuccesses(successActions) {
32
- this.stepSuccesses = successActions;
33
- this.successRules.push(...successActions);
67
+ /**
68
+ * Takes a Rules instance and combines the rules
69
+ * @function combineRules
70
+ * @param {Rules} rulesObj
71
+ */
72
+ combineRules(rulesObj) {
73
+ this.failureRules.push(...rulesObj.failureRules);
74
+ this.successRules.push(...rulesObj.successRules);
34
75
  }
35
76
 
36
77
  runRules(successRules = false) {
@@ -48,6 +89,11 @@ class Rules {
48
89
  this.logger.notice(`Running onFailure Rules`);
49
90
  }
50
91
 
92
+ if (this.rules.length)
93
+ this.logger.notice(
94
+ "==================================================================================",
95
+ );
96
+
51
97
  for (const rule of this.rules) {
52
98
  if (rule.criteria) {
53
99
  const passedCriteria = this.criteriaChecks(rule);
@@ -60,6 +106,7 @@ class Rules {
60
106
  obj.goto = true;
61
107
  if (rule.stepId) obj.stepId = rule.stepId;
62
108
  else obj.workflowId = rule.workflowId;
109
+ break;
63
110
  } else {
64
111
  obj.name = rule.name;
65
112
  obj.retry = true;
@@ -67,30 +114,51 @@ class Rules {
67
114
  if (rule.workflowId) obj.workflowId = rule.workflowId;
68
115
  obj.retryLimit = rule?.retryLimit || 1;
69
116
  if (rule.retryAfter) obj.retryAfter = rule.retryAfter;
117
+ break;
70
118
  }
71
119
  }
72
120
  }
73
121
  }
74
122
 
123
+ if (this.rules.length)
124
+ this.logger.notice(
125
+ "==================================================================================",
126
+ );
127
+
75
128
  return obj;
76
129
  }
77
130
 
131
+ /**
132
+ * @function criteriaChecks
133
+ * @private
134
+ * @param {successActionsObject|failureActionsObject} rule
135
+ * @returns {boolean[]}
136
+ */
78
137
  criteriaChecks(rule) {
79
138
  const passedCriteria = [];
80
139
 
81
- this.logger.notice(
82
- "==================================================================================",
83
- );
84
-
85
140
  for (const criteriaObject of rule.criteria) {
86
141
  if (criteriaObject?.type) {
87
142
  if (criteriaObject.type === "regex") {
143
+ this.logger.notice(
144
+ `checking ${rule.name} rule: ${criteriaObject.context} matches ${criteriaObject.condition} `,
145
+ );
88
146
  const hasPassedCheck = this.expression.checkRegexExpression(
89
147
  criteriaObject.context,
90
148
  criteriaObject.condition,
91
149
  );
92
150
 
93
- if (hasPassedCheck) hasPassed.push(true);
151
+ if (hasPassedCheck) {
152
+ this.logger.success(
153
+ `${criteriaObject.context} matched ${criteriaObject.condition}`,
154
+ );
155
+ passedCriteria.push(hasPassedCheck);
156
+ } else {
157
+ this.logger.error(
158
+ `${criteriaObject.context} did not match ${criteriaObject.condition}`,
159
+ );
160
+ }
161
+ // if (hasPassedCheck) hasPassed.push(true);
94
162
  } else {
95
163
  }
96
164
  } else {
@@ -110,20 +178,22 @@ class Rules {
110
178
  }
111
179
  }
112
180
 
113
- this.logger.notice(
114
- "==================================================================================",
115
- );
116
-
117
181
  return passedCriteria;
118
182
  }
119
183
 
184
+ /**
185
+ * @function buildFailureRules
186
+ * @private
187
+ */
120
188
  buildFailureRules() {
121
- this.failureRules.reverse();
122
189
  this.rules = this.failureRules;
123
190
  }
124
191
 
192
+ /**
193
+ * @function buildSuccessRules
194
+ * @private
195
+ */
125
196
  buildSuccessRules() {
126
- this.successRules.reverse();
127
197
  this.rules = this.successRules;
128
198
  }
129
199
  }