Skip to content

Commit 24b3454

Browse files
author
James Brundage
committed
Updating Rest Transpiler: Handling multiple QueryString values (#139)
1 parent 4587e5a commit 24b3454

File tree

1 file changed

+32
-6
lines changed

1 file changed

+32
-6
lines changed

Transpilers/Rest.psx.ps1

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,11 @@ If a parameter is a [switch], it will be turned into a [bool].
137137
[PSObject]
138138
$QueryParameter,
139139

140+
# If provided, will join multiple values of a query by this string.
141+
# If the string is '&', each value will be provided as a key-value pair.
142+
[string]
143+
$JoinQueryValue,
144+
140145
# A script block to be run on each output.
141146
[ScriptBlock]
142147
$ForEachOutput
@@ -274,7 +279,7 @@ process {
274279
begin {
275280
$myCmd = $MyInvocation.MyCommand
276281
function ConvertRestInput {
277-
param([Collections.IDictionary]$RestInput = @{})
282+
param([Collections.IDictionary]$RestInput = @{}, [switch]$ToQueryString)
278283
foreach ($ri in @($RestInput.GetEnumerator())) {
279284
$RestParameterAttributes = @($myCmd.Parameters[$ri.Key].Attributes)
280285
$restParameterName = $ri.Key
@@ -297,7 +302,14 @@ process {
297302
$restParameterValue -as [bool]
298303
}
299304
else {
300-
$restParameterValue
305+
if ($ToQueryString -and
306+
$restParameterValue -is [Array] -and
307+
$JoinQueryValue) {
308+
$restParameterValue -join $JoinQueryValue
309+
} else {
310+
$restParameterValue
311+
}
312+
301313
}
302314

303315
if ($restParameterValue -is [Collections.IDictionary]) {
@@ -320,7 +332,7 @@ process {
320332
{
321333
begin {
322334
function ConvertRestInput {
323-
param([Collections.IDictionary]$RestInput = @{})
335+
param([Collections.IDictionary]$RestInput = @{}, [switch]$ToQueryString)
324336
foreach ($ri in @($RestInput.GetEnumerator())) {
325337

326338
$restParameterValue = $ri.Value
@@ -332,7 +344,13 @@ process {
332344
$restParameterValue -as [bool]
333345
}
334346
else {
335-
$restParameterValue
347+
if ($ToQueryString -and
348+
$restParameterValue -is [Array] -and
349+
$JoinQueryValue) {
350+
$restParameterValue -join $JoinQueryValue
351+
} else {
352+
$restParameterValue
353+
}
336354
}
337355

338356
$RestInput[$ri.Key] = $restParameterValue
@@ -373,6 +391,7 @@ process {
373391
`$contentType = '$contentType'
374392
`$bodyParameterNames = @('$($bodyParameterNames -join "','")')
375393
`$queryParameterNames = @('$($queryParameterNames -join "','")')
394+
`$joinQueryValue = '$joinQueryValue'
376395
`$uriParameterNames = @('$($uriParameterNames -join "','")')
377396
`$endpoints = @("$($endpoint -join "','")")
378397
`$ForEachOutput = {
@@ -472,15 +491,22 @@ process {
472491
}
473492
{
474493
process {
475-
$queryParams = ConvertRestInput $queryParams
494+
$queryParams = ConvertRestInput $queryParams -ToQueryString
476495

477496
if ($invokerCommandinfo.Parameters['QueryParameter'] -and
478497
$invokerCommandinfo.Parameters['QueryParameter'].ParameterType -eq [Collections.IDictionary]) {
479498
$invokeSplat.QueryParameter = $QueryParams
480499
} else {
481500
$queryParamStr =
482501
@(foreach ($qp in $QueryParams.GetEnumerator()) {
483-
"$($qp.Key)=$([Web.HttpUtility]::UrlEncode($qpValue).Replace('+', '%20'))"
502+
$qpValue = $qp.value
503+
if ($JoinQueryValue -eq '&') {
504+
foreach ($qVal in $qpValue -split '&') {
505+
"$($qp.Key)=$([Web.HttpUtility]::UrlEncode($qValue).Replace('+', '%20'))"
506+
}
507+
} else {
508+
"$($qp.Key)=$([Web.HttpUtility]::UrlEncode($qpValue).Replace('+', '%20'))"
509+
}
484510
}) -join '&'
485511
if ($invokeSplat.Uri.Contains('?')) {
486512
$invokeSplat.Uri = "$($invokeSplat.Uri)" + '&' + $queryParamStr

0 commit comments

Comments
 (0)