24 lines
820 B
JavaScript
24 lines
820 B
JavaScript
import { useRuntimeConfig } from "#imports";
|
|
import { defu } from "defu";
|
|
import { createRouter as createRadixRouter, toRouteMatcher } from "radix3";
|
|
import { withoutBase, withoutTrailingSlash } from "ufo";
|
|
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]) => [withoutTrailingSlash(path), rules])
|
|
)
|
|
})
|
|
);
|
|
return (path) => {
|
|
return defu({}, ..._routeRulesMatcher.matchAll(
|
|
// radix3 does not support trailing slashes
|
|
withoutBase(withoutTrailingSlash(withoutQuery(path)), app.baseURL)
|
|
).reverse());
|
|
};
|
|
}
|