-
Notifications
You must be signed in to change notification settings - Fork 0
/
route.ts
38 lines (31 loc) · 942 Bytes
/
route.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import type { Option } from "fun/option";
import type { Context } from "./context.ts";
import type { Handler } from "./handler.ts";
import type { RouteParser, RouteString } from "./parser.ts";
import { some } from "fun/option";
export type Route<V, S> = {
readonly route: RouteString;
readonly parser: RouteParser<V>;
readonly handler: Handler<Context<S, V>, Response, unknown>;
};
// deno-lint-ignore no-explicit-any
export type AnyRoute<S> = Route<any, S>;
export function route<V, S>(
route: RouteString,
parser: RouteParser<V>,
handler: Handler<Context<S, V>, Response, unknown>,
): Route<V, S> {
return { route, parser, handler };
}
const emptyPath = some({}) as Option<unknown>;
const NotFound = new Response("Not Found", { status: 404 });
/**
* Default 404 Route
*/
export function notFound<S>(): AnyRoute<S> {
return route(
"GET /*",
() => emptyPath,
(s) => Promise.resolve([NotFound, s]),
);
}