-
-
Notifications
You must be signed in to change notification settings - Fork 47
/
get-action.ts
48 lines (39 loc) · 1.33 KB
/
get-action.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
import { Content } from '../renderer/content.ts';
import { MetaRoute } from '../models/meta-route.ts';
import { getPathNameFromUrl, getRouteFromFullPath, getRouteFromRegex, getRouteParams } from './route.utils.ts';
export function getAction(routes: MetaRoute[], method: string, url: string): MetaRoute | null {
const pathname: string = getPathNameFromUrl(url);
const routeParams: { [key: string]: any } = {};
let route = getRouteFromFullPath(routes, pathname, method);
if (!route) {
route = getRouteFromRegex(routes, pathname, method);
// gets route params from route
if (route) {
const params = getRouteParams(route.route);
const routeMatch = pathname.split('/');
params.forEach((p) => {
routeParams[p.el] = routeMatch[p.i];
});
}
}
if (route) {
return {
target: route.target,
action: route.action,
params: route.params,
routeParams,
} as MetaRoute;
}
return null;
}
export function notFoundAction() {
return Content('Not found', 404); // TODO: enum http status
}
export function optionsAllowedAction() {
const headers = new Headers();
headers.set('Allow', '*');
return {
status: 200, // TODO: enum http status
headers,
};
}