Skip to content

Commit

Permalink
refactor(client): improve workflow handling logic
Browse files Browse the repository at this point in the history
  • Loading branch information
rifont committed Nov 7, 2024
1 parent b351651 commit 3b61691
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
11 changes: 6 additions & 5 deletions packages/framework/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
ProviderNotFoundError,
StepControlCompilationFailedError,
StepNotFoundError,
WorkflowAlreadyExistsError,
WorkflowNotFoundError,
StepExecutionFailedError,
isFrameworkError,
Expand Down Expand Up @@ -46,6 +45,7 @@ import {
import { validateData } from './validators';

import { mockSchema } from './jsonSchemaFaker';
import { prettyPrintDiscovery } from './resources/workflow/pretty-print-discovery';

function isRuntimeInDevelopment() {
return ['development', undefined].includes(process.env.NODE_ENV);
Expand Down Expand Up @@ -94,12 +94,13 @@ export class Client {
return builtConfiguration;
}

public async addWorkflows(workflows: Array<Workflow>) {
public async addWorkflows(workflows: Array<Workflow>): Promise<void> {
for (const workflow of workflows) {
const definition = await workflow.discover();
if (this.discoveredWorkflows.some((existing) => existing.workflowId === definition.workflowId)) {
throw new WorkflowAlreadyExistsError(definition.workflowId);
if (this.discoveredWorkflows.some((existing) => existing.workflowId === workflow.id)) {
return;
} else {
const definition = await workflow.discover();
prettyPrintDiscovery(definition);
this.discoveredWorkflows.push(definition);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import { discoverActionStepFactory } from './discover-action-step-factory';
import { discoverChannelStepFactory } from './discover-channel-step-factory';
import { discoverCustomStepFactory } from './discover-custom-step-factory';
import { mapPreferences } from './map-preferences';
import { prettyPrintDiscovery } from './pretty-print-discovery';

/**
* Define a new notification workflow.
Expand Down Expand Up @@ -149,12 +148,11 @@ export function workflow<
} as never,
});

prettyPrintDiscovery(newWorkflow);

return newWorkflow;
};

return {
id: workflowId,
trigger,
discover,
};
Expand Down
4 changes: 4 additions & 0 deletions packages/framework/src/types/discover.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ export type DiscoverWorkflowOutput = {

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type Workflow<T_Payload = any> = {
/**
* The unique identifier of the workflow.
*/
id: string;
/**
* Trigger an event for this workflow with a strongly typed and validated `payload`, derived from the `payloadSchema`.
*
Expand Down

0 comments on commit 3b61691

Please sign in to comment.