|
| 1 | +import { Parser } from './Parser' |
| 2 | + |
| 3 | +export class Route { |
| 4 | + /** |
| 5 | + * Get the query string in form data format |
| 6 | + * |
| 7 | + * @param route The route to get the query string |
| 8 | + * @return The string with query string |
| 9 | + */ |
| 10 | + getQueryString(route: string): string { |
| 11 | + const queryIndex = route.search(/\?(.*)/) |
| 12 | + |
| 13 | + if (queryIndex === -1) return route |
| 14 | + |
| 15 | + return route.substring(queryIndex) |
| 16 | + } |
| 17 | + |
| 18 | + /** |
| 19 | + * Remove query params from the route |
| 20 | + * |
| 21 | + * @param route The route to remove the queryParams |
| 22 | + * @return The route without the query params |
| 23 | + */ |
| 24 | + removeQueryParams(route: string): string { |
| 25 | + if (this.getQueryString(route) === route) return route |
| 26 | + |
| 27 | + return route.replace(this.getQueryString(route), '') |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * Get object with ?&queryParams values from route |
| 32 | + * |
| 33 | + * @param route The route to get the queryParams |
| 34 | + * @return The object of queryParams found inside route |
| 35 | + */ |
| 36 | + getQueryParamsValue(route: string): any { |
| 37 | + return new Parser().formDataToJson(this.getQueryString(route)) |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Get array with ?&queryParams name from route |
| 42 | + * |
| 43 | + * @param route The route to get the queryParams |
| 44 | + * @return The array name of queryParams found inside route |
| 45 | + */ |
| 46 | + getQueryParamsName(route: string): string[] { |
| 47 | + const queryNames = [] |
| 48 | + let queryString = this.getQueryString(route) |
| 49 | + |
| 50 | + if (queryString.startsWith('?')) queryString = queryString.replace('?', '') |
| 51 | + |
| 52 | + queryString |
| 53 | + .split('&') |
| 54 | + .forEach(queries => |
| 55 | + queryNames.push(decodeURIComponent(queries.split('=')[0])), |
| 56 | + ) |
| 57 | + |
| 58 | + return queryNames |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Get object with :params values from route |
| 63 | + * |
| 64 | + * @param route The route to get the params |
| 65 | + * @return The object of params found inside route |
| 66 | + */ |
| 67 | + getParamsValue(routeWithParams: string, routeWithValues: string): any { |
| 68 | + routeWithParams = this.removeQueryParams(routeWithParams) |
| 69 | + routeWithValues = this.removeQueryParams(routeWithValues) |
| 70 | + |
| 71 | + const params = {} |
| 72 | + |
| 73 | + const routeWithParamsArray = routeWithParams.split('/') |
| 74 | + const routeWithValuesArray = routeWithValues.split('/') |
| 75 | + |
| 76 | + if (routeWithParamsArray.length !== routeWithValuesArray.length) { |
| 77 | + throw new Error('DIFFERENT_ROUTES') |
| 78 | + } |
| 79 | + |
| 80 | + routeWithParamsArray.forEach((param, i) => { |
| 81 | + if (!param.startsWith(':')) return |
| 82 | + |
| 83 | + params[decodeURIComponent(param.replace(':', ''))] = decodeURIComponent( |
| 84 | + routeWithValuesArray[i], |
| 85 | + ) |
| 86 | + }) |
| 87 | + |
| 88 | + return params |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Get array with :params name from route |
| 93 | + * |
| 94 | + * @param route The route to get the params |
| 95 | + * @return The array name of params found inside route |
| 96 | + */ |
| 97 | + getParamsName(route: string): string[] { |
| 98 | + route = this.removeQueryParams(route) |
| 99 | + |
| 100 | + const replaceDots = (value: string): string => |
| 101 | + decodeURIComponent(value.replace(':', '')) |
| 102 | + |
| 103 | + return route.split('/').reduce((results, r) => { |
| 104 | + if (r.match(':')) { |
| 105 | + results.push( |
| 106 | + r.includes('|') ? replaceDots(r.split('|')[0]) : replaceDots(r), |
| 107 | + ) |
| 108 | + } |
| 109 | + |
| 110 | + return results |
| 111 | + }, []) |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Create a matcher RegExp for any route |
| 116 | + * |
| 117 | + * @param route The route to create the matcher |
| 118 | + * @return The matcher RegExp based on route |
| 119 | + */ |
| 120 | + createMatcher(route: string): RegExp { |
| 121 | + route = this.removeQueryParams(route) |
| 122 | + |
| 123 | + const routeArray = route.split('/') |
| 124 | + |
| 125 | + routeArray.forEach((r, i) => { |
| 126 | + if (r === '') return |
| 127 | + if (r.startsWith(':')) { |
| 128 | + // Match with any value RegExp |
| 129 | + routeArray[i] = `(?:\\/[\\w-]+)` |
| 130 | + |
| 131 | + return |
| 132 | + } |
| 133 | + |
| 134 | + // Match only with value of ${r} RegExp |
| 135 | + routeArray[i] = `(?:\\/${r}\\b)` |
| 136 | + }) |
| 137 | + |
| 138 | + route = routeArray.join('') |
| 139 | + |
| 140 | + return new RegExp(`^${route}$`) |
| 141 | + } |
| 142 | +} |
0 commit comments