Badgerati / Pode

Pode is a Cross-Platform PowerShell web framework for creating REST APIs, Web Sites, and TCP/SMTP servers
https://badgerati.github.io/Pode
MIT License
830 stars 92 forks source link

Add Functions for Handling Route WebEvent Data and Serialization in Pode #1353

Open mdaneri opened 2 months ago

mdaneri commented 2 months ago

Summary

This pull request adds several new functions to the Pode project for handling web event data and for serializing/deserializing hashtables. The new functions are:

New Functions

Get-PodeBody

Retrieves the body data from the current Pode web event. This function is useful for accessing the main content sent in a web request, including PUT, POST, or any other methods that support a body.

Example:

Get-PodeBody

# This command returns the body data of the current web route. Equivalent to `$WebEvent.Data`

Get-PodeQuery

Retrieves a specific query parameter value from the current Pode web event. This function is useful for accessing query parameters passed in the URL of a web request.

Example:

Get-PodeQuery -Name 'userId'

# This command returns the value of the 'userId' query parameter from the current web route. Equivalent to `$WebEvent.Query['userId']`

Get-PodeParameter

Retrieves a specific parameter value from the current Pode web event. This function is useful for accessing parameters passed in the body or URL of a web request.

Example:

Get-PodeParameter -Name 'action'

# This command returns the value of the 'action' parameter from the current web route. Equivalent to `$WebEvent.Parameters['action']`

ConvertTo-PodeSerializedString

Converts a hashtable to a serialized string using a specified serialization style. It supports various serialization styles such as 'Simple', 'Label', 'Matrix', 'Query', 'Form', 'SpaceDelimited', 'PipeDelimited', and 'DeepObject'. An optional 'Explode' switch can be used to modify the serialization format for certain styles.

This function is useful for example for callback scenarios.

Examples:

$hashtable = @{
    name = 'value'
    anotherName = 'anotherValue'
}
$serialized = ConvertTo-PodeSerializedString -Hashtable $hashtable -Style 'Query'
Write-Output $serialized

# This command serializes the hashtable using the 'Query' style.
$hashtable = @{
    name = 'value'
    anotherName = 'anotherValue'
}
$serializedExplode = ConvertTo-PodeSerializedString -Hashtable $hashtable -Style 'DeepObject' -Explode
Write-Output $serializedExplode

# This command serializes the hashtable using the 'DeepObject' style with the 'Explode' switch.

ConvertFrom-PodeSerializedString

Converts a serialized string back into a hashtable, automatically detecting the serialization style based on common delimiters and formats.

This function is useful when receiving complex data as a parameter without using JSON, YAML, or XML encoding.

Examples:

# Simple style
$serialized = "name=value,anotherName=anotherValue"
$hashtable = ConvertFrom-PodeSerializedString -SerializedString $serialized
Write-Output $hashtable

# This command deserializes the string using the 'Simple' style.
# Label style
$serialized = ".name.value.anotherName.anotherValue"
$hashtable = ConvertFrom-PodeSerializedString -SerializedString $serialized
Write-Output $hashtable

# This command deserializes the string using the 'Label' style.
# Query style
$serialized = "name=value&anotherName=anotherValue"
$hashtable = ConvertFrom-PodeSerializedString -SerializedString $serialized
Write-Output $hashtable

# This command deserializes the string using the 'Query' style.
mdaneri commented 2 months ago

Still missing documentation

mdaneri commented 1 month ago

Documentation is here now I'm unsure whether to keep the ConvertTo-PodeSerializedString and ConvertFrom-PodeSerializedString. I wrote them because I was thinking of using them for the async routes, but in the end, I don't need them. What do you think are those functions useful?