1
1
// Claude-authored implementation of RFC 6570 URI Templates
2
2
3
- type Variables = Record < string , string | string [ ] > ;
3
+ export type Variables = Record < string , string | string [ ] > ;
4
4
5
5
const MAX_TEMPLATE_LENGTH = 1000000 ; // 1MB
6
6
const MAX_VARIABLE_LENGTH = 1000000 ; // 1MB
7
7
const MAX_TEMPLATE_EXPRESSIONS = 10000 ;
8
8
const MAX_REGEX_LENGTH = 1000000 ; // 1MB
9
9
10
10
export 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 {
12
27
if ( str . length > max ) {
13
28
throw new Error (
14
29
`${ context } exceeds maximum length of ${ max } characters (got ${ str . length } )` ,
@@ -60,7 +75,7 @@ export class UriTemplate {
60
75
const exploded = expr . includes ( "*" ) ;
61
76
const names = this . getNames ( expr ) ;
62
77
const name = names [ 0 ] ;
63
-
78
+
64
79
// Validate variable name length
65
80
for ( const name of names ) {
66
81
UriTemplate . validateLength (
@@ -263,7 +278,11 @@ export class UriTemplate {
263
278
}
264
279
265
280
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
+ ) ;
267
286
const regex = new RegExp ( pattern ) ;
268
287
const match = uri . match ( regex ) ;
269
288
@@ -284,4 +303,4 @@ export class UriTemplate {
284
303
285
304
return result ;
286
305
}
287
- }
306
+ }
0 commit comments