@@ -3,7 +3,6 @@ const DEFAULT_HEADERS = {
3
3
"Content-Type" : "application/json" ,
4
4
} ;
5
5
6
- const LEADING_QUESTION_RE = / ^ \? + / ;
7
6
const PATH_PARAM_RE = / \{ [ ^ { } ] + \} / g;
8
7
9
8
/**
@@ -19,7 +18,7 @@ export default function createClient(clientOptions) {
19
18
} = clientOptions ?? { } ;
20
19
let baseUrl = baseOptions . baseUrl ?? "" ;
21
20
if ( baseUrl . endsWith ( "/" ) ) {
22
- baseUrl = baseUrl . slice ( 0 , - 1 ) ; // remove trailing slash
21
+ baseUrl = baseUrl . substring ( 0 , baseUrl . length - 1 ) ;
23
22
}
24
23
25
24
/**
@@ -293,12 +292,13 @@ export function createQuerySerializer(options) {
293
292
const search = [ ] ;
294
293
if ( queryParams && typeof queryParams === "object" ) {
295
294
for ( const name in queryParams ) {
296
- if ( queryParams [ name ] === undefined || queryParams [ name ] === null ) {
295
+ const value = queryParams [ name ] ;
296
+ if ( value === undefined || value === null ) {
297
297
continue ;
298
298
}
299
- if ( Array . isArray ( queryParams [ name ] ) ) {
299
+ if ( Array . isArray ( value ) ) {
300
300
search . push (
301
- serializeArrayParam ( name , queryParams [ name ] , {
301
+ serializeArrayParam ( name , value , {
302
302
style : "form" ,
303
303
explode : true ,
304
304
...options ?. array ,
@@ -307,9 +307,9 @@ export function createQuerySerializer(options) {
307
307
) ;
308
308
continue ;
309
309
}
310
- if ( typeof queryParams [ name ] === "object" ) {
310
+ if ( typeof value === "object" ) {
311
311
search . push (
312
- serializeObjectParam ( name , queryParams [ name ] , {
312
+ serializeObjectParam ( name , value , {
313
313
style : "deepObject" ,
314
314
explode : true ,
315
315
...options ?. object ,
@@ -318,7 +318,7 @@ export function createQuerySerializer(options) {
318
318
) ;
319
319
continue ;
320
320
}
321
- search . push ( serializePrimitiveParam ( name , queryParams [ name ] , options ) ) ;
321
+ search . push ( serializePrimitiveParam ( name , value , options ) ) ;
322
322
}
323
323
}
324
324
return search . join ( "&" ) ;
@@ -331,64 +331,52 @@ export function createQuerySerializer(options) {
331
331
* @see https://swagger.io/docs/specification/serialization/#path
332
332
*/
333
333
export function defaultPathSerializer ( pathname , pathParams ) {
334
- const matches = pathname . match ( PATH_PARAM_RE ) ;
335
- if ( ! matches || ! matches . length ) {
336
- return undefined ;
337
- }
338
334
let nextURL = pathname ;
339
- for ( const match of matches ) {
340
- let paramName = match . substring ( 1 , match . length - 1 ) ;
335
+ for ( const match of pathname . match ( PATH_PARAM_RE ) ?? [ ] ) {
336
+ let name = match . substring ( 1 , match . length - 1 ) ;
341
337
let explode = false ;
342
338
let style = "simple" ;
343
- if ( paramName . endsWith ( "*" ) ) {
339
+ if ( name . endsWith ( "*" ) ) {
344
340
explode = true ;
345
- paramName = paramName . substring ( 0 , paramName . length - 1 ) ;
341
+ name = name . substring ( 0 , name . length - 1 ) ;
346
342
}
347
- if ( paramName . startsWith ( "." ) ) {
343
+ if ( name . startsWith ( "." ) ) {
348
344
style = "label" ;
349
- paramName = paramName . substring ( 1 ) ;
350
- } else if ( paramName . startsWith ( ";" ) ) {
345
+ name = name . substring ( 1 ) ;
346
+ } else if ( name . startsWith ( ";" ) ) {
351
347
style = "matrix" ;
352
- paramName = paramName . substring ( 1 ) ;
348
+ name = name . substring ( 1 ) ;
353
349
}
354
350
if (
355
351
! pathParams ||
356
- pathParams [ paramName ] === undefined ||
357
- pathParams [ paramName ] === null
352
+ pathParams [ name ] === undefined ||
353
+ pathParams [ name ] === null
358
354
) {
359
355
continue ;
360
356
}
361
- if ( Array . isArray ( pathParams [ paramName ] ) ) {
357
+ const value = pathParams [ name ] ;
358
+ if ( Array . isArray ( value ) ) {
362
359
nextURL = nextURL . replace (
363
360
match ,
364
- serializeArrayParam ( paramName , pathParams [ paramName ] , {
365
- style,
366
- explode,
367
- } ) ,
361
+ serializeArrayParam ( name , value , { style, explode } ) ,
368
362
) ;
369
363
continue ;
370
364
}
371
- if ( typeof pathParams [ paramName ] === "object" ) {
365
+ if ( typeof value === "object" ) {
372
366
nextURL = nextURL . replace (
373
367
match ,
374
- serializeObjectParam ( paramName , pathParams [ paramName ] , {
375
- style,
376
- explode,
377
- } ) ,
368
+ serializeObjectParam ( name , value , { style, explode } ) ,
378
369
) ;
379
370
continue ;
380
371
}
381
372
if ( style === "matrix" ) {
382
373
nextURL = nextURL . replace (
383
374
match ,
384
- `;${ serializePrimitiveParam ( paramName , pathParams [ paramName ] ) } ` ,
375
+ `;${ serializePrimitiveParam ( name , value ) } ` ,
385
376
) ;
386
377
continue ;
387
378
}
388
- nextURL = nextURL . replace (
389
- match ,
390
- style === "label" ? `.${ pathParams [ paramName ] } ` : pathParams [ paramName ] ,
391
- ) ;
379
+ nextURL = nextURL . replace ( match , style === "label" ? `.${ value } ` : value ) ;
392
380
continue ;
393
381
}
394
382
return nextURL ;
@@ -411,9 +399,10 @@ export function createFinalURL(pathname, options) {
411
399
if ( options . params ?. path ) {
412
400
finalURL = defaultPathSerializer ( finalURL , options . params . path ) ;
413
401
}
414
- const search = options
415
- . querySerializer ( options . params . query ?? { } )
416
- . replace ( LEADING_QUESTION_RE , "" ) ;
402
+ let search = options . querySerializer ( options . params . query ?? { } ) ;
403
+ if ( search . startsWith ( "?" ) ) {
404
+ search = search . substring ( 1 ) ;
405
+ }
417
406
if ( search ) {
418
407
finalURL += `?${ search } ` ;
419
408
}
0 commit comments