fix: hooks auto-generated types to have precise input options (#11886)

This commit is contained in:
Harminder Virk
2025-03-18 15:41:01 +05:30
committed by GitHub
parent 3f2bdb59cb
commit ff3885500c
3 changed files with 24 additions and 11 deletions
+5
View File
@@ -0,0 +1,5 @@
---
"@medusajs/workflows-sdk": patch
---
fix: hooks auto-generated types to have precise input options
@@ -4,7 +4,10 @@ import { WorkflowData, WorkflowDataProperties } from "../type"
/** /**
* Workflow response class encapsulates the return value of a workflow * Workflow response class encapsulates the return value of a workflow
*/ */
export class WorkflowResponse<TResult, THooks = []> { export class WorkflowResponse<
TResult,
const THooks extends readonly unknown[] = []
> {
__type: typeof OrchestrationUtils.SymbolMedusaWorkflowResponse = __type: typeof OrchestrationUtils.SymbolMedusaWorkflowResponse =
OrchestrationUtils.SymbolMedusaWorkflowResponse OrchestrationUtils.SymbolMedusaWorkflowResponse
@@ -27,19 +27,24 @@ export type StepFunctionReturnConfig<TOutput> = {
type KeysOfUnion<T> = T extends T ? keyof T : never type KeysOfUnion<T> = T extends T ? keyof T : never
export type HookHandler = (...args: any[]) => void | Promise<void> export type HookHandler = (...args: any[]) => void | Promise<void>
type ConvertHookToObject<THook> = THook extends Hook<infer Name, infer Input>
? {
[K in Name]: <TOutput, TCompensateInput>(
invoke: InvokeFn<Input, TOutput, TCompensateInput>,
compensate?: CompensateFn<TCompensateInput>
) => void
}
: never
/** /**
* Helper to convert an array of hooks to functions * Helper to convert an array of hooks to functions
*/ */
type ConvertHooksToFunctions<THooks extends any[]> = { type ConvertHooksToFunctions<THooks extends any[]> = THooks extends [
[K in keyof THooks]: THooks[K] extends Hook<infer Name, infer Input> infer A,
? { ...infer R
[Fn in Name]: <TOutput, TCompensateInput>( ]
invoke: InvokeFn<Input, TOutput, TCompensateInput>, ? ConvertHookToObject<A> & ConvertHooksToFunctions<R>
compensate?: CompensateFn<TCompensateInput> : {}
) => void
}
: never
}[number]
/** /**
* A step function to be used in a workflow. * A step function to be used in a workflow.