vike
Version:
The Framework *You* Control - Next.js & Nuxt alternative for unprecedented flexibility and dependability.
74 lines (73 loc) • 4.67 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.resolveRouteFunction = resolveRouteFunction;
exports.assertRouteParams = assertRouteParams;
exports.assertSyncRouting = assertSyncRouting;
exports.warnDeprecatedAllowKey = warnDeprecatedAllowKey;
const execHook_js_1 = require("../hooks/execHook.js");
const preparePageContextForPublicUsage_js_1 = require("../preparePageContextForPublicUsage.js");
const utils_js_1 = require("./utils.js");
const picocolors_1 = __importDefault(require("@brillout/picocolors"));
async function resolveRouteFunction(routeFunction, pageContext, routeFunctionFilePath) {
let { hookReturn: result } = (0, execHook_js_1.execHookDirectSync)({
hookFn: routeFunction,
hookFilePath: routeFunctionFilePath,
hookName: 'route',
}, pageContext, preparePageContextForPublicUsage_js_1.preparePageContextForPublicUsage);
assertSyncRouting(result, `The Route Function ${routeFunctionFilePath}`);
// TODO/v1-release: make resolveRouteFunction() and route() sync
//* We disallow asynchronous routing, because we need to check whether a link is a Vike link in a synchronous fashion before calling ev.preventDefault() in the 'click' event listener
result = await result;
//*/
if (result === false) {
return null;
}
if (result === true) {
result = {};
}
(0, utils_js_1.assertUsage)((0, utils_js_1.isPlainObject)(result), `The Route Function ${routeFunctionFilePath} should return a boolean or a plain JavaScript object (but it's ${picocolors_1.default.cyan(`typeof result === ${JSON.stringify(typeof result)}`)} instead)`);
// AFAICT this return interface is superfluous. Should we soft-deprecate it and remove it?
if ('match' in result) {
const { match } = result;
(0, utils_js_1.assertUsage)(typeof match === 'boolean', `The ${picocolors_1.default.cyan('match')} value returned by the Route Function ${routeFunctionFilePath} should be a boolean.`);
if (!match) {
return null;
}
}
let precedence = null;
if ('precedence' in result) {
precedence = result.precedence;
(0, utils_js_1.assertUsage)(typeof precedence === 'number', `The ${picocolors_1.default.cyan('precedence')} value returned by the Route Function ${routeFunctionFilePath} should be a number.`);
}
assertRouteParams(result, `The ${picocolors_1.default.cyan('routeParams')} object returned by the Route Function ${routeFunctionFilePath} should`);
const routeParams = result.routeParams || {};
(0, utils_js_1.assertUsage)(!('pageContext' in result), `Providing ${picocolors_1.default.cyan('pageContext')} in Route Functions is prohibited, see https://vike.dev/route-function#cannot-provide-pagecontext`);
(0, utils_js_1.assert)((0, utils_js_1.isPlainObject)(routeParams));
Object.keys(result).forEach((key) => {
(0, utils_js_1.assertUsage)(key === 'match' || key === 'routeParams' || key === 'precedence', `The Route Function ${routeFunctionFilePath} returned an object with an unknown property ${picocolors_1.default.cyan(key)} (the known properties are ${picocolors_1.default.cyan('match')}, ${picocolors_1.default.cyan('routeParams')}, and ${picocolors_1.default.cyan('precedence')})`);
});
return {
precedence,
routeParams,
};
}
// TODO/v1-release: remove, and make routing synchronous (enabling Vike to synchronously check whether a link is a Vike link before even calling ev.preventDefault())
function assertSyncRouting(res, errPrefix) {
(0, utils_js_1.assertWarning)(!(0, utils_js_1.isPromise)(res), `${errPrefix} returned a promise, but asynchronous routing is deprecated and will be removed in the next major release, see https://vike.dev/route-function#async`, { onlyOnce: true });
}
// TODO/v1-release: remove
function warnDeprecatedAllowKey() {
const allowKey = picocolors_1.default.cyan('iKnowThePerformanceRisksOfAsyncRouteFunctions');
(0, utils_js_1.assertWarning)(false, `${allowKey} is deprecated and will be removed in the next major release`, { onlyOnce: true });
}
function assertRouteParams(result, errPrefix) {
(0, utils_js_1.assert)(errPrefix.endsWith(' should'));
if (!(0, utils_js_1.hasProp)(result, 'routeParams')) {
return;
}
(0, utils_js_1.assert)(errPrefix.endsWith(' should'));
(0, utils_js_1.assertUsage)((0, utils_js_1.hasProp)(result, 'routeParams', 'string{}'), `${errPrefix} be an object holding string values.`);
}