Skip to content

Commit 4ebbb75

Browse files
committed
feat(utils): remove validateURLRoutes
no export `validateURLRoutes` from API BREAKING CHANGE: no export `validateURLRoutes` close #29
1 parent 1870d07 commit 4ebbb75

6 files changed

Lines changed: 27 additions & 349 deletions

File tree

README.md

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -135,45 +135,6 @@ URL patterns can be defined using the
135135
- RegExp groups (`/books/(\\d+)`) which make arbitrarily complex regex matches
136136
with a few limitations.
137137

138-
### Check routes validity
139-
140-
The router **never throws** an error. If the route is invalid, it will be
141-
eliminated just.
142-
143-
To make sure that URLRoutes are valid in advance, you can use the validate
144-
function.
145-
146-
For example, `?` as pathname is an invalid pattern.
147-
148-
```ts
149-
import {
150-
URLRouter,
151-
URLRoutes,
152-
validateURLRoutes,
153-
} from "https://deno.land/x/http_router@$VERSION/mod.ts";
154-
155-
const routes: URLRoutes = {
156-
"?": () => new Response(),
157-
};
158-
const result = validateURLRoutes(routes);
159-
160-
if (result !== true) {
161-
// do something
162-
}
163-
164-
const handler = URLRouter(routes);
165-
```
166-
167-
The validate function returns `true` in case of success, or an object
168-
representing the contents of the `Error` in case of failure.
169-
170-
Invalid route means the following:
171-
172-
- Invalid `URLPattern`
173-
- Duplicate `URLPattern`
174-
175-
You are completely free to do this or not.
176-
177138
### Nested route pathname
178139

179140
`nest` is nested URL pathname convertor. It provides a hierarchy of routing

deps.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,5 @@ export {
2020
unsafe,
2121
} from "https://deno.land/x/result_js@1.0.0/mod.ts";
2222
export { concatPath } from "https://deno.land/x/url_concat@1.0.0-beta.1/mod.ts";
23-
export { head, prop } from "https://deno.land/x/prelude_js@1.0.0-beta.3/mod.ts";
23+
export { prop } from "https://deno.land/x/prelude_js@1.0.0-beta.3/mod.ts";
2424
export { LRUMap } from "https://deno.land/x/lru_map@1.0.0-beta.1/mod.ts";

mod.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@ export {
1919
type URLRouterConstructor,
2020
type URLRoutes,
2121
} from "./types.ts";
22-
export { nest, validateURLRoutes } from "./utils.ts";
22+
export { nest } from "./utils.ts";

routers.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
HttpMethodRoutes,
66
MethodRouterConstructor,
77
RouterOptions,
8+
URLPatternRoute,
89
URLRouteHandler,
910
URLRouteHandlerContext,
1011
URLRouterConstructor,
@@ -13,15 +14,17 @@ import {
1314
import {
1415
Handler,
1516
HttpMethod,
17+
isIterable,
1618
isOk,
1719
isResponse,
1820
LRUMap,
1921
partition,
2022
prop,
23+
Result,
2124
Status,
2225
STATUS_TEXT,
26+
unsafe,
2327
} from "./deps.ts";
24-
import { route2URLPatternRoute, urlPatternRouteFrom } from "./utils.ts";
2528

2629
interface MatchedCache {
2730
readonly handler: URLRouteHandler;
@@ -199,3 +202,21 @@ function handleNotFound(): Response {
199202
statusText: STATUS_TEXT[status],
200203
});
201204
}
205+
206+
function urlPatternRouteFrom(
207+
routes: URLRoutes,
208+
): Iterable<URLPatternRoute> {
209+
return isIterable(routes)
210+
? routes
211+
: Object.entries(routes).map(([pathname, handler]) =>
212+
[{ pathname }, handler] as const
213+
);
214+
}
215+
216+
function route2URLPatternRoute(
217+
route: URLPatternRoute,
218+
): Result<[URLPattern, URLRouteHandler], TypeError> {
219+
return unsafe(
220+
() => [new URLPattern(route[0]), route[1]],
221+
);
222+
}

utils.ts

Lines changed: 2 additions & 159 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,8 @@
11
// Copyright 2022-latest the httpland authors. All rights reserved. MIT license.
22
// This module is browser compatible.
33

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";
206

217
/** Nested URL pathname convertor.
228
* It provides a hierarchy of routing tables.
@@ -46,146 +32,3 @@ export function nest(
4632
return { ...acc, [concatPath(root, path)]: handler };
4733
}, {} as PathnameRoutes);
4834
}
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

Comments
 (0)