trainingpeaks-sdk
Version:
TypeScript SDK for TrainingPeaks API integration
74 lines (73 loc) • 3.23 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.WorkoutStructureSchema = exports.WorkoutStructureElementSchema = exports.WorkoutElementTypeSchema = exports.WorkoutStepSchema = exports.WorkoutTargetSchema = exports.WorkoutLengthSchema = exports.WorkoutIntensityTargetTypeSchema = exports.WorkoutIntensityMetricSchema = exports.WorkoutLengthMetricSchema = exports.WorkoutIntensityClassSchema = exports.WorkoutLengthUnitSchema = void 0;
const zod_1 = require("zod");
const types_1 = require("../../types/index.js");
exports.WorkoutLengthUnitSchema = zod_1.z.nativeEnum(types_1.LengthUnit);
exports.WorkoutIntensityClassSchema = zod_1.z.nativeEnum(types_1.IntensityClass);
exports.WorkoutLengthMetricSchema = zod_1.z.nativeEnum(types_1.LengthMetric);
exports.WorkoutIntensityMetricSchema = zod_1.z.nativeEnum(types_1.IntensityMetric);
exports.WorkoutIntensityTargetTypeSchema = zod_1.z.nativeEnum(types_1.IntensityTargetType);
exports.WorkoutLengthSchema = zod_1.z.object({
value: zod_1.z.number().nonnegative().finite(),
unit: exports.WorkoutLengthUnitSchema,
});
exports.WorkoutTargetSchema = zod_1.z
.object({
minValue: zod_1.z.number().nonnegative().finite(),
maxValue: zod_1.z.number().nonnegative().finite(),
})
.refine((data) => data.minValue <= data.maxValue, {
message: 'minValue cannot be greater than maxValue',
path: ['minValue'],
});
exports.WorkoutStepSchema = zod_1.z
.object({
name: zod_1.z.string().min(1).max(100),
length: exports.WorkoutLengthSchema,
targets: zod_1.z.array(exports.WorkoutTargetSchema),
intensityClass: exports.WorkoutIntensityClassSchema,
openDuration: zod_1.z.boolean().default(false),
})
.refine((data) => {
if (data.intensityClass === types_1.IntensityClass.REST)
return true;
return data.targets.length > 0;
}, {
message: 'Non-rest steps must have at least one target',
path: ['targets'],
});
exports.WorkoutElementTypeSchema = zod_1.z.nativeEnum(types_1.ElementType);
exports.WorkoutStructureElementSchema = zod_1.z
.object({
type: exports.WorkoutElementTypeSchema,
length: exports.WorkoutLengthSchema,
steps: zod_1.z.array(exports.WorkoutStepSchema).min(1),
begin: zod_1.z.number().nonnegative(),
end: zod_1.z.number().nonnegative(),
})
.refine((data) => data.end > data.begin, {
message: 'end time must be greater than begin time',
path: ['end'],
});
exports.WorkoutStructureSchema = zod_1.z
.object({
structure: zod_1.z.array(exports.WorkoutStructureElementSchema).min(1),
polyline: zod_1.z.array(zod_1.z.array(zod_1.z.number())),
primaryLengthMetric: exports.WorkoutLengthMetricSchema,
primaryIntensityMetric: exports.WorkoutIntensityMetricSchema,
primaryIntensityTargetOrRange: exports.WorkoutIntensityTargetTypeSchema,
})
.refine((data) => {
for (let i = 0; i < data.structure.length - 1; i++) {
const current = data.structure[i];
const next = data.structure[i + 1];
if (current && next && current.end > next.begin) {
return false;
}
}
return true;
}, {
message: 'Workout structure elements cannot overlap',
path: ['structure'],
});