mirror of
https://github.com/cpvalente/ontime.git
synced 2026-08-11 18:33:53 +00:00
update API - part 1 (#709)
* api: `test-ontime` -> `version` * api: `ontime-poll` -> `poll` * api: `start-next` -> `startnext` * api: `start-next` -> `startnext` --------- Co-authored-by: Carlos Valente <34649812+cpvalente@users.noreply.github.com> Co-authored-by: Carlos Valente <carlosvalente@pm.me>
This commit is contained in:
committed by
GitHub
parent
cb8ba776e2
commit
0cd2ca4191
@@ -5,6 +5,7 @@ import { Server } from 'node-osc';
|
||||
import { IAdapter } from './IAdapter.js';
|
||||
import { dispatchFromAdapter, type ChangeOptions } from '../controllers/integrationController.js';
|
||||
import { logger } from '../classes/Logger.js';
|
||||
import { objectFromPath } from './utils/parse.js';
|
||||
|
||||
export class OscServer implements IAdapter {
|
||||
private readonly osc: Server;
|
||||
@@ -18,6 +19,7 @@ export class OscServer implements IAdapter {
|
||||
// message should look like /ontime/{path}/{params?} {args} where
|
||||
// ontime: fixed message for app
|
||||
// path: command to be called
|
||||
// params: used to create a nested object to patch with
|
||||
// args: extra data, only used on some API entries (delay, goto)
|
||||
|
||||
// split message
|
||||
@@ -38,7 +40,7 @@ export class OscServer implements IAdapter {
|
||||
|
||||
let transformedPayload: unknown = args;
|
||||
// we need to transform the params for the change endpoint
|
||||
// OSC: ontime/change/{eventID}/{propertyName} value
|
||||
// OSC: /ontime/change/{eventID}/{propertyName} value
|
||||
if (path === 'change') {
|
||||
if (params.length < 2) {
|
||||
logger.error(LogOrigin.Rx, 'OSC IN: No params provided for change');
|
||||
@@ -59,10 +61,11 @@ export class OscServer implements IAdapter {
|
||||
property,
|
||||
value,
|
||||
} satisfies ChangeOptions;
|
||||
} else if (params.length) {
|
||||
transformedPayload = objectFromPath(params, args);
|
||||
}
|
||||
|
||||
try {
|
||||
// we dont reply on OSC
|
||||
dispatchFromAdapter(
|
||||
path,
|
||||
{
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
import { objectFromPath } from '../parse.js';
|
||||
|
||||
describe('objectFromPath()', () => {
|
||||
it('start index', () => {
|
||||
const arr = ['index'];
|
||||
const value = 1;
|
||||
const objExpected = { index: 1 };
|
||||
|
||||
const obj = objectFromPath(arr, value);
|
||||
expect(obj).toStrictEqual(objExpected);
|
||||
});
|
||||
it('set timer message text', () => {
|
||||
const arr = ['timer', 'text'];
|
||||
const value = 'hello';
|
||||
const objExpected = { timer: { text: value } };
|
||||
|
||||
const obj = objectFromPath(arr, value);
|
||||
expect(obj).toStrictEqual(objExpected);
|
||||
});
|
||||
it('set timer message text and visible', () => {
|
||||
const arr = ['timer'];
|
||||
const value = { text: 'hello', visible: true };
|
||||
const objExpected = { timer: value };
|
||||
|
||||
const obj = objectFromPath(arr, value);
|
||||
expect(obj).toStrictEqual(objExpected);
|
||||
});
|
||||
|
||||
it('nests object with undefined value', () => {
|
||||
const arr = ['a', 'b', 'c', 'd'];
|
||||
const objExpected = { a: { b: { c: { d: undefined } } } };
|
||||
|
||||
const obj = objectFromPath(arr);
|
||||
expect(obj).toStrictEqual(objExpected);
|
||||
});
|
||||
it('empty array creates undefinde object', () => {
|
||||
const arr = [];
|
||||
const value = '1234567890';
|
||||
const objExpected = null;
|
||||
|
||||
const obj = objectFromPath(arr, value);
|
||||
expect(obj).toStrictEqual(objExpected);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,11 @@
|
||||
/**
|
||||
* @description Creates a nested object with keys from an array and assigns the `value` to the last key
|
||||
* @param {array} path - array to be nested
|
||||
* @param {string} value - value to assign
|
||||
* @returns {object | null} nested object or null if no object was created
|
||||
*/
|
||||
export const objectFromPath = (path: string[], value?: unknown): object | null => {
|
||||
const obj = path.reduceRight((result, key) => ({ [key]: result }), value);
|
||||
if (typeof obj === 'object') return obj;
|
||||
return null;
|
||||
};
|
||||
Reference in New Issue
Block a user