diff --git a/types/defines/rpc.d.ts b/types/defines/rpc.d.ts index 6dcbd72a309..dd08791145b 100644 --- a/types/defines/rpc.d.ts +++ b/types/defines/rpc.d.ts @@ -326,15 +326,32 @@ declare namespace CloudflareWorkersModule { config: WorkflowStepConfig; }; + export type WorkflowRollbackContext = { + error: Error; + output: T | undefined; + stepName: string; + }; + + export type WorkflowRollbackHandler = ( + ctx: WorkflowRollbackContext + ) => Promise; + + export type WorkflowStepRollbackOptions = { + rollback?: WorkflowRollbackHandler; + rollbackConfig?: WorkflowStepConfig; + }; + export abstract class WorkflowStep { do>( name: string, - callback: (ctx: WorkflowStepContext) => Promise + callback: (ctx: WorkflowStepContext) => Promise, + rollbackOptions?: WorkflowStepRollbackOptions ): Promise; do>( name: string, config: WorkflowStepConfig, - callback: (ctx: WorkflowStepContext) => Promise + callback: (ctx: WorkflowStepContext) => Promise, + rollbackOptions?: WorkflowStepRollbackOptions ): Promise; sleep: (name: string, duration: WorkflowSleepDuration) => Promise; sleepUntil: (name: string, timestamp: Date | number) => Promise; diff --git a/types/generated-snapshot/experimental/index.d.ts b/types/generated-snapshot/experimental/index.d.ts index a24240d382e..05a7b80f542 100755 --- a/types/generated-snapshot/experimental/index.d.ts +++ b/types/generated-snapshot/experimental/index.d.ts @@ -14420,15 +14420,29 @@ declare namespace CloudflareWorkersModule { attempt: number; config: WorkflowStepConfig; }; + export type WorkflowRollbackContext = { + error: Error; + output: T | undefined; + stepName: string; + }; + export type WorkflowRollbackHandler = ( + ctx: WorkflowRollbackContext, + ) => Promise; + export type WorkflowStepRollbackOptions = { + rollback?: WorkflowRollbackHandler; + rollbackConfig?: WorkflowStepConfig; + }; export abstract class WorkflowStep { do>( name: string, callback: (ctx: WorkflowStepContext) => Promise, + rollbackOptions?: WorkflowStepRollbackOptions, ): Promise; do>( name: string, config: WorkflowStepConfig, callback: (ctx: WorkflowStepContext) => Promise, + rollbackOptions?: WorkflowStepRollbackOptions, ): Promise; sleep: (name: string, duration: WorkflowSleepDuration) => Promise; sleepUntil: (name: string, timestamp: Date | number) => Promise; diff --git a/types/generated-snapshot/experimental/index.ts b/types/generated-snapshot/experimental/index.ts index a221881cb04..f8d6cf276f5 100755 --- a/types/generated-snapshot/experimental/index.ts +++ b/types/generated-snapshot/experimental/index.ts @@ -14391,15 +14391,29 @@ export declare namespace CloudflareWorkersModule { attempt: number; config: WorkflowStepConfig; }; + export type WorkflowRollbackContext = { + error: Error; + output: T | undefined; + stepName: string; + }; + export type WorkflowRollbackHandler = ( + ctx: WorkflowRollbackContext, + ) => Promise; + export type WorkflowStepRollbackOptions = { + rollback?: WorkflowRollbackHandler; + rollbackConfig?: WorkflowStepConfig; + }; export abstract class WorkflowStep { do>( name: string, callback: (ctx: WorkflowStepContext) => Promise, + rollbackOptions?: WorkflowStepRollbackOptions, ): Promise; do>( name: string, config: WorkflowStepConfig, callback: (ctx: WorkflowStepContext) => Promise, + rollbackOptions?: WorkflowStepRollbackOptions, ): Promise; sleep: (name: string, duration: WorkflowSleepDuration) => Promise; sleepUntil: (name: string, timestamp: Date | number) => Promise; diff --git a/types/generated-snapshot/latest/index.d.ts b/types/generated-snapshot/latest/index.d.ts index 989b8c88bbf..22e027373fe 100755 --- a/types/generated-snapshot/latest/index.d.ts +++ b/types/generated-snapshot/latest/index.d.ts @@ -13752,15 +13752,29 @@ declare namespace CloudflareWorkersModule { attempt: number; config: WorkflowStepConfig; }; + export type WorkflowRollbackContext = { + error: Error; + output: T | undefined; + stepName: string; + }; + export type WorkflowRollbackHandler = ( + ctx: WorkflowRollbackContext, + ) => Promise; + export type WorkflowStepRollbackOptions = { + rollback?: WorkflowRollbackHandler; + rollbackConfig?: WorkflowStepConfig; + }; export abstract class WorkflowStep { do>( name: string, callback: (ctx: WorkflowStepContext) => Promise, + rollbackOptions?: WorkflowStepRollbackOptions, ): Promise; do>( name: string, config: WorkflowStepConfig, callback: (ctx: WorkflowStepContext) => Promise, + rollbackOptions?: WorkflowStepRollbackOptions, ): Promise; sleep: (name: string, duration: WorkflowSleepDuration) => Promise; sleepUntil: (name: string, timestamp: Date | number) => Promise; diff --git a/types/generated-snapshot/latest/index.ts b/types/generated-snapshot/latest/index.ts index ed0c2cd4c74..b1053ace589 100755 --- a/types/generated-snapshot/latest/index.ts +++ b/types/generated-snapshot/latest/index.ts @@ -13723,15 +13723,29 @@ export declare namespace CloudflareWorkersModule { attempt: number; config: WorkflowStepConfig; }; + export type WorkflowRollbackContext = { + error: Error; + output: T | undefined; + stepName: string; + }; + export type WorkflowRollbackHandler = ( + ctx: WorkflowRollbackContext, + ) => Promise; + export type WorkflowStepRollbackOptions = { + rollback?: WorkflowRollbackHandler; + rollbackConfig?: WorkflowStepConfig; + }; export abstract class WorkflowStep { do>( name: string, callback: (ctx: WorkflowStepContext) => Promise, + rollbackOptions?: WorkflowStepRollbackOptions, ): Promise; do>( name: string, config: WorkflowStepConfig, callback: (ctx: WorkflowStepContext) => Promise, + rollbackOptions?: WorkflowStepRollbackOptions, ): Promise; sleep: (name: string, duration: WorkflowSleepDuration) => Promise; sleepUntil: (name: string, timestamp: Date | number) => Promise; diff --git a/types/test/types/rpc.ts b/types/test/types/rpc.ts index 6d274d52c6a..01f64c490b6 100644 --- a/types/test/types/rpc.ts +++ b/types/test/types/rpc.ts @@ -9,6 +9,7 @@ import { RpcTarget, WorkerEntrypoint, } from 'cloudflare:workers'; +import type {WorkflowStep} from 'cloudflare:workers'; import { expectTypeOf } from 'expect-type'; // Check `cache` export from `cloudflare:workers` has the expected type. @@ -794,3 +795,29 @@ export default >{ return new Response(); }, }; + +declare const workflowStep: WorkflowStep; + +expectTypeOf( + workflowStep.do('step with rollback', async (): Promise => 'ok', { + rollback: async (ctx) => { + expectTypeOf(ctx.error).toEqualTypeOf(); + expectTypeOf(ctx.output).toEqualTypeOf(); + expectTypeOf(ctx.stepName).toEqualTypeOf(); + }, + }) +).toMatchTypeOf>(); + +workflowStep.do( + 'configured rollback', + {retries: {limit: 0, delay: 0}}, + async (): Promise => 'ok', + { + rollback: async (ctx) => { + expectTypeOf(ctx.output).toEqualTypeOf(); + }, + rollbackConfig: {retries: {limit: 0, delay: 0}}, + } +); + +workflowStep.do('empty rollback options', async () => 'ok', {});