-
-
Notifications
You must be signed in to change notification settings - Fork 47
/
get-action.ts
68 lines (56 loc) · 1.59 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import { RouteMetadata } from "../metadata/route.ts";
import {
getPathNameFromUrl,
getRouteFromFullPath,
getRouteParams,
getRouteWithRegex,
getRouteWithRouteParams,
} from "./route.utils.ts";
// TODO
// Add 3 Map route for search:
// - full pathes
// - with route params (example: 'api/:param')
// - regex routes
const ActionsMemoCache: Map<string, RouteMetadata> = new Map();
// Find action from routes
export function getAction(
routes: RouteMetadata[],
method: string,
url: string,
): RouteMetadata | null {
if (ActionsMemoCache.has(method + "-" + url)) {
return ActionsMemoCache.get(method + "-" + url)!;
}
const pathname: string = getPathNameFromUrl(url);
const routeParams: { [key: string]: any } = {};
let route = getRouteFromFullPath(routes, pathname, method);
if (!route) {
route = getRouteWithRegex(routes, pathname, method);
}
if (!route) {
route = getRouteWithRouteParams(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) {
const obj = {
areaObject: route.areaObject,
controllerObject: route.controllerObject,
actionObject: route.actionObject,
target: route.target,
action: route.action,
actionMetadata: route.actionMetadata,
params: route.params,
routeParams,
} as RouteMetadata;
ActionsMemoCache.set(method + "-" + url, obj);
return obj;
}
return null;
}