@directus/api
Version:
Directus is a real-time API and App dashboard for managing SQL database content
51 lines (49 loc) • 1.65 kB
JavaScript
import async_handler_default from "../../../../utils/async-handler.js";
import { callReference } from "./call-reference.js";
import express from "express";
//#region src/extensions/lib/sandbox/register/route.ts
function registerRouteGenerator(endpointName, endpointRouter) {
const router = express.Router();
endpointRouter.use(`/${endpointName}`, router);
const registerRoute = (path, method, cb) => {
if (path.typeof !== "string") throw new TypeError("Route path has to be of type string");
if (method.typeof !== "string") throw new TypeError("Route method has to be of type string");
if (cb.typeof !== "function") throw new TypeError("Route handler has to be of type function");
const pathCopied = path.copySync();
const methodCopied = method.copySync();
const handler = async_handler_default(async (req, res) => {
const responseCopied = await (await callReference(cb, [{
url: req.url,
headers: req.headers,
body: req.body
}])).copy();
res.status(responseCopied.status).send(responseCopied.body);
});
switch (methodCopied) {
case "GET":
router.get(pathCopied, handler);
break;
case "POST":
router.post(pathCopied, handler);
break;
case "PUT":
router.put(pathCopied, handler);
break;
case "PATCH":
router.patch(pathCopied, handler);
break;
case "DELETE":
router.delete(pathCopied, handler);
break;
}
};
const unregisterFunction = () => {
endpointRouter.stack = endpointRouter.stack.filter((layer) => router !== layer.handle);
};
return {
register: registerRoute,
unregisterFunction
};
}
//#endregion
export { registerRouteGenerator };