@@ -255,31 +255,26 @@ export class UriTemplate {
255255
256256 // Build regex pattern for path (non-query) parts
257257 let pattern = '^' ;
258- const names : Array < { name : string ; exploded : boolean ; isQuery : boolean } > = [ ] ;
258+ const names : Array < { name : string ; exploded : boolean } > = [ ] ;
259259 const queryParts : Array < { name : string ; exploded : boolean } > = [ ] ;
260260
261261 for ( const part of this . parts ) {
262262 if ( typeof part === 'string' ) {
263263 pattern += this . escapeRegExp ( part ) ;
264+ } else if ( part . operator === '?' || part . operator === '&' ) {
265+ for ( const name of part . names ) {
266+ queryParts . push ( { name, exploded : part . exploded } ) ;
267+ }
264268 } else {
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- }
269+ const patterns = this . partToRegExp ( part ) ;
270+ for ( const { pattern : partPattern , name } of patterns ) {
271+ pattern += partPattern ;
272+ names . push ( { name, exploded : part . exploded } ) ;
277273 }
278274 }
279275 }
280276
281- // Match the path part (without query parameters)
282- pattern += '(?:\\?.*)?$' ; // Allow optional query string at the end
277+ pattern += '$' ;
283278 UriTemplate . validateLength ( pattern , MAX_REGEX_LENGTH , 'Generated regex pattern' ) ;
284279 const regex = new RegExp ( pattern ) ;
285280 const match = pathPart . match ( regex ) ;
@@ -288,29 +283,16 @@ export class UriTemplate {
288283
289284 const result : Variables = { } ;
290285
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- }
286+ for ( const [ i , { name, exploded } ] of names . entries ( ) ) {
287+ const value = match [ i + 1 ] ! ;
288+ const cleanName = name . replace ( '*' , '' ) ;
289+ result [ cleanName ] = exploded && value . includes ( ',' ) ? value . split ( ',' ) : value ;
305290 }
306291
307- // Extract query parameters from query string
308292 if ( queryParts . length > 0 ) {
309293 const queryParams = new Map < string , string > ( ) ;
310294 if ( queryPart ) {
311- // Parse query string
312- const pairs = queryPart . split ( '&' ) ;
313- for ( const pair of pairs ) {
295+ for ( const pair of queryPart . split ( '&' ) ) {
314296 const equalIndex = pair . indexOf ( '=' ) ;
315297 if ( equalIndex !== - 1 ) {
316298 const key = decodeURIComponent ( pair . slice ( 0 , equalIndex ) ) ;
@@ -320,18 +302,12 @@ export class UriTemplate {
320302 }
321303 }
322304
323- // Extract values for each expected query parameter
324305 for ( const { name, exploded } of queryParts ) {
325306 const cleanName = name . replace ( '*' , '' ) ;
326307 const value = queryParams . get ( cleanName ) ;
327308
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- }
309+ if ( value === undefined ) continue ;
310+ result [ cleanName ] = exploded && value . includes ( ',' ) ? value . split ( ',' ) : value ;
335311 }
336312 }
337313
0 commit comments