PHP 8.2.30
Preview: buildRouter.js Size: 3.08 KB
//proc/thread-self/root/home/byroehnu/.trash/node_modules11/@adminjs/express/lib/buildRouter.js

import { Router as AdminRouter } from "adminjs";
import { Router } from "express";
import formidableMiddleware from "express-formidable";
import path from "path";
import { WrongArgumentError } from "./errors.js";
import { log } from "./logger.js";
import { convertToExpressRoute } from "./convertRoutes.js";
const INVALID_ADMINJS_INSTANCE = "You have to pass an instance of AdminJS to the buildRouter() function";
export const initializeAdmin = (admin) => {
    var _a;
    if (((_a = admin === null || admin === void 0 ? void 0 : admin.constructor) === null || _a === void 0 ? void 0 : _a.name) !== "AdminJS") {
        throw new WrongArgumentError(INVALID_ADMINJS_INSTANCE);
    }
    admin.initialize().then(() => {
        log.debug("AdminJS: bundle ready");
    });
};
export const routeHandler = ({ admin, route }) => async (req, res, next) => {
    try {
        const controller = new route.Controller({ admin }, req.session && req.session.adminUser);
        const { params, query } = req;
        const method = req.method.toLowerCase();
        const payload = Object.assign(Object.assign({}, (req.fields || {})), (req.files || {}));
        const html = await controller[route.action](Object.assign(Object.assign({}, req), { params,
            query,
            payload,
            method }), res);
        if (route.contentType) {
            res.set({ "Content-Type": route.contentType });
        }
        if (html) {
            res.send(html);
        }
    }
    catch (e) {
        next(e);
    }
};
export const buildRoute = ({ route, router, admin, }) => {
    // we have to change routes defined in AdminJS from {recordId} to :recordId
    const expressPath = convertToExpressRoute(route.path);
    if (route.method === "GET") {
        router.get(expressPath, routeHandler({ admin, route }));
    }
    if (route.method === "POST") {
        router.post(expressPath, routeHandler({ admin, route }));
    }
};
export const buildRoutes = ({ admin, routes, router, }) => {
    routes.forEach((route) => buildRoute({ route, router, admin }));
};
export const buildAssets = ({ admin, assets, routes, router, }) => {
    // Note: We want components.bundle.js to be globally available. In production it is served as a .js asset, meanwhile
    // in local environments it's a route with "bundleComponents" action assigned.
    const componentBundlerRoute = routes.find((r) => r.action === "bundleComponents");
    if (componentBundlerRoute) {
        buildRoute({ route: componentBundlerRoute, router, admin });
    }
    assets.forEach((asset) => {
        router.get(asset.path, async (_req, res) => {
            res.sendFile(path.resolve(asset.src));
        });
    });
};
export const buildRouter = (admin, predefinedRouter, formidableOptions) => {
    initializeAdmin(admin);
    const { routes, assets } = AdminRouter;
    const router = predefinedRouter !== null && predefinedRouter !== void 0 ? predefinedRouter : Router();
    // todo fix types
    router.use(formidableMiddleware(formidableOptions));
    buildAssets({ admin, assets, routes, router });
    buildRoutes({ admin, routes, router });
    return router;
};

Directory Contents

Dirs: 1 × Files: 14

Name Size Perms Modified Actions
- drwxr-xr-x 2026-02-28 00:29:10
Edit Download
1.51 KB lrw-r--r-- 2026-02-28 00:28:44
Edit Download
2.99 KB lrw-r--r-- 2026-02-28 00:27:30
Edit Download
1.15 KB lrw-r--r-- 2026-02-28 00:28:48
Edit Download
3.08 KB lrw-r--r-- 2026-02-28 00:27:34
Edit Download
76 B lrw-r--r-- 2026-02-28 00:28:52
Edit Download
102 B lrw-r--r-- 2026-02-28 00:27:44
Edit Download
491 B lrw-r--r-- 2026-02-28 00:28:56
Edit Download
1.07 KB lrw-r--r-- 2026-02-28 00:27:48
Edit Download
3.68 KB lrw-r--r-- 2026-02-28 00:28:58
Edit Download
3.41 KB lrw-r--r-- 2026-02-28 00:28:00
Edit Download
172 B lrw-r--r-- 2026-02-28 00:29:00
Edit Download
255 B lrw-r--r-- 2026-02-28 00:28:04
Edit Download
1.17 KB lrw-r--r-- 2026-02-28 00:29:12
Edit Download
11 B lrw-r--r-- 2026-02-28 00:28:24
Edit Download

If ZipArchive is unavailable, a .tar will be created (no compression).