winterspec
Version:
Write Winter-CG compatible routes with filesystem routing and tons of features
27 lines (26 loc) • 1.31 kB
JavaScript
import { createServer } from "node:http";
import { getRouteMatcher } from "next-route-matcher";
import { normalizeRouteMap } from "../lib/normalize-route-map.js";
import { transformToNodeBuilder, } from "../edge/transform-to-node.js";
import { makeRequestAgainstWinterSpec, } from "../types/winter-spec.js";
export const createWinterSpecFromRouteMap = (routeMap, winterSpecOptions) => {
const formattedRoutes = normalizeRouteMap(routeMap);
const routeMatcher = getRouteMatcher(Object.keys(formattedRoutes));
const routeMapWithHandlers = Object.fromEntries(Object.entries(formattedRoutes).map(([routeFormatted, route]) => [
routeFormatted,
routeMap[route],
]));
const winterSpec = {
routeMatcher,
routeMapWithHandlers,
makeRequest: async (req, opts) => makeRequestAgainstWinterSpec(winterSpec, opts)(req),
...winterSpecOptions,
};
return winterSpec;
};
export const createNodeServerFromRouteMap = async (routeMap, transformToNodeOptions, winterSpecOptions) => {
const winterSpec = createWinterSpecFromRouteMap(routeMap, winterSpecOptions);
const transformToNode = transformToNodeBuilder(transformToNodeOptions);
const server = createServer(transformToNode(makeRequestAgainstWinterSpec(winterSpec)));
return server;
};