Skip to content

Commit 48bba72

Browse files
Merge pull request #2968 from cloudflare/sidharthachatterjee/getByIdgetByName
Split Workflow get into getById and getByName
2 parents f8278b1 + 863cab8 commit 48bba72

File tree

5 files changed

+54
-29
lines changed

5 files changed

+54
-29
lines changed

src/cloudflare/internal/test/workflows/workflows-api-test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export const tests = {
1717

1818
{
1919
// Test get instance
20-
const instance = await env.workflow.get('bar');
20+
const instance = await env.workflow.getById('bar');
2121
assert.deepStrictEqual(instance.id, 'bar');
2222
}
2323
},

src/cloudflare/internal/test/workflows/workflows-mock.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export default {
66
async fetch(request, env, ctx) {
77
const data = await request.json();
88

9-
if (request.url.includes('/get') && request.method === 'POST') {
9+
if (request.url.includes('/getById') && request.method === 'POST') {
1010
return Response.json(
1111
{
1212
result: {

src/cloudflare/internal/workflows-api.ts

+20-13
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ async function callFetcher<T>(
3939
}
4040
}
4141

42-
class InstanceImpl implements Instance {
42+
class InstanceImpl implements WorkflowInstance {
4343
private readonly fetcher: Fetcher;
4444
public readonly id: string;
4545

@@ -86,24 +86,31 @@ class WorkflowImpl {
8686
this.fetcher = fetcher;
8787
}
8888

89-
public async get(id: string): Promise<Instance> {
90-
const result = await callFetcher<{ instanceId: string }>(
91-
this.fetcher,
92-
'/get',
93-
{ id }
94-
);
89+
public async getById(id: string): Promise<WorkflowInstance> {
90+
const result = await callFetcher<{
91+
instanceId: string;
92+
instanceName: string;
93+
}>(this.fetcher, '/getById', { id });
94+
95+
return new InstanceImpl(result.instanceId, this.fetcher);
96+
}
97+
98+
public async getByName(name: string): Promise<WorkflowInstance> {
99+
const result = await callFetcher<{
100+
instanceId: string;
101+
instanceName: string;
102+
}>(this.fetcher, '/getByName', { name });
95103

96104
return new InstanceImpl(result.instanceId, this.fetcher);
97105
}
98106

99107
public async create(
100108
options?: WorkflowInstanceCreateOptions
101-
): Promise<Instance> {
102-
const result = await callFetcher<{ instanceId: string }>(
103-
this.fetcher,
104-
'/create',
105-
options ?? {}
106-
);
109+
): Promise<WorkflowInstance> {
110+
const result = await callFetcher<{
111+
instanceId: string;
112+
instanceName: string;
113+
}>(this.fetcher, '/create', options ?? {});
107114

108115
return new InstanceImpl(result.instanceId, this.fetcher);
109116
}

src/cloudflare/internal/workflows.d.ts

+15-6
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,32 @@ declare abstract class Workflow {
2828
* @param id Id for the instance of this Workflow
2929
* @returns A promise that resolves with a handle for the Instance
3030
*/
31-
public get(id: string): Promise<Instance>;
31+
public getById(id: string): Promise<WorkflowInstance>;
32+
33+
/**
34+
* Get a handle to an existing instance of the Workflow.
35+
* @param name Name for the instance of this Workflow
36+
* @returns A promise that resolves with a handle for the Instance
37+
*/
38+
public getByName(name: string): Promise<WorkflowInstance>;
3239

3340
/**
3441
* Create a new instance and return a handle to it. If a provided id exists, an error will be thrown.
35-
* @param options optional fields to customize the instance creation
42+
* @param options Options when creating an instance including name and params
3643
* @returns A promise that resolves with a handle for the Instance
3744
*/
38-
public create(options?: WorkflowInstanceCreateOptions): Promise<Instance>;
45+
public create(
46+
options?: WorkflowInstanceCreateOptions
47+
): Promise<WorkflowInstance>;
3948
}
4049

4150
interface WorkflowInstanceCreateOptions {
4251
/**
43-
* Name to create the instance of this Workflow with - it should always be unique
52+
* A name for your Workflow instance. Must be unique within the Workflow.
4453
*/
4554
name?: string;
4655
/**
47-
* The payload to send over to this instance, this is optional since you might need to pass params into the instance
56+
* The event payload the Workflow instance is triggered with
4857
*/
4958
params?: unknown;
5059
}
@@ -69,7 +78,7 @@ interface WorkflowError {
6978
message: string;
7079
}
7180

72-
declare abstract class Instance {
81+
declare abstract class WorkflowInstance {
7382
public id: string;
7483

7584
/**

types/defines/workflows.d.ts

+17-8
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,32 @@ declare abstract class Workflow {
1414
* @param id Id for the instance of this Workflow
1515
* @returns A promise that resolves with a handle for the Instance
1616
*/
17-
public get(id: string): Promise<Instance>;
17+
public getById(id: string): Promise<WorkflowInstance>;
18+
19+
/**
20+
* Get a handle to an existing instance of the Workflow.
21+
* @param name Name for the instance of this Workflow
22+
* @returns A promise that resolves with a handle for the Instance
23+
*/
24+
public getByName(name: string): Promise<WorkflowInstance>;
1825

1926
/**
2027
* Create a new instance and return a handle to it. If a provided id exists, an error will be thrown.
21-
* @param options optional fields to customize the instance creation
28+
* @param options Options when creating an instance including name and params
2229
* @returns A promise that resolves with a handle for the Instance
2330
*/
24-
public create(options?: WorkflowInstanceCreateOptions): Promise<Instance>;
31+
public create(
32+
options?: WorkflowInstanceCreateOptions
33+
): Promise<WorkflowInstance>;
2534
}
2635

2736
interface WorkflowInstanceCreateOptions {
2837
/**
29-
* Name to create the instance of this Workflow with - it should always be unique
38+
* A name for your Workflow instance. Must be unique within the Workflow.
3039
*/
3140
name?: string;
3241
/**
33-
* The payload to send over to this instance, this is optional since you might need to pass params into the instance
42+
* The event payload the Workflow instance is triggered with
3443
*/
3544
params?: unknown;
3645
}
@@ -55,7 +64,7 @@ interface WorkflowError {
5564
message: string;
5665
}
5766

58-
declare abstract class Instance {
67+
declare abstract class WorkflowInstance {
5968
public id: string;
6069

6170
/**
@@ -69,9 +78,9 @@ declare abstract class Instance {
6978
public resume(): Promise<void>;
7079

7180
/**
72-
* Abort the instance. If it is errored, terminated or complete, an error will be thrown.
81+
* Terminate the instance. If it is errored, terminated or complete, an error will be thrown.
7382
*/
74-
public abort(): Promise<void>;
83+
public terminate(): Promise<void>;
7584

7685
/**
7786
* Restart the instance.

0 commit comments

Comments
 (0)