@lafken/state-machine 0.10.1

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.
Files changed (33) hide show
  1. package/LICENCE +21 -0
  2. package/README.md +383 -0
  3. package/lib/index.d.ts +2 -0
  4. package/lib/index.js +18 -0
  5. package/lib/main/index.d.ts +2 -0
  6. package/lib/main/index.js +18 -0
  7. package/lib/main/param/index.d.ts +2 -0
  8. package/lib/main/param/index.js +18 -0
  9. package/lib/main/param/param.d.ts +105 -0
  10. package/lib/main/param/param.js +115 -0
  11. package/lib/main/param/param.types.d.ts +77 -0
  12. package/lib/main/param/param.types.js +2 -0
  13. package/lib/main/state-machine/index.d.ts +2 -0
  14. package/lib/main/state-machine/index.js +18 -0
  15. package/lib/main/state-machine/state-machine.d.ts +113 -0
  16. package/lib/main/state-machine/state-machine.js +133 -0
  17. package/lib/main/state-machine/state-machine.types.d.ts +977 -0
  18. package/lib/main/state-machine/state-machine.types.js +7 -0
  19. package/lib/resolver/index.d.ts +1 -0
  20. package/lib/resolver/index.js +17 -0
  21. package/lib/resolver/resolver.d.ts +6 -0
  22. package/lib/resolver/resolver.js +28 -0
  23. package/lib/resolver/state-machine/schema/schema.d.ts +34 -0
  24. package/lib/resolver/state-machine/schema/schema.js +431 -0
  25. package/lib/resolver/state-machine/schema/schema.types.d.ts +149 -0
  26. package/lib/resolver/state-machine/schema/schema.types.js +2 -0
  27. package/lib/resolver/state-machine/schema/schema.utils.d.ts +15 -0
  28. package/lib/resolver/state-machine/schema/schema.utils.js +53 -0
  29. package/lib/resolver/state-machine/state-machine.d.ts +19 -0
  30. package/lib/resolver/state-machine/state-machine.js +96 -0
  31. package/lib/resolver/state-machine/state-machine.types.d.ts +7 -0
  32. package/lib/resolver/state-machine/state-machine.types.js +2 -0
  33. package/package.json +87 -0
@@ -0,0 +1,77 @@
1
+ import type { AllowedTypes, ArrayField, BooleanField, GetResourceProps, NumberField, ObjectField, StringField } from '@lafken/common';
2
+ export type JsonAtaString = `{%${string}%}`;
3
+ export type ExecutionSource = 'id' | `input.${string}` | 'name' | 'role_arn' | 'start_time' | 'redrive_count' | 'redrive_time';
4
+ export type StateMachineSource = 'id' | 'name';
5
+ export type StateSource = 'entered_time' | 'name' | 'retry_count';
6
+ export type TaskSource = 'token';
7
+ type ParamContextBase<C, T> = {
8
+ /**
9
+ * Identifies the context where the source is obtain
10
+ */
11
+ context: C;
12
+ /**
13
+ * Value or context parameter
14
+ */
15
+ source: T;
16
+ /**
17
+ * Field data type.
18
+ *
19
+ * Specifies the type of the field. By default, the type is inferred
20
+ * from the property that decorates the field. However, it can be
21
+ * explicitly set to a primitive type such as `String`, `Number`,
22
+ * `Boolean`, or to another payload type.
23
+ *
24
+ * This ensures correct parsing, validation, and serialization of the field's value.
25
+ */
26
+ type?: AllowedTypes;
27
+ };
28
+ export type InputParamContext = ParamContextBase<'input', string>;
29
+ export type ExecutionParamContext = ParamContextBase<'execution', ExecutionSource>;
30
+ export type StateParamContext = ParamContextBase<'state', StateSource>;
31
+ export type StateMachineParamContext = ParamContextBase<'state_machine', StateMachineSource>;
32
+ export type TaskParamContext = ParamContextBase<'task', TaskSource>;
33
+ export type CustomParamContext = {
34
+ context: 'custom';
35
+ /**
36
+ * A simple value
37
+ */
38
+ value?: any;
39
+ /**
40
+ * You can extend this value with other
41
+ */
42
+ type?: String | Number | Boolean | Function;
43
+ };
44
+ export type JsonAtaParamContext = {
45
+ context: 'jsonata';
46
+ /**
47
+ * A simple value
48
+ */
49
+ value: JsonAtaString;
50
+ };
51
+ export type ParamContext = ExecutionParamContext | InputParamContext | StateParamContext | StateMachineParamContext | TaskParamContext | CustomParamContext | JsonAtaParamContext;
52
+ export type ParamProps = {
53
+ /**
54
+ * Name of property
55
+ *
56
+ * @default class property name
57
+ */
58
+ name?: string;
59
+ } & ParamContext;
60
+ export type StateMachineParamBase = {
61
+ context: ParamContext['context'];
62
+ value: any;
63
+ source: any;
64
+ name: string;
65
+ };
66
+ export type StateMachineStringParam = StringField & StateMachineParamBase;
67
+ export type StateMachineNumberParam = NumberField & StateMachineParamBase;
68
+ export type StateMachineBooleanParam = BooleanField & StateMachineParamBase;
69
+ export type StateMachineObjectParam = Omit<ObjectField, 'properties'> & StateMachineParamBase & {
70
+ properties: StateMachineParamMetadata[];
71
+ };
72
+ export type StateMachineArrayParam = Omit<ArrayField, 'items'> & StateMachineParamBase & {
73
+ items: StateMachineParamMetadata;
74
+ };
75
+ export type StateMachineParamMetadata = StateMachineStringParam | StateMachineNumberParam | StateMachineBooleanParam | StateMachineObjectParam | StateMachineArrayParam;
76
+ export type IntegrationOptionsParams = GetResourceProps;
77
+ export {};
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,2 @@
1
+ export * from './state-machine';
2
+ export * from './state-machine.types';
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./state-machine"), exports);
18
+ __exportStar(require("./state-machine.types"), exports);
@@ -0,0 +1,113 @@
1
+ import 'reflect-metadata';
2
+ import { type DefaultMethod, type HandlerStateProps, type StateMachineBaseProps, type StateMachineResourceProps } from './state-machine.types';
3
+ export declare const RESOURCE_TYPE: "STATE_MACHINE";
4
+ /**
5
+ * Class decorator that registers a class as a **nested** state machine
6
+ * definition.
7
+ *
8
+ * A nested state machine defines a reusable sub-workflow that can be
9
+ * referenced inside a `parallel` branch or a `map` state of a
10
+ * parent `@StateMachine`. It is not deployed as a standalone resource
11
+ * but embedded within the parent definition.
12
+ *
13
+ * @typeParam T - The class being decorated.
14
+ * @param props - State machine definition including `startAt` and
15
+ * optional `minify`.
16
+ *
17
+ * @example
18
+ * ```ts
19
+ * @NestedStateMachine({
20
+ * startAt: {
21
+ * type: 'wait',
22
+ * seconds: 2,
23
+ * next: { type: 'succeed' },
24
+ * },
25
+ * })
26
+ * export class RetryBranch {}
27
+ *
28
+ * @StateMachine({
29
+ * startAt: {
30
+ * type: 'parallel',
31
+ * branches: [RetryBranch],
32
+ * },
33
+ * })
34
+ * export class OrderFlow {}
35
+ * ```
36
+ */
37
+ export declare const NestedStateMachine: <T extends Function>(props: StateMachineBaseProps<keyof T["prototype"]>) => (constructor: T) => void;
38
+ /**
39
+ * Class decorator that registers a class as an AWS Step Functions
40
+ * state machine resource.
41
+ *
42
+ * The decorated class defines a complete workflow. Use `@State` on
43
+ * its methods to declare task states backed by Lambda functions or
44
+ * AWS service integrations, and configure the execution flow through
45
+ * the `startAt` option.
46
+ *
47
+ * @typeParam T - The class being decorated.
48
+ * @param props - State machine configuration (startAt, executionType,
49
+ * services, minify).
50
+ *
51
+ * @example
52
+ * ```ts
53
+ * @StateMachine({ startAt: 'validate' })
54
+ * export class OrderFlow {
55
+ * @State({ next: 'process' })
56
+ * validate(@Event(OrderInput) input: OrderInput) {}
57
+ *
58
+ * @State({ end: true })
59
+ * process(@Event(OrderInput) input: OrderInput) {}
60
+ * }
61
+ * ```
62
+ *
63
+ * @example
64
+ * ```ts
65
+ * // Declarative flow without Lambda handlers
66
+ * @StateMachine({
67
+ * startAt: {
68
+ * type: 'wait',
69
+ * seconds: 5,
70
+ * next: { type: 'succeed' },
71
+ * },
72
+ * })
73
+ * export class DelayedFlow {}
74
+ * ```
75
+ */
76
+ export declare const StateMachine: <T extends Function>(props: StateMachineResourceProps<keyof T["prototype"]>) => (constructor: T) => void;
77
+ /**
78
+ * Method decorator that registers a handler as a **Task** state inside
79
+ * a `@StateMachine`.
80
+ *
81
+ * The decorated method becomes a Lambda function (or an AWS service
82
+ * integration) that Step Functions invokes as part of the workflow.
83
+ * Use `next` to chain to the following state or `end: true` to mark
84
+ * it as a terminal state. Service integrations are configured via
85
+ * `integrationService`, `action`, and `mode`.
86
+ *
87
+ * @typeParam T - The class that owns the method.
88
+ * @typeParam K - The method key being decorated.
89
+ * @param props - Optional state configuration (next, end, assign,
90
+ * integrationService, action, mode, lambda, etc.).
91
+ *
92
+ * @example
93
+ * ```ts
94
+ * @StateMachine({ startAt: 'step1' })
95
+ * export class MyFlow {
96
+ * @State({ next: 'step2', assign: { count: 1 } })
97
+ * step1(@Event(Input) input: Input) {}
98
+ *
99
+ * @State({ end: true })
100
+ * step2(@Event(Input) input: Input) {}
101
+ * }
102
+ * ```
103
+ *
104
+ * @example
105
+ * ```ts
106
+ * // AWS service integration (SQS)
107
+ * @State({ integrationService: 'sqs', action: 'sendMessage', mode: 'token' })
108
+ * send(@IntegrationOptions() { getResourceValue }: GetResourceProps) {
109
+ * return { QueueUrl: getResourceValue('queue::orders', 'id') };
110
+ * }
111
+ * ```
112
+ */
113
+ export declare const State: <T extends Record<K, DefaultMethod>, K extends keyof T>(props?: HandlerStateProps<T>) => (target: T, methodName: K, descriptor: PropertyDescriptor) => any;
@@ -0,0 +1,133 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.State = exports.StateMachine = exports.NestedStateMachine = exports.RESOURCE_TYPE = void 0;
4
+ require("reflect-metadata");
5
+ const common_1 = require("@lafken/common");
6
+ const state_machine_types_1 = require("./state-machine.types");
7
+ exports.RESOURCE_TYPE = 'STATE_MACHINE';
8
+ /**
9
+ * Class decorator that registers a class as a **nested** state machine
10
+ * definition.
11
+ *
12
+ * A nested state machine defines a reusable sub-workflow that can be
13
+ * referenced inside a `parallel` branch or a `map` state of a
14
+ * parent `@StateMachine`. It is not deployed as a standalone resource
15
+ * but embedded within the parent definition.
16
+ *
17
+ * @typeParam T - The class being decorated.
18
+ * @param props - State machine definition including `startAt` and
19
+ * optional `minify`.
20
+ *
21
+ * @example
22
+ * ```ts
23
+ * @NestedStateMachine({
24
+ * startAt: {
25
+ * type: 'wait',
26
+ * seconds: 2,
27
+ * next: { type: 'succeed' },
28
+ * },
29
+ * })
30
+ * export class RetryBranch {}
31
+ *
32
+ * @StateMachine({
33
+ * startAt: {
34
+ * type: 'parallel',
35
+ * branches: [RetryBranch],
36
+ * },
37
+ * })
38
+ * export class OrderFlow {}
39
+ * ```
40
+ */
41
+ const NestedStateMachine = (props) => (constructor) => (0, common_1.createResourceDecorator)({
42
+ type: state_machine_types_1.StateMachineReflectKeys.nested,
43
+ callerFileIndex: 6,
44
+ })(props)(constructor);
45
+ exports.NestedStateMachine = NestedStateMachine;
46
+ /**
47
+ * Class decorator that registers a class as an AWS Step Functions
48
+ * state machine resource.
49
+ *
50
+ * The decorated class defines a complete workflow. Use `@State` on
51
+ * its methods to declare task states backed by Lambda functions or
52
+ * AWS service integrations, and configure the execution flow through
53
+ * the `startAt` option.
54
+ *
55
+ * @typeParam T - The class being decorated.
56
+ * @param props - State machine configuration (startAt, executionType,
57
+ * services, minify).
58
+ *
59
+ * @example
60
+ * ```ts
61
+ * @StateMachine({ startAt: 'validate' })
62
+ * export class OrderFlow {
63
+ * @State({ next: 'process' })
64
+ * validate(@Event(OrderInput) input: OrderInput) {}
65
+ *
66
+ * @State({ end: true })
67
+ * process(@Event(OrderInput) input: OrderInput) {}
68
+ * }
69
+ * ```
70
+ *
71
+ * @example
72
+ * ```ts
73
+ * // Declarative flow without Lambda handlers
74
+ * @StateMachine({
75
+ * startAt: {
76
+ * type: 'wait',
77
+ * seconds: 5,
78
+ * next: { type: 'succeed' },
79
+ * },
80
+ * })
81
+ * export class DelayedFlow {}
82
+ * ```
83
+ */
84
+ const StateMachine = (props) => (constructor) => (0, common_1.createResourceDecorator)({
85
+ type: exports.RESOURCE_TYPE,
86
+ callerFileIndex: 6,
87
+ })(props)(constructor);
88
+ exports.StateMachine = StateMachine;
89
+ /**
90
+ * Method decorator that registers a handler as a **Task** state inside
91
+ * a `@StateMachine`.
92
+ *
93
+ * The decorated method becomes a Lambda function (or an AWS service
94
+ * integration) that Step Functions invokes as part of the workflow.
95
+ * Use `next` to chain to the following state or `end: true` to mark
96
+ * it as a terminal state. Service integrations are configured via
97
+ * `integrationService`, `action`, and `mode`.
98
+ *
99
+ * @typeParam T - The class that owns the method.
100
+ * @typeParam K - The method key being decorated.
101
+ * @param props - Optional state configuration (next, end, assign,
102
+ * integrationService, action, mode, lambda, etc.).
103
+ *
104
+ * @example
105
+ * ```ts
106
+ * @StateMachine({ startAt: 'step1' })
107
+ * export class MyFlow {
108
+ * @State({ next: 'step2', assign: { count: 1 } })
109
+ * step1(@Event(Input) input: Input) {}
110
+ *
111
+ * @State({ end: true })
112
+ * step2(@Event(Input) input: Input) {}
113
+ * }
114
+ * ```
115
+ *
116
+ * @example
117
+ * ```ts
118
+ * // AWS service integration (SQS)
119
+ * @State({ integrationService: 'sqs', action: 'sendMessage', mode: 'token' })
120
+ * send(@IntegrationOptions() { getResourceValue }: GetResourceProps) {
121
+ * return { QueueUrl: getResourceValue('queue::orders', 'id') };
122
+ * }
123
+ * ```
124
+ */
125
+ const State = (props) => (target, methodName, descriptor) => {
126
+ return (0, common_1.createLambdaDecorator)({
127
+ getLambdaMetadata: (props) => ({
128
+ ...props,
129
+ name: methodName,
130
+ }),
131
+ })(props)(target, methodName, descriptor);
132
+ };
133
+ exports.State = State;