@cloudflare/util-routes 5.0.45 → 5.0.47

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,22 @@
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.0.47](http://stash.cfops.it:7999/fe/stratus/compare/@cloudflare/util-routes@5.0.46...@cloudflare/util-routes@5.0.47) (2020-02-12)
7
+
8
+ **Note:** Version bump only for package @cloudflare/util-routes
9
+
10
+
11
+
12
+
13
+
14
+ ## [5.0.46](http://stash.cfops.it:7999/fe/stratus/compare/@cloudflare/util-routes@5.0.45...@cloudflare/util-routes@5.0.46) (2020-02-10)
15
+
16
+ **Note:** Version bump only for package @cloudflare/util-routes
17
+
18
+
19
+
20
+
21
+
6
22
  ## [5.0.45](http://stash.cfops.it:7999/fe/stratus/compare/@cloudflare/util-routes@5.0.44...@cloudflare/util-routes@5.0.45) (2020-02-10)
7
23
 
8
24
  **Note:** Version bump only for package @cloudflare/util-routes
package/package.json CHANGED
@@ -1,9 +1,9 @@
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.0.45",
4
+ "version": "5.0.47",
5
5
  "types": "./dist/index.d.ts",
6
- "main": "lib/index.js",
6
+ "main": "src/index.ts",
7
7
  "module": "es/index.js",
8
8
  "jsnext:main": "es/index.js",
9
9
  "author": "Frontend Team <frontend@cloudflare.com>",
@@ -13,6 +13,7 @@
13
13
  ],
14
14
  "license": "UNLICENSED",
15
15
  "publishConfig": {
16
+ "main": "lib/index.js",
16
17
  "registry": "http://registry.npmjs.org/",
17
18
  "access": "restricted"
18
19
  },
@@ -25,7 +26,7 @@
25
26
  "path-to-regexp": "^3.0.0"
26
27
  },
27
28
  "devDependencies": {
28
- "@cloudflare/types": "^6.4.16"
29
+ "@cloudflare/types": "^6.4.18"
29
30
  },
30
- "gitHead": "5b7ec7be1d1939506d2d08b2d1c2c77a393b536b"
31
+ "gitHead": "88916f170a5a79963b140a5eb6158ed2277f3ac6"
31
32
  }
package/src/index.d.ts ADDED
@@ -0,0 +1 @@
1
+ export * from './route';
package/src/index.ts ADDED
@@ -0,0 +1 @@
1
+ export * from './route'
package/src/route.d.ts ADDED
@@ -0,0 +1,22 @@
1
+ import pathToRegExp from 'path-to-regexp';
2
+ import { ValueOfArray, Tail, Prepend, Head } from '@cloudflare/types';
3
+ export declare class RoutePatternResult<Param extends string> {
4
+ readonly pattern: string;
5
+ /**
6
+ * Interperolate arguments into the route pattern
7
+ */
8
+ readonly toUrl: (args: {
9
+ [K in Param]: string;
10
+ }, options?: pathToRegExp.PathFunctionOptions) => string;
11
+ constructor(pattern: string);
12
+ }
13
+ /**
14
+ * Tag a template string with route data
15
+ */
16
+ 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[] = []> = {
18
+ returnResult: RoutePatternResult<ValueOfArray<Result>>;
19
+ recurse: RoutePatternResultFromParams<Tail<Params>, Prepend<ExtractParam<Head<Params>>, Result>>;
20
+ }[Head<Params> extends never ? 'returnResult' : 'recurse'];
21
+ declare type ExtractParam<T extends string | RoutePatternResult<string>> = T extends RoutePatternResult<infer S> ? S : T;
22
+ export {};
package/src/route.ts ADDED
@@ -0,0 +1,62 @@
1
+ import pathToRegExp from 'path-to-regexp'
2
+ import { ValueOfArray, Tail, Prepend, Head } from '@cloudflare/types'
3
+
4
+ export class RoutePatternResult<Param extends string> {
5
+ public readonly pattern: string
6
+
7
+ /**
8
+ * Interperolate arguments into the route pattern
9
+ */
10
+ readonly toUrl: (
11
+ args: { [K in Param]: string },
12
+ options?: pathToRegExp.PathFunctionOptions,
13
+ ) => string
14
+
15
+ constructor(pattern: string) {
16
+ this.pattern = pattern
17
+ this.toUrl = pathToRegExp.compile(this.pattern)
18
+ }
19
+ }
20
+
21
+ /**
22
+ * Tag a template string with route data
23
+ */
24
+ export function route<
25
+ Param extends string,
26
+ ParamOrPatternResult extends Array<Param | RoutePatternResult<string>>
27
+ >(
28
+ strings: TemplateStringsArray,
29
+ ...paramNames: ParamOrPatternResult
30
+ ): RoutePatternResultFromParams<ParamOrPatternResult> {
31
+ const pattern = strings.reduce((accum, str, i) => {
32
+ let result = accum + str
33
+ const paramName = paramNames[i]
34
+
35
+ if (paramName instanceof RoutePatternResult) {
36
+ result += paramName.pattern
37
+ } else if (typeof paramName === 'string') {
38
+ result += ':' + paramName
39
+ }
40
+
41
+ return result
42
+ }, '')
43
+
44
+ // Without casting to any, we get the following error:
45
+ // > Type instantiation is excessively deep and possibly infinite.
46
+ return new RoutePatternResult<Param>(pattern) as any
47
+ }
48
+
49
+ type RoutePatternResultFromParams<
50
+ Params extends Array<string | RoutePatternResult<string>>,
51
+ Result extends string[] = []
52
+ > = {
53
+ returnResult: RoutePatternResult<ValueOfArray<Result>>
54
+ recurse: RoutePatternResultFromParams<
55
+ Tail<Params>,
56
+ Prepend<ExtractParam<Head<Params>>, Result>
57
+ >
58
+ }[Head<Params> extends never ? 'returnResult' : 'recurse']
59
+
60
+ type ExtractParam<
61
+ T extends string | RoutePatternResult<string>
62
+ > = T extends RoutePatternResult<infer S> ? S : T
package/es/index.d.js DELETED
@@ -1 +0,0 @@
1
- export * from './route';
package/es/route.d.js DELETED
@@ -1,3 +0,0 @@
1
- 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; }
2
-
3
- export {};
package/lib/index.d.js DELETED
@@ -1,17 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
-
7
- var _route = require("./route");
8
-
9
- Object.keys(_route).forEach(function (key) {
10
- if (key === "default" || key === "__esModule") return;
11
- Object.defineProperty(exports, key, {
12
- enumerable: true,
13
- get: function get() {
14
- return _route[key];
15
- }
16
- });
17
- });
package/lib/route.d.js DELETED
@@ -1,3 +0,0 @@
1
- "use strict";
2
-
3
- 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; }