@rudderstack/workflow-engine
Version:
A generic workflow execution engine
134 lines • 5.21 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.StepUtils = void 0;
const errors_1 = require("../../errors");
const types_1 = require("../types");
const common_1 = require("./common");
const stepNameRegex = /^[a-zA-Z][0-9a-zA-Z]*$/;
class StepUtils {
static getStepType(step) {
if (StepUtils.isBatchStep(step)) {
return types_1.StepType.Batch;
}
if (StepUtils.isCustomStep(step)) {
return types_1.StepType.Custom;
}
if (StepUtils.isWorkflowStep(step)) {
return types_1.StepType.Workflow;
}
if (StepUtils.isSimpleStep(step)) {
return types_1.StepType.Simple;
}
return types_1.StepType.Unknown;
}
static isBatchStep(step) {
return step.type === types_1.StepType.Batch;
}
static isCustomStep(step) {
return step.type === types_1.StepType.Custom;
}
static isWorkflowStep(step) {
return !!step.steps?.length || !!step.workflowStepPath;
}
static isSimpleStep(step) {
return (!!step.identity ||
!!step.template ||
!!step.templatePath ||
!!step.functionName ||
!!step.externalWorkflow);
}
static populateElseStep(step) {
if (step.else) {
// eslint-disable-next-line no-param-reassign
step.else.type = StepUtils.getStepType(step.else);
this.populateElseStep(step.else);
}
}
static populateSteps(steps) {
for (const step of steps) {
step.type = StepUtils.getStepType(step);
this.populateElseStep(step);
}
}
static checkForStepNameDuplicates(steps) {
const duplicateNames = common_1.CommonUtils.findDuplicateStrings(steps.map((step) => step.name));
if (duplicateNames.length > 0) {
throw new errors_1.StepCreationError(`found duplicate step names: ${duplicateNames}`);
}
}
static validateSteps(steps, notAllowedTypes) {
const notAllowed = notAllowedTypes ?? [];
notAllowed.push(types_1.StepType.Unknown);
this.checkForStepNameDuplicates(steps);
for (let i = 0; i < steps.length; i += 1) {
StepUtils.validateStep(steps[i], i, notAllowed);
}
}
static validateStepName(step, index) {
if (!step.name) {
throw new errors_1.StepCreationError(`step#${index} should have a name`);
}
if (!stepNameRegex.exec(step.name)) {
throw new errors_1.StepCreationError('step name is invalid', step.name);
}
}
static validateStepType(step, index, notAllowed) {
if (notAllowed.includes(step.type)) {
throw new errors_1.StepCreationError('unsupported step type', step.name);
}
}
static validateElseStep(step, index, notAllowed) {
if (step.else) {
if (!step.condition) {
throw new errors_1.StepCreationError('else step should be used in a step with condition', step.name);
}
else {
this.validateStep(step.else, index, notAllowed);
}
}
}
static validateLoopStep(step) {
if (step.loopCondition && !step.loopOverInput) {
throw new errors_1.StepCreationError('loopCondition should be used with loopOverInput', step.name);
}
if (step.loopOverInput && step.type === types_1.StepType.Batch) {
throw new errors_1.StepCreationError('loopOverInput is not supported for batch step', step.name);
}
}
static validateOnComplete(step) {
if (step.onComplete === types_1.StepExitAction.Return && !step.condition) {
throw new errors_1.StepCreationError('"onComplete = return" should be used in a step with condition', step.name);
}
}
static validateStep(step, index, notAllowed) {
this.validateStepName(step, index);
this.validateStepType(step, index, notAllowed);
this.validateElseStep(step, index, notAllowed);
this.validateLoopStep(step);
this.validateOnComplete(step);
if (step.type === types_1.StepType.Batch) {
this.ValidateBatchStep(step);
}
if (step.type === types_1.StepType.Custom) {
this.ValidateCustomStep(step);
}
}
static ValidateBatchStep(step) {
if (!step.batches && !step.executor) {
throw new errors_1.StepCreationError('batches or executor is required for batch step', step.name);
}
if (step.batches && step.executor) {
throw new errors_1.StepCreationError('only one of batches or executor should be specified', step.name);
}
}
static ValidateCustomStep(step) {
if (!step.provider && !step.executor) {
throw new errors_1.StepCreationError('provider or executor is required for custom step', step.name);
}
if (step.executor && step.provider) {
throw new errors_1.StepCreationError('only one of provider or executor should be specified', step.name);
}
}
}
exports.StepUtils = StepUtils;
//# sourceMappingURL=step.js.map