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

View File

@@ -0,0 +1,5 @@
---
"@medusajs/workflows-sdk": patch
---
fix: hooks auto-generated types to have precise input options

View File

@@ -4,7 +4,10 @@ import { WorkflowData, WorkflowDataProperties } from "../type"
/**
* 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 =
OrchestrationUtils.SymbolMedusaWorkflowResponse

View File

@@ -27,19 +27,24 @@ export type StepFunctionReturnConfig<TOutput> = {
type KeysOfUnion<T> = T extends T ? keyof T : never
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
*/
type ConvertHooksToFunctions<THooks extends any[]> = {
[K in keyof THooks]: THooks[K] extends Hook<infer Name, infer Input>
? {
[Fn in Name]: <TOutput, TCompensateInput>(
invoke: InvokeFn<Input, TOutput, TCompensateInput>,
compensate?: CompensateFn<TCompensateInput>
) => void
}
: never
}[number]
type ConvertHooksToFunctions<THooks extends any[]> = THooks extends [
infer A,
...infer R
]
? ConvertHookToObject<A> & ConvertHooksToFunctions<R>
: {}
/**
* A step function to be used in a workflow.