winterspec
Version:
Write Winter-CG compatible routes with filesystem routing and tons of features
19 lines (18 loc) • 802 B
JavaScript
import { getRouteMatcher } from "next-route-matcher";
import { normalizeRouteMap } from "../lib/normalize-route-map.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) => makeRequestAgainstWinterSpec(winterSpec)(req),
...winterSpecOptions,
};
return winterSpec;
};