UNPKG

vike

Version:

The Framework *You* Control - Next.js & Nuxt alternative for unprecedented flexibility and dependability.

43 lines (42 loc) 1.99 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.executeGuardHook = executeGuardHook; const getHook_js_1 = require("../hooks/getHook.js"); const utils_js_1 = require("./utils.js"); const executeHook_js_1 = require("../hooks/executeHook.js"); const errIntro = 'The guard() hook defined by'; async function executeGuardHook(pageContext, prepareForUserConsumption) { let hook; if (pageContext._pageFilesAll.length > 0) { // V0.4 design (0, utils_js_1.assert)(pageContext._pageConfigs.length === 0); hook = findPageGuard(pageContext.pageId, pageContext._pageFilesAll); } else { // V1 design hook = (0, getHook_js_1.getHookFromPageContext)(pageContext, 'guard'); } if (!hook) return; const guard = hook.hookFn; let pageContextForUserConsumption = pageContext; const res = prepareForUserConsumption(pageContext); if (res) pageContextForUserConsumption = res; const hookResult = await (0, executeHook_js_1.executeHook)(() => guard(pageContextForUserConsumption), hook, pageContext); (0, utils_js_1.assertUsage)(hookResult === undefined, `${errIntro} ${hook.hookFilePath} returns a value, but guard() shouldn't return any value`); } function findPageGuard(pageId, pageFilesAll) { const pageRouteFile = pageFilesAll.find((p) => p.pageId === pageId && p.fileType === '.page.route'); if (!pageRouteFile) return null; const { filePath, fileExports } = pageRouteFile; (0, utils_js_1.assert)(fileExports); // loadPageRoutes() should already have been called const hookFn = fileExports.guard; if (!hookFn) return null; const hookFilePath = filePath; const hookTimeout = (0, getHook_js_1.getHookTimeoutDefault)('guard'); (0, utils_js_1.assertUsage)((0, utils_js_1.isCallable)(hookFn), `${errIntro} ${hookFilePath} should be a function`); return { hookFn, hookName: 'guard', hookFilePath, hookTimeout }; }