-
Notifications
You must be signed in to change notification settings - Fork 1
/
types.ts
76 lines (64 loc) · 1.51 KB
/
types.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
68
69
70
71
72
73
74
75
76
import {
ServerRequest,
Response,
} from "https://deno.land/std@v0.51.0/http/server.ts";
export type Options = {
readonly port: number;
};
export enum Method {
get = "GET",
post = "POST",
put = "PUT",
delete = "DELETE",
option = "OPTION",
}
export type Route = {
readonly method: Method;
readonly path: string;
readonly validation: Array<Validator>;
readonly controller: Controller;
};
export type Validator = {};
export type Middleware = {};
interface Controller {
response(req: MicroraptorRequest): void;
}
export class MicroraptorRequest {
readonly request: ServerRequest;
route: Route;
query: any = {};
param: any = {};
cookie: any = {};
constructor(
req: ServerRequest,
route: Route,
) {
this.request = req;
this.route = route;
this.query = this.getQuery();
this.param = this.getParam();
this.cookie = this.getCookie();
}
private getQuery(): any {
return {};
}
private getParam(): any {
if (this.route.path.indexOf(":") !== -1) {
const regex = new RegExp(
this.route.path.replace(/:([a-zA-Z0-9_]*)/g, "([^\/]*)"),
);
const values: Array<string> = (regex.exec(this.request.url) ?? []).splice(
1,
);
const keys: Array<string> = (regex.exec(this.route.path) ?? []).splice(
1,
).map((item) => item.replace(":", ""));
const data = keys.reduce((o, k, i) => ({ ...o, [k]: values[i] }), {});
return data;
}
return {};
}
private getCookie(): any {
return {};
}
}