11// Claude-authored implementation of RFC 6570 URI Templates
22
3- type Variables = Record < string , string | string [ ] > ;
3+ export type Variables = Record < string , string | string [ ] > ;
44
55const MAX_TEMPLATE_LENGTH = 1000000 ; // 1MB
66const MAX_VARIABLE_LENGTH = 1000000 ; // 1MB
77const MAX_TEMPLATE_EXPRESSIONS = 10000 ;
88const MAX_REGEX_LENGTH = 1000000 ; // 1MB
99
1010export class UriTemplate {
11- private static validateLength ( str : string , max : number , context : string ) : void {
11+ /**
12+ * Returns true if the given string contains any URI template expressions.
13+ * A template expression is a sequence of characters enclosed in curly braces,
14+ * like {foo} or {?bar}.
15+ */
16+ static isTemplate ( str : string ) : boolean {
17+ // Look for any sequence of characters between curly braces
18+ // that isn't just whitespace
19+ return / \{ [ ^ } \s ] + \} / . test ( str ) ;
20+ }
21+
22+ private static validateLength (
23+ str : string ,
24+ max : number ,
25+ context : string ,
26+ ) : void {
1227 if ( str . length > max ) {
1328 throw new Error (
1429 `${ context } exceeds maximum length of ${ max } characters (got ${ str . length } )` ,
@@ -60,7 +75,7 @@ export class UriTemplate {
6075 const exploded = expr . includes ( "*" ) ;
6176 const names = this . getNames ( expr ) ;
6277 const name = names [ 0 ] ;
63-
78+
6479 // Validate variable name length
6580 for ( const name of names ) {
6681 UriTemplate . validateLength (
@@ -263,7 +278,11 @@ export class UriTemplate {
263278 }
264279
265280 pattern += "$" ;
266- UriTemplate . validateLength ( pattern , MAX_REGEX_LENGTH , "Generated regex pattern" ) ;
281+ UriTemplate . validateLength (
282+ pattern ,
283+ MAX_REGEX_LENGTH ,
284+ "Generated regex pattern" ,
285+ ) ;
267286 const regex = new RegExp ( pattern ) ;
268287 const match = uri . match ( regex ) ;
269288
@@ -284,4 +303,4 @@ export class UriTemplate {
284303
285304 return result ;
286305 }
287- }
306+ }
0 commit comments