From 1459d752fb43a6c8cf92fc41151783ed131412fe Mon Sep 17 00:00:00 2001 From: Sid Chatterjee Date: Wed, 9 Oct 2024 21:05:43 +0100 Subject: [PATCH] Make Workflows instance name public and accept it instead of id for create and get --- .../test/workflows/workflows-api-test.js | 4 +- .../internal/test/workflows/workflows-mock.js | 6 ++- src/cloudflare/internal/workflows-api.ts | 38 +++++++++++-------- types/defines/workflows.d.ts | 11 +++--- 4 files changed, 35 insertions(+), 24 deletions(-) diff --git a/src/cloudflare/internal/test/workflows/workflows-api-test.js b/src/cloudflare/internal/test/workflows/workflows-api-test.js index c133adc2ab8..e775de6d4df 100644 --- a/src/cloudflare/internal/test/workflows/workflows-api-test.js +++ b/src/cloudflare/internal/test/workflows/workflows-api-test.js @@ -9,13 +9,13 @@ export const tests = { { // Test create instance const instance = await env.workflow.create('foo', { bar: 'baz' }); - assert.deepStrictEqual(instance.id, 'foo'); + assert.deepStrictEqual(instance.name, 'foo'); } { // Test get instance const instance = await env.workflow.get('bar'); - assert.deepStrictEqual(instance.id, 'bar'); + assert.deepStrictEqual(instance.name, 'bar'); } }, }; diff --git a/src/cloudflare/internal/test/workflows/workflows-mock.js b/src/cloudflare/internal/test/workflows/workflows-mock.js index 4a3e175c164..ca32f1c9a10 100644 --- a/src/cloudflare/internal/test/workflows/workflows-mock.js +++ b/src/cloudflare/internal/test/workflows/workflows-mock.js @@ -10,7 +10,8 @@ export default { return Response.json( { result: { - instanceId: data.id, + instanceId: 'id', + instanceName: data.name, }, }, { @@ -26,7 +27,8 @@ export default { return Response.json( { result: { - instanceId: data.id, + instanceId: 'id', + instanceName: data.name, }, }, { diff --git a/src/cloudflare/internal/workflows-api.ts b/src/cloudflare/internal/workflows-api.ts index aa5ce3a8654..751156f9ff9 100644 --- a/src/cloudflare/internal/workflows-api.ts +++ b/src/cloudflare/internal/workflows-api.ts @@ -49,9 +49,11 @@ async function callFetcher( class InstanceImpl implements Instance { private readonly fetcher: Fetcher; public readonly id: string; + public readonly name: string; - public constructor(id: string, fetcher: Fetcher) { + public constructor(id: string, name: string, fetcher: Fetcher) { this.id = id; + this.name = name; this.fetcher = fetcher; } @@ -93,24 +95,30 @@ class WorkflowImpl { this.fetcher = fetcher; } - public async get(id: string): Promise { - const result = await callFetcher<{ instanceId: string }>( - this.fetcher, - '/get', - { id } - ); + public async get(name: string): Promise { + const result = await callFetcher<{ + instanceId: string; + instanceName: string; + }>(this.fetcher, '/get', { name }); - return new InstanceImpl(result.instanceId, this.fetcher); + return new InstanceImpl( + result.instanceId, + result.instanceName, + this.fetcher + ); } - public async create(id: string, params: object): Promise { - const result = await callFetcher<{ instanceId: string }>( - this.fetcher, - '/create', - { id, params } - ); + public async create(name: string, params: object): Promise { + const result = await callFetcher<{ + instanceId: string; + instanceName: string; + }>(this.fetcher, '/create', { name, params }); - return new InstanceImpl(result.instanceId, this.fetcher); + return new InstanceImpl( + result.instanceId, + result.instanceName, + this.fetcher + ); } } diff --git a/types/defines/workflows.d.ts b/types/defines/workflows.d.ts index 6858b5e0fab..5cea83ec496 100644 --- a/types/defines/workflows.d.ts +++ b/types/defines/workflows.d.ts @@ -15,18 +15,18 @@ declare module "cloudflare:workflows" { declare abstract class Workflow { /** * Get a handle to an existing instance of the Workflow. - * @param id Id for the instance of this Workflow + * @param name Name of the instance of this Workflow * @returns A promise that resolves with a handle for the Instance */ - public get(id: string): Promise; + public get(name: string): Promise; /** - * Create a new instance and return a handle to it. If a provided id exists, an error will be thrown. - * @param id Id to create the instance of this Workflow with + * Create a new instance and return a handle to it. If a provided instance name exists, an error will be thrown. + * @param name Name to create the instance of this Workflow with * @param params The payload to send over to this instance * @returns A promise that resolves with a handle for the Instance */ - public create(id: string, params: object): Promise; + public create(name: string, params: object): Promise; } type InstanceStatus = { @@ -49,6 +49,7 @@ interface WorkflowError { declare abstract class Instance { public id: string; + public name: string; /** * Pause the instance.