0.1.6Updated a month ago
import type { ResolvedFreshConfig, FreshContext   } from "$fresh/server.ts";

const NOOP_COMPONENT = () => null as any;
const NOOP_NEXT = () => Promise.resolve(new Response(null, { status: 500 }));

export const create_fresh_context = (
  config: ResolvedFreshConfig,
  req: Request,
  connInfo: Deno.ServeHandlerInfo<Deno.NetAddr>
): FreshContext => {
  const url = new URL(req.url);

  url.pathname = url.pathname.replaceAll(/\/+/g, "/");

  const ctx: FreshContext = {
    url,
    params: {},
    config,
    basePath: import.meta.dirname!,
    localAddr: connInfo.remoteAddr,
    remoteAddr: connInfo.remoteAddr,
    state: {},
    isPartial: false,
    destination: "route",
    error: undefined,
    codeFrame: undefined,
    Component: NOOP_COMPONENT,
    next: NOOP_NEXT,
    render: NOOP_NEXT,
    renderNotFound: async (data) => {
      ctx.data = data;
      return NOOP_NEXT();
    },
    route: "",
    get pattern() {
      return ctx.route;
    },
    data: undefined,
  };

  return ctx;
};