trainingpeaks-sdk
Version:
TypeScript SDK for TrainingPeaks API integration
56 lines (55 loc) • 1.73 kB
JavaScript
import { ValidationError } from '../../domain/errors/domain-errors.js';
import { IntensityClass, LengthUnit } from '../../types/index.js';
export class WorkoutStepBuilder {
constructor() {
this.step = {
name: '',
length: { value: 0, unit: LengthUnit.MINUTE },
targets: [],
intensityClass: IntensityClass.ACTIVE,
openDuration: false,
};
}
name(name) {
this.step = { ...this.step, name };
return this;
}
duration(minutes) {
this.step = {
...this.step,
length: { value: minutes, unit: LengthUnit.MINUTE },
};
return this;
}
distance(value, unit = LengthUnit.KILOMETER) {
this.step = { ...this.step, length: { value, unit } };
return this;
}
addTarget(minValue, maxValue) {
if (minValue < 0) {
throw new ValidationError('Minimum target value must be non-negative', 'minValue');
}
if (maxValue < 0) {
throw new ValidationError('Maximum target value must be non-negative', 'maxValue');
}
if (minValue >= maxValue) {
throw new ValidationError('Minimum target value must be less than maximum target value', 'target');
}
this.step = {
...this.step,
targets: [...this.step.targets, { minValue, maxValue }],
};
return this;
}
intensityClass(intensityClass) {
this.step = { ...this.step, intensityClass };
return this;
}
openDuration(open = true) {
this.step = { ...this.step, openDuration: open };
return this;
}
build() {
return { ...this.step };
}
}