Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions types/defines/rpc.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,15 +326,32 @@ declare namespace CloudflareWorkersModule {
config: WorkflowStepConfig;
};

export type WorkflowRollbackContext<T = unknown> = {
error: Error;
output: T | undefined;
stepName: string;
};

export type WorkflowRollbackHandler<T = unknown> = (
ctx: WorkflowRollbackContext<T>
) => Promise<void>;

export type WorkflowStepRollbackOptions<T = unknown> = {
rollback?: WorkflowRollbackHandler<T>;
rollbackConfig?: WorkflowStepConfig;
};

export abstract class WorkflowStep {
do<T extends Rpc.Serializable<T>>(
name: string,
callback: (ctx: WorkflowStepContext) => Promise<T>
callback: (ctx: WorkflowStepContext) => Promise<T>,
rollbackOptions?: WorkflowStepRollbackOptions<T>
): Promise<T>;
Comment thread
vaishnav-mk marked this conversation as resolved.
do<T extends Rpc.Serializable<T>>(
name: string,
config: WorkflowStepConfig,
callback: (ctx: WorkflowStepContext) => Promise<T>
callback: (ctx: WorkflowStepContext) => Promise<T>,
rollbackOptions?: WorkflowStepRollbackOptions<T>
): Promise<T>;
Comment thread
vaishnav-mk marked this conversation as resolved.
sleep: (name: string, duration: WorkflowSleepDuration) => Promise<void>;
sleepUntil: (name: string, timestamp: Date | number) => Promise<void>;
Expand Down
14 changes: 14 additions & 0 deletions types/generated-snapshot/experimental/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14420,15 +14420,29 @@ declare namespace CloudflareWorkersModule {
attempt: number;
config: WorkflowStepConfig;
};
export type WorkflowRollbackContext<T = unknown> = {
error: Error;
output: T | undefined;
stepName: string;
};
export type WorkflowRollbackHandler<T = unknown> = (
ctx: WorkflowRollbackContext<T>,
) => Promise<void>;
export type WorkflowStepRollbackOptions<T = unknown> = {
rollback?: WorkflowRollbackHandler<T>;
rollbackConfig?: WorkflowStepConfig;
};
export abstract class WorkflowStep {
do<T extends Rpc.Serializable<T>>(
name: string,
callback: (ctx: WorkflowStepContext) => Promise<T>,
rollbackOptions?: WorkflowStepRollbackOptions<T>,
): Promise<T>;
do<T extends Rpc.Serializable<T>>(
name: string,
config: WorkflowStepConfig,
callback: (ctx: WorkflowStepContext) => Promise<T>,
rollbackOptions?: WorkflowStepRollbackOptions<T>,
): Promise<T>;
sleep: (name: string, duration: WorkflowSleepDuration) => Promise<void>;
sleepUntil: (name: string, timestamp: Date | number) => Promise<void>;
Expand Down
14 changes: 14 additions & 0 deletions types/generated-snapshot/experimental/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14391,15 +14391,29 @@ export declare namespace CloudflareWorkersModule {
attempt: number;
config: WorkflowStepConfig;
};
export type WorkflowRollbackContext<T = unknown> = {
error: Error;
output: T | undefined;
stepName: string;
};
export type WorkflowRollbackHandler<T = unknown> = (
ctx: WorkflowRollbackContext<T>,
) => Promise<void>;
export type WorkflowStepRollbackOptions<T = unknown> = {
rollback?: WorkflowRollbackHandler<T>;
rollbackConfig?: WorkflowStepConfig;
};
export abstract class WorkflowStep {
do<T extends Rpc.Serializable<T>>(
name: string,
callback: (ctx: WorkflowStepContext) => Promise<T>,
rollbackOptions?: WorkflowStepRollbackOptions<T>,
): Promise<T>;
do<T extends Rpc.Serializable<T>>(
name: string,
config: WorkflowStepConfig,
callback: (ctx: WorkflowStepContext) => Promise<T>,
rollbackOptions?: WorkflowStepRollbackOptions<T>,
): Promise<T>;
sleep: (name: string, duration: WorkflowSleepDuration) => Promise<void>;
sleepUntil: (name: string, timestamp: Date | number) => Promise<void>;
Expand Down
14 changes: 14 additions & 0 deletions types/generated-snapshot/latest/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13752,15 +13752,29 @@ declare namespace CloudflareWorkersModule {
attempt: number;
config: WorkflowStepConfig;
};
export type WorkflowRollbackContext<T = unknown> = {
error: Error;
output: T | undefined;
stepName: string;
};
export type WorkflowRollbackHandler<T = unknown> = (
ctx: WorkflowRollbackContext<T>,
) => Promise<void>;
export type WorkflowStepRollbackOptions<T = unknown> = {
rollback?: WorkflowRollbackHandler<T>;
rollbackConfig?: WorkflowStepConfig;
};
export abstract class WorkflowStep {
do<T extends Rpc.Serializable<T>>(
name: string,
callback: (ctx: WorkflowStepContext) => Promise<T>,
rollbackOptions?: WorkflowStepRollbackOptions<T>,
): Promise<T>;
do<T extends Rpc.Serializable<T>>(
name: string,
config: WorkflowStepConfig,
callback: (ctx: WorkflowStepContext) => Promise<T>,
rollbackOptions?: WorkflowStepRollbackOptions<T>,
): Promise<T>;
sleep: (name: string, duration: WorkflowSleepDuration) => Promise<void>;
sleepUntil: (name: string, timestamp: Date | number) => Promise<void>;
Expand Down
14 changes: 14 additions & 0 deletions types/generated-snapshot/latest/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13723,15 +13723,29 @@ export declare namespace CloudflareWorkersModule {
attempt: number;
config: WorkflowStepConfig;
};
export type WorkflowRollbackContext<T = unknown> = {
error: Error;
output: T | undefined;
stepName: string;
};
export type WorkflowRollbackHandler<T = unknown> = (
ctx: WorkflowRollbackContext<T>,
) => Promise<void>;
export type WorkflowStepRollbackOptions<T = unknown> = {
rollback?: WorkflowRollbackHandler<T>;
rollbackConfig?: WorkflowStepConfig;
};
export abstract class WorkflowStep {
do<T extends Rpc.Serializable<T>>(
name: string,
callback: (ctx: WorkflowStepContext) => Promise<T>,
rollbackOptions?: WorkflowStepRollbackOptions<T>,
): Promise<T>;
do<T extends Rpc.Serializable<T>>(
name: string,
config: WorkflowStepConfig,
callback: (ctx: WorkflowStepContext) => Promise<T>,
rollbackOptions?: WorkflowStepRollbackOptions<T>,
): Promise<T>;
sleep: (name: string, duration: WorkflowSleepDuration) => Promise<void>;
sleepUntil: (name: string, timestamp: Date | number) => Promise<void>;
Expand Down
27 changes: 27 additions & 0 deletions types/test/types/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
RpcTarget,
WorkerEntrypoint,
} from 'cloudflare:workers';
import type {WorkflowStep} from 'cloudflare:workers';
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: this import could be merged with the block above using inline type syntax:

Suggested change
import type {WorkflowStep} from 'cloudflare:workers';
import {
cache,
DurableObject,
RpcStub,
RpcTarget,
WorkerEntrypoint,
type WorkflowStep,
} from 'cloudflare:workers';

import { expectTypeOf } from 'expect-type';

// Check `cache` export from `cloudflare:workers` has the expected type.
Expand Down Expand Up @@ -794,3 +795,29 @@ export default <ExportedHandler<Env>>{
return new Response();
},
};

declare const workflowStep: WorkflowStep;

expectTypeOf(
workflowStep.do('step with rollback', async (): Promise<string> => 'ok', {
rollback: async (ctx) => {
expectTypeOf(ctx.error).toEqualTypeOf<Error>();
expectTypeOf(ctx.output).toEqualTypeOf<string | undefined>();
expectTypeOf(ctx.stepName).toEqualTypeOf<string>();
},
})
).toMatchTypeOf<Promise<string>>();

workflowStep.do(
'configured rollback',
{retries: {limit: 0, delay: 0}},
async (): Promise<string> => 'ok',
{
rollback: async (ctx) => {
expectTypeOf(ctx.output).toEqualTypeOf<string | undefined>();
},
rollbackConfig: {retries: {limit: 0, delay: 0}},
}
);

workflowStep.do('empty rollback options', async () => 'ok', {});
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding tests without any rollback options to verify backward compatibility of the existing overloads:

Suggested change
workflowStep.do('empty rollback options', async () => 'ok', {});
workflowStep.do('empty rollback options', async () => 'ok', {});
// Verify existing call signatures still work without rollback options
workflowStep.do('no rollback 2-arg', async (): Promise<string> => 'ok');
workflowStep.do('no rollback 3-arg', {retries: {limit: 1, delay: 0}}, async (): Promise<string> => 'ok');

Loading