Dynamic access to Operators in both front and backend

This commit is contained in:
Felix Kaspar
2024-02-23 23:48:03 +01:00
parent 244fb36195
commit 644e0ceae9
18 changed files with 593 additions and 74 deletions

View File

@@ -6,6 +6,7 @@ const PORT = 8000;
// server-node: backend api
import api from "./routes/api/api-controller";
import { listOperatorNames } from "@stirling-pdf/shared-operations/src/workflow/operatorAccessor";
app.use("/api", api);
// serve
@@ -16,3 +17,5 @@ app.listen(PORT, () => {
process.on('unhandledRejection', (reason, promise) => {
console.error('Unhandled Rejection at:', promise, 'reason:', reason);
});
console.log("Available Modules: ", listOperatorNames())

View File

@@ -2,7 +2,7 @@ import express, { Request, Response } from "express";
const router = express.Router();
import multer from "multer";
const upload = multer();
import { getOperatorByName } from "@stirling-pdf/shared-operations/src/workflow/getOperatorByName";
import { getOperatorByName } from "@stirling-pdf/shared-operations/src/workflow/operatorAccessor";
import { Operator } from "@stirling-pdf/shared-operations/src/functions";
import { PdfFile } from "@stirling-pdf/shared-operations/src/wrappers/PdfFile";
@@ -11,14 +11,14 @@ import { Action } from "@stirling-pdf/shared-operations/declarations/Action";
import { JoiPDFFileSchema } from "@stirling-pdf/shared-operations/src/wrappers/PdfFileJoi";
router.post("/:func", upload.array("file"), async function(req: Request, res: Response) {
handleEndpoint(req, res);
await handleEndpoint(req, res);
});
router.post("/:dir/:func", upload.array("file"), async function(req: Request, res: Response) {
handleEndpoint(req, res);
await handleEndpoint(req, res);
});
function handleEndpoint(req: Request, res: Response) {
async function handleEndpoint(req: Request, res: Response) {
if(!req.files || req.files.length == 0) {
res.status(400).json({error: "no input file(s) were provided"});
return;
@@ -31,7 +31,8 @@ function handleEndpoint(req: Request, res: Response) {
}
const pdfFiles: PdfFile[] = validationResults.value;
const operator = getOperatorByName(req.params.func);
const operator = await getOperatorByName(req.params.func);
if(operator) {
const action: Action = {type: req.params.func, values: req.body};