Files
rk-backend/src/_dto/motor.dto.ts
T
2026-10-26 07:34:23 +01:00

43 lines
1.7 KiB
TypeScript

import { BadRequestException } from '@nestjs/common';
import type { MotorConfig, MotorOptions } from '../graphics/elements/types';
export class MotorRenderDto {
config!: MotorConfig;
options?: Partial<MotorOptions>;
}
export function parseMotorRequest(input: unknown): MotorRenderDto {
const object = (value: unknown): value is Record<string, unknown> =>
typeof value === 'object' && value !== null && !Array.isArray(value);
if (!object(input) || Object.keys(input).some(key => !['config', 'options'].includes(key)) || !object(input.config))
throw new BadRequestException('Erwartet werden config und optional options');
const config = input.config;
if (Object.keys(config).some(key => !['scale', 'raster'].includes(key)))
throw new BadRequestException('Motor-config erlaubt nur scale und raster');
const scale = config.scale ?? 2, raster = config.raster ?? 4;
if (typeof scale !== 'number' ||
!Number.isFinite(scale) ||
scale <= 0 ||
typeof raster !== 'number' ||
!Number.isFinite(raster) ||
raster <= 0 ||
!Number.isFinite(3 * scale * raster)
)
throw new BadRequestException('scale und raster müssen positive endliche Zahlen sein');
const options = input.options === undefined ? {} : input.options;
if (!object(options) ||
Object.entries(options).some(([key, value]) => !['colorInactive', 'colorActive', 'colorError'].includes(key) ||
typeof value !== 'string' || !/^#[0-9a-fA-F]{6}$/.test(value))
)
throw new BadRequestException('Motor-options erlaubt colorInactive, colorActive und colorError als Hexfarbe #RRGGBB');
return { config: { scale, raster }, options: options as Partial<MotorOptions> };
}