| | | 1 | | <# |
| | | 2 | | .SYNOPSIS |
| | | 3 | | Retrieves a request route value from the HTTP request. |
| | | 4 | | .DESCRIPTION |
| | | 5 | | This function accesses the current HTTP request context and retrieves the value |
| | | 6 | | of the specified request route value by name. |
| | | 7 | | .PARAMETER Name |
| | | 8 | | The name of the request route value to retrieve from the HTTP request. |
| | | 9 | | .PARAMETER AsInt |
| | | 10 | | If specified, converts the route value to an integer. |
| | | 11 | | .PARAMETER AsBool |
| | | 12 | | If specified, converts the route value to a boolean. |
| | | 13 | | .PARAMETER AsDouble |
| | | 14 | | If specified, converts the route value to a double. |
| | | 15 | | .PARAMETER AsString |
| | | 16 | | If specified, converts the route value to a string. |
| | | 17 | | .PARAMETER ThrowIfMissing |
| | | 18 | | If specified, throws an error if the route value is not found. |
| | | 19 | | By default, it returns $null if not found. |
| | | 20 | | .EXAMPLE |
| | | 21 | | $value = Get-KrRequestRouteParam -Name "param1" |
| | | 22 | | Retrieves the value of the request route value "param1" from the HTTP request. |
| | | 23 | | .EXAMPLE |
| | | 24 | | $id = Get-KrRequestRouteParam -Name "id" -AsInt -ThrowIfMissing |
| | | 25 | | Retrieves the value of the route parameter "id" as an integer, throwing an error if it's missing. |
| | | 26 | | .EXAMPLE |
| | | 27 | | $flag = Get-KrRequestRouteParam -Name "flag" -AsBool |
| | | 28 | | Retrieves the value of the route parameter "flag" as a boolean. |
| | | 29 | | .OUTPUTS |
| | | 30 | | Returns the value of the specified request route value, or $null if not found. |
| | | 31 | | .NOTES |
| | | 32 | | This function is designed to be used in the context of a Kestrun server response. |
| | | 33 | | #> |
| | | 34 | | function Get-KrRequestRouteParam { |
| | | 35 | | [KestrunRuntimeApi('Route')] |
| | | 36 | | [CmdletBinding(defaultParameterSetName = 'default')] |
| | | 37 | | [OutputType([string])] |
| | | 38 | | [OutputType([int])] |
| | | 39 | | [OutputType([bool])] |
| | | 40 | | [OutputType([double])] |
| | | 41 | | [OutputType([object])] |
| | | 42 | | param( |
| | | 43 | | [Parameter(Mandatory = $true)] |
| | | 44 | | [string]$Name, |
| | | 45 | | |
| | | 46 | | [Parameter(parameterSetName = 'Int')] |
| | | 47 | | [switch]$AsInt, |
| | | 48 | | |
| | | 49 | | [Parameter(parameterSetName = 'Bool')] |
| | | 50 | | [switch]$AsBool, |
| | | 51 | | |
| | | 52 | | [Parameter(parameterSetName = 'Double')] |
| | | 53 | | [switch]$AsDouble, |
| | | 54 | | |
| | | 55 | | [Parameter(parameterSetName = 'String')] |
| | | 56 | | [switch]$AsString, |
| | | 57 | | |
| | | 58 | | [Parameter()] |
| | | 59 | | [switch]$ThrowIfMissing |
| | | 60 | | ) |
| | 0 | 61 | | if ($null -ne $Context -and $null -ne $Context.Request -and $null -ne $Context.Request.RouteValues) { |
| | 0 | 62 | | if ($ThrowIfMissing -and -not $Context.Request.RouteValues.ContainsKey($Name)) { |
| | 0 | 63 | | throw [System.ArgumentException]::new("Missing required route parameter: $Name") |
| | | 64 | | } |
| | | 65 | | # Get the route parameter value from the request |
| | 0 | 66 | | $value = $Context.Request.RouteValues[$Name] |
| | | 67 | | |
| | 0 | 68 | | if ($AsInt) { |
| | 0 | 69 | | return [int]$value |
| | | 70 | | } |
| | 0 | 71 | | if ($AsBool) { |
| | 0 | 72 | | return [bool]$value |
| | | 73 | | } |
| | 0 | 74 | | if ($AsDouble) { |
| | 0 | 75 | | return [double]$value |
| | | 76 | | } |
| | 0 | 77 | | if ($AsString) { |
| | 0 | 78 | | return $value.ToString() |
| | | 79 | | } |
| | 0 | 80 | | return $value |
| | | 81 | | } else { |
| | | 82 | | # Outside of route context |
| | 0 | 83 | | Write-KrOutsideRouteWarning |
| | | 84 | | } |
| | | 85 | | } |