import { createContext } from "effection";
import { match } from "path-to-regexp";
import { concat } from "./middleware.js";
const ParamsContext = createContext("revolution.params");
export function* useParams() {
    return (yield* ParamsContext.expect());
}
function buildMethodRoute(method, handler) {
    return function* (request, next) {
        if (request.method === method) {
            return yield* handler(request, next);
        }
        else {
            return yield* next(request);
        }
    };
}
export function GET(handler) {
    return buildMethodRoute("GET", handler);
}
export function POST(handler) {
    return buildMethodRoute("POST", handler);
}
export function PUT(handler) {
    return buildMethodRoute("PUT", handler);
}
export function PATCH(handler) {
    return buildMethodRoute("PATCH", handler);
}
export function DELETE(handler) {
    return buildMethodRoute("DELETE", handler);
}
export function route(path, ...middlewares) {
    const inlinedMiddleware = concat(...middlewares);
    return function* (request, next) {
        let pathname = new URL(request.url).pathname;
        let result = match(path)(pathname);
        if (result) {
            yield* ParamsContext.set(result.params);
            return yield* inlinedMiddleware(request, next);
        }
        else {
            return yield* next(request);
        }
    };
}
//# sourceMappingURL=route.js.map