All files / src/dev-server proxy.ts

92.85% Statements 13/14
62.5% Branches 5/8
100% Functions 3/3
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 29 30 31 32 332x 2x 2x       2x 11x 6x 6x 6x 6x 6x 6x                 2x               11x    
import fp from "fastify-plugin";
import proxyPlugin from "@fastify/http-proxy";
import { logProxy } from "./logger.js";
import type { ProxyItem } from "./config.js";
 
const createProxy =
  (server: any, silent: boolean, httpProxy?: string) =>
  ({ from, to, opts }: ProxyItem): void => {
    Iif (!silent) logProxy(from, to);
    const prefix = from.endsWith("/") ? from.slice(0, -1) : from;
    const url = new URL(to);
    const upstream = url.origin;
    const rewritePrefix = url.pathname;
    server.register(proxyPlugin, {
      prefix,
      upstream,
      rewritePrefix,
      undici: httpProxy ? { proxy: httpProxy } : false,
      ...opts,
    });
  };
 
export default fp(
  async (
    server: any,
    {
      proxy = [],
      silent = false,
      httpProxy,
    }: { proxy?: ProxyItem[]; silent?: boolean; httpProxy?: string },
  ) => proxy.forEach(createProxy(server, silent, httpProxy)),
);