deno.land / x / duck_web_framework@0.1.1 / utils / URL.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import { QueryOrParams } from "../request.ts";
/** * Removes slash repetitions from the url. Ex. //some/path///string// -> /some/path/string/ * @param {string} url the url * @returns {string} url withour repetitions */export function removeSlashDoubles(url: string): string { return url.replaceAll(/\/{2,}/ig, "/");}
/** * Removes trailing slash from url, ex. /some/path/ -> /some/path * @param {string} url the url * @returns {string} url without trailing slash */export function removeTrailingSlash(url: string): string { if (url.length > 1 && url.endsWith("/")) return url.slice(0, -1); return url;}
/** * Applies {@link removeSlashDoubles} and {@link removeTrailingSlash} on the url * @param url the url * @returns {string} modified url */export function sanitize(url: string): string { return removeTrailingSlash(removeSlashDoubles(url)).toLowerCase();}
/** * Checks whether request url matches url from router (model url) if it has params * @param {string} requestUrl * @param {string} modelUrl * @returns {boolean} */export function matchWithParams(requestUrl: string, modelUrl: string): boolean { const modelParts = modelUrl.split("/"); const requestParts = requestUrl.split("/"); if (modelParts.length !== requestParts.length) return false;
for (let i = 0; i < modelParts.length; i++) { if (modelParts[i].startsWith(":")) continue;
if (modelParts[i] !== requestParts[i]) return false; } return true;}
/** * Checks whether model url contains params * @param url * @returns {boolean} */export function hasParams(url: string): boolean { return /:/.test(url);}
/** * Extracts parameters from given requestUrl based on modelUrl * @param requestUrl * @param modelUrl * @returns {QueryOrParams} */export function extractParams( requestUrl: string, modelUrl: string,): QueryOrParams { let params: QueryOrParams = {}; if (hasParams(modelUrl) && matchWithParams(requestUrl, modelUrl)) { const modelParts = modelUrl.split("/"); const requestParts = requestUrl.split("?")[0].split("/"); for (let i = 0; i < modelParts.length; i++) { if (modelParts[i].startsWith(":")) { const name = modelParts[i].slice(1); const value = requestParts[i]; params[name] = value; } } } return params;}
/** * Checks whether request url matches url from router (model url) * @param requestUrl the request url * @param modelUrl the model url * @returns {boolean} */export function matchPath(requestUrl: string, modelUrl: string): boolean { /* For now, only check if equal TODO: - match with parameters like /user/:id */ modelUrl = sanitize(modelUrl); const noQueryString = sanitize(requestUrl.split("?")[0]);
if (hasParams(modelUrl)) return matchWithParams(noQueryString, modelUrl);
// only middleware can have * in path so we don't have to look for params if (modelUrl.endsWith("*")) return noQueryString.startsWith(modelUrl.slice(0, -2));
return noQueryString === modelUrl;}
duck_web_framework

Version Info

Tagged at
3 years ago

External Dependencies

1 external dependency