Skip to content

Commit 5e976b1

Browse files
authored
Merge pull request #86 from nibble-4bits/feature/populate-input-starttime-context
Feature/populate input starttime context
2 parents 6219478 + 71b4598 commit 5e976b1

File tree

4 files changed

+40
-5
lines changed

4 files changed

+40
-5
lines changed

README.md

+10-1
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,16 @@ Each execution is independent of all others, meaning that you can concurrently c
162162
- `waitTimeOverrides?`: An [object that overrides](/docs/feature-support.md#wait-state-duration-override) the wait duration of the specified `Wait` states. The specified override duration should be in milliseconds.
163163
- `retryIntervalOverrides?`: An [object that overrides](/docs/feature-support.md#retry-field-interval-override) the pause duration of the specified state's `Retry` field. The specified override duration should be a number in milliseconds; or an array of numbers, where each number represents milliseconds.
164164
- `noThrowOnAbort?`: If this option is set to `true`, aborting the execution will simply return `null` as result instead of throwing.
165-
- `context?`: An object that will be used as the [Context Object](https://docs.aws.amazon.com/step-functions/latest/dg/input-output-contextobject.html) for the execution. If not passed, the Context Object will default to an empty object. This option is useful to mock the Context Object in case your definition references it in a JSONPath.
165+
- `context?`: An object that will be used as the [Context Object](https://docs.aws.amazon.com/step-functions/latest/dg/input-output-contextobject.html) for the execution. If not passed, the Context Object will default to the following object:
166+
```js
167+
{
168+
"Execution": {
169+
"Input": /* input passed to the execution */,
170+
"StartTime": /* ISO 8601 timestamp of when the execution started */
171+
}
172+
}
173+
```
174+
This option is useful to mock the fields of the Context Object in case your definition references it in a JSONPath.
166175

167176
#### Return value
168177

examples/custom-context-object.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,16 @@ const machineDefinition = {
99
Parameters: {
1010
'execId.$': '$$.Execution.Id', // JSONPaths starting with `$$` query the context object, not the input
1111
'execName.$': '$$.Execution.Name',
12+
'execInput.$': '$$.Execution.Input', // '$$.Execution.Input' and '$$.Execution.StartTime' are always prepopulated, even if you don't explicitly provide them
13+
'execStartTime.$': '$$.Execution.StartTime',
1214
},
1315
End: true,
1416
},
1517
},
1618
};
1719

1820
const stateMachine = new StateMachine(machineDefinition);
19-
const myInput = {};
21+
const myInput = { number: 10, string: 'Hello!' };
2022
const contextObject = {
2123
Execution: {
2224
Id: 'some execution id',
@@ -28,4 +30,10 @@ const execution = stateMachine.run(myInput, {
2830
});
2931

3032
const result = await execution.result;
31-
console.log(result); // Logs `{ execId: 'some execution id', execName: 'execution name' }`
33+
console.log(result); // Logs the following object:
34+
// {
35+
// execId: 'some execution id',
36+
// execName: 'execution name',
37+
// execInput: { number: 10, string: 'Hello!' },
38+
// execStartTime: '2023-12-13T02:10:53.153Z'
39+
// }

src/stateMachine/StateMachine.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,14 @@ export class StateMachine {
144144
private async execute(input: JSONValue, options: ExecuteOptions, cleanupFn: () => void): Promise<JSONValue> {
145145
options.eventLogger.dispatchExecutionStartedEvent(input);
146146

147-
const context = options.runOptions?.context ?? {};
147+
const context = {
148+
...options.runOptions?.context,
149+
Execution: {
150+
...options.runOptions?.context?.Execution,
151+
Input: input,
152+
StartTime: new Date().toISOString(),
153+
},
154+
};
148155
let currState = this.definition.States[this.definition.StartAt];
149156
let currStateName = this.definition.StartAt;
150157
let currInput = cloneDeep(input);

src/typings/Context.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,12 @@
1-
export type Context = Record<string, unknown>;
1+
import type { JSONValue } from './JSONValue';
2+
3+
type ContextExecution = {
4+
Input?: JSONValue;
5+
StartTime?: string;
6+
[other: string]: unknown;
7+
};
8+
9+
export type Context = {
10+
Execution?: ContextExecution;
11+
[other: string]: unknown;
12+
};

0 commit comments

Comments
 (0)