@cloudflare/util-routes 5.2.6 → 5.2.7

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
@@ -3,6 +3,14 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ ## [5.2.7](http://stash.cfops.it:7999/fe/stratus/compare/@cloudflare/util-routes@5.2.6...@cloudflare/util-routes@5.2.7) (2022-05-05)
7
+
8
+ **Note:** Version bump only for package @cloudflare/util-routes
9
+
10
+
11
+
12
+
13
+
6
14
  ## [5.2.6](http://stash.cfops.it:7999/fe/stratus/compare/@cloudflare/util-routes@5.2.5...@cloudflare/util-routes@5.2.6) (2022-05-04)
7
15
 
8
16
  **Note:** Version bump only for package @cloudflare/util-routes
package/dist/index.d.ts CHANGED
@@ -1 +1,2 @@
1
1
  export * from './route';
2
+ export * from './useRoute';
package/dist/route.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import pathToRegExp from 'path-to-regexp';
1
+ import pathToRegExp, { Key } from 'path-to-regexp';
2
2
  import { ValueOfArray, Tail, Prepend, Head } from '@cloudflare/types';
3
3
  export declare class RoutePatternResult<Param extends string> {
4
4
  readonly pattern: string;
@@ -8,13 +8,15 @@ export declare class RoutePatternResult<Param extends string> {
8
8
  readonly toUrl: (args: {
9
9
  [K in Param]: string;
10
10
  }, options?: pathToRegExp.PathFunctionOptions) => string;
11
+ readonly keys: Key[];
12
+ readonly expression: RegExp;
11
13
  constructor(pattern: string);
12
14
  }
13
15
  /**
14
16
  * Tag a template string with route data
15
17
  */
16
18
  export declare function route<Param extends string, ParamOrPatternResult extends Array<Param | RoutePatternResult<string>>>(strings: TemplateStringsArray, ...paramNames: ParamOrPatternResult): RoutePatternResultFromParams<ParamOrPatternResult>;
17
- declare type RoutePatternResultFromParams<Params extends Array<string | RoutePatternResult<string>>, Result extends string[] = []> = {
19
+ export declare type RoutePatternResultFromParams<Params extends Array<string | RoutePatternResult<string>>, Result extends string[] = []> = {
18
20
  returnResult: RoutePatternResult<ValueOfArray<Result>>;
19
21
  recurse: RoutePatternResultFromParams<Tail<Params>, Prepend<ExtractParam<Head<Params>>, Result>>;
20
22
  }[Head<Params> extends never ? 'returnResult' : 'recurse'];
@@ -22,4 +24,3 @@ export declare type ExtractParam<T extends string | RoutePatternResult<string>>
22
24
  export declare type ExtractRouteParamsInterface<T extends string | RoutePatternResult<string>> = {
23
25
  [K in ExtractParam<T>]: string | undefined;
24
26
  };
25
- export {};
@@ -0,0 +1,4 @@
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
+ };
package/es/index.js CHANGED
@@ -1 +1,2 @@
1
- export * from './route';
1
+ export * from './route';
2
+ export * from './useRoute';
package/es/route.js CHANGED
@@ -10,6 +10,11 @@ export class RoutePatternResult {
10
10
 
11
11
  _defineProperty(this, "toUrl", void 0);
12
12
 
13
+ _defineProperty(this, "keys", []);
14
+
15
+ _defineProperty(this, "expression", void 0);
16
+
17
+ this.expression = pathToRegExp(pattern, this.keys);
13
18
  this.pattern = pattern;
14
19
  this.toUrl = pathToRegExp.compile(this.pattern);
15
20
  }
package/es/useRoute.js ADDED
@@ -0,0 +1,16 @@
1
+ export function useRoute(route, useParams) {
2
+ const result = {};
3
+ const params = useParams();
4
+
5
+ for (const _ref of route.keys) {
6
+ const name = _ref.name;
7
+
8
+ if (typeof params[name] !== 'string') {
9
+ throw new Error(`useRoute did not find the required parameters for this route: ${route.pattern}. Missing parameter: ${name}`);
10
+ }
11
+
12
+ result[name] = params[name];
13
+ }
14
+
15
+ return result;
16
+ }
package/lib/index.js CHANGED
@@ -14,4 +14,16 @@ Object.keys(_route).forEach(function (key) {
14
14
  return _route[key];
15
15
  }
16
16
  });
17
+ });
18
+
19
+ var _useRoute = require("./useRoute");
20
+
21
+ Object.keys(_useRoute).forEach(function (key) {
22
+ if (key === "default" || key === "__esModule") return;
23
+ Object.defineProperty(exports, key, {
24
+ enumerable: true,
25
+ get: function get() {
26
+ return _useRoute[key];
27
+ }
28
+ });
17
29
  });
package/lib/route.js CHANGED
@@ -25,6 +25,11 @@ function RoutePatternResult(pattern) {
25
25
 
26
26
  _defineProperty(this, "toUrl", void 0);
27
27
 
28
+ _defineProperty(this, "keys", []);
29
+
30
+ _defineProperty(this, "expression", void 0);
31
+
32
+ this.expression = (0, _pathToRegexp.default)(pattern, this.keys);
28
33
  this.pattern = pattern;
29
34
  this.toUrl = _pathToRegexp.default.compile(this.pattern);
30
35
  };
@@ -0,0 +1,42 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.useRoute = useRoute;
7
+
8
+ function useRoute(route, useParams) {
9
+ var result = {};
10
+ var params = useParams();
11
+ var _iteratorNormalCompletion = true;
12
+ var _didIteratorError = false;
13
+ var _iteratorError = undefined;
14
+
15
+ try {
16
+ for (var _iterator = route.keys[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
17
+ var _ref2 = _step.value;
18
+ var name = _ref2.name;
19
+
20
+ if (typeof params[name] !== 'string') {
21
+ throw new Error("useRoute did not find the required parameters for this route: ".concat(route.pattern, ". Missing parameter: ").concat(name));
22
+ }
23
+
24
+ result[name] = params[name];
25
+ }
26
+ } catch (err) {
27
+ _didIteratorError = true;
28
+ _iteratorError = err;
29
+ } finally {
30
+ try {
31
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
32
+ _iterator.return();
33
+ }
34
+ } finally {
35
+ if (_didIteratorError) {
36
+ throw _iteratorError;
37
+ }
38
+ }
39
+ }
40
+
41
+ return result;
42
+ }
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.2.6",
4
+ "version": "5.2.7",
5
5
  "types": "./dist/index.d.ts",
6
6
  "main": "lib/index.js",
7
7
  "module": "es/index.js",
@@ -21,8 +21,8 @@
21
21
  "autoGeneratedReadme": false
22
22
  },
23
23
  "dependencies": {
24
- "@cloudflare/types": "^6.14.16",
24
+ "@cloudflare/types": "^6.14.17",
25
25
  "path-to-regexp": "^3.0.0"
26
26
  },
27
- "gitHead": "a3180c1ee69ed6fbe5943fd2a16fb2e9950becdc"
27
+ "gitHead": "810c5cdf263deb6f1996bdea357064221f82a1bd"
28
28
  }