| | | 1 | | <# |
| | | 2 | | .SYNOPSIS |
| | | 3 | | Writes CSV data to the HTTP response body. |
| | | 4 | | .DESCRIPTION |
| | | 5 | | Sends a raw CSV payload to the client and optionally sets the HTTP status |
| | | 6 | | code and content type. |
| | | 7 | | .PARAMETER InputObject |
| | | 8 | | The CSV content to write to the response body. This can be a string or any |
| | | 9 | | other object that can be converted to a string. |
| | | 10 | | .PARAMETER StatusCode |
| | | 11 | | The HTTP status code to set for the response. Defaults to 200 (OK). |
| | | 12 | | .PARAMETER ContentType |
| | | 13 | | The content type of the response. If not specified, defaults to "text/csv". |
| | | 14 | | .PARAMETER Delimiter |
| | | 15 | | The character to use as the delimiter in the CSV output. Defaults to a comma (`,`). |
| | | 16 | | .PARAMETER IncludeTypeInformation |
| | | 17 | | Switch to include type information in the CSV output. |
| | | 18 | | .PARAMETER QuoteFields |
| | | 19 | | An array of field names to always quote in the CSV output. |
| | | 20 | | .PARAMETER UseQuotes |
| | | 21 | | Specifies how to quote fields in the CSV output. Accepts values from the |
| | | 22 | | `Microsoft.PowerShell.Commands.BaseCsvWritingCommand+QuoteKind` enum. |
| | | 23 | | .PARAMETER NoHeader |
| | | 24 | | Switch to omit the header row from the CSV output. |
| | | 25 | | .EXAMPLE |
| | | 26 | | Write-KrCsvResponse -InputObject "Name,Age`nAlice,30`nBob,25" -StatusCode 200 |
| | | 27 | | Writes the CSV data to the response body with a 200 OK status code. |
| | | 28 | | .NOTES |
| | | 29 | | This function is designed to be used in the context of a Kestrun server response. |
| | | 30 | | #> |
| | | 31 | | function Write-KrCsvResponse { |
| | | 32 | | [KestrunRuntimeApi('Route')] |
| | | 33 | | [CmdletBinding()] |
| | | 34 | | param( |
| | | 35 | | [Parameter(Mandatory = $true, ValueFromPipeline = $true)] |
| | | 36 | | [object]$InputObject, |
| | | 37 | | [Parameter()] |
| | | 38 | | [int]$StatusCode = 200, |
| | | 39 | | [Parameter()] |
| | | 40 | | [string]$ContentType, |
| | | 41 | | [Parameter()] |
| | | 42 | | [char]$Delimiter, |
| | | 43 | | [Parameter()] |
| | | 44 | | [switch]$IncludeTypeInformation, |
| | | 45 | | [Parameter()] |
| | | 46 | | [string[]]$QuoteFields, |
| | | 47 | | [Parameter()] |
| | | 48 | | [Microsoft.PowerShell.Commands.BaseCsvWritingCommand+QuoteKind]$UseQuotes, |
| | | 49 | | [Parameter()] |
| | | 50 | | [switch]$NoHeader |
| | | 51 | | ) |
| | | 52 | | begin { |
| | | 53 | | # Collect all piped items |
| | 0 | 54 | | $items = @() |
| | 0 | 55 | | $ContentType = [string]::IsNullOrEmpty($ContentType) ? 'text/csv' : $ContentType |
| | | 56 | | } |
| | | 57 | | process { |
| | | 58 | | # Accumulate; no output yet |
| | 0 | 59 | | $items += $InputObject |
| | | 60 | | } |
| | | 61 | | end { |
| | | 62 | | # Only works inside a route script block where $Context is available |
| | 0 | 63 | | if ($null -eq $Context -or $null -eq $Context.Response) { |
| | 0 | 64 | | Write-KrOutsideRouteWarning |
| | | 65 | | return |
| | | 66 | | } |
| | | 67 | | # - single item by default when only one was piped |
| | | 68 | | # - array if multiple items were piped |
| | | 69 | | |
| | 0 | 70 | | $params = @{} |
| | 0 | 71 | | if ($Delimiter) { $params['Delimiter'] = $Delimiter } |
| | 0 | 72 | | if ($IncludeTypeInformation.IsPresent) { $params['IncludeTypeInformation'] = $true } |
| | 0 | 73 | | if ($QuoteFields) { $params['QuoteFields'] = $QuoteFields } |
| | 0 | 74 | | if ($UseQuotes) { $params['UseQuotes'] = $UseQuotes } |
| | 0 | 75 | | if ($NoHeader.IsPresent) { $params['NoHeader'] = $true } |
| | | 76 | | |
| | | 77 | | # Convert the payload to CSV |
| | 0 | 78 | | $csv = $items | ConvertTo-Csv @params |
| | | 79 | | # Write the CSV response |
| | 0 | 80 | | $Context.Response.WriteTextResponse($csv -join "`n", $StatusCode, $ContentType) |
| | | 81 | | } |
| | | 82 | | } |
| | | 83 | | |