Files
ontime/apps/server/src/api-integration/integration.router.ts
T
Alex Christoffer Rasmussen a4b15970de Upgrade expressjs (#1633)
* upgrade expressjs

* migration

* reenable test

* extend timeout on download test

* fixup! migration

* move empty body test from controller to validator

* enusre not empty

* extract validation function

* fixup! reenable test

* remove thin controllers

* disable e2e test of project file download
2025-06-12 15:02:35 +02:00

56 lines
1.7 KiB
TypeScript

/**
* API Router
* User to handle all requests which affect runtime
* It is a mirror implementation of OSC and Websocket Adapters
*
*/
import { ErrorResponse, LogOrigin } from 'ontime-types';
import express, { type Request, type Response } from 'express';
import { logger } from '../classes/Logger.js';
import { integrationPayloadFromPath } from '../adapters/utils/parse.js';
import { dispatchFromAdapter } from './integration.controller.js';
import { getErrorMessage } from 'ontime-utils';
import { isEmptyObject } from '../utils/parserUtils.js';
export const integrationRouter = express.Router();
const helloMessage = 'You have reached Ontime API server';
integrationRouter.get('/', (_req: Request, res: Response<{ message: string }>) => {
res.status(200).json({ message: helloMessage });
});
/**
* All calls are sent to the dispatcher
*/
integrationRouter.get('/*splat', (req: Request, res: Response<ErrorResponse | { payload: unknown }>) => {
let action = req.path.substring(1);
if (!action) {
res.status(400).json({ message: 'No action found' });
return;
}
try {
const actionArray = action.split('/');
const query = isEmptyObject(req.query) ? undefined : (req.query as object);
let payload: unknown = {};
if (actionArray.length > 1) {
// @ts-expect-error -- we decide to give up on typing here
action = actionArray.shift();
payload = integrationPayloadFromPath(actionArray, query);
} else {
payload = query;
}
const reply = dispatchFromAdapter(action, payload, 'http');
res.status(202).json(reply);
} catch (error) {
const errorMessage = getErrorMessage(error);
logger.error(LogOrigin.Rx, `HTTP IN: ${errorMessage}`);
res.status(500).send({ message: errorMessage });
}
});