All files / apps/huella-legal/layers/legacy/server/middleware legacy.ts

100% Statements 16/16
90% Branches 9/10
100% Functions 2/2
100% Lines 13/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 291x   1x 6x 6x 6x       13x   13x 1x 1x     12x   7x   7x   6x            
const WORDPRESS_PATHS = /^\/(?:wp-content\/|wp-admin(?:\/|$)|wp-login\.php$|wp-json(?:\/|$))/;
 
const withoutTrailingSlashes = (url: string): string => {
    let end = url.length;
    while (url.endsWith("/", end)) end--;
    return url.slice(0, end);
};
 
export default defineEventHandler((event) => {
    const url = getRequestURL(event);
 
    if (url.pathname === "/" && url.searchParams.has("s")) {
        const query = new URLSearchParams({ query: url.searchParams.get("s") ?? "" });
        return sendRedirect(event, `/buscar/?${query}`, 301);
    }
 
    if (!WORDPRESS_PATHS.test(url.pathname)) return;
 
    const { baseURL } = useRuntimeConfig(event).content.vendor;
 
    if (new URL(baseURL).host === url.host) return;
 
    return sendRedirect(
        event,
        `${withoutTrailingSlashes(baseURL)}${url.pathname}${url.search}`,
        301,
    );
});