@medusajs/workflows-sdk 0.2.0-next-20231124140416

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 (55) hide show
  1. package/dist/helper/empty-handler.d.ts +1 -0
  2. package/dist/helper/empty-handler.js +6 -0
  3. package/dist/helper/empty-handler.js.map +1 -0
  4. package/dist/helper/index.d.ts +4 -0
  5. package/dist/helper/index.js +21 -0
  6. package/dist/helper/index.js.map +1 -0
  7. package/dist/helper/merge-data.d.ts +8 -0
  8. package/dist/helper/merge-data.js +47 -0
  9. package/dist/helper/merge-data.js.map +1 -0
  10. package/dist/helper/pipe.d.ts +52 -0
  11. package/dist/helper/pipe.js +80 -0
  12. package/dist/helper/pipe.js.map +1 -0
  13. package/dist/helper/workflow-export.d.ts +16 -0
  14. package/dist/helper/workflow-export.js +69 -0
  15. package/dist/helper/workflow-export.js.map +1 -0
  16. package/dist/index.d.ts +3 -0
  17. package/dist/index.js +33 -0
  18. package/dist/index.js.map +1 -0
  19. package/dist/utils/composer/create-step.d.ts +105 -0
  20. package/dist/utils/composer/create-step.js +153 -0
  21. package/dist/utils/composer/create-step.js.map +1 -0
  22. package/dist/utils/composer/create-workflow.d.ts +124 -0
  23. package/dist/utils/composer/create-workflow.js +136 -0
  24. package/dist/utils/composer/create-workflow.js.map +1 -0
  25. package/dist/utils/composer/helpers/index.d.ts +3 -0
  26. package/dist/utils/composer/helpers/index.js +20 -0
  27. package/dist/utils/composer/helpers/index.js.map +1 -0
  28. package/dist/utils/composer/helpers/proxy.d.ts +2 -0
  29. package/dist/utils/composer/helpers/proxy.js +27 -0
  30. package/dist/utils/composer/helpers/proxy.js.map +1 -0
  31. package/dist/utils/composer/helpers/resolve-value.d.ts +4 -0
  32. package/dist/utils/composer/helpers/resolve-value.js +59 -0
  33. package/dist/utils/composer/helpers/resolve-value.js.map +1 -0
  34. package/dist/utils/composer/helpers/step-response.d.ts +48 -0
  35. package/dist/utils/composer/helpers/step-response.js +80 -0
  36. package/dist/utils/composer/helpers/step-response.js.map +1 -0
  37. package/dist/utils/composer/helpers/symbol.d.ts +8 -0
  38. package/dist/utils/composer/helpers/symbol.js +12 -0
  39. package/dist/utils/composer/helpers/symbol.js.map +1 -0
  40. package/dist/utils/composer/hook.d.ts +94 -0
  41. package/dist/utils/composer/hook.js +125 -0
  42. package/dist/utils/composer/hook.js.map +1 -0
  43. package/dist/utils/composer/index.d.ts +9 -0
  44. package/dist/utils/composer/index.js +26 -0
  45. package/dist/utils/composer/index.js.map +1 -0
  46. package/dist/utils/composer/parallelize.d.ts +42 -0
  47. package/dist/utils/composer/parallelize.js +58 -0
  48. package/dist/utils/composer/parallelize.js.map +1 -0
  49. package/dist/utils/composer/transform.d.ts +83 -0
  50. package/dist/utils/composer/transform.js +29 -0
  51. package/dist/utils/composer/transform.js.map +1 -0
  52. package/dist/utils/composer/type.d.ts +70 -0
  53. package/dist/utils/composer/type.js +3 -0
  54. package/dist/utils/composer/type.js.map +1 -0
  55. package/package.json +41 -0
@@ -0,0 +1,124 @@
1
+ import { LocalWorkflow } from "@medusajs/orchestration";
2
+ import { LoadedModule, MedusaContainer } from "@medusajs/types";
3
+ import { FlowRunOptions, WorkflowResult } from "../../helper";
4
+ import { WorkflowData, WorkflowDataProperties } from "./type";
5
+ /**
6
+ * An exported workflow, which is the type of a workflow constructed by the {@link createWorkflow} function. The exported workflow can be invoked to create
7
+ * an executable workflow, optionally within a specified container. So, to execute the workflow, you must invoke the exported workflow, then run the
8
+ * `run` method of the exported workflow.
9
+ *
10
+ * @example
11
+ * To execute a workflow:
12
+ *
13
+ * ```ts
14
+ * myWorkflow()
15
+ * .run({
16
+ * input: {
17
+ * name: "John"
18
+ * }
19
+ * })
20
+ * .then(({ result }) => {
21
+ * console.log(result)
22
+ * })
23
+ * ```
24
+ *
25
+ * 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
26
+ * within a Medusa resource such as an API Route or a Subscriber.
27
+ *
28
+ * For example:
29
+ *
30
+ * ```ts
31
+ * import type {
32
+ * MedusaRequest,
33
+ * MedusaResponse
34
+ * } from "@medusajs/medusa";
35
+ * import myWorkflow from "../../../workflows/hello-world";
36
+ *
37
+ * export async function GET(
38
+ * req: MedusaRequest,
39
+ * res: MedusaResponse
40
+ * ) {
41
+ * const { result } = await myWorkflow(req.scope)
42
+ * .run({
43
+ * input: {
44
+ * name: req.query.name as string
45
+ * }
46
+ * })
47
+ *
48
+ * res.send(result)
49
+ * }
50
+ * ```
51
+ */
52
+ type ReturnWorkflow<TData, TResult, THooks extends Record<string, Function>> = {
53
+ <TDataOverride = undefined, TResultOverride = undefined>(container?: LoadedModule[] | MedusaContainer): Omit<LocalWorkflow, "run"> & {
54
+ run: (args?: FlowRunOptions<TDataOverride extends undefined ? TData : TDataOverride>) => Promise<WorkflowResult<TResultOverride extends undefined ? TResult : TResultOverride>>;
55
+ };
56
+ } & THooks;
57
+ /**
58
+ * This function creates a workflow with the provided name and a constructor function.
59
+ * The constructor function builds the workflow from steps created by the {@link createStep} function.
60
+ * The returned workflow is an exported workflow of type {@link ReturnWorkflow}, meaning it's not executed right away. To execute it,
61
+ * invoke the exported workflow, then run its `run` method.
62
+ *
63
+ * @typeParam TData - The type of the input passed to the composer function.
64
+ * @typeParam TResult - The type of the output returned by the composer function.
65
+ * @typeParam THooks - The type of hooks defined in the workflow.
66
+ *
67
+ * @returns The created workflow. You can later execute the workflow by invoking it, then using its `run` method.
68
+ *
69
+ * @example
70
+ * import { createWorkflow } from "@medusajs/workflows-sdk"
71
+ * import { MedusaRequest, MedusaResponse, Product } from "@medusajs/medusa"
72
+ * import {
73
+ * createProductStep,
74
+ * getProductStep,
75
+ * createPricesStep
76
+ * } from "./steps"
77
+ *
78
+ * interface WorkflowInput {
79
+ * title: string
80
+ * }
81
+ *
82
+ * const myWorkflow = createWorkflow<
83
+ * WorkflowInput,
84
+ * Product
85
+ * >("my-workflow", (input) => {
86
+ * // Everything here will be executed and resolved later
87
+ * // during the execution. Including the data access.
88
+ *
89
+ * const product = createProductStep(input)
90
+ * const prices = createPricesStep(product)
91
+ * return getProductStep(product.id)
92
+ * }
93
+ * )
94
+ *
95
+ * export async function GET(
96
+ * req: MedusaRequest,
97
+ * res: MedusaResponse
98
+ * ) {
99
+ * const { result: product } = await myWorkflow(req.scope)
100
+ * .run({
101
+ * input: {
102
+ * title: "Shirt"
103
+ * }
104
+ * })
105
+ *
106
+ * res.json({
107
+ * product
108
+ * })
109
+ * }
110
+ */
111
+ export declare function createWorkflow<TData, TResult, THooks extends Record<string, Function> = Record<string, Function>>(
112
+ /**
113
+ * The name of the workflow.
114
+ */
115
+ name: string,
116
+ /**
117
+ * The constructor function that is executed when the `run` method in {@link ReturnWorkflow} is used.
118
+ * The function can't be an arrow function or an asynchronus function. It also can't directly manipulate data.
119
+ * You'll have to use the {@link transform} function if you need to directly manipulate data.
120
+ */
121
+ composer: (input: WorkflowData<TData>) => void | WorkflowData<TResult> | {
122
+ [K in keyof TResult]: WorkflowData<TResult[K]> | WorkflowDataProperties<TResult[K]>;
123
+ }): ReturnWorkflow<TData, TResult, THooks>;
124
+ export {};
@@ -0,0 +1,136 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createWorkflow = void 0;
4
+ const orchestration_1 = require("@medusajs/orchestration");
5
+ const helper_1 = require("../../helper");
6
+ const helpers_1 = require("./helpers");
7
+ const proxy_1 = require("./helpers/proxy");
8
+ global[helpers_1.SymbolMedusaWorkflowComposerContext] = null;
9
+ /**
10
+ * This function creates a workflow with the provided name and a constructor function.
11
+ * The constructor function builds the workflow from steps created by the {@link createStep} function.
12
+ * The returned workflow is an exported workflow of type {@link ReturnWorkflow}, meaning it's not executed right away. To execute it,
13
+ * invoke the exported workflow, then run its `run` method.
14
+ *
15
+ * @typeParam TData - The type of the input passed to the composer function.
16
+ * @typeParam TResult - The type of the output returned by the composer function.
17
+ * @typeParam THooks - The type of hooks defined in the workflow.
18
+ *
19
+ * @returns The created workflow. You can later execute the workflow by invoking it, then using its `run` method.
20
+ *
21
+ * @example
22
+ * import { createWorkflow } from "@medusajs/workflows-sdk"
23
+ * import { MedusaRequest, MedusaResponse, Product } from "@medusajs/medusa"
24
+ * import {
25
+ * createProductStep,
26
+ * getProductStep,
27
+ * createPricesStep
28
+ * } from "./steps"
29
+ *
30
+ * interface WorkflowInput {
31
+ * title: string
32
+ * }
33
+ *
34
+ * const myWorkflow = createWorkflow<
35
+ * WorkflowInput,
36
+ * Product
37
+ * >("my-workflow", (input) => {
38
+ * // Everything here will be executed and resolved later
39
+ * // during the execution. Including the data access.
40
+ *
41
+ * const product = createProductStep(input)
42
+ * const prices = createPricesStep(product)
43
+ * return getProductStep(product.id)
44
+ * }
45
+ * )
46
+ *
47
+ * export async function GET(
48
+ * req: MedusaRequest,
49
+ * res: MedusaResponse
50
+ * ) {
51
+ * const { result: product } = await myWorkflow(req.scope)
52
+ * .run({
53
+ * input: {
54
+ * title: "Shirt"
55
+ * }
56
+ * })
57
+ *
58
+ * res.json({
59
+ * product
60
+ * })
61
+ * }
62
+ */
63
+ function createWorkflow(
64
+ /**
65
+ * The name of the workflow.
66
+ */
67
+ name,
68
+ /**
69
+ * The constructor function that is executed when the `run` method in {@link ReturnWorkflow} is used.
70
+ * The function can't be an arrow function or an asynchronus function. It also can't directly manipulate data.
71
+ * You'll have to use the {@link transform} function if you need to directly manipulate data.
72
+ */
73
+ composer) {
74
+ const handlers = new Map();
75
+ if (orchestration_1.WorkflowManager.getWorkflow(name)) {
76
+ orchestration_1.WorkflowManager.unregister(name);
77
+ }
78
+ orchestration_1.WorkflowManager.register(name, undefined, handlers);
79
+ const context = {
80
+ workflowId: name,
81
+ flow: orchestration_1.WorkflowManager.getTransactionDefinition(name),
82
+ handlers,
83
+ hooks_: [],
84
+ hooksCallback_: {},
85
+ hookBinder: (name, fn) => {
86
+ context.hooks_.push(name);
87
+ return fn(context);
88
+ },
89
+ stepBinder: (fn) => {
90
+ return fn.bind(context)();
91
+ },
92
+ parallelizeBinder: (fn) => {
93
+ return fn.bind(context)();
94
+ },
95
+ };
96
+ global[helpers_1.SymbolMedusaWorkflowComposerContext] = context;
97
+ const inputPlaceHolder = (0, proxy_1.proxify)({
98
+ __type: helpers_1.SymbolInputReference,
99
+ __step__: "",
100
+ });
101
+ const returnedStep = composer.apply(context, [inputPlaceHolder]);
102
+ delete global[helpers_1.SymbolMedusaWorkflowComposerContext];
103
+ orchestration_1.WorkflowManager.update(name, context.flow, handlers);
104
+ const workflow = (0, helper_1.exportWorkflow)(name);
105
+ const mainFlow = (container) => {
106
+ const workflow_ = workflow(container);
107
+ const originalRun = workflow_.run;
108
+ workflow_.run = (async (args) => {
109
+ args ?? (args = {});
110
+ args.resultFrom ?? (args.resultFrom = returnedStep?.__type === helpers_1.SymbolWorkflowStep
111
+ ? returnedStep.__step__
112
+ : undefined);
113
+ // Forwards the input to the ref object on composer.apply
114
+ const workflowResult = (await originalRun(args));
115
+ workflowResult.result = await (0, helpers_1.resolveValue)(workflowResult.result || returnedStep, workflowResult.transaction.getContext());
116
+ return workflowResult;
117
+ });
118
+ return workflow_;
119
+ };
120
+ let shouldRegisterHookHandler = true;
121
+ for (const hook of context.hooks_) {
122
+ mainFlow[hook] = (fn) => {
123
+ var _a;
124
+ (_a = context.hooksCallback_)[hook] ?? (_a[hook] = []);
125
+ if (!shouldRegisterHookHandler) {
126
+ console.warn(`A hook handler has already been registered for the ${hook} hook. The current handler registration will be skipped.`);
127
+ return;
128
+ }
129
+ context.hooksCallback_[hook].push(fn);
130
+ shouldRegisterHookHandler = false;
131
+ };
132
+ }
133
+ return mainFlow;
134
+ }
135
+ exports.createWorkflow = createWorkflow;
136
+ //# sourceMappingURL=create-workflow.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"create-workflow.js","sourceRoot":"","sources":["../../../src/utils/composer/create-workflow.ts"],"names":[],"mappings":";;;AAAA,2DAIgC;AAEhC,yCAA6E;AAM7E,uCAKkB;AACl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AyB,EAAE;gBAC9B,OAAO,CAAC,IAAI,CACV,sDAAsD,IAAI,0DAA0D,CACrH,CAAA;gBACD,OAAM;aACP;YAED,OAAO,CAAC,cAAc,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,CAAA;YACrC,yBAAyB,GAAG,KAAK,CAAA;QACnC,CAAC,CAAA;KACF;IAED,OAAO,QAAkD,CAAA;AAC3D,CAAC;AA1HD,wCA0HC"}
@@ -0,0 +1,3 @@
1
+ export * from "./step-response";
2
+ export * from "./symbol";
3
+ export * from "./resolve-value";
@@ -0,0 +1,20 @@
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("./step-response"), exports);
18
+ __exportStar(require("./symbol"), exports);
19
+ __exportStar(require("./resolve-value"), exports);
20
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/utils/composer/helpers/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,kDAA+B;AAC/B,2CAAwB;AACxB,kDAA+B"}
@@ -0,0 +1,2 @@
1
+ import { WorkflowData } from "../type";
2
+ export declare function proxify<T>(obj: WorkflowData<any>): T;
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.proxify = void 0;
4
+ const transform_1 = require("../transform");
5
+ const symbol_1 = require("./symbol");
6
+ const resolve_value_1 = require("./resolve-value");
7
+ function proxify(obj) {
8
+ return new Proxy(obj, {
9
+ get(target, prop) {
10
+ if (prop in target) {
11
+ return target[prop];
12
+ }
13
+ return (0, transform_1.transform)(target[prop], async function (input, context) {
14
+ const { invoke } = context;
15
+ let output = target.__type === symbol_1.SymbolInputReference ||
16
+ target.__type === symbol_1.SymbolWorkflowStepTransformer
17
+ ? target
18
+ : invoke?.[obj.__step__]?.output;
19
+ output = await (0, resolve_value_1.resolveValue)(output, context);
20
+ output = output?.[prop];
21
+ return output && JSON.parse(JSON.stringify(output));
22
+ });
23
+ },
24
+ });
25
+ }
26
+ exports.proxify = proxify;
27
+ //# sourceMappingURL=proxy.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"proxy.js","sourceRoot":"","sources":["../../../../src/utils/composer/helpers/proxy.ts"],"names":[],"mappings":";;;AAAA,4CAAwC;AAExC,qCAA8E;AAC9E,mDAA8C;AAE9C,SAAgB,OAAO,CAAI,GAAsB;IAC/C,OAAO,IAAI,KAAK,CAAC,GAAG,EAAE;QACpB,GAAG,CAAC,MAAW,EAAE,IAAqB;YACpC,IAAI,IAAI,IAAI,MAAM,EAAE;gBAClB,OAAO,MAAM,CAAC,IAAI,CAAC,CAAA;aACpB;YAED,OAAO,IAAA,qBAAS,EAAC,MAAM,CAAC,IAAI,CAAC,EAAE,KAAK,WAAW,KAAK,EAAE,OAAO;gBAC3D,MAAM,EAAE,MAAM,EAAE,GAAG,OAAqC,CAAA;gBACxD,IAAI,MAAM,GACR,MAAM,CAAC,MAAM,KAAK,6BAAoB;oBACtC,MAAM,CAAC,MAAM,KAAK,sCAA6B;oBAC7C,CAAC,CAAC,MAAM;oBACR,CAAC,CAAC,MAAM,EAAE,CAAC,GAAG,CAAC,QAAQ,CAAC,EAAE,MAAM,CAAA;gBAEpC,MAAM,GAAG,MAAM,IAAA,4BAAY,EAAC,MAAM,EAAE,OAAO,CAAC,CAAA;gBAC5C,MAAM,GAAG,MAAM,EAAE,CAAC,IAAI,CAAC,CAAA;gBAEvB,OAAO,MAAM,IAAI,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC,CAAA;YACrD,CAAC,CAAC,CAAA;QACJ,CAAC;KACF,CAAiB,CAAA;AACpB,CAAC;AAtBD,0BAsBC"}
@@ -0,0 +1,4 @@
1
+ /**
2
+ * @internal
3
+ */
4
+ export declare function resolveValue(input: any, transactionContext: any): Promise<any>;
@@ -0,0 +1,59 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.resolveValue = void 0;
4
+ const utils_1 = require("@medusajs/utils");
5
+ const symbol_1 = require("./symbol");
6
+ async function resolveProperty(property, transactionContext) {
7
+ const { invoke: invokeRes } = transactionContext;
8
+ if (property?.__type === symbol_1.SymbolInputReference) {
9
+ return transactionContext.payload;
10
+ }
11
+ else if (property?.__type === symbol_1.SymbolWorkflowStepTransformer) {
12
+ return await property.__resolver(transactionContext);
13
+ }
14
+ else if (property?.__type === symbol_1.SymbolWorkflowHook) {
15
+ return await property.__value(transactionContext);
16
+ }
17
+ else if (property?.__type === symbol_1.SymbolWorkflowStep) {
18
+ const output = invokeRes[property.__step__]?.output;
19
+ if (output?.__type === symbol_1.SymbolWorkflowStepResponse) {
20
+ return output.output;
21
+ }
22
+ return output;
23
+ }
24
+ else if (property?.__type === symbol_1.SymbolWorkflowStepResponse) {
25
+ return property.output;
26
+ }
27
+ else {
28
+ return property;
29
+ }
30
+ }
31
+ /**
32
+ * @internal
33
+ */
34
+ async function resolveValue(input, transactionContext) {
35
+ const unwrapInput = async (inputTOUnwrap, parentRef) => {
36
+ if (inputTOUnwrap == null) {
37
+ return inputTOUnwrap;
38
+ }
39
+ if (Array.isArray(inputTOUnwrap)) {
40
+ return await (0, utils_1.promiseAll)(inputTOUnwrap.map((i) => unwrapInput(i, transactionContext)));
41
+ }
42
+ if (typeof inputTOUnwrap !== "object") {
43
+ return inputTOUnwrap;
44
+ }
45
+ for (const key of Object.keys(inputTOUnwrap)) {
46
+ parentRef[key] = await resolveProperty(inputTOUnwrap[key], transactionContext);
47
+ if (typeof parentRef[key] === "object") {
48
+ await unwrapInput(parentRef[key], parentRef[key]);
49
+ }
50
+ }
51
+ return parentRef;
52
+ };
53
+ const result = input?.__type
54
+ ? await resolveProperty(input, transactionContext)
55
+ : await unwrapInput(input, {});
56
+ return result && JSON.parse(JSON.stringify(result));
57
+ }
58
+ exports.resolveValue = resolveValue;
59
+ //# sourceMappingURL=resolve-value.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"resolve-value.js","sourceRoot":"","sources":["../../../../src/utils/composer/helpers/resolve-value.ts"],"names":[],"mappings":";;;AAAA,2CAA4C;AAC5C,qCAMiB;AAEjB,KAAK,UAAU,eAAe,CAAC,QAAQ,EAAE,kBAAkB;IACzD,MAAM,EAAE,MAAM,EAAE,SAAS,EAAE,GAAG,kBAAkB,CAAA;IAEhD,IAAI,QAAQ,EAAE,MAAM,KAAK,6BAAoB,EAAE;QAC7C,OAAO,kBAAkB,CAAC,OAAO,CAAA;KAClC;SAAM,IAAI,QAAQ,EAAE,MAAM,KAAK,sCAA6B,EAAE;QAC7D,OAAO,MAAM,QAAQ,CAAC,UAAU,CAAC,kBAAkB,CAAC,CAAA;KACrD;SAAM,IAAI,QAAQ,EAAE,MAAM,KAAK,2BAAkB,EAAE;QAClD,OAAO,MAAM,QAAQ,CAAC,OAAO,CAAC,kBAAkB,CAAC,CAAA;KAClD;SAAM,IAAI,QAAQ,EAAE,MAAM,KAAK,2BAAkB,EAAE;QAClD,MAAM,MAAM,GAAG,SAAS,CAAC,QAAQ,CAAC,QAAQ,CAAC,EAAE,MAAM,CAAA;QACnD,IAAI,MAAM,EAAE,MAAM,KAAK,mCAA0B,EAAE;YACjD,OAAO,MAAM,CAAC,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IAAI,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC,CAAA;AACrD,CAAC;AAtCD,oCAsCC"}
@@ -0,0 +1,48 @@
1
+ /**
2
+ * This class is used to create the response returned by a step. A step return its data by returning an instance of `StepResponse`.
3
+ *
4
+ * @typeParam TOutput - The type of the output of the step.
5
+ * @typeParam TCompensateInput -
6
+ * The type of the compensation input. If the step doesn't specify any compensation input, then the type of `TCompensateInput` is the same
7
+ * as that of `TOutput`.
8
+ */
9
+ export declare class StepResponse<TOutput, TCompensateInput = TOutput> {
10
+ #private;
11
+ /**
12
+ * The constructor of the StepResponse
13
+ *
14
+ * @typeParam TOutput - The type of the output of the step.
15
+ * @typeParam TCompensateInput -
16
+ * The type of the compensation input. If the step doesn't specify any compensation input, then the type of `TCompensateInput` is the same
17
+ * as that of `TOutput`.
18
+ */
19
+ constructor(
20
+ /**
21
+ * The output of the step.
22
+ */
23
+ output: TOutput,
24
+ /**
25
+ * The input to be passed as a parameter to the step's compensation function. If not provided, the `output` will be provided instead.
26
+ */
27
+ compensateInput?: TCompensateInput);
28
+ /**
29
+ * @internal
30
+ */
31
+ get __type(): symbol;
32
+ /**
33
+ * @internal
34
+ */
35
+ get output(): TOutput;
36
+ /**
37
+ * @internal
38
+ */
39
+ get compensateInput(): TCompensateInput;
40
+ /**
41
+ * @internal
42
+ */
43
+ toJSON(): {
44
+ __type: symbol;
45
+ output: TOutput;
46
+ compensateInput: TCompensateInput | undefined;
47
+ };
48
+ }
@@ -0,0 +1,80 @@
1
+ "use strict";
2
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
3
+ if (kind === "m") throw new TypeError("Private method is not writable");
4
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
5
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
6
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
7
+ };
8
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
9
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
10
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
11
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
12
+ };
13
+ var _StepResponse___type, _StepResponse_output, _StepResponse_compensateInput;
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.StepResponse = void 0;
16
+ const symbol_1 = require("./symbol");
17
+ /**
18
+ * This class is used to create the response returned by a step. A step return its data by returning an instance of `StepResponse`.
19
+ *
20
+ * @typeParam TOutput - The type of the output of the step.
21
+ * @typeParam TCompensateInput -
22
+ * The type of the compensation input. If the step doesn't specify any compensation input, then the type of `TCompensateInput` is the same
23
+ * as that of `TOutput`.
24
+ */
25
+ class StepResponse {
26
+ /**
27
+ * The constructor of the StepResponse
28
+ *
29
+ * @typeParam TOutput - The type of the output of the step.
30
+ * @typeParam TCompensateInput -
31
+ * The type of the compensation input. If the step doesn't specify any compensation input, then the type of `TCompensateInput` is the same
32
+ * as that of `TOutput`.
33
+ */
34
+ constructor(
35
+ /**
36
+ * The output of the step.
37
+ */
38
+ output,
39
+ /**
40
+ * The input to be passed as a parameter to the step's compensation function. If not provided, the `output` will be provided instead.
41
+ */
42
+ compensateInput) {
43
+ _StepResponse___type.set(this, symbol_1.SymbolWorkflowStepResponse);
44
+ _StepResponse_output.set(this, void 0);
45
+ _StepResponse_compensateInput.set(this, void 0);
46
+ __classPrivateFieldSet(this, _StepResponse_output, output, "f");
47
+ __classPrivateFieldSet(this, _StepResponse_compensateInput, (compensateInput ?? output), "f");
48
+ }
49
+ /**
50
+ * @internal
51
+ */
52
+ get __type() {
53
+ return __classPrivateFieldGet(this, _StepResponse___type, "f");
54
+ }
55
+ /**
56
+ * @internal
57
+ */
58
+ get output() {
59
+ return __classPrivateFieldGet(this, _StepResponse_output, "f");
60
+ }
61
+ /**
62
+ * @internal
63
+ */
64
+ get compensateInput() {
65
+ return __classPrivateFieldGet(this, _StepResponse_compensateInput, "f");
66
+ }
67
+ /**
68
+ * @internal
69
+ */
70
+ toJSON() {
71
+ return {
72
+ __type: __classPrivateFieldGet(this, _StepResponse___type, "f"),
73
+ output: __classPrivateFieldGet(this, _StepResponse_output, "f"),
74
+ compensateInput: __classPrivateFieldGet(this, _StepResponse_compensateInput, "f"),
75
+ };
76
+ }
77
+ }
78
+ exports.StepResponse = StepResponse;
79
+ _StepResponse___type = new WeakMap(), _StepResponse_output = new WeakMap(), _StepResponse_compensateInput = new WeakMap();
80
+ //# sourceMappingURL=step-response.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"step-response.js","sourceRoot":"","sources":["../../../../src/utils/composer/helpers/step-response.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,qCAAqD;AAErD;;;;;;;GAOG;AACH,MAAa,YAAY;IAKvB;;;;;;;OAOG;IACH;IACE;;OAEG;IACH,MAAe;IACf;;OAEG;IACH,eAAkC;QApB3B,+BAAU,mCAA0B,EAAA;QACpC,uCAAgB;QAChB,gDAAmC;QAoB1C,uBAAA,IAAI,wBAAW,MAAM,MAAA,CAAA;QACrB,uBAAA,IAAI,iCAAoB,CAAC,eAAe,IAAI,MAAM,CAAqB,MAAA,CAAA;IACzE,CAAC;IAED;;OAEG;IACH,IAAI,MAAM;QACR,OAAO,uBAAA,IAAI,4BAAQ,CAAA;IACrB,CAAC;IAED;;OAEG;IACH,IAAI,MAAM;QACR,OAAO,uBAAA,IAAI,4BAAQ,CAAA;IACrB,CAAC;IAED;;OAEG;IACH,IAAI,eAAe;QACjB,OAAO,uBAAA,IAAI,qCAAqC,CAAA;IAClD,CAAC;IAED;;OAEG;IACH,MAAM;QACJ,OAAO;YACL,MAAM,EAAE,uBAAA,IAAI,4BAAQ;YACpB,MAAM,EAAE,uBAAA,IAAI,4BAAQ;YACpB,eAAe,EAAE,uBAAA,IAAI,qCAAiB;SACvC,CAAA;IACH,CAAC;CACF;AA1DD,oCA0DC"}
@@ -0,0 +1,8 @@
1
+ export declare const SymbolMedusaWorkflowComposerContext: unique symbol;
2
+ export declare const SymbolInputReference: unique symbol;
3
+ export declare const SymbolWorkflowStep: unique symbol;
4
+ export declare const SymbolWorkflowHook: unique symbol;
5
+ export declare const SymbolWorkflowWorkflowData: unique symbol;
6
+ export declare const SymbolWorkflowStepResponse: unique symbol;
7
+ export declare const SymbolWorkflowStepBind: unique symbol;
8
+ export declare const SymbolWorkflowStepTransformer: unique symbol;
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SymbolWorkflowStepTransformer = exports.SymbolWorkflowStepBind = exports.SymbolWorkflowStepResponse = exports.SymbolWorkflowWorkflowData = exports.SymbolWorkflowHook = exports.SymbolWorkflowStep = exports.SymbolInputReference = exports.SymbolMedusaWorkflowComposerContext = void 0;
4
+ exports.SymbolMedusaWorkflowComposerContext = Symbol.for("MedusaWorkflowComposerContext");
5
+ exports.SymbolInputReference = Symbol.for("WorkflowInputReference");
6
+ exports.SymbolWorkflowStep = Symbol.for("WorkflowStep");
7
+ exports.SymbolWorkflowHook = Symbol.for("WorkflowHook");
8
+ exports.SymbolWorkflowWorkflowData = Symbol.for("WorkflowWorkflowData");
9
+ exports.SymbolWorkflowStepResponse = Symbol.for("WorkflowStepResponse");
10
+ exports.SymbolWorkflowStepBind = Symbol.for("WorkflowStepBind");
11
+ exports.SymbolWorkflowStepTransformer = Symbol.for("WorkflowStepTransformer");
12
+ //# sourceMappingURL=symbol.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"symbol.js","sourceRoot":"","sources":["../../../../src/utils/composer/helpers/symbol.ts"],"names":[],"mappings":";;;AAAa,QAAA,mCAAmC,GAAG,MAAM,CAAC,GAAG,CAC3D,+BAA+B,CAChC,CAAA;AACY,QAAA,oBAAoB,GAAG,MAAM,CAAC,GAAG,CAAC,wBAAwB,CAAC,CAAA;AAC3D,QAAA,kBAAkB,GAAG,MAAM,CAAC,GAAG,CAAC,cAAc,CAAC,CAAA;AAC/C,QAAA,kBAAkB,GAAG,MAAM,CAAC,GAAG,CAAC,cAAc,CAAC,CAAA;AAC/C,QAAA,0BAA0B,GAAG,MAAM,CAAC,GAAG,CAAC,sBAAsB,CAAC,CAAA;AAC/D,QAAA,0BAA0B,GAAG,MAAM,CAAC,GAAG,CAAC,sBAAsB,CAAC,CAAA;AAC/D,QAAA,sBAAsB,GAAG,MAAM,CAAC,GAAG,CAAC,kBAAkB,CAAC,CAAA;AACvD,QAAA,6BAA6B,GAAG,MAAM,CAAC,GAAG,CACrD,yBAAyB,CAC1B,CAAA"}
@@ -0,0 +1,94 @@
1
+ import { WorkflowData } from "./type";
2
+ /**
3
+ *
4
+ * @ignore
5
+ *
6
+ * This function allows you to add hooks in your workflow that provide access to some data. Then, consumers of that workflow can add a handler function that performs
7
+ * an action with the provided data or modify it.
8
+ *
9
+ * For example, in a "create product" workflow, you may add a hook after the product is created, providing access to the created product.
10
+ * Then, developers using that workflow can hook into that point to access the product, modify its attributes, then return the updated product.
11
+ *
12
+ * @typeParam TOutput - The expected output of the hook's handler function.
13
+ * @returns The output of handler functions of this hook. If there are no handler functions, the output is `undefined`.
14
+ *
15
+ * @example
16
+ * import {
17
+ * createWorkflow,
18
+ * StepExecutionContext,
19
+ * hook,
20
+ * transform
21
+ * } from "@medusajs/workflows-sdk"
22
+ * import {
23
+ * createProductStep,
24
+ * getProductStep,
25
+ * createPricesStep
26
+ * } from "./steps"
27
+ * import {
28
+ * MedusaRequest,
29
+ * MedusaResponse,
30
+ * Product, ProductService
31
+ * } from "@medusajs/medusa"
32
+ *
33
+ * interface WorkflowInput {
34
+ * title: string
35
+ * }
36
+ *
37
+ * const myWorkflow = createWorkflow<
38
+ * WorkflowInput,
39
+ * Product
40
+ * >("my-workflow",
41
+ * function (input) {
42
+ * const product = createProductStep(input)
43
+ *
44
+ * const hookProduct = hook<Product>("createdProductHook", product)
45
+ *
46
+ * const newProduct = transform({
47
+ * product,
48
+ * hookProduct
49
+ * }, (input) => {
50
+ * return input.hookProduct || input.product
51
+ * })
52
+ *
53
+ * const prices = createPricesStep(newProduct)
54
+ *
55
+ * return getProductStep(product.id)
56
+ * }
57
+ * )
58
+ *
59
+ * myWorkflow.createdProductHook(
60
+ * async (product, context: StepExecutionContext) => {
61
+ * const productService: ProductService = context.container.resolve("productService")
62
+ *
63
+ * const updatedProduct = await productService.update(product.id, {
64
+ * description: "a cool shirt"
65
+ * })
66
+ *
67
+ * return updatedProduct
68
+ * })
69
+ *
70
+ * export async function POST(
71
+ * req: MedusaRequest,
72
+ * res: MedusaResponse
73
+ * ) {
74
+ * const { result: product } = await myWorkflow(req.scope)
75
+ * .run({
76
+ * input: {
77
+ * title: req.body.title
78
+ * }
79
+ * })
80
+ *
81
+ * res.json({
82
+ * product
83
+ * })
84
+ * }
85
+ */
86
+ export declare function hook<TOutput>(
87
+ /**
88
+ * The name of the hook. This will be used by the consumer to add a handler method for the hook.
89
+ */
90
+ name: string,
91
+ /**
92
+ * The data that a handler function receives as a parameter.
93
+ */
94
+ value: any): WorkflowData<TOutput>;