26 lines
1.0 KiB
JavaScript
26 lines
1.0 KiB
JavaScript
import { createRouter as createRadixRouter, toRouteMatcher } from "radix3";
|
|
import { defu } from "defu";
|
|
import { parseURL, withoutBase, withoutTrailingSlash } from "ufo";
|
|
import { useRuntimeConfig } from "#imports";
|
|
export function withoutQuery(path) {
|
|
return path.split("?")[0];
|
|
}
|
|
export function createNitroRouteRuleMatcher() {
|
|
const { nitro, app } = useRuntimeConfig();
|
|
const _routeRulesMatcher = toRouteMatcher(
|
|
createRadixRouter({
|
|
routes: Object.fromEntries(
|
|
Object.entries(nitro?.routeRules || {}).map(([path, rules]) => [path === "/" ? path : withoutTrailingSlash(path), rules])
|
|
)
|
|
})
|
|
);
|
|
return (pathOrUrl) => {
|
|
const path = pathOrUrl[0] === "/" ? pathOrUrl : parseURL(pathOrUrl, app.baseURL).pathname;
|
|
const pathWithoutQuery = withoutQuery(path);
|
|
return defu({}, ..._routeRulesMatcher.matchAll(
|
|
// radix3 does not support trailing slashes
|
|
withoutBase(pathWithoutQuery === "/" ? pathWithoutQuery : withoutTrailingSlash(pathWithoutQuery), app.baseURL)
|
|
).reverse());
|
|
};
|
|
}
|