Skip to content

Commit b5384b4

Browse files
committed
fix: encode query parameters with encodeURIComponent
URLSearchParams encodes literal space (' ') as '+' which is according to spec. However, some proxies, for example envoy does not currently support decoding '+' to ' ', which means that spaces can not be sent as query parameters.
1 parent 790a0ef commit b5384b4

File tree

3 files changed

+97
-150
lines changed

3 files changed

+97
-150
lines changed

examples/proto/gen/typescript/einride/example/freight/v1/index.ts

Lines changed: 64 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -372,15 +372,13 @@ export function createFreightServiceClient(
372372
}
373373
const path = `v1/${request.name}`; // eslint-disable-line quotes
374374
const body = null;
375-
const query = new URLSearchParams();
376-
let hasQuery = false; // eslint-disable-line prefer-const
375+
const queryParams: string[] = [];
377376
if (request.name) {
378-
hasQuery = true;
379-
query.set("name", request.name.toString());
377+
queryParams.push("name=" + encodeURIComponent(request.name.toString()));
380378
}
381379
let uri = path;
382-
if (hasQuery) {
383-
uri += "?" + query.toString();
380+
if (queryParams.length > 0) {
381+
uri += "?" + queryParams.join("&");
384382
}
385383
return handler({
386384
path: uri,
@@ -391,19 +389,16 @@ export function createFreightServiceClient(
391389
ListShippers(request) {
392390
const path = `v1/shippers`; // eslint-disable-line quotes
393391
const body = null;
394-
const query = new URLSearchParams();
395-
let hasQuery = false; // eslint-disable-line prefer-const
392+
const queryParams: string[] = [];
396393
if (request.pageSize) {
397-
hasQuery = true;
398-
query.set("pageSize", request.pageSize.toString());
394+
queryParams.push("pageSize=" + encodeURIComponent(request.pageSize.toString()));
399395
}
400396
if (request.pageToken) {
401-
hasQuery = true;
402-
query.set("pageToken", request.pageToken.toString());
397+
queryParams.push("pageToken=" + encodeURIComponent(request.pageToken.toString()));
403398
}
404399
let uri = path;
405-
if (hasQuery) {
406-
uri += "?" + query.toString();
400+
if (queryParams.length > 0) {
401+
uri += "?" + queryParams.join("&");
407402
}
408403
return handler({
409404
path: uri,
@@ -414,11 +409,10 @@ export function createFreightServiceClient(
414409
CreateShipper(request) {
415410
const path = `v1/shippers`; // eslint-disable-line quotes
416411
const body = JSON.stringify(request?.shipper ?? {});
417-
const query = new URLSearchParams();
418-
let hasQuery = false; // eslint-disable-line prefer-const
412+
const queryParams: string[] = [];
419413
let uri = path;
420-
if (hasQuery) {
421-
uri += "?" + query.toString();
414+
if (queryParams.length > 0) {
415+
uri += "?" + queryParams.join("&");
422416
}
423417
return handler({
424418
path: uri,
@@ -432,15 +426,13 @@ export function createFreightServiceClient(
432426
}
433427
const path = `v1/${request.shipper.name}`; // eslint-disable-line quotes
434428
const body = JSON.stringify(request?.shipper ?? {});
435-
const query = new URLSearchParams();
436-
let hasQuery = false; // eslint-disable-line prefer-const
429+
const queryParams: string[] = [];
437430
if (request.updateMask) {
438-
hasQuery = true;
439-
query.set("updateMask", request.updateMask.toString());
431+
queryParams.push("updateMask=" + encodeURIComponent(request.updateMask.toString()));
440432
}
441433
let uri = path;
442-
if (hasQuery) {
443-
uri += "?" + query.toString();
434+
if (queryParams.length > 0) {
435+
uri += "?" + queryParams.join("&");
444436
}
445437
return handler({
446438
path: uri,
@@ -454,15 +446,13 @@ export function createFreightServiceClient(
454446
}
455447
const path = `v1/${request.name}`; // eslint-disable-line quotes
456448
const body = null;
457-
const query = new URLSearchParams();
458-
let hasQuery = false; // eslint-disable-line prefer-const
449+
const queryParams: string[] = [];
459450
if (request.name) {
460-
hasQuery = true;
461-
query.set("name", request.name.toString());
451+
queryParams.push("name=" + encodeURIComponent(request.name.toString()));
462452
}
463453
let uri = path;
464-
if (hasQuery) {
465-
uri += "?" + query.toString();
454+
if (queryParams.length > 0) {
455+
uri += "?" + queryParams.join("&");
466456
}
467457
return handler({
468458
path: uri,
@@ -476,15 +466,13 @@ export function createFreightServiceClient(
476466
}
477467
const path = `v1/${request.name}`; // eslint-disable-line quotes
478468
const body = null;
479-
const query = new URLSearchParams();
480-
let hasQuery = false; // eslint-disable-line prefer-const
469+
const queryParams: string[] = [];
481470
if (request.name) {
482-
hasQuery = true;
483-
query.set("name", request.name.toString());
471+
queryParams.push("name=" + encodeURIComponent(request.name.toString()));
484472
}
485473
let uri = path;
486-
if (hasQuery) {
487-
uri += "?" + query.toString();
474+
if (queryParams.length > 0) {
475+
uri += "?" + queryParams.join("&");
488476
}
489477
return handler({
490478
path: uri,
@@ -498,23 +486,19 @@ export function createFreightServiceClient(
498486
}
499487
const path = `v1/${request.parent}/sites`; // eslint-disable-line quotes
500488
const body = null;
501-
const query = new URLSearchParams();
502-
let hasQuery = false; // eslint-disable-line prefer-const
489+
const queryParams: string[] = [];
503490
if (request.parent) {
504-
hasQuery = true;
505-
query.set("parent", request.parent.toString());
491+
queryParams.push("parent=" + encodeURIComponent(request.parent.toString()));
506492
}
507493
if (request.pageSize) {
508-
hasQuery = true;
509-
query.set("pageSize", request.pageSize.toString());
494+
queryParams.push("pageSize=" + encodeURIComponent(request.pageSize.toString()));
510495
}
511496
if (request.pageToken) {
512-
hasQuery = true;
513-
query.set("pageToken", request.pageToken.toString());
497+
queryParams.push("pageToken=" + encodeURIComponent(request.pageToken.toString()));
514498
}
515499
let uri = path;
516-
if (hasQuery) {
517-
uri += "?" + query.toString();
500+
if (queryParams.length > 0) {
501+
uri += "?" + queryParams.join("&");
518502
}
519503
return handler({
520504
path: uri,
@@ -528,15 +512,13 @@ export function createFreightServiceClient(
528512
}
529513
const path = `v1/${request.parent}/sites`; // eslint-disable-line quotes
530514
const body = JSON.stringify(request?.site ?? {});
531-
const query = new URLSearchParams();
532-
let hasQuery = false; // eslint-disable-line prefer-const
515+
const queryParams: string[] = [];
533516
if (request.parent) {
534-
hasQuery = true;
535-
query.set("parent", request.parent.toString());
517+
queryParams.push("parent=" + encodeURIComponent(request.parent.toString()));
536518
}
537519
let uri = path;
538-
if (hasQuery) {
539-
uri += "?" + query.toString();
520+
if (queryParams.length > 0) {
521+
uri += "?" + queryParams.join("&");
540522
}
541523
return handler({
542524
path: uri,
@@ -550,15 +532,13 @@ export function createFreightServiceClient(
550532
}
551533
const path = `v1/${request.site.name}`; // eslint-disable-line quotes
552534
const body = JSON.stringify(request?.site ?? {});
553-
const query = new URLSearchParams();
554-
let hasQuery = false; // eslint-disable-line prefer-const
535+
const queryParams: string[] = [];
555536
if (request.updateMask) {
556-
hasQuery = true;
557-
query.set("updateMask", request.updateMask.toString());
537+
queryParams.push("updateMask=" + encodeURIComponent(request.updateMask.toString()));
558538
}
559539
let uri = path;
560-
if (hasQuery) {
561-
uri += "?" + query.toString();
540+
if (queryParams.length > 0) {
541+
uri += "?" + queryParams.join("&");
562542
}
563543
return handler({
564544
path: uri,
@@ -572,15 +552,13 @@ export function createFreightServiceClient(
572552
}
573553
const path = `v1/${request.name}`; // eslint-disable-line quotes
574554
const body = null;
575-
const query = new URLSearchParams();
576-
let hasQuery = false; // eslint-disable-line prefer-const
555+
const queryParams: string[] = [];
577556
if (request.name) {
578-
hasQuery = true;
579-
query.set("name", request.name.toString());
557+
queryParams.push("name=" + encodeURIComponent(request.name.toString()));
580558
}
581559
let uri = path;
582-
if (hasQuery) {
583-
uri += "?" + query.toString();
560+
if (queryParams.length > 0) {
561+
uri += "?" + queryParams.join("&");
584562
}
585563
return handler({
586564
path: uri,
@@ -594,15 +572,13 @@ export function createFreightServiceClient(
594572
}
595573
const path = `v1/${request.name}`; // eslint-disable-line quotes
596574
const body = null;
597-
const query = new URLSearchParams();
598-
let hasQuery = false; // eslint-disable-line prefer-const
575+
const queryParams: string[] = [];
599576
if (request.name) {
600-
hasQuery = true;
601-
query.set("name", request.name.toString());
577+
queryParams.push("name=" + encodeURIComponent(request.name.toString()));
602578
}
603579
let uri = path;
604-
if (hasQuery) {
605-
uri += "?" + query.toString();
580+
if (queryParams.length > 0) {
581+
uri += "?" + queryParams.join("&");
606582
}
607583
return handler({
608584
path: uri,
@@ -616,23 +592,19 @@ export function createFreightServiceClient(
616592
}
617593
const path = `v1/${request.parent}/shipments`; // eslint-disable-line quotes
618594
const body = null;
619-
const query = new URLSearchParams();
620-
let hasQuery = false; // eslint-disable-line prefer-const
595+
const queryParams: string[] = [];
621596
if (request.parent) {
622-
hasQuery = true;
623-
query.set("parent", request.parent.toString());
597+
queryParams.push("parent=" + encodeURIComponent(request.parent.toString()));
624598
}
625599
if (request.pageSize) {
626-
hasQuery = true;
627-
query.set("pageSize", request.pageSize.toString());
600+
queryParams.push("pageSize=" + encodeURIComponent(request.pageSize.toString()));
628601
}
629602
if (request.pageToken) {
630-
hasQuery = true;
631-
query.set("pageToken", request.pageToken.toString());
603+
queryParams.push("pageToken=" + encodeURIComponent(request.pageToken.toString()));
632604
}
633605
let uri = path;
634-
if (hasQuery) {
635-
uri += "?" + query.toString();
606+
if (queryParams.length > 0) {
607+
uri += "?" + queryParams.join("&");
636608
}
637609
return handler({
638610
path: uri,
@@ -646,15 +618,13 @@ export function createFreightServiceClient(
646618
}
647619
const path = `v1/${request.parent}/shipments`; // eslint-disable-line quotes
648620
const body = JSON.stringify(request?.shipment ?? {});
649-
const query = new URLSearchParams();
650-
let hasQuery = false; // eslint-disable-line prefer-const
621+
const queryParams: string[] = [];
651622
if (request.parent) {
652-
hasQuery = true;
653-
query.set("parent", request.parent.toString());
623+
queryParams.push("parent=" + encodeURIComponent(request.parent.toString()));
654624
}
655625
let uri = path;
656-
if (hasQuery) {
657-
uri += "?" + query.toString();
626+
if (queryParams.length > 0) {
627+
uri += "?" + queryParams.join("&");
658628
}
659629
return handler({
660630
path: uri,
@@ -668,15 +638,13 @@ export function createFreightServiceClient(
668638
}
669639
const path = `v1/${request.shipment.name}`; // eslint-disable-line quotes
670640
const body = JSON.stringify(request?.shipment ?? {});
671-
const query = new URLSearchParams();
672-
let hasQuery = false; // eslint-disable-line prefer-const
641+
const queryParams: string[] = [];
673642
if (request.updateMask) {
674-
hasQuery = true;
675-
query.set("updateMask", request.updateMask.toString());
643+
queryParams.push("updateMask=" + encodeURIComponent(request.updateMask.toString()));
676644
}
677645
let uri = path;
678-
if (hasQuery) {
679-
uri += "?" + query.toString();
646+
if (queryParams.length > 0) {
647+
uri += "?" + queryParams.join("&");
680648
}
681649
return handler({
682650
path: uri,
@@ -690,15 +658,13 @@ export function createFreightServiceClient(
690658
}
691659
const path = `v1/${request.name}`; // eslint-disable-line quotes
692660
const body = null;
693-
const query = new URLSearchParams();
694-
let hasQuery = false; // eslint-disable-line prefer-const
661+
const queryParams: string[] = [];
695662
if (request.name) {
696-
hasQuery = true;
697-
query.set("name", request.name.toString());
663+
queryParams.push("name=" + encodeURIComponent(request.name.toString()));
698664
}
699665
let uri = path;
700-
if (hasQuery) {
701-
uri += "?" + query.toString();
666+
if (queryParams.length > 0) {
667+
uri += "?" + queryParams.join("&");
702668
}
703669
return handler({
704670
path: uri,

0 commit comments

Comments
 (0)