|
1 | 1 | // Claude-authored implementation of RFC 6570 URI Templates |
2 | 2 |
|
3 | | -export type Variables = Record<string, string | string[]>; |
| 3 | +export type Variables = Record<string, string | string[] | undefined>; |
4 | 4 |
|
5 | 5 | const MAX_TEMPLATE_LENGTH = 1000000; // 1MB |
6 | 6 | const MAX_VARIABLE_LENGTH = 1000000; // 1MB |
@@ -247,38 +247,91 @@ export class UriTemplate { |
247 | 247 |
|
248 | 248 | match(uri: string): Variables | null { |
249 | 249 | UriTemplate.validateLength(uri, MAX_TEMPLATE_LENGTH, 'URI'); |
| 250 | + |
| 251 | + // Split URI into path and query parts |
| 252 | + const queryIndex = uri.indexOf('?'); |
| 253 | + const pathPart = queryIndex === -1 ? uri : uri.slice(0, queryIndex); |
| 254 | + const queryPart = queryIndex === -1 ? '' : uri.slice(queryIndex + 1); |
| 255 | + |
| 256 | + // Build regex pattern for path (non-query) parts |
250 | 257 | let pattern = '^'; |
251 | | - const names: Array<{ name: string; exploded: boolean }> = []; |
| 258 | + const names: Array<{ name: string; exploded: boolean; isQuery: boolean }> = []; |
| 259 | + const queryParts: Array<{ name: string; exploded: boolean }> = []; |
252 | 260 |
|
253 | 261 | for (const part of this.parts) { |
254 | 262 | if (typeof part === 'string') { |
255 | 263 | pattern += this.escapeRegExp(part); |
256 | 264 | } else { |
257 | | - const patterns = this.partToRegExp(part); |
258 | | - for (const { pattern: partPattern, name } of patterns) { |
259 | | - pattern += partPattern; |
260 | | - names.push({ name, exploded: part.exploded }); |
| 265 | + if (part.operator === '?' || part.operator === '&') { |
| 266 | + // Collect query parameter names for later extraction |
| 267 | + for (const name of part.names) { |
| 268 | + queryParts.push({ name, exploded: part.exploded }); |
| 269 | + } |
| 270 | + } else { |
| 271 | + // Handle non-query parts normally |
| 272 | + const patterns = this.partToRegExp(part); |
| 273 | + for (const { pattern: partPattern, name } of patterns) { |
| 274 | + pattern += partPattern; |
| 275 | + names.push({ name, exploded: part.exploded, isQuery: false }); |
| 276 | + } |
261 | 277 | } |
262 | 278 | } |
263 | 279 | } |
264 | 280 |
|
265 | | - pattern += '$'; |
| 281 | + // Match the path part (without query parameters) |
| 282 | + pattern += '(?:\\?.*)?$'; // Allow optional query string at the end |
266 | 283 | UriTemplate.validateLength(pattern, MAX_REGEX_LENGTH, 'Generated regex pattern'); |
267 | 284 | const regex = new RegExp(pattern); |
268 | | - const match = uri.match(regex); |
| 285 | + const match = pathPart.match(regex); |
269 | 286 |
|
270 | 287 | if (!match) return null; |
271 | 288 |
|
272 | 289 | const result: Variables = {}; |
273 | | - for (let i = 0; i < names.length; i++) { |
274 | | - const { name, exploded } = names[i]!; |
275 | | - const value = match[i + 1]!; |
276 | | - const cleanName = name.replace('*', ''); |
277 | 290 |
|
278 | | - if (exploded && value.includes(',')) { |
279 | | - result[cleanName] = value.split(','); |
280 | | - } else { |
281 | | - result[cleanName] = value; |
| 291 | + // Extract non-query parameters |
| 292 | + let matchIndex = 0; |
| 293 | + for (const { name, exploded, isQuery } of names) { |
| 294 | + if (!isQuery) { |
| 295 | + const value = match[matchIndex + 1]; |
| 296 | + const cleanName = name.replace('*', ''); |
| 297 | + |
| 298 | + if (exploded && value && value.includes(',')) { |
| 299 | + result[cleanName] = value.split(','); |
| 300 | + } else { |
| 301 | + result[cleanName] = value; |
| 302 | + } |
| 303 | + matchIndex++; |
| 304 | + } |
| 305 | + } |
| 306 | + |
| 307 | + // Extract query parameters from query string |
| 308 | + if (queryParts.length > 0) { |
| 309 | + const queryParams = new Map<string, string>(); |
| 310 | + if (queryPart) { |
| 311 | + // Parse query string |
| 312 | + const pairs = queryPart.split('&'); |
| 313 | + for (const pair of pairs) { |
| 314 | + const equalIndex = pair.indexOf('='); |
| 315 | + if (equalIndex !== -1) { |
| 316 | + const key = decodeURIComponent(pair.slice(0, equalIndex)); |
| 317 | + const value = decodeURIComponent(pair.slice(equalIndex + 1)); |
| 318 | + queryParams.set(key, value); |
| 319 | + } |
| 320 | + } |
| 321 | + } |
| 322 | + |
| 323 | + // Extract values for each expected query parameter |
| 324 | + for (const { name, exploded } of queryParts) { |
| 325 | + const cleanName = name.replace('*', ''); |
| 326 | + const value = queryParams.get(cleanName); |
| 327 | + |
| 328 | + if (value === undefined) { |
| 329 | + result[cleanName] = ''; |
| 330 | + } else if (exploded && value.includes(',')) { |
| 331 | + result[cleanName] = value.split(','); |
| 332 | + } else { |
| 333 | + result[cleanName] = value; |
| 334 | + } |
282 | 335 | } |
283 | 336 | } |
284 | 337 |
|
|
0 commit comments