UNPKG

midwinter

Version:

A next-gen middleware engine built for the WinterCG environments.

124 lines (118 loc) 3.6 kB
import './chunk-BEENSAMV.mjs'; import { Midwinter } from './chunk-WXZQARCU.mjs'; import { createRouter as createRouter$1, toRouteMatcher } from 'radix3'; // src/plugins/routing/routers/util.ts var WILDCARD_METHOD_KEY = "*"; // src/plugins/routing/routers/radix.ts var createRouter = (initialRoutes) => { const routes = {}; for (const route of initialRoutes) { add(route); } const router = createRouter$1({ routes }); const matcher = toRouteMatcher(router); function add(route) { routes[route.path] ??= {}; for (const _method of route.methods) { const method = _method.toUpperCase(); routes[route.path][method] ??= []; routes[route.path][method].push(route.payload); } } function match(request) { const path = new URL(request.url).pathname; const method = request.method; const match2 = router.lookup(path); if (!match2) return; const { params: _, ...methods } = match2; if (!methods) return; const handlers = methods[method] ?? methods[WILDCARD_METHOD_KEY]; return handlers?.[0]; } function matchAll(request) { const path = new URL(request.url).pathname; const method = request.method; const matches = matcher.matchAll(path); const result = []; for (const match2 of matches) { const { params: _, ...methods } = match2; if (!methods) continue; const specificHandlers = methods[method] ?? []; const wildcardHandlers = methods[WILDCARD_METHOD_KEY] ?? []; result.push(...specificHandlers, ...wildcardHandlers); } return result; } return { match, matchAll }; }; // src/plugins/routing/util.ts var parsePathParams = (path) => { const parts = path.split("/"); const params = []; for (const part of parts) { if (part.startsWith(":")) { params.push(part.replace(/^:/, "")); } } return params; }; // src/plugins/routing/index.ts var init = (opts = {}) => { const { router = createRouter } = opts; const route = (config) => { if (config.path == null) { return new Midwinter(config); } return new Midwinter({ ...config, params: parsePathParams(config.path) }); }; return { route, prefixed(prefix) { return (config) => route({ ...config, path: `${prefix}${config.path ?? ""}` }); }, createRouter(routes, opts2 = {}) { const { onNotFound = () => { return Response.json({ code: "NOT_FOUND" }, { status: 404 }); }, onError = () => { return Response.json({ code: "SERVER_EXCEPTION" }, { status: 500 }); }, keepTrailingSlashes = false } = opts2; const _routes = (Array.isArray(routes) ? routes : Object.values(routes)).map( (route2) => { const { method, path } = route2.meta ?? {}; const _path = String(path); const _method = method == null ? WILDCARD_METHOD_KEY : method; return { methods: Array.isArray(_method) ? _method : [String(_method)], path: keepTrailingSlashes ? _path : _path.replace(/\/+$/, ""), payload: route2 }; } ); const _router = router(_routes); return async (request) => { try { const handler = _router.match(request); if (handler) { const response = await handler(request); if (response) return response; } return onNotFound(request); } catch (e) { return onError(e); } }; } }; }; var RadixRouter = createRouter; export { RadixRouter, init };