|
1 | 1 | // Copyright 2022-latest the httpland authors. All rights reserved. MIT license. |
2 | 2 | // This module is browser compatible. |
3 | 3 |
|
4 | | -import { |
5 | | - concatPath, |
6 | | - head, |
7 | | - isIterable, |
8 | | - isOk, |
9 | | - partition, |
10 | | - prop, |
11 | | - Result, |
12 | | - unsafe, |
13 | | -} from "./deps.ts"; |
14 | | -import { |
15 | | - PathnameRoutes, |
16 | | - URLPatternRoute, |
17 | | - URLRouteHandler, |
18 | | - URLRoutes, |
19 | | -} from "./types.ts"; |
| 4 | +import { concatPath } from "./deps.ts"; |
| 5 | +import { PathnameRoutes } from "./types.ts"; |
20 | 6 |
|
21 | 7 | /** Nested URL pathname convertor. |
22 | 8 | * It provides a hierarchy of routing tables. |
@@ -46,146 +32,3 @@ export function nest( |
46 | 32 | return { ...acc, [concatPath(root, path)]: handler }; |
47 | 33 | }, {} as PathnameRoutes); |
48 | 34 | } |
49 | | - |
50 | | -/** Returns all elements in the given value that produce a intersect value using the given selector. */ |
51 | | -export function intersectBy<T>( |
52 | | - value: Iterable<T>, |
53 | | - selector: (current: T, prev: T) => boolean, |
54 | | -): T[] { |
55 | | - const selectedValues: T[] = []; |
56 | | - const ret: T[] = []; |
57 | | - |
58 | | - for (const element of value) { |
59 | | - const has = selectedValues.some((v) => selector(element, v)); |
60 | | - |
61 | | - if (has) { |
62 | | - if (!ret.find((v) => selector(element, v))) { |
63 | | - ret.push(element); |
64 | | - } |
65 | | - } else { |
66 | | - selectedValues.push(element); |
67 | | - } |
68 | | - } |
69 | | - |
70 | | - return ret; |
71 | | -} |
72 | | - |
73 | | -/** Check `URLPattern` object equality. */ |
74 | | -export function equalsURLPattern(left: URLPattern, right: URLPattern): boolean { |
75 | | - const props: readonly (keyof URLPattern)[] = [ |
76 | | - "exec", |
77 | | - "hash", |
78 | | - "hostname", |
79 | | - "password", |
80 | | - "pathname", |
81 | | - "port", |
82 | | - "protocol", |
83 | | - "search", |
84 | | - "test", |
85 | | - "username", |
86 | | - ] as const; |
87 | | - |
88 | | - return props.every((prop) => equalsProp(prop, left, right)); |
89 | | -} |
90 | | - |
91 | | -function equalsProp<T extends PropertyKey, U extends { [k in T]: unknown }>( |
92 | | - prop: T, |
93 | | - left: U, |
94 | | - right: U, |
95 | | -): boolean { |
96 | | - return left[prop] === right[prop]; |
97 | | -} |
98 | | - |
99 | | -/** Validate {@link URLRoutes}. |
100 | | - * |
101 | | - * ```ts |
102 | | - * import { |
103 | | - * URLRouter, |
104 | | - * URLRoutes, |
105 | | - * validateURLRoutes, |
106 | | - * } from "https://deno.land/x/http_router@$VERSION/mod.ts"; |
107 | | - * |
108 | | - * const routes: URLRoutes = { |
109 | | - * "?": () => new Response(), |
110 | | - * }; |
111 | | - * const result = validateURLRoutes(routes); |
112 | | - * |
113 | | - * if (result !== true) { |
114 | | - * // do something |
115 | | - * } |
116 | | - * |
117 | | - * const handler = URLRouter(routes); |
118 | | - * ``` |
119 | | - */ |
120 | | -export function validateURLRoutes( |
121 | | - routes: URLRoutes, |
122 | | -): true | AggregateError | TypeError { |
123 | | - const iterable = urlPatternRouteFrom(routes); |
124 | | - const entries = Array.from(iterable).map(route2URLPatternRoute); |
125 | | - const [okResults, errorResults] = partition(entries, isOk); |
126 | | - |
127 | | - if (errorResults.length) { |
128 | | - const errors = errorResults.map(prop("value")); |
129 | | - |
130 | | - return AggregateError(errors, "Invalid URL pattern."); |
131 | | - } |
132 | | - |
133 | | - const urlPatterns = okResults.map(prop("value")).map<URLPattern>(head); |
134 | | - const intersections = intersectBy(urlPatterns, equalsURLPattern); |
135 | | - |
136 | | - if (intersections.length) { |
137 | | - return new TypeError( |
138 | | - `Duplicate same meaning routes. ${inspect(intersections)}`, |
139 | | - ); |
140 | | - } |
141 | | - |
142 | | - return true; |
143 | | -} |
144 | | - |
145 | | -export function urlPatternRouteFrom( |
146 | | - routes: URLRoutes, |
147 | | -): Iterable<URLPatternRoute> { |
148 | | - return isIterable(routes) |
149 | | - ? routes |
150 | | - : Object.entries(routes).map(([pathname, handler]) => |
151 | | - [{ pathname }, handler] as const |
152 | | - ); |
153 | | -} |
154 | | - |
155 | | -export function route2URLPatternRoute( |
156 | | - route: URLPatternRoute, |
157 | | -): Result<[URLPattern, URLRouteHandler], TypeError> { |
158 | | - return unsafe( |
159 | | - () => [new URLPattern(route[0]), route[1]], |
160 | | - ); |
161 | | -} |
162 | | - |
163 | | -export function inspectURLPattern(value: URLPattern): string { |
164 | | - const { |
165 | | - hash, |
166 | | - hostname, |
167 | | - protocol, |
168 | | - username, |
169 | | - password, |
170 | | - port, |
171 | | - pathname, |
172 | | - search, |
173 | | - } = value; |
174 | | - |
175 | | - return `URLPattern { |
176 | | - protocol: "${protocol}", |
177 | | - username: "${username}", |
178 | | - password: "${password}", |
179 | | - hostname: "${hostname}", |
180 | | - port: "${port}", |
181 | | - pathname: "${pathname}", |
182 | | - search: "${search}", |
183 | | - hash: "${hash}" |
184 | | -}`; |
185 | | -} |
186 | | - |
187 | | -export function inspect(value: URLPattern[]): string { |
188 | | - return `[ |
189 | | - ${value.map(inspectURLPattern).join(", \n ")} |
190 | | -]`; |
191 | | -} |
0 commit comments