Skip to content

Commit

Permalink
Merge d026242 into 34d1038
Browse files Browse the repository at this point in the history
  • Loading branch information
Badgerati committed Oct 10, 2019
2 parents 34d1038 + d026242 commit 2a83b0f
Showing 1 changed file with 82 additions and 15 deletions.
97 changes: 82 additions & 15 deletions src/Public/Responses.ps1
Expand Up @@ -124,6 +124,9 @@ The content type of the data being written.
.PARAMETER MaxAge
The maximum age to cache the value on the browser, in seconds.
.PARAMETER StatusCode
The status code to set against the response.
.PARAMETER Cache
Should the value be cached by browsers, or not?
Expand All @@ -135,6 +138,9 @@ Write-PodeTextResponse -Value '{"name": "Rick"}' -ContentType 'application/json'
.EXAMPLE
Write-PodeTextResponse -Bytes (Get-Content -Path ./some/image.png -Raw -AsByteStream) -Cache -MaxAge 1800
.EXAMPLE
Write-PodeTextResponse -Value 'Untitled Text Response' -StatusCode 418
#>
function Write-PodeTextResponse
{
Expand All @@ -156,13 +162,22 @@ function Write-PodeTextResponse
[int]
$MaxAge = 3600,

[Parameter()]
[int]
$StatusCode = 200,

[switch]
$Cache
)

$isStringValue = ($PSCmdlet.ParameterSetName -ieq 'string')
$isByteValue = ($PSCmdlet.ParameterSetName -ieq 'bytes')

# set the status code of the response, but only if it's not 200 (to prevent overriding)
if ($StatusCode -ne 200) {
Set-PodeResponseStatus -Code $StatusCode -NoErrorPage
}

# if there's nothing to write, return
if ($isStringValue -and [string]::IsNullOrWhiteSpace($Value)) {
return
Expand Down Expand Up @@ -254,6 +269,9 @@ The content type of the file's contents - this overrides the file's extension.
.PARAMETER MaxAge
The maximum age to cache the file's content on the browser, in seconds.
.PARAMETER StatusCode
The status code to set against the response.
.PARAMETER Cache
Should the file's content be cached by browsers, or not?
Expand All @@ -268,6 +286,9 @@ Write-PodeFileResponse -Path 'C:/Files/Stuff.txt' -ContentType 'application/json
.EXAMPLE
Write-PodeFileResponse -Path 'C:/Views/Index.pode' -Data @{ Counter = 2 }
.EXAMPLE
Write-PodeFileResponse -Path 'C:/Files/Stuff.txt' -StatusCode 201
#>
function Write-PodeFileResponse
{
Expand All @@ -289,6 +310,10 @@ function Write-PodeFileResponse
[int]
$MaxAge = 3600,

[Parameter()]
[int]
$StatusCode = 200,

[switch]
$Cache
)
Expand All @@ -313,7 +338,7 @@ function Write-PodeFileResponse
$subExt = (Protect-PodeValue -Value $subExt -Default $mainExt)

$ContentType = (Protect-PodeValue -Value $ContentType -Default (Get-PodeContentType -Extension $subExt))
Write-PodeTextResponse -Value $content -ContentType $ContentType
Write-PodeTextResponse -Value $content -ContentType $ContentType -StatusCode $StatusCode
}

# this is a static file
Expand All @@ -326,7 +351,7 @@ function Write-PodeFileResponse
}

$ContentType = (Protect-PodeValue -Value $ContentType -Default (Get-PodeContentType -Extension $mainExt))
Write-PodeTextResponse -Bytes $content -ContentType $ContentType -MaxAge $MaxAge -Cache:$Cache
Write-PodeTextResponse -Bytes $content -ContentType $ContentType -MaxAge $MaxAge -StatusCode $StatusCode -Cache:$Cache
}
}

Expand All @@ -343,6 +368,9 @@ A String, PSObject, or HashTable value.
.PARAMETER Path
The path to a CSV file.
.PARAMETER StatusCode
The status code to set against the response.
.EXAMPLE
Write-PodeCsvResponse -Value "Name`nRick"
Expand All @@ -361,7 +389,11 @@ function Write-PodeCsvResponse

[Parameter(Mandatory=$true, ParameterSetName='File')]
[string]
$Path
$Path,

[Parameter()]
[int]
$StatusCode = 200
)

switch ($PSCmdlet.ParameterSetName.ToLowerInvariant()) {
Expand Down Expand Up @@ -393,7 +425,7 @@ function Write-PodeCsvResponse
$Value = [string]::Empty
}

Write-PodeTextResponse -Value $Value -ContentType 'text/csv'
Write-PodeTextResponse -Value $Value -ContentType 'text/csv' -StatusCode $StatusCode
}

<#
Expand All @@ -409,6 +441,9 @@ A String, PSObject, or HashTable value.
.PARAMETER Path
The path to a HTML file.
.PARAMETER StatusCode
The status code to set against the response.
.EXAMPLE
Write-PodeHtmlResponse -Value '<html><body>Hello!</body></html>'
Expand All @@ -427,7 +462,11 @@ function Write-PodeHtmlResponse

[Parameter(Mandatory=$true, ParameterSetName='File')]
[string]
$Path
$Path,

[Parameter()]
[int]
$StatusCode = 200
)

switch ($PSCmdlet.ParameterSetName.ToLowerInvariant()) {
Expand All @@ -449,7 +488,7 @@ function Write-PodeHtmlResponse
$Value = [string]::Empty
}

Write-PodeTextResponse -Value $Value -ContentType 'text/html'
Write-PodeTextResponse -Value $Value -ContentType 'text/html' -StatusCode $StatusCode
}

<#
Expand All @@ -465,6 +504,9 @@ A String, PSObject, or HashTable value.
.PARAMETER Path
The path to a Markdown file.
.PARAMETER StatusCode
The status code to set against the response.
.PARAMETER AsHtml
If supplied, the Markdown will be converted to HTML. (This is only supported in PS7+)
Expand All @@ -485,6 +527,10 @@ function Write-PodeMarkdownResponse
[string]
$Path,

[Parameter()]
[int]
$StatusCode = 200,

[switch]
$AsHtml
)
Expand All @@ -510,7 +556,7 @@ function Write-PodeMarkdownResponse
}
}

Write-PodeTextResponse -Value $Value -ContentType $mimeType
Write-PodeTextResponse -Value $Value -ContentType $mimeType -StatusCode $StatusCode
}

<#
Expand All @@ -529,11 +575,14 @@ The path to a JSON file.
.PARAMETER Depth
The Depth to generate the JSON document - the larger this value the worse performance gets.
.PARAMETER StatusCode
The status code to set against the response.
.EXAMPLE
Write-PodeJsonResponse -Value '{"name": "Rick"}'
.EXAMPLE
Write-PodeJsonResponse -Value @{ Name = 'Rick' }
Write-PodeJsonResponse -Value @{ Name = 'Rick' } -StatusCode 201
.EXAMPLE
Write-PodeJsonResponse -Path 'E:/Files/Names.json'
Expand All @@ -551,7 +600,11 @@ function Write-PodeJsonResponse

[Parameter()]
[int]
$Depth = 10
$Depth = 10,

[Parameter()]
[int]
$StatusCode = 200
)

switch ($PSCmdlet.ParameterSetName.ToLowerInvariant()) {
Expand All @@ -577,7 +630,7 @@ function Write-PodeJsonResponse
$Value = '{}'
}

Write-PodeTextResponse -Value $Value -ContentType 'application/json'
Write-PodeTextResponse -Value $Value -ContentType 'application/json' -StatusCode $StatusCode
}

<#
Expand All @@ -593,11 +646,14 @@ A String, PSObject, or HashTable value.
.PARAMETER Path
The path to an XML file.
.PARAMETER StatusCode
The status code to set against the response.
.EXAMPLE
Write-PodeXmlResponse -Value '<root><name>Rick</name></root>'
.EXAMPLE
Write-PodeXmlResponse -Value @{ Name = 'Rick' }
Write-PodeXmlResponse -Value @{ Name = 'Rick' } -StatusCode 201
.EXAMPLE
Write-PodeXmlResponse -Path 'E:/Files/Names.xml'
Expand All @@ -611,7 +667,11 @@ function Write-PodeXmlResponse

[Parameter(Mandatory=$true, ParameterSetName='File')]
[string]
$Path
$Path,

[Parameter()]
[int]
$StatusCode = 200
)

switch ($PSCmdlet.ParameterSetName.ToLowerInvariant()) {
Expand All @@ -636,7 +696,7 @@ function Write-PodeXmlResponse
$Value = [string]::Empty
}

Write-PodeTextResponse -Value $Value -ContentType 'text/xml'
Write-PodeTextResponse -Value $Value -ContentType 'text/xml' -StatusCode $StatusCode
}

<#
Expand All @@ -652,6 +712,9 @@ The path to a View, relative to the "/views" directory. (Extension is optional).
.PARAMETER Data
Any dynamic data to supply to a dynamic View.
.PARAMETER StatusCode
The status code to set against the response.
.PARAMETER FlashMessages
Automatically supply all Flash messages in the current session to the View.
Expand All @@ -676,6 +739,10 @@ function Write-PodeViewResponse
[hashtable]
$Data = @{},

[Parameter()]
[int]
$StatusCode = 200,

[switch]
$FlashMessages
)
Expand Down Expand Up @@ -722,11 +789,11 @@ function Write-PodeViewResponse

switch ($engine.ToLowerInvariant()) {
'md' {
Write-PodeMarkdownResponse -Value $value -AsHtml
Write-PodeMarkdownResponse -Value $value -StatusCode $StatusCode -AsHtml
}

default {
Write-PodeHtmlResponse -Value $value
Write-PodeHtmlResponse -Value $value -StatusCode $StatusCode
}
}
}
Expand Down

0 comments on commit 2a83b0f

Please sign in to comment.