UNPKG

@premieroctet/next-admin

Version:

Next-Admin provides a customizable and turnkey admin dashboard for applications built with Next.js and powered by the Prisma ORM. It aims to simplify the development process by providing a turnkey admin system that can be easily integrated into your proje

225 lines (224 loc) 9.39 kB
"use strict"; var __webpack_require__ = {}; (()=>{ __webpack_require__.d = (exports1, definition)=>{ for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, { enumerable: true, get: definition[key] }); }; })(); (()=>{ __webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop); })(); (()=>{ __webpack_require__.r = (exports1)=>{ if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, { value: 'Module' }); Object.defineProperty(exports1, '__esModule', { value: true }); }; })(); var __webpack_exports__ = {}; __webpack_require__.r(__webpack_exports__); __webpack_require__.d(__webpack_exports__, { createHandler: ()=>createHandler }); const external_next_connect_namespaceObject = require("next-connect"); const HookError_js_namespaceObject = require("./exceptions/HookError.js"); const options_js_namespaceObject = require("./handlers/options.js"); const resources_js_namespaceObject = require("./handlers/resources.js"); const external_types_js_namespaceObject = require("./types.js"); const permissions_js_namespaceObject = require("./utils/permissions.js"); const prisma_js_namespaceObject = require("./utils/prisma.js"); const server_js_namespaceObject = require("./utils/server.js"); const globals_js_namespaceObject = require("./utils/globals.js"); const createHandler = ({ apiBasePath, options, prisma, paramKey = "nextadmin", onRequest })=>{ const router = (0, external_next_connect_namespaceObject.createRouter)(); router.use(async (req, res, next)=>{ await (0, globals_js_namespaceObject.initGlobals)(); return next(); }); if (onRequest) router.use(onRequest); router.get(`${apiBasePath}/:model/raw`, async (req, res)=>{ const resources = (0, server_js_namespaceObject.getResources)(options); const resource = (0, server_js_namespaceObject.getResourceFromParams)([ req.query[paramKey][0] ], resources); if (!resource) return res.status(404).json({ error: "Resource not found" }); let ids = req.query.ids; ids = Array.isArray(ids) ? ids.map((id)=>(0, server_js_namespaceObject.formatId)(resource, id)) : ids?.split(",").map((id)=>(0, server_js_namespaceObject.formatId)(resource, id)); const depth = req.query.depth; if (depth && isNaN(Number(depth))) return res.status(400).json({ error: "Depth should be a number" }); const data = await (0, prisma_js_namespaceObject.getRawData)({ prisma, resource, resourceIds: ids, maxDepth: depth ? Number(depth) : void 0 }); return res.json(data); }).post(`${apiBasePath}/:model/actions/:id`, async (req, res)=>{ const resources = (0, server_js_namespaceObject.getResources)(options); const id = req.query[paramKey].at(-1); const resource = (0, server_js_namespaceObject.getResourceFromParams)([ req.query[paramKey][0] ], resources); if (!resource) return res.status(404).json({ type: "error", message: "Resource not found" }); const modelAction = options?.model?.[resource]?.actions?.find((action)=>action.id === id); if (!modelAction) return res.status(404).json({ type: "error", message: "Action not found" }); if ("type" in modelAction && "dialog" === modelAction.type) return res.status(404).json({ type: "error", message: "Action not found" }); let body; try { body = await (0, server_js_namespaceObject.getJsonBody)(req); } catch { return res.status(400).json({ type: "error", message: "Invalid JSON body" }); } try { const result = await modelAction.action(body); return res.json(result ?? null); } catch (e) { return res.status(500).json({ type: "error", message: e.message }); } }).post(`${apiBasePath}/options`, async (req, res)=>{ let body; try { body = await (0, server_js_namespaceObject.getJsonBody)(req); } catch { return res.status(400).json({ error: "Invalid JSON body" }); } const data = await (0, options_js_namespaceObject.handleOptionsSearch)(body, prisma, options); return res.json(data); }).post(`${apiBasePath}/:model/:id?`, async (req, res)=>{ const resources = (0, server_js_namespaceObject.getResources)(options); const resource = (0, server_js_namespaceObject.getResourceFromParams)([ req.query[paramKey][0] ], resources); if (!resource) return res.status(404).json({ error: "Resource not found" }); const body = await (0, server_js_namespaceObject.getFormDataValues)(req); const id = 2 === req.query[paramKey].length ? (0, server_js_namespaceObject.formatId)(resource, req.query[paramKey].at(-1)) : void 0; const editOptions = options?.model?.[resource]?.edit; const mode = id ? "edit" : "create"; try { const transformedBody = await editOptions?.hooks?.beforeDb?.(body, mode, req); let response = await (0, resources_js_namespaceObject.submitResource)({ prisma, resource, body: transformedBody ?? body, id, options, schema: (0, globals_js_namespaceObject.getSchema)() }); if (response.error) return res.status(400).json({ error: response.error, validation: response.validation }); response = await editOptions?.hooks?.afterDb?.(response, mode, req) ?? response; return res.status(id ? 200 : 201).json(response); } catch (e) { if (e instanceof HookError_js_namespaceObject.HookError) return res.status(e.status).json(e.data); return res.status(500).json({ error: e.message }); } }).delete(`${apiBasePath}/:model/:id`, async (req, res)=>{ const resources = (0, server_js_namespaceObject.getResources)(options); const resource = (0, server_js_namespaceObject.getResourceFromParams)([ req.query[paramKey][0] ], resources); if (!resource) return res.status(404).json({ error: "Resource not found" }); if (!(0, permissions_js_namespaceObject.hasPermission)(options?.model?.[resource], external_types_js_namespaceObject.Permission.DELETE)) return res.status(403).json({ error: "You don't have permission to delete this resource" }); try { const deleted = await (0, resources_js_namespaceObject.deleteResource)({ body: [ req.query[paramKey][1] ], prisma, resource, modelOptions: options?.model?.[resource] }); if (!deleted) throw new Error("Deletion failed"); return res.json({ ok: true }); } catch (e) { return res.status(500).json({ error: e.message }); } }).delete(`${apiBasePath}/:model`, async (req, res)=>{ const resources = (0, server_js_namespaceObject.getResources)(options); const resource = (0, server_js_namespaceObject.getResourceFromParams)([ req.query[paramKey][0] ], resources); if (!resource) return res.status(404).json({ error: "Resource not found" }); if (!(0, permissions_js_namespaceObject.hasPermission)(options?.model?.[resource], external_types_js_namespaceObject.Permission.DELETE)) return res.status(403).json({ error: "You don't have permission to delete this resource" }); let body; try { body = await (0, server_js_namespaceObject.getJsonBody)(req); } catch { return res.status(400).json({ error: "Invalid JSON body" }); } try { await (0, resources_js_namespaceObject.deleteResource)({ body, prisma, resource, modelOptions: options?.model?.[resource] }); return res.json({ ok: true }); } catch (e) { return res.status(500).json({ error: e.message }); } }); const executeRouteHandler = (req, res)=>router.run(req, res); return { run: executeRouteHandler, router }; }; exports.createHandler = __webpack_exports__.createHandler; for(var __webpack_i__ in __webpack_exports__)if (-1 === [ "createHandler" ].indexOf(__webpack_i__)) exports[__webpack_i__] = __webpack_exports__[__webpack_i__]; Object.defineProperty(exports, '__esModule', { value: true });