@8medusa/workflows-sdk 2.7.0
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/dist/helper/index.d.ts +3 -0
- package/dist/helper/index.d.ts.map +1 -0
- package/dist/helper/index.js +19 -0
- package/dist/helper/index.js.map +1 -0
- package/dist/helper/type.d.ts +66 -0
- package/dist/helper/type.d.ts.map +1 -0
- package/dist/helper/type.js +3 -0
- package/dist/helper/type.js.map +1 -0
- package/dist/helper/workflow-export.d.ts +6 -0
- package/dist/helper/workflow-export.d.ts.map +1 -0
- package/dist/helper/workflow-export.js +253 -0
- package/dist/helper/workflow-export.js.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +34 -0
- package/dist/index.js.map +1 -0
- package/dist/medusa-workflow.d.ts +3 -0
- package/dist/medusa-workflow.d.ts.map +1 -0
- package/dist/medusa-workflow.js +19 -0
- package/dist/medusa-workflow.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/dist/utils/_playground.d.ts +2 -0
- package/dist/utils/_playground.d.ts.map +1 -0
- package/dist/utils/_playground.js +79 -0
- package/dist/utils/_playground.js.map +1 -0
- package/dist/utils/composer/create-hook.d.ts +60 -0
- package/dist/utils/composer/create-hook.d.ts.map +1 -0
- package/dist/utils/composer/create-hook.js +93 -0
- package/dist/utils/composer/create-hook.js.map +1 -0
- package/dist/utils/composer/create-step.d.ts +134 -0
- package/dist/utils/composer/create-step.d.ts.map +1 -0
- package/dist/utils/composer/create-step.js +237 -0
- package/dist/utils/composer/create-step.js.map +1 -0
- package/dist/utils/composer/create-workflow.d.ts +71 -0
- package/dist/utils/composer/create-workflow.d.ts.map +1 -0
- package/dist/utils/composer/create-workflow.js +209 -0
- package/dist/utils/composer/create-workflow.js.map +1 -0
- package/dist/utils/composer/helpers/create-step-handler.d.ts +20 -0
- package/dist/utils/composer/helpers/create-step-handler.d.ts.map +1 -0
- package/dist/utils/composer/helpers/create-step-handler.js +70 -0
- package/dist/utils/composer/helpers/create-step-handler.js.map +1 -0
- package/dist/utils/composer/helpers/index.d.ts +3 -0
- package/dist/utils/composer/helpers/index.d.ts.map +1 -0
- package/dist/utils/composer/helpers/index.js +19 -0
- package/dist/utils/composer/helpers/index.js.map +1 -0
- package/dist/utils/composer/helpers/proxy.d.ts +3 -0
- package/dist/utils/composer/helpers/proxy.d.ts.map +1 -0
- package/dist/utils/composer/helpers/proxy.js +25 -0
- package/dist/utils/composer/helpers/proxy.js.map +1 -0
- package/dist/utils/composer/helpers/resolve-value.d.ts +5 -0
- package/dist/utils/composer/helpers/resolve-value.d.ts.map +1 -0
- package/dist/utils/composer/helpers/resolve-value.js +64 -0
- package/dist/utils/composer/helpers/resolve-value.js.map +1 -0
- package/dist/utils/composer/helpers/step-response.d.ts +126 -0
- package/dist/utils/composer/helpers/step-response.d.ts.map +1 -0
- package/dist/utils/composer/helpers/step-response.js +166 -0
- package/dist/utils/composer/helpers/step-response.js.map +1 -0
- package/dist/utils/composer/helpers/workflow-response.d.ts +20 -0
- package/dist/utils/composer/helpers/workflow-response.d.ts.map +1 -0
- package/dist/utils/composer/helpers/workflow-response.js +16 -0
- package/dist/utils/composer/helpers/workflow-response.js.map +1 -0
- package/dist/utils/composer/index.d.ts +11 -0
- package/dist/utils/composer/index.d.ts.map +1 -0
- package/dist/utils/composer/index.js +27 -0
- package/dist/utils/composer/index.js.map +1 -0
- package/dist/utils/composer/parallelize.d.ts +43 -0
- package/dist/utils/composer/parallelize.d.ts.map +1 -0
- package/dist/utils/composer/parallelize.js +57 -0
- package/dist/utils/composer/parallelize.js.map +1 -0
- package/dist/utils/composer/transform.d.ts +79 -0
- package/dist/utils/composer/transform.d.ts.map +1 -0
- package/dist/utils/composer/transform.js +40 -0
- package/dist/utils/composer/transform.js.map +1 -0
- package/dist/utils/composer/type.d.ts +227 -0
- package/dist/utils/composer/type.d.ts.map +1 -0
- package/dist/utils/composer/type.js +3 -0
- package/dist/utils/composer/type.js.map +1 -0
- package/dist/utils/composer/when.d.ts +58 -0
- package/dist/utils/composer/when.d.ts.map +1 -0
- package/dist/utils/composer/when.js +56 -0
- package/dist/utils/composer/when.js.map +1 -0
- package/package.json +67 -0
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
import { LocalWorkflow, OrchestratorBuilder, TransactionContext as OriginalWorkflowTransactionContext, TransactionModelOptions, TransactionPayload, TransactionStepsDefinition, WorkflowHandler } from "@8medusa/orchestration";
|
|
2
|
+
import { Context, LoadedModule, MedusaContainer } from "@8medusa/types";
|
|
3
|
+
import { ExportedWorkflow } from "../../helper";
|
|
4
|
+
import { Hook } from "./create-hook";
|
|
5
|
+
import { CompensateFn, InvokeFn } from "./create-step";
|
|
6
|
+
export type StepFunctionResult<TOutput extends unknown | unknown[] = unknown> = (this: CreateWorkflowComposerContext) => WorkflowData<TOutput>;
|
|
7
|
+
export type StepFunctionReturnConfig<TOutput> = {
|
|
8
|
+
config(config: {
|
|
9
|
+
name?: string;
|
|
10
|
+
} & Omit<TransactionStepsDefinition, "next" | "uuid" | "action">): WorkflowData<TOutput>;
|
|
11
|
+
};
|
|
12
|
+
type KeysOfUnion<T> = T extends T ? keyof T : never;
|
|
13
|
+
export type HookHandler = (...args: any[]) => void | Promise<void>;
|
|
14
|
+
type ConvertHookToObject<THook> = THook extends Hook<infer Name, infer Input, infer Output> ? {
|
|
15
|
+
[K in Name]: <TCompensateInput>(invoke: InvokeFn<Input, Output, TCompensateInput>, compensate?: CompensateFn<TCompensateInput>) => void;
|
|
16
|
+
} : never;
|
|
17
|
+
/**
|
|
18
|
+
* Helper to convert an array of hooks to functions
|
|
19
|
+
*/
|
|
20
|
+
type ConvertHooksToFunctions<THooks extends any[]> = THooks extends [
|
|
21
|
+
infer A,
|
|
22
|
+
...infer R
|
|
23
|
+
] ? ConvertHookToObject<A> & ConvertHooksToFunctions<R> : {};
|
|
24
|
+
/**
|
|
25
|
+
* A step function to be used in a workflow.
|
|
26
|
+
*
|
|
27
|
+
* @typeParam TInput - The type of the input of the step.
|
|
28
|
+
* @typeParam TOutput - The type of the output of the step.
|
|
29
|
+
*/
|
|
30
|
+
export type StepFunction<TInput, TOutput = unknown> = (KeysOfUnion<TInput> extends [] ? {
|
|
31
|
+
(): WorkflowData<TOutput> & StepFunctionReturnConfig<TOutput>;
|
|
32
|
+
} : {
|
|
33
|
+
(input: WorkflowData<TInput> | TInput): WorkflowData<TOutput> & StepFunctionReturnConfig<TOutput>;
|
|
34
|
+
}) & WorkflowDataProperties<TOutput>;
|
|
35
|
+
export type WorkflowDataProperties<T = unknown> = {
|
|
36
|
+
__type: string;
|
|
37
|
+
__step__: string;
|
|
38
|
+
};
|
|
39
|
+
/**
|
|
40
|
+
* This type is used to encapsulate the input or output type of all utils.
|
|
41
|
+
*
|
|
42
|
+
* @typeParam T - The type of a step's input or result.
|
|
43
|
+
*/
|
|
44
|
+
export type WorkflowData<T = unknown> = (T extends Array<infer Item> ? Array<Item | WorkflowData<Item>> : T extends object ? {
|
|
45
|
+
[Key in keyof T]: T[Key] | WorkflowData<T[Key]>;
|
|
46
|
+
} : T & WorkflowDataProperties<T>) & T & WorkflowDataProperties<T> & {
|
|
47
|
+
config(config: {
|
|
48
|
+
name?: string;
|
|
49
|
+
} & Omit<TransactionStepsDefinition, "next" | "uuid" | "action">): WorkflowData<T>;
|
|
50
|
+
};
|
|
51
|
+
export type CreateWorkflowComposerContext = {
|
|
52
|
+
__type: string;
|
|
53
|
+
hooks_: {
|
|
54
|
+
declared: string[];
|
|
55
|
+
registered: string[];
|
|
56
|
+
};
|
|
57
|
+
hooksCallback_: Record<string, HookHandler>;
|
|
58
|
+
workflowId: string;
|
|
59
|
+
flow: OrchestratorBuilder;
|
|
60
|
+
isAsync: boolean;
|
|
61
|
+
handlers: WorkflowHandler;
|
|
62
|
+
stepBinder: <TOutput = unknown>(fn: StepFunctionResult) => WorkflowData<TOutput>;
|
|
63
|
+
hookBinder: (name: string, fn: () => HookHandler) => void;
|
|
64
|
+
parallelizeBinder: <TOutput extends (WorkflowData | undefined)[] = WorkflowData[]>(fn: (this: CreateWorkflowComposerContext) => TOutput) => TOutput;
|
|
65
|
+
};
|
|
66
|
+
/**
|
|
67
|
+
* The step's context.
|
|
68
|
+
*/
|
|
69
|
+
export interface StepExecutionContext {
|
|
70
|
+
/**
|
|
71
|
+
* The ID of the workflow.
|
|
72
|
+
*/
|
|
73
|
+
workflowId: string;
|
|
74
|
+
/**
|
|
75
|
+
* The attempt number of the step.
|
|
76
|
+
*/
|
|
77
|
+
attempt: number;
|
|
78
|
+
/**
|
|
79
|
+
* The idempoency key of the step.
|
|
80
|
+
*/
|
|
81
|
+
idempotencyKey: string;
|
|
82
|
+
/**
|
|
83
|
+
* The idempoency key of the parent step.
|
|
84
|
+
*/
|
|
85
|
+
parentStepIdempotencyKey?: string;
|
|
86
|
+
/**
|
|
87
|
+
* Whether to prevent release events.
|
|
88
|
+
*/
|
|
89
|
+
preventReleaseEvents?: boolean;
|
|
90
|
+
/**
|
|
91
|
+
* The name of the step.
|
|
92
|
+
*/
|
|
93
|
+
stepName: string;
|
|
94
|
+
/**
|
|
95
|
+
* The action of the step.
|
|
96
|
+
*/
|
|
97
|
+
action: "invoke" | "compensate";
|
|
98
|
+
/**
|
|
99
|
+
* The container used to access resources, such as services, in the step.
|
|
100
|
+
*/
|
|
101
|
+
container: MedusaContainer;
|
|
102
|
+
/**
|
|
103
|
+
* Metadata passed in the input.
|
|
104
|
+
*/
|
|
105
|
+
metadata: TransactionPayload["metadata"];
|
|
106
|
+
/**
|
|
107
|
+
* {@inheritDoc types!Context}
|
|
108
|
+
*/
|
|
109
|
+
context: Context;
|
|
110
|
+
/**
|
|
111
|
+
* A string indicating the ID of the group to aggregate the events to be emitted at a later point.
|
|
112
|
+
*/
|
|
113
|
+
eventGroupId?: string;
|
|
114
|
+
/**
|
|
115
|
+
* A string indicating the ID of the current transaction.
|
|
116
|
+
*/
|
|
117
|
+
transactionId?: string;
|
|
118
|
+
/**
|
|
119
|
+
* Get access to the result returned by a named step. Returns undefined
|
|
120
|
+
* when step is not found or when nothing was returned.
|
|
121
|
+
*
|
|
122
|
+
* Adding a space hides the method from the autocomplete
|
|
123
|
+
*/
|
|
124
|
+
" getStepResult"(stepId: string, action?: "invoke" | "compensate"): any;
|
|
125
|
+
}
|
|
126
|
+
export type WorkflowTransactionContext = StepExecutionContext & OriginalWorkflowTransactionContext & {
|
|
127
|
+
invoke: {
|
|
128
|
+
[key: string]: {
|
|
129
|
+
output: any;
|
|
130
|
+
};
|
|
131
|
+
};
|
|
132
|
+
};
|
|
133
|
+
/**
|
|
134
|
+
* An exported workflow, which is the type of a workflow constructed by the {@link createWorkflow} function. The exported workflow can be invoked to create
|
|
135
|
+
* an executable workflow, optionally within a specified container. So, to execute the workflow, you must invoke the exported workflow, then run the
|
|
136
|
+
* `run` method of the exported workflow.
|
|
137
|
+
*
|
|
138
|
+
* @example
|
|
139
|
+
* To execute a workflow:
|
|
140
|
+
*
|
|
141
|
+
* ```ts
|
|
142
|
+
* myWorkflow()
|
|
143
|
+
* .run({
|
|
144
|
+
* input: {
|
|
145
|
+
* name: "John"
|
|
146
|
+
* }
|
|
147
|
+
* })
|
|
148
|
+
* .then(({ result }) => {
|
|
149
|
+
* console.log(result)
|
|
150
|
+
* })
|
|
151
|
+
* ```
|
|
152
|
+
*
|
|
153
|
+
* To specify the container of the workflow, you can pass it as an argument to the call of the exported workflow. This is necessary when executing the workflow
|
|
154
|
+
* within a Medusa resource such as an API Route or a Subscriber.
|
|
155
|
+
*
|
|
156
|
+
* For example:
|
|
157
|
+
*
|
|
158
|
+
* ```ts
|
|
159
|
+
* import type {
|
|
160
|
+
* MedusaRequest,
|
|
161
|
+
* MedusaResponse
|
|
162
|
+
* } from "@8medusa/medusa";
|
|
163
|
+
* import myWorkflow from "../../../workflows/hello-world";
|
|
164
|
+
*
|
|
165
|
+
* export async function GET(
|
|
166
|
+
* req: MedusaRequest,
|
|
167
|
+
* res: MedusaResponse
|
|
168
|
+
* ) {
|
|
169
|
+
* const { result } = await myWorkflow(req.scope)
|
|
170
|
+
* .run({
|
|
171
|
+
* input: {
|
|
172
|
+
* name: req.query.name as string
|
|
173
|
+
* }
|
|
174
|
+
* })
|
|
175
|
+
*
|
|
176
|
+
* res.send(result)
|
|
177
|
+
* }
|
|
178
|
+
* ```
|
|
179
|
+
*/
|
|
180
|
+
export type ReturnWorkflow<TData, TResult, THooks extends any[]> = {
|
|
181
|
+
<TDataOverride = undefined, TResultOverride = undefined>(container?: LoadedModule[] | MedusaContainer): Omit<LocalWorkflow, "run" | "registerStepSuccess" | "registerStepFailure" | "cancel"> & ExportedWorkflow<TData, TResult, TDataOverride, TResultOverride>;
|
|
182
|
+
} & {
|
|
183
|
+
/**
|
|
184
|
+
* This method executes the workflow as a step. Useful when running a workflow within another.
|
|
185
|
+
*
|
|
186
|
+
* Learn more in [this documentation](https://docs.medusajs.com/learn/fundamentals/workflows/execute-another-workflow).
|
|
187
|
+
*
|
|
188
|
+
* @param param0 - The options to execute the workflow.
|
|
189
|
+
* @returns The workflow's result
|
|
190
|
+
*/
|
|
191
|
+
runAsStep: ({ input, }: {
|
|
192
|
+
/**
|
|
193
|
+
* The workflow's input.
|
|
194
|
+
*/
|
|
195
|
+
input: TData | WorkflowData<TData>;
|
|
196
|
+
}) => ReturnType<StepFunction<TData, TResult>>;
|
|
197
|
+
/**
|
|
198
|
+
* This method executes a workflow.
|
|
199
|
+
*
|
|
200
|
+
* @param args - The options to execute the workflow.
|
|
201
|
+
* @returns Details of the workflow's execution, including its result.
|
|
202
|
+
*/
|
|
203
|
+
run: <TDataOverride = undefined, TResultOverride = undefined>(...args: Parameters<ExportedWorkflow<TData, TResult, TDataOverride, TResultOverride>["run"]>) => ReturnType<ExportedWorkflow<TData, TResult, TDataOverride, TResultOverride>["run"]>;
|
|
204
|
+
/**
|
|
205
|
+
* This method retrieves the workflow's name.
|
|
206
|
+
*/
|
|
207
|
+
getName: () => string;
|
|
208
|
+
/**
|
|
209
|
+
* This method sets the workflow's configurations.
|
|
210
|
+
*/
|
|
211
|
+
config: (config: TransactionModelOptions) => void;
|
|
212
|
+
/**
|
|
213
|
+
* The workflow's exposed hooks, used to register a handler to consume the hook.
|
|
214
|
+
*
|
|
215
|
+
* Learn more in [this documentation](https://docs.medusajs.com/learn/fundamentals/workflows/workflow-hooks#how-to-consume-a-hook).
|
|
216
|
+
*/
|
|
217
|
+
hooks: ConvertHooksToFunctions<THooks>;
|
|
218
|
+
};
|
|
219
|
+
/**
|
|
220
|
+
* Extract the raw type of the expected input data of a workflow.
|
|
221
|
+
*
|
|
222
|
+
* @example
|
|
223
|
+
* type WorkflowInputData = UnwrapWorkflowInputDataType<typeof myWorkflow>
|
|
224
|
+
*/
|
|
225
|
+
export type UnwrapWorkflowInputDataType<T extends ReturnWorkflow<any, any, any>> = T extends ReturnWorkflow<infer TData, infer R, infer THooks> ? TData : never;
|
|
226
|
+
export {};
|
|
227
|
+
//# sourceMappingURL=type.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"type.d.ts","sourceRoot":"","sources":["../../../src/utils/composer/type.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,aAAa,EACb,mBAAmB,EACnB,kBAAkB,IAAI,kCAAkC,EACxD,uBAAuB,EACvB,kBAAkB,EAClB,0BAA0B,EAC1B,eAAe,EAChB,MAAM,wBAAwB,CAAA;AAC/B,OAAO,EAAE,OAAO,EAAE,YAAY,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAA;AACvE,OAAO,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAA;AAC/C,OAAO,EAAE,IAAI,EAAE,MAAM,eAAe,CAAA;AACpC,OAAO,EAAE,YAAY,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAA;AAEtD,MAAM,MAAM,kBAAkB,CAAC,OAAO,SAAS,OAAO,GAAG,OAAO,EAAE,GAAG,OAAO,IAC1E,CAAC,IAAI,EAAE,6BA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AAED;;;;;GAKG;AACH,MAAM,MAAM,2BAA2B,CACrC,CAAC,SAAS,cAAc,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC,IACrC,CAAC,SAAS,cAAc,CAAC,MAAM,KAAK,EAAE,MAAM,CAAC,EAAE,MAAM,MAAM,CAAC,GAAG,KAAK,GAAG,KAAK,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"type.js","sourceRoot":"","sources":["../../../src/utils/composer/type.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { StepExecutionContext, WorkflowData } from "./type";
|
|
2
|
+
type ConditionFunction<T extends object | WorkflowData> = (input: T extends WorkflowData<infer U> ? U : T extends object ? {
|
|
3
|
+
[K in keyof T]: T[K] extends WorkflowData<infer U> ? U : T[K];
|
|
4
|
+
} : {}, context: StepExecutionContext) => boolean;
|
|
5
|
+
type ThenFunc = <ThenResolver extends () => any>(resolver: ThenResolver) => ReturnType<ThenResolver> extends WorkflowData<infer ReturnedWorkflowData> ? WorkflowData<ReturnedWorkflowData> | undefined : ReturnType<ThenResolver>;
|
|
6
|
+
/**
|
|
7
|
+
* This function allows you to execute steps only if a condition is satisfied. As you can't use if conditions in
|
|
8
|
+
* a workflow's constructor function, use `when-then` instead.
|
|
9
|
+
*
|
|
10
|
+
* Learn more about why you can't use if conditions and `when-then` in [this documentation](https://docs.medusajs.com/learn/fundamentals/workflows/conditions).
|
|
11
|
+
*
|
|
12
|
+
* @param values - The data to pass to the second parameter function.
|
|
13
|
+
* @param condition - A function that returns a boolean value, indicating whether the steps in `then` should be executed.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* import {
|
|
17
|
+
* createWorkflow,
|
|
18
|
+
* WorkflowResponse,
|
|
19
|
+
* when,
|
|
20
|
+
* } from "@8medusa/framework/workflows-sdk"
|
|
21
|
+
* // step imports...
|
|
22
|
+
*
|
|
23
|
+
* export const workflow = createWorkflow(
|
|
24
|
+
* "workflow",
|
|
25
|
+
* function (input: {
|
|
26
|
+
* is_active: boolean
|
|
27
|
+
* }) {
|
|
28
|
+
*
|
|
29
|
+
* const result = when(
|
|
30
|
+
* input,
|
|
31
|
+
* (input) => {
|
|
32
|
+
* return input.is_active
|
|
33
|
+
* }
|
|
34
|
+
* ).then(() => {
|
|
35
|
+
* const stepResult = isActiveStep()
|
|
36
|
+
* return stepResult
|
|
37
|
+
* })
|
|
38
|
+
*
|
|
39
|
+
* // executed without condition
|
|
40
|
+
* const anotherStepResult = anotherStep(result)
|
|
41
|
+
*
|
|
42
|
+
* return new WorkflowResponse(
|
|
43
|
+
* anotherStepResult
|
|
44
|
+
* )
|
|
45
|
+
* }
|
|
46
|
+
* )
|
|
47
|
+
*/
|
|
48
|
+
export declare function when<T extends object | WorkflowData, Then extends Function>(values: T, condition: ConditionFunction<T>): {
|
|
49
|
+
then: ThenFunc;
|
|
50
|
+
};
|
|
51
|
+
/**
|
|
52
|
+
* @internal
|
|
53
|
+
*/
|
|
54
|
+
export declare function when<T extends object | WorkflowData, Then extends Function>(name: string, values: T, condition: ConditionFunction<T>): {
|
|
55
|
+
then: ThenFunc;
|
|
56
|
+
};
|
|
57
|
+
export {};
|
|
58
|
+
//# sourceMappingURL=when.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"when.d.ts","sourceRoot":"","sources":["../../../src/utils/composer/when.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,oBAAoB,EAAE,YAAY,EAAE,MAAM,QAAQ,CAAA;AAE3D,KAAK,iBAAiB,CAAC,CAAC,SAAS,MAAM,GAAG,YAAY,IAAI,CACxD,KAAK,EAAE,CAAC,SAAS,YAAY,CAAC,MAAM,CAAC,CAAC,GAClC,CAAC,GACD,CAAC,SAAS,MAAM,GAChB;KAAG,CAAC,IAAI,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,SAAS,YAAY,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;CAAE,GACjE,EAAE,EACN,OAAO,EAAE,oBAAoB,KAC1B,OAAO,CAAA;AAEZ,KAAK,QAAQ,GAAG,CAAC,YAAY,SAAS,MAAM,GAAG,EAC7C,QAAQ,EAAE,YAAY,KACnB,UAAU,CAAC,YAAY,CAAC,SAAS,YAAY,CAAC,MAAM,oBAAoB,CAAC,GAC1E,YAAY,CAAC,oBAAoB,CAAC,GAAG,SAAS,GAC9C,UAAU,CAAC,YAAY,CAAC,CAAA;AAE5B;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAyCG;AACH,wBAAgB,IAAI,CAAC,CAAC,SAAS,MAAM,GAAG,YAAY,EAAE,IAAI,SAAS,QAAQ,EACzE,MAAM,EAAE,CAAC,EACT,SAAS,EAAE,iBAAiB,CAAC,CAAC,CAAC,GAC9B;IACD,IAAI,EAAE,QAAQ,CAAA;CACf,CAAA;AAED;;GAEG;AACH,wBAAgB,IAAI,CAAC,CAAC,SAAS,MAAM,GAAG,YAAY,EAAE,IAAI,SAAS,QAAQ,EACzE,IAAI,EAAE,MAAM,EACZ,MAAM,EAAE,CAAC,EACT,SAAS,EAAE,iBAAiB,CAAC,CAAC,CAAC,GAC9B;IACD,IAAI,EAAE,QAAQ,CAAA;CACf,CAAA"}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.when = when;
|
|
4
|
+
const utils_1 = require("@8medusa/utils");
|
|
5
|
+
const ulid_1 = require("ulid");
|
|
6
|
+
const create_step_1 = require("./create-step");
|
|
7
|
+
const step_response_1 = require("./helpers/step-response");
|
|
8
|
+
/**
|
|
9
|
+
* @internal
|
|
10
|
+
*/
|
|
11
|
+
function when(...args) {
|
|
12
|
+
let [name, input, condition] = args;
|
|
13
|
+
if (args.length === 2) {
|
|
14
|
+
condition = input;
|
|
15
|
+
input = name;
|
|
16
|
+
name = undefined;
|
|
17
|
+
}
|
|
18
|
+
if (typeof condition !== "function") {
|
|
19
|
+
throw new Error(`"when condition" must be a function`);
|
|
20
|
+
}
|
|
21
|
+
global[utils_1.OrchestrationUtils.SymbolMedusaWorkflowComposerCondition] = {
|
|
22
|
+
input,
|
|
23
|
+
condition,
|
|
24
|
+
steps: [],
|
|
25
|
+
};
|
|
26
|
+
let thenCalled = false;
|
|
27
|
+
process.nextTick(() => {
|
|
28
|
+
if (!thenCalled) {
|
|
29
|
+
throw new Error(`".then" is missing after "when" condition`);
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
return {
|
|
33
|
+
then: (fn) => {
|
|
34
|
+
thenCalled = true;
|
|
35
|
+
const ret = fn();
|
|
36
|
+
let returnStep = ret;
|
|
37
|
+
const applyCondition = global[utils_1.OrchestrationUtils.SymbolMedusaWorkflowComposerCondition].steps;
|
|
38
|
+
if ((0, utils_1.isDefined)(ret) &&
|
|
39
|
+
ret?.__type !== utils_1.OrchestrationUtils.SymbolWorkflowStep) {
|
|
40
|
+
if (!(0, utils_1.isDefined)(name)) {
|
|
41
|
+
name = "when-then-" + (0, ulid_1.ulid)();
|
|
42
|
+
const context = global[utils_1.OrchestrationUtils.SymbolMedusaWorkflowComposerContext];
|
|
43
|
+
console.warn(`${context.workflowId}: "when" name should be defined. A random one will be assigned to it, which is not recommended for production.\n`, condition.toString());
|
|
44
|
+
}
|
|
45
|
+
const retStep = (0, create_step_1.createStep)(name, ({ input }) => new step_response_1.StepResponse(input));
|
|
46
|
+
returnStep = retStep({ input: ret });
|
|
47
|
+
}
|
|
48
|
+
for (const step of applyCondition) {
|
|
49
|
+
step.if(input, condition);
|
|
50
|
+
}
|
|
51
|
+
delete global[utils_1.OrchestrationUtils.SymbolMedusaWorkflowComposerCondition];
|
|
52
|
+
return returnStep;
|
|
53
|
+
},
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
//# sourceMappingURL=when.js.map
|
|
@@ -0,0 +1 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IAAA,iBAAS,EAAC,IAAI,CAAC,EAAE,CAAC;oBACrB,IAAI,GAAG,YAAY,GAAG,IAAA,WAAI,GAAE,CAAA;oBAC5B,MAAM,OAAO,GACX,MAAM,CAAC,0BAAkB,CAAC,mCAAmC,CAAC,CAAA;oBAEhE,OAAO,CAAC,IAAI,CACV,GAAG,OAAO,CAAC,UAAU,kHAAkH,EACvI,SAAS,CAAC,QAAQ,EAAE,CACrB,CAAA;gBACH,CAAC;gBAED,MAAM,OAAO,GAAG,IAAA,wBAAU,EACxB,IAAI,EACJ,CAAC,EAAE,KAAK,EAAkB,EAAE,EAAE,CAAC,IAAI,4BAAY,CAAC,KAAK,CAAC,CACvD,CAAA;gBACD,UAAU,GAAG,OAAO,CAAC,EAAE,KAAK,EAAE,GAAG,EAAE,CAAC,CAAA;YACtC,CAAC;YAED,KAAK,MAAM,IAAI,IAAI,cAAc,EAAE,CAAC;gBAClC,IAAI,CAAC,EAAE,CAAC,KAAK,EAAE,SAAS,CAAC,CAAA;YAC3B,CAAC;YAED,OAAO,MAAM,CAAC,0BAAkB,CAAC,qCAAqC,CAAC,CAAA;YAEvE,OAAO,UAAU,CAAA;QACnB,CAAC;KACF,CAAA;AACH,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@8medusa/workflows-sdk",
|
|
3
|
+
"version": "2.7.0",
|
|
4
|
+
"description": "Set of workflows tooling for Medusa",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": "./dist/index.js",
|
|
8
|
+
"./composer": "./dist/utils/composer/index.js"
|
|
9
|
+
},
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "https://github.com/medusajs/medusa",
|
|
13
|
+
"directory": "packages/core/workflows-sdk"
|
|
14
|
+
},
|
|
15
|
+
"engines": {
|
|
16
|
+
"node": ">=20.0.0"
|
|
17
|
+
},
|
|
18
|
+
"publishConfig": {
|
|
19
|
+
"access": "public"
|
|
20
|
+
},
|
|
21
|
+
"files": [
|
|
22
|
+
"dist",
|
|
23
|
+
"!dist/**/__tests__",
|
|
24
|
+
"!dist/**/__mocks__",
|
|
25
|
+
"!dist/**/__fixtures__"
|
|
26
|
+
],
|
|
27
|
+
"author": "Medusa",
|
|
28
|
+
"license": "MIT",
|
|
29
|
+
"devDependencies": {
|
|
30
|
+
"@mikro-orm/core": "6.4.3",
|
|
31
|
+
"@mikro-orm/knex": "6.4.3",
|
|
32
|
+
"@mikro-orm/migrations": "6.4.3",
|
|
33
|
+
"@mikro-orm/postgresql": "6.4.3",
|
|
34
|
+
"@swc/core": "^1.7.28",
|
|
35
|
+
"@swc/jest": "^0.2.36",
|
|
36
|
+
"awilix": "^8.0.1",
|
|
37
|
+
"expect-type": "^0.20.0",
|
|
38
|
+
"jest": "^29.7.0",
|
|
39
|
+
"pg": "^8.13.0",
|
|
40
|
+
"rimraf": "^5.0.1",
|
|
41
|
+
"typescript": "^5.6.2",
|
|
42
|
+
"zod": "3.22.4"
|
|
43
|
+
},
|
|
44
|
+
"dependencies": {
|
|
45
|
+
"@8medusa/modules-sdk": "2.7.0",
|
|
46
|
+
"@8medusa/orchestration": "2.7.0",
|
|
47
|
+
"@8medusa/types": "2.7.0",
|
|
48
|
+
"@8medusa/utils": "2.7.0",
|
|
49
|
+
"ulid": "^2.3.0"
|
|
50
|
+
},
|
|
51
|
+
"peerDependencies": {
|
|
52
|
+
"@mikro-orm/core": "6.4.3",
|
|
53
|
+
"@mikro-orm/knex": "6.4.3",
|
|
54
|
+
"@mikro-orm/migrations": "6.4.3",
|
|
55
|
+
"@mikro-orm/postgresql": "6.4.3",
|
|
56
|
+
"awilix": "^8.0.1",
|
|
57
|
+
"express": "^4.21.0",
|
|
58
|
+
"pg": "^8.13.0",
|
|
59
|
+
"zod": "3.22.4"
|
|
60
|
+
},
|
|
61
|
+
"scripts": {
|
|
62
|
+
"build": "rimraf dist && tsc --build",
|
|
63
|
+
"watch": "tsc --build --watch",
|
|
64
|
+
"test": "jest --runInBand --bail --forceExit",
|
|
65
|
+
"test:run": "node ./dist/utils/_playground.js"
|
|
66
|
+
}
|
|
67
|
+
}
|