@cloudflare/util-routes 5.2.35 → 5.3.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 CHANGED
@@ -1,5 +1,17 @@
1
1
  # Change Log
2
2
 
3
+ ## 5.3.0
4
+
5
+ ### Minor Changes
6
+
7
+ - a4dce672bb: Add support for optional params
8
+
9
+ ## 5.2.36
10
+
11
+ ### Patch Changes
12
+
13
+ - 1bc29ca267: Fix: Use IS_RELEASE_CANDIDATE to enable a11y source file attributes in preview builds
14
+
3
15
  ## 5.2.35
4
16
 
5
17
  ### Patch Changes
package/README.md CHANGED
@@ -30,3 +30,23 @@ const domainRoute = route`${domainsRoute}/${'domainName'}`
30
30
  // => /api/v4/accounts/123/domains/abc.com
31
31
  domainRoute.toUrl({ accountId: '123', domainName: 'abc.com' })
32
32
  ```
33
+
34
+ ### Optional Parameters
35
+
36
+ Parameters can be marked optional with a `?` suffix. TypeScript will make them optional in the `toUrl` signature:
37
+
38
+ ```typescript
39
+ const apiRoute = route`/api/${'filter?'}`
40
+
41
+ // filter is optional in toUrl
42
+ apiRoute.toUrl({}) // => /api
43
+ apiRoute.toUrl({ filter: 'active' }) // => /api/active
44
+
45
+ // Composing routes with optional params
46
+ const accountsRoute = route`/accounts/${'accountId'}`
47
+ const accountZonesRoute = route`${accountsRoute}/zones/${'filter?'}`
48
+
49
+ // TypeScript knows filter is optional
50
+ accountZonesRoute.toUrl({ accountId: '123' }) // => /accounts/123/zones
51
+ accountZonesRoute.toUrl({ accountId: '123', filter: 'active' }) // => /accounts/123/zones/active
52
+ ```
package/dist/route.d.ts CHANGED
@@ -6,7 +6,9 @@ export declare class RoutePatternResult<Param extends string> {
6
6
  * Interperolate arguments into the route pattern
7
7
  */
8
8
  readonly toUrl: (args: {
9
- [K in Param]: string;
9
+ [K in RequiredParams<Param>]: string;
10
+ } & {
11
+ [K in OptionalParams<Param>]?: string;
10
12
  }, options?: pathToRegExp.PathFunctionOptions) => string;
11
13
  readonly keys: Key[];
12
14
  readonly expression: RegExp;
@@ -20,7 +22,11 @@ export declare type RoutePatternResultFromParams<Params extends Array<string | R
20
22
  returnResult: RoutePatternResult<ValueOfArray<Result>>;
21
23
  recurse: RoutePatternResultFromParams<Tail<Params>, Prepend<ExtractParam<Head<Params>>, Result>>;
22
24
  }[Head<Params> extends never ? 'returnResult' : 'recurse'];
25
+ export declare type StripOptional<T extends string> = T extends `${infer Name}?` ? Name : T;
26
+ export declare type IsOptionalParam<T extends string> = T extends `${string}?` ? true : false;
23
27
  export declare type ExtractParam<T extends string | RoutePatternResult<string>> = T extends RoutePatternResult<infer S> ? S : T;
24
28
  export declare type ExtractRouteParamsInterface<T extends string | RoutePatternResult<string>> = {
25
29
  [K in ExtractParam<T>]: string | undefined;
26
30
  };
31
+ export declare type RequiredParams<Params extends string> = Params extends `${infer P}?` ? never : Params extends string ? Params : never;
32
+ export declare type OptionalParams<Params extends string> = Params extends `${infer P}?` ? P : never;
package/es/route.js CHANGED
@@ -30,7 +30,9 @@ export function route(strings) {
30
30
  if (paramName instanceof RoutePatternResult) {
31
31
  result += paramName.pattern;
32
32
  } else if (typeof paramName === 'string') {
33
- result += ':' + paramName;
33
+ var isOptional = paramName.endsWith('?');
34
+ var cleanName = isOptional ? paramName.slice(0, -1) : paramName;
35
+ result += ':' + cleanName + (isOptional ? '?' : '');
34
36
  }
35
37
 
36
38
  return result;
package/lib/route.js CHANGED
@@ -50,7 +50,9 @@ function route(strings) {
50
50
  if (paramName instanceof RoutePatternResult) {
51
51
  result += paramName.pattern;
52
52
  } else if (typeof paramName === 'string') {
53
- result += ':' + paramName;
53
+ var isOptional = paramName.endsWith('?');
54
+ var cleanName = isOptional ? paramName.slice(0, -1) : paramName;
55
+ result += ':' + cleanName + (isOptional ? '?' : '');
54
56
  }
55
57
 
56
58
  return result;
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.35",
4
+ "version": "5.3.0",
5
5
  "main": "lib/index.js",
6
6
  "module": "es/index.js",
7
7
  "author": "Frontend Team <frontend@cloudflare.com>",