@cloudflare/util-routes 5.3.0 → 5.4.1

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 CHANGED
@@ -1,5 +1,17 @@
1
1
  # Change Log
2
2
 
3
+ ## 5.4.1
4
+
5
+ ### Patch Changes
6
+
7
+ - 8c2729a131: Support `PathPattern` options in `useMatchRoute` for caseSensitive and end matching
8
+
9
+ ## 5.4.0
10
+
11
+ ### Minor Changes
12
+
13
+ - 3f146fca1c: Add useMatchRoute hook; fix optional parameter handling in useRoute
14
+
3
15
  ## 5.3.0
4
16
 
5
17
  ### Minor Changes
package/dist/index.d.ts CHANGED
@@ -1,2 +1,3 @@
1
1
  export * from './route';
2
2
  export * from './useRoute';
3
+ export * from './useMatchRoute';
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,54 @@
1
+ import { ExtractParam, RouteParams, RoutePatternResult } from './route';
2
+ export interface PathPattern {
3
+ /**
4
+ * Should be `true` if the static portions of the `path` should be matched in
5
+ * the same case.
6
+ */
7
+ caseSensitive?: boolean;
8
+ /**
9
+ * Should be `true` if this pattern should match the entire URL pathname.
10
+ */
11
+ end?: boolean;
12
+ }
13
+ export declare function useMatchRoute<T extends RoutePatternResult<any>>(route: T, useMatch: (str: string | PathPattern) => any, options?: PathPattern): PathMatch<RouteParams<ExtractParam<T>>> | null;
14
+ /**
15
+ * A PathMatch contains info about how a PathPattern matched on a URL pathname.
16
+ */
17
+ export interface PathMatch<Params extends {}> {
18
+ /**
19
+ * The names and values of dynamic parameters in the URL.
20
+ */
21
+ params: Params;
22
+ /**
23
+ * The portion of the URL pathname that was matched.
24
+ */
25
+ pathname: string;
26
+ /**
27
+ * The portion of the URL pathname that was matched before child routes.
28
+ */
29
+ pathnameBase: string;
30
+ /**
31
+ * The pattern that was used to match.
32
+ */
33
+ pattern: PathPattern;
34
+ }
35
+ /**
36
+ * A PathPattern is used to match on some portion of a URL pathname.
37
+ */
38
+ export interface PathPattern<Path extends string = string> {
39
+ /**
40
+ * A string to match against a URL pathname. May contain `:id`-style segments
41
+ * to indicate placeholders for dynamic parameters. May also end with `/*` to
42
+ * indicate matching the rest of the URL pathname.
43
+ */
44
+ path: Path;
45
+ /**
46
+ * Should be `true` if the static portions of the `path` should be matched in
47
+ * the same case.
48
+ */
49
+ caseSensitive?: boolean;
50
+ /**
51
+ * Should be `true` if this pattern should match the entire URL pathname.
52
+ */
53
+ end?: boolean;
54
+ }
@@ -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
@@ -1,2 +1,3 @@
1
1
  export * from './route';
2
- export * from './useRoute';
2
+ export * from './useRoute';
3
+ export * from './useMatchRoute';
@@ -0,0 +1,14 @@
1
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
2
+
3
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
4
+
5
+ function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
6
+
7
+ export function useMatchRoute(route, useMatch, options) {
8
+ return useMatch(_objectSpread(_objectSpread({}, options), {}, {
9
+ path: route.pattern
10
+ }));
11
+ }
12
+ /**
13
+ * A PathMatch contains info about how a PathPattern matched on a URL pathname.
14
+ */
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,21 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.useMatchRoute = useMatchRoute;
7
+
8
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
9
+
10
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
11
+
12
+ function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
13
+
14
+ function useMatchRoute(route, useMatch, options) {
15
+ return useMatch(_objectSpread(_objectSpread({}, options), {}, {
16
+ path: route.pattern
17
+ }));
18
+ }
19
+ /**
20
+ * A PathMatch contains info about how a PathPattern matched on a URL pathname.
21
+ */
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 name = _step.value.name;
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.3.0",
4
+ "version": "5.4.1",
5
5
  "main": "lib/index.js",
6
6
  "module": "es/index.js",
7
7
  "author": "Frontend Team <frontend@cloudflare.com>",