Services
Core services provided by the workflow module including OrchestratorService and StateRouterHelperFactory.
OrchestratorService
The main service responsible for orchestrating workflow execution and state transitions.
Import
import { OrchestratorService } from 'nestflow-js/core';Methods
transit(event)
Processes a workflow event and executes the appropriate state transition.
Signature
async transit(params: IWorkflowEvent): Promise<TransitResult>Parameters
params: Workflow event object:event: Event name that triggers a transitionurn: Unique resource name (entity identifier)payload?: Optional event payloadattempt: Retry attempt number
Returns
Promise resolving to a TransitResult indicating the workflow outcome (final, idle, continued, or no_transition). See TransitResult for details.
Example
@Injectable()
export class OrderService {
constructor(private orchestrator: OrchestratorService) {}
async processOrderEvent(orderId: string, event: string, data: any) {
await this.orchestrator.transit({
event: event,
urn: orderId,
payload: data,
attempt: 0,
});
}
}getRetryConfig(event)
Returns the retry configuration for a given event handler, if one was set via @WithRetry.
Signature
getRetryConfig(event: string): IBackoffRetryConfig | undefinedParameters
event: The event name to look up retry config for
Returns
The IBackoffRetryConfig if the handler has @WithRetry, otherwise undefined.
Behavior
- Loads the entity using the URN
- Finds the appropriate transition based on current state and event
- Validates transition conditions
- Executes the event handler
- Updates entity state
- Handles retries if configured
- Processes automatic transitions if applicable
Error Handling
- Throws
BadRequestExceptionif no workflow is found for the event - Throws
BadRequestExceptionif no valid transition is found - Updates entity to failed state on error
- Respects
UnretriableExceptionto prevent retries
Lifecycle
The service initializes routes on module initialization (onModuleInit):
- Discovers all workflow classes
- Extracts workflow definitions and handlers
- Resolves entity services
- Builds route map for event handling
StateRouterHelperFactory
Factory for creating router helpers that assist with state routing logic.
Import
import { StateRouterHelperFactory } from 'nestflow-js/core';Methods
create(event, entityService, definition, logger)
Creates a new router helper instance.
Signature
create(
event: string,
entityService: IWorkflowEntity,
definition: IWorkflowDefinition,
logger: Logger
): StateRouterHelperRouterService
Service responsible for routing workflow events to appropriate handlers.
Note
This service is used internally by OrchestratorService and typically doesn't need to be used directly.
Usage Example
import { Module, Injectable } from '@nestjs/common';
import { WorkflowModule, OrchestratorService } from 'nestflow-js/core';
import type { IWorkflowEvent } from 'nestflow-js/core';
@Injectable()
export class WorkflowProcessor {
constructor(private orchestrator: OrchestratorService) {}
async processEvent(event: IWorkflowEvent) {
try {
await this.orchestrator.transit(event);
console.log('Event processed successfully');
} catch (error) {
console.error('Failed to process event:', error);
throw error;
}
}
}
@Module({
imports: [
WorkflowModule.register({
entities: [],
workflows: [],
}),
],
providers: [WorkflowProcessor],
})
export class AppModule {}