mirror of
https://github.com/cpvalente/ontime.git
synced 2026-08-06 07:53:54 +00:00
7c1d2f4554
* refactor: remove unneeded async * chore: add express Router type to all routes * refactor: don't use index in react key * refactor: correctly get error message in excel route * refactor: avoid exporting muteable values
54 lines
1.8 KiB
TypeScript
54 lines
1.8 KiB
TypeScript
/**
|
|
* API Router
|
|
* User to handle all requests which affect runtime
|
|
* It is a mirror implementation of OSC and Websocket Adapters
|
|
*
|
|
*/
|
|
|
|
import express, { Router, type Request, type Response } from 'express';
|
|
import { ErrorResponse, LogOrigin } from 'ontime-types';
|
|
import { getErrorMessage } from 'ontime-utils';
|
|
|
|
import { integrationPayloadFromPath } from '../adapters/utils/parse.js';
|
|
import { logger } from '../classes/Logger.js';
|
|
import { isEmptyObject } from '../utils/parserUtils.js';
|
|
import { dispatchFromAdapter } from './integration.controller.js';
|
|
|
|
export const integrationRouter: Router = 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', async (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 = await 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 });
|
|
}
|
|
});
|