-
Notifications
You must be signed in to change notification settings - Fork 17
/
handler.ts
67 lines (53 loc) · 1.57 KB
/
handler.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { Response } from "./response.ts";
import { Params } from "./params.ts";
export enum Method {
GET = "GET",
POST = "POST",
PUT = "PUT",
PATCH = "PATCH",
DELETE = "DELETE",
OPTIONS = "OPTIONS",
LINK = "LINK",
UNLINK = "UNLINK",
}
export type Context = {
readonly path: string;
readonly method: Method;
params: Params;
};
export type Handler = BasicHandler | AsyncHandler;
export interface BasicHandler {
(ctx: Context): Response;
}
export interface AsyncHandler {
(ctx: Context): Promise<Response>;
}
export type HandlerConfig = {
path: string;
method: Method;
handler: Handler;
};
export function get(path: string, handler: Handler): HandlerConfig {
return { path, method: Method.GET, handler };
}
export function post(path: string, handler: Handler): HandlerConfig {
return { path, method: Method.POST, handler };
}
export function put(path: string, handler: Handler): HandlerConfig {
return { path, method: Method.PUT, handler };
}
export function patch(path: string, handler: Handler): HandlerConfig {
return { path, method: Method.PATCH, handler };
}
export function del(path: string, handler: Handler): HandlerConfig {
return { path, method: Method.DELETE, handler };
}
export function options(path: string, handler: Handler): HandlerConfig {
return { path, method: Method.OPTIONS, handler };
}
export function link(path: string, handler: Handler): HandlerConfig {
return { path, method: Method.LINK, handler };
}
export function unlink(path: string, handler: Handler): HandlerConfig {
return { path, method: Method.UNLINK, handler };
}