Improve change api (#628)

* fix: jsdoc comments

* more human casting of string values to boolean

* add coerceColour to get named colors to work

* cue can't pass isKeyOfType event check

* dont mutate value

* alloow cue in eventDef

* lint

* css named or hex formatting

* "sideEffects": false

* test colour hex regex

* roll eventDef back to master

* parse property

* don't export cssColour names

* remove import

* only allow string types to colour

* handle transparent colour

* mix alpha with bg colour

* only allow updating events

* descriptive names

* add test for getAccessibleColour

* readability

* move isColourHex to regex-utils

* simplify and add test
This commit is contained in:
Alex Christoffer Rasmussen
2023-12-08 12:55:04 +01:00
committed by GitHub
parent a93cd3e9b1
commit 1343818917
12 changed files with 322 additions and 27 deletions
@@ -1,8 +1,9 @@
import { LogOrigin, OntimeEvent } from 'ontime-types';
import { EventLoader } from '../classes/event-loader/EventLoader.js';
import { editEvent } from '../services/rundown-service/RundownService.js';
import { coerceString, coerceNumber, coerceBoolean } from '../utils/coerceType.js';
import { coerceString, coerceNumber, coerceBoolean, coerceColour } from '../utils/coerceType.js';
import { logger } from '../classes/Logger.js';
import { isKeyOfType, isOntimeEvent } from 'ontime-types/src/utils/guards.js';
const whitelistedPayload = {
title: coerceString,
@@ -16,7 +17,8 @@ const whitelistedPayload = {
isPublic: coerceBoolean,
skip: coerceBoolean,
colour: coerceString,
colour: coerceColour,
user0: coerceString,
user1: coerceString,
user2: coerceString,
@@ -29,12 +31,12 @@ const whitelistedPayload = {
user9: coerceString,
};
export function parse(field: string, value: unknown) {
if (!Object.hasOwn(whitelistedPayload, field)) {
throw new Error(`Field ${field} not permitted`);
export function parse(property: string, value: unknown) {
if (!isKeyOfType(property, whitelistedPayload)) {
throw new Error(`Property ${property} not permitted`);
}
const parserFn = whitelistedPayload[field];
return parserFn(value);
const parserFn = whitelistedPayload[property];
return { parsedProperty: property, parsedPayload: parserFn(value) };
}
/**
@@ -49,8 +51,10 @@ export function updateEvent(
newValue: OntimeEvent[typeof propertyName],
) {
const event = EventLoader.getEventWithId(eventId);
if (event) {
if (!isOntimeEvent(event)) {
throw new Error(`Can only update events`);
}
const propertiesToUpdate = { [propertyName]: newValue };
// Handles the special case for duration
@@ -2,8 +2,6 @@ import { messageService } from '../services/message-service/MessageService.js';
import { PlaybackService } from '../services/PlaybackService.js';
import { eventStore } from '../stores/EventStore.js';
import { parse, updateEvent } from './integrationController.config.js';
import { isKeyOfType } from 'ontime-types/src/utils/guards.js';
import { event } from '../models/eventsDefinition.js';
export type ChangeOptions = {
eventId: string;
@@ -272,11 +270,8 @@ export function dispatchFromAdapter(
// WS: {type: 'change', payload: { eventId, property, value } }
case 'change': {
const { eventId, property, value } = payload as ChangeOptions;
if (!isKeyOfType(property, event)) {
throw new Error(`Cannot update unknown event property ${property}`);
}
const parsedPayload = parse(property, value);
return updateEvent(eventId, property, parsedPayload);
const { parsedPayload, parsedProperty } = parse(property, value);
return updateEvent(eventId, parsedProperty, parsedPayload);
}
default: {