feat: init
This commit is contained in:
67
node_modules/nitropack/dist/runtime/internal/route-rules.mjs
generated
vendored
Normal file
67
node_modules/nitropack/dist/runtime/internal/route-rules.mjs
generated
vendored
Normal file
@@ -0,0 +1,67 @@
|
||||
import defu from "defu";
|
||||
import {
|
||||
eventHandler,
|
||||
proxyRequest,
|
||||
sendRedirect,
|
||||
setHeaders
|
||||
} from "h3";
|
||||
import { createRouter as createRadixRouter, toRouteMatcher } from "radix3";
|
||||
import { getQuery, joinURL, withQuery, withoutBase } from "ufo";
|
||||
import { useRuntimeConfig } from "./config.mjs";
|
||||
const config = useRuntimeConfig();
|
||||
const _routeRulesMatcher = toRouteMatcher(
|
||||
createRadixRouter({ routes: config.nitro.routeRules })
|
||||
);
|
||||
export function createRouteRulesHandler(ctx) {
|
||||
return eventHandler((event) => {
|
||||
const routeRules = getRouteRules(event);
|
||||
if (routeRules.headers) {
|
||||
setHeaders(event, routeRules.headers);
|
||||
}
|
||||
if (routeRules.redirect) {
|
||||
let target = routeRules.redirect.to;
|
||||
if (target.endsWith("/**")) {
|
||||
let targetPath = event.path;
|
||||
const strpBase = routeRules.redirect._redirectStripBase;
|
||||
if (strpBase) {
|
||||
targetPath = withoutBase(targetPath, strpBase);
|
||||
}
|
||||
target = joinURL(target.slice(0, -3), targetPath);
|
||||
} else if (event.path.includes("?")) {
|
||||
const query = getQuery(event.path);
|
||||
target = withQuery(target, query);
|
||||
}
|
||||
return sendRedirect(event, target, routeRules.redirect.statusCode);
|
||||
}
|
||||
if (routeRules.proxy) {
|
||||
let target = routeRules.proxy.to;
|
||||
if (target.endsWith("/**")) {
|
||||
let targetPath = event.path;
|
||||
const strpBase = routeRules.proxy._proxyStripBase;
|
||||
if (strpBase) {
|
||||
targetPath = withoutBase(targetPath, strpBase);
|
||||
}
|
||||
target = joinURL(target.slice(0, -3), targetPath);
|
||||
} else if (event.path.includes("?")) {
|
||||
const query = getQuery(event.path);
|
||||
target = withQuery(target, query);
|
||||
}
|
||||
return proxyRequest(event, target, {
|
||||
fetch: ctx.localFetch,
|
||||
...routeRules.proxy
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
export function getRouteRules(event) {
|
||||
event.context._nitro = event.context._nitro || {};
|
||||
if (!event.context._nitro.routeRules) {
|
||||
event.context._nitro.routeRules = getRouteRulesForPath(
|
||||
withoutBase(event.path.split("?")[0], useRuntimeConfig().app.baseURL)
|
||||
);
|
||||
}
|
||||
return event.context._nitro.routeRules;
|
||||
}
|
||||
export function getRouteRulesForPath(path) {
|
||||
return defu({}, ..._routeRulesMatcher.matchAll(path).reverse());
|
||||
}
|
||||
Reference in New Issue
Block a user