@cloudflare/util-routes 5.3.0 → 5.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +6 -0
- package/dist/index.d.ts +1 -0
- package/dist/route.d.ts +7 -5
- package/dist/useMatchRoute.d.ts +43 -0
- package/dist/useRoute.d.ts +2 -4
- package/es/index.js +2 -1
- package/es/useMatchRoute.js +6 -0
- package/es/useRoute.js +3 -2
- package/lib/index.js +13 -0
- package/lib/useMatchRoute.js +13 -0
- package/lib/useRoute.js +4 -2
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
package/dist/index.d.ts
CHANGED
package/dist/route.d.ts
CHANGED
|
@@ -5,11 +5,7 @@ export declare class RoutePatternResult<Param extends string> {
|
|
|
5
5
|
/**
|
|
6
6
|
* Interperolate arguments into the route pattern
|
|
7
7
|
*/
|
|
8
|
-
readonly toUrl: (args:
|
|
9
|
-
[K in RequiredParams<Param>]: string;
|
|
10
|
-
} & {
|
|
11
|
-
[K in OptionalParams<Param>]?: string;
|
|
12
|
-
}, options?: pathToRegExp.PathFunctionOptions) => string;
|
|
8
|
+
readonly toUrl: (args: RouteParams<Param>, options?: pathToRegExp.PathFunctionOptions) => string;
|
|
13
9
|
readonly keys: Key[];
|
|
14
10
|
readonly expression: RegExp;
|
|
15
11
|
constructor(pattern: string);
|
|
@@ -30,3 +26,9 @@ export declare type ExtractRouteParamsInterface<T extends string | RoutePatternR
|
|
|
30
26
|
};
|
|
31
27
|
export declare type RequiredParams<Params extends string> = Params extends `${infer P}?` ? never : Params extends string ? Params : never;
|
|
32
28
|
export declare type OptionalParams<Params extends string> = Params extends `${infer P}?` ? P : never;
|
|
29
|
+
/** Maps route params to their resolved types: required params are `string`, optional params are `string | undefined`. */
|
|
30
|
+
export declare type RouteParams<Params extends string> = {
|
|
31
|
+
[K in RequiredParams<Params>]: string;
|
|
32
|
+
} & {
|
|
33
|
+
[K in OptionalParams<Params>]?: string;
|
|
34
|
+
};
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { ExtractParam, RouteParams, RoutePatternResult } from './route';
|
|
2
|
+
export declare function useMatchRoute<T extends RoutePatternResult<any>>(route: T, useMatch: (str: string) => any): PathMatch<RouteParams<ExtractParam<T>>> | null;
|
|
3
|
+
/**
|
|
4
|
+
* A PathMatch contains info about how a PathPattern matched on a URL pathname.
|
|
5
|
+
*/
|
|
6
|
+
export interface PathMatch<Params extends {}> {
|
|
7
|
+
/**
|
|
8
|
+
* The names and values of dynamic parameters in the URL.
|
|
9
|
+
*/
|
|
10
|
+
params: Params;
|
|
11
|
+
/**
|
|
12
|
+
* The portion of the URL pathname that was matched.
|
|
13
|
+
*/
|
|
14
|
+
pathname: string;
|
|
15
|
+
/**
|
|
16
|
+
* The portion of the URL pathname that was matched before child routes.
|
|
17
|
+
*/
|
|
18
|
+
pathnameBase: string;
|
|
19
|
+
/**
|
|
20
|
+
* The pattern that was used to match.
|
|
21
|
+
*/
|
|
22
|
+
pattern: PathPattern;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* A PathPattern is used to match on some portion of a URL pathname.
|
|
26
|
+
*/
|
|
27
|
+
export interface PathPattern<Path extends string = string> {
|
|
28
|
+
/**
|
|
29
|
+
* A string to match against a URL pathname. May contain `:id`-style segments
|
|
30
|
+
* to indicate placeholders for dynamic parameters. May also end with `/*` to
|
|
31
|
+
* indicate matching the rest of the URL pathname.
|
|
32
|
+
*/
|
|
33
|
+
path: Path;
|
|
34
|
+
/**
|
|
35
|
+
* Should be `true` if the static portions of the `path` should be matched in
|
|
36
|
+
* the same case.
|
|
37
|
+
*/
|
|
38
|
+
caseSensitive?: boolean;
|
|
39
|
+
/**
|
|
40
|
+
* Should be `true` if this pattern should match the entire URL pathname.
|
|
41
|
+
*/
|
|
42
|
+
end?: boolean;
|
|
43
|
+
}
|
package/dist/useRoute.d.ts
CHANGED
|
@@ -1,4 +1,2 @@
|
|
|
1
|
-
import { ExtractParam, RoutePatternResult } from './route';
|
|
2
|
-
export declare function useRoute<T extends RoutePatternResult<any>>(route: T, useParams: () => any):
|
|
3
|
-
[K in ExtractParam<T>]: string;
|
|
4
|
-
};
|
|
1
|
+
import { ExtractParam, RouteParams, RoutePatternResult } from './route';
|
|
2
|
+
export declare function useRoute<T extends RoutePatternResult<any>>(route: T, useParams: () => any): RouteParams<ExtractParam<T>>;
|
package/es/index.js
CHANGED
package/es/useRoute.js
CHANGED
|
@@ -3,9 +3,10 @@ export function useRoute(route, useParams) {
|
|
|
3
3
|
var params = useParams();
|
|
4
4
|
|
|
5
5
|
for (var {
|
|
6
|
-
name
|
|
6
|
+
name,
|
|
7
|
+
optional
|
|
7
8
|
} of route.keys) {
|
|
8
|
-
if (typeof params[name] !== 'string') {
|
|
9
|
+
if (!optional && typeof params[name] !== 'string') {
|
|
9
10
|
throw new Error("useRoute did not find the required parameters for this route: ".concat(route.pattern, ". Missing parameter: ").concat(name));
|
|
10
11
|
}
|
|
11
12
|
|
package/lib/index.js
CHANGED
|
@@ -28,4 +28,17 @@ Object.keys(_useRoute).forEach(function (key) {
|
|
|
28
28
|
return _useRoute[key];
|
|
29
29
|
}
|
|
30
30
|
});
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
var _useMatchRoute = require("./useMatchRoute");
|
|
34
|
+
|
|
35
|
+
Object.keys(_useMatchRoute).forEach(function (key) {
|
|
36
|
+
if (key === "default" || key === "__esModule") return;
|
|
37
|
+
if (key in exports && exports[key] === _useMatchRoute[key]) return;
|
|
38
|
+
Object.defineProperty(exports, key, {
|
|
39
|
+
enumerable: true,
|
|
40
|
+
get: function get() {
|
|
41
|
+
return _useMatchRoute[key];
|
|
42
|
+
}
|
|
43
|
+
});
|
|
31
44
|
});
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.useMatchRoute = useMatchRoute;
|
|
7
|
+
|
|
8
|
+
function useMatchRoute(route, useMatch) {
|
|
9
|
+
return useMatch(route.pattern);
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* A PathMatch contains info about how a PathPattern matched on a URL pathname.
|
|
13
|
+
*/
|
package/lib/useRoute.js
CHANGED
|
@@ -20,9 +20,11 @@ function useRoute(route, useParams) {
|
|
|
20
20
|
|
|
21
21
|
try {
|
|
22
22
|
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
23
|
-
var
|
|
23
|
+
var _step$value = _step.value,
|
|
24
|
+
name = _step$value.name,
|
|
25
|
+
optional = _step$value.optional;
|
|
24
26
|
|
|
25
|
-
if (typeof params[name] !== 'string') {
|
|
27
|
+
if (!optional && typeof params[name] !== 'string') {
|
|
26
28
|
throw new Error("useRoute did not find the required parameters for this route: ".concat(route.pattern, ". Missing parameter: ").concat(name));
|
|
27
29
|
}
|
|
28
30
|
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cloudflare/util-routes",
|
|
3
3
|
"description": "Wraps path-to-regexp with a convenient, type-safe tagged template function",
|
|
4
|
-
"version": "5.
|
|
4
|
+
"version": "5.4.0",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"module": "es/index.js",
|
|
7
7
|
"author": "Frontend Team <frontend@cloudflare.com>",
|