import { LocalWorkflow, OrchestratorBuilder, TransactionContext as OriginalWorkflowTransactionContext, TransactionModelOptions, TransactionPayload, TransactionStepsDefinition, WorkflowHandler } from "@medusajs/orchestration"; import { Context, LoadedModule, MedusaContainer } from "@medusajs/types"; import { ExportedWorkflow } from "../../helper"; import { Hook } from "./create-hook"; import { CompensateFn, InvokeFn } from "./create-step"; export type StepFunctionResult = (this: CreateWorkflowComposerContext) => WorkflowData; export type StepFunctionReturnConfig = { config(config: { name?: string; } & Omit): WorkflowData; }; type KeysOfUnion = T extends T ? keyof T : never; export type HookHandler = (...args: any[]) => void | Promise; type ConvertHookToObject = THook extends Hook ? { [K in Name]: (invoke: InvokeFn, compensate?: CompensateFn) => void; } : never; /** * Helper to convert an array of hooks to functions */ type ConvertHooksToFunctions = THooks extends [ infer A, ...infer R ] ? ConvertHookToObject & ConvertHooksToFunctions : {}; export type Void = { " $$type": "void"; }; /** * A step function to be used in a workflow. * * @typeParam TInput - The type of the input of the step. * @typeParam TOutput - The type of the output of the step. */ export type StepFunction = (KeysOfUnion extends [] ? { (): TOutput & {} extends never ? WorkflowData & StepFunctionReturnConfig : WorkflowData & StepFunctionReturnConfig; } : { (input: WorkflowData | TInput): WorkflowData & StepFunctionReturnConfig; }) & WorkflowDataProperties; export type WorkflowDataProperties = { __type: string; __step__: string; }; /** * This type is used to encapsulate the input or output type of all utils. * * @typeParam T - The type of a step's input or result. */ export type WorkflowData = (T extends Array ? Array> : T extends object ? { [Key in keyof T]: T[Key] | WorkflowData; } : T & WorkflowDataProperties) & T & WorkflowDataProperties & { config(config: { name?: string; } & Omit): WorkflowData; }; export type CreateWorkflowComposerContext = { __type: string; hooks_: { declared: string[]; registered: string[]; }; hooksCallback_: Record; workflowId: string; flow: OrchestratorBuilder; isAsync: boolean; handlers: WorkflowHandler; overriddenHandler: WorkflowHandler; stepBinder: (fn: StepFunctionResult) => WorkflowData; hookBinder: (name: string, fn: () => HookHandler) => void; stepConditions_: Record boolean | WorkflowData; input: any; }>; parallelizeBinder: (fn: (this: CreateWorkflowComposerContext) => TOutput) => TOutput; }; /** * The step's context. */ export interface StepExecutionContext { /** * The ID of the workflow. */ workflowId: string; /** * The attempt number of the step. */ attempt: number; /** * The idempoency key of the step. */ idempotencyKey: string; /** * The idempoency key of the parent step. */ parentStepIdempotencyKey?: string; /** * Whether to prevent release events. */ preventReleaseEvents?: boolean; /** * The name of the step. */ stepName: string; /** * The action of the step. */ action: "invoke" | "compensate"; /** * The container used to access resources, such as services, in the step. */ container: MedusaContainer; /** * Metadata passed in the input. */ metadata: TransactionPayload["metadata"]; /** * {@inheritDoc types!Context} */ context: Context; /** * A string indicating the ID of the group to aggregate the events to be emitted at a later point. */ eventGroupId?: string; /** * A string indicating the ID of the current transaction. */ transactionId?: string; /** * A string indicating the ID of the current run. */ runId?: string; /** * Get access to the result returned by a named step. Returns undefined * when step is not found or when nothing was returned. * * Adding a space hides the method from the autocomplete */ " getStepResult"(stepId: string, action?: "invoke" | "compensate"): any; /** * Get access to the definition of the step. */ " stepDefinition": TransactionStepsDefinition; } export type WorkflowTransactionContext = StepExecutionContext & OriginalWorkflowTransactionContext & { invoke: { [key: string]: { output: any; }; }; }; /** * An exported workflow, which is the type of a workflow constructed by the {@link createWorkflow} function. The exported workflow can be invoked to create * an executable workflow, optionally within a specified container. So, to execute the workflow, you must invoke the exported workflow, then run the * `run` method of the exported workflow. * * @example * To execute a workflow: * * ```ts * myWorkflow() * .run({ * input: { * name: "John" * } * }) * .then(({ result }) => { * console.log(result) * }) * ``` * * 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 * within a Medusa resource such as an API Route or a Subscriber. * * For example: * * ```ts * import type { * MedusaRequest, * MedusaResponse * } from "@medusajs/medusa"; * import myWorkflow from "../../../workflows/hello-world"; * * export async function GET( * req: MedusaRequest, * res: MedusaResponse * ) { * const { result } = await myWorkflow(req.scope) * .run({ * input: { * name: req.query.name as string * } * }) * * res.send(result) * } * ``` */ export type ReturnWorkflow = { (container?: LoadedModule[] | MedusaContainer): Omit & ExportedWorkflow; } & { /** * This method executes the workflow as a step. Useful when running a workflow within another. * * Learn more in [this documentation](https://docs.medusajs.com/learn/fundamentals/workflows/execute-another-workflow). * * @param param0 - The options to execute the workflow. * @returns The workflow's result */ runAsStep: ({ input, }: { /** * The workflow's input. */ input: TData | WorkflowData; }) => ReturnType>; /** * This method executes a workflow. * * @param args - The options to execute the workflow. * @returns Details of the workflow's execution, including its result. */ run: (...args: Parameters["run"]>) => ReturnType["run"]>; /** * This method retrieves the workflow's name. */ getName: () => string; /** * This method sets the workflow's configurations. */ config: (config: TransactionModelOptions) => void; /** * The workflow's exposed hooks, used to register a handler to consume the hook. * * Learn more in [this documentation](https://docs.medusajs.com/learn/fundamentals/workflows/workflow-hooks#how-to-consume-a-hook). */ hooks: ConvertHooksToFunctions; }; /** * Extract the raw type of the expected input data of a workflow. * * @example * type WorkflowInputData = UnwrapWorkflowInputDataType */ export type UnwrapWorkflowInputDataType> = T extends ReturnWorkflow ? TData : never; export {}; //# sourceMappingURL=type.d.ts.map