From 4ccf76aaec34ec69577488699af33b6595687d23 Mon Sep 17 00:00:00 2001 From: Tyas Lambrechts <10156034+eltyBelgium@users.noreply.github.com> Date: Fri, 7 Jun 2024 22:38:35 +0200 Subject: [PATCH 1/5] Update Routes.ps1 add filepath to PodeRouteGroup --- src/Public/Routes.ps1 | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/Public/Routes.ps1 b/src/Public/Routes.ps1 index 6eb919972..9f25605d0 100644 --- a/src/Public/Routes.ps1 +++ b/src/Public/Routes.ps1 @@ -1164,11 +1164,22 @@ function Add-PodeRouteGroup { $AllowAnon, [string[]] - $OADefinitionTag + $OADefinitionTag, + + [Parameter(Mandatory = $true, ParameterSetName = 'file')] + [string] + $FilePath ) + + if (Test-PodeIsEmpty $Routes) { + if ($PSCmdlet.ParameterSetName -ieq 'file') { + $Routes = Convert-PodeFileToScriptBlock -FilePath $FilePath + }else{ + throw 'No scriptblock for -Routes passed' + } } if ($Path -eq '/') { @@ -2759,4 +2770,4 @@ function Test-PodeSignalRoute { # check for routes return (Test-PodeRouteInternal -Method $Method -Path $Path -Protocol $endpoint.Protocol -Address $endpoint.Address) -} \ No newline at end of file +} From 824c999ec53838b56073ac9ce20bb634fdddf81c Mon Sep 17 00:00:00 2001 From: Tyas Lambrechts Date: Fri, 7 Jun 2024 22:55:04 +0200 Subject: [PATCH 2/5] Tests are Ok, Docs are Ok. Close issue #1329 --- src/Public/Routes.ps1 | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/Public/Routes.ps1 b/src/Public/Routes.ps1 index 9f25605d0..a5f6fe207 100644 --- a/src/Public/Routes.ps1 +++ b/src/Public/Routes.ps1 @@ -1095,8 +1095,13 @@ An Array of strings representing the unique tag for the API specification. This tag helps in distinguishing between different versions or types of API specifications within the application. You can use this tag to reference the specific API documentation, schema, or version that your function interacts with. +.PARAMETER FilePath +A literal, or relative, path to a file containing a ScriptBlock for the Route's main logic. + .EXAMPLE Add-PodeRouteGroup -Path '/api' -Routes { Add-PodeRoute -Path '/route1' -Etc } +.EXAMPLE +Add-PodeRouteGroup -Path '/api' -FilePath '/routes/file.ps1' #> function Add-PodeRouteGroup { [CmdletBinding()] @@ -1171,15 +1176,16 @@ function Add-PodeRouteGroup { $FilePath ) - + if (Test-PodeIsEmpty $Routes) { - if ($PSCmdlet.ParameterSetName -ieq 'file') { - $Routes = Convert-PodeFileToScriptBlock -FilePath $FilePath - }else{ - - throw 'No scriptblock for -Routes passed' - } + if ($PSCmdlet.ParameterSetName -ieq 'file') { + $Routes = Convert-PodeFileToScriptBlock -FilePath $FilePath + } + else { + + throw 'No scriptblock for -Routes passed' + } } if ($Path -eq '/') { From 6cf141016999c2f9937f502cd361ea78bcacd360 Mon Sep 17 00:00:00 2001 From: Tyas Lambrechts Date: Tue, 11 Jun 2024 10:32:46 +0200 Subject: [PATCH 3/5] feat: Add FilePath parameter to Add-PodeSignalRouteGroup function --- src/Public/Routes.ps1 | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/Public/Routes.ps1 b/src/Public/Routes.ps1 index a5f6fe207..0340cfa02 100644 --- a/src/Public/Routes.ps1 +++ b/src/Public/Routes.ps1 @@ -1100,6 +1100,7 @@ A literal, or relative, path to a file containing a ScriptBlock for the Route's .EXAMPLE Add-PodeRouteGroup -Path '/api' -Routes { Add-PodeRoute -Path '/route1' -Etc } + .EXAMPLE Add-PodeRouteGroup -Path '/api' -FilePath '/routes/file.ps1' #> @@ -1584,8 +1585,14 @@ The EndpointName of an Endpoint(s) to use for the Signal Routes. .PARAMETER IfExists Specifies what action to take when a Signal Route already exists. (Default: Default) +.PARAMETER FilePath +A literal, or relative, path to a file containing a ScriptBlock for the Route's main logic. + .EXAMPLE Add-PodeSignalRouteGroup -Path '/signals' -Routes { Add-PodeSignalRoute -Path '/signal1' -Etc } + +.EXAMPLE +Add-PodeSignalRouteGroup -Path '/api' -FilePath '/routes/file.ps1' #> function Add-PodeSignalRouteGroup { [CmdletBinding()] @@ -1605,11 +1612,22 @@ function Add-PodeSignalRouteGroup { [Parameter()] [ValidateSet('Default', 'Error', 'Overwrite', 'Skip')] [string] - $IfExists = 'Default' + $IfExists = 'Default', + + [Parameter(Mandatory = $true, ParameterSetName = 'file')] + [string] + $FilePath ) + i if (Test-PodeIsEmpty $Routes) { - throw 'No scriptblock for -Routes passed' + if ($PSCmdlet.ParameterSetName -ieq 'file') { + $Routes = Convert-PodeFileToScriptBlock -FilePath $FilePath + } + else { + + throw 'No scriptblock for -Routes passed' + } } if ($Path -eq '/') { From 04c22ab3c9ff6a3be2efa17ffd107dcc9112bd29 Mon Sep 17 00:00:00 2001 From: Tyas Lambrechts Date: Sat, 15 Jun 2024 08:24:47 +0200 Subject: [PATCH 4/5] Refactor route group functions to use default parameter set for routes The commit refactors the `Add-PodeRouteGroup`, `Add-PodeStaticRouteGroup`, and `Add-PodeSignalRouteGroup` functions to use the default parameter set for routes. This change allows for a more concise and consistent code structure. Additionally, it adds support for specifying a file path when adding route groups. - Set the default parameter set to 'Routes' in `[CmdletBinding()]` - Update the mandatory parameter attribute for `$Routes` in each function - Add a new parameter, `$FilePath`, with mandatory attribute and 'File' parameter set in each function - Modify the logic to check if `$Routes` is empty and handle it accordingly Examples: ``` Add-PodeRouteGroup -Path '/api' -Routes { Add-PodeRoute -Path '/route1' -Etc } Add-PodeRouteGroup -Path '/api' -FilePath '/routes/file.ps1' Add-PodeStaticRouteGroup -Path '/static' -Routes { Add-PodeStaticRoute -Path '/images' -Etc } Add-PodeStaticRouteGroup -Path '/static' -FilePath '/routes/file.ps1' Add-PodeSignalRouteGroup -Path '/signals' -Routes { Add-PodeSignalRoute -Path '/signal1' } Add-PodeSignalRouteGroup -Path '/api' --- src/Public/Routes.ps1 | 40 ++++++++++++++++++++++++++-------------- 1 file changed, 26 insertions(+), 14 deletions(-) diff --git a/src/Public/Routes.ps1 b/src/Public/Routes.ps1 index 0340cfa02..8aff48a28 100644 --- a/src/Public/Routes.ps1 +++ b/src/Public/Routes.ps1 @@ -1105,13 +1105,13 @@ Add-PodeRouteGroup -Path '/api' -Routes { Add-PodeRoute -Path '/route1' -Etc } Add-PodeRouteGroup -Path '/api' -FilePath '/routes/file.ps1' #> function Add-PodeRouteGroup { - [CmdletBinding()] + [CmdletBinding(DefaultParameterSetName = 'Routes')] param( [Parameter()] [string] $Path, - [Parameter(Mandatory = $true)] + [Parameter(Mandatory = $true, ParameterSetName = 'Routes')] [scriptblock] $Routes, @@ -1172,7 +1172,7 @@ function Add-PodeRouteGroup { [string[]] $OADefinitionTag, - [Parameter(Mandatory = $true, ParameterSetName = 'file')] + [Parameter(Mandatory = $true, ParameterSetName = 'File')] [string] $FilePath ) @@ -1180,7 +1180,7 @@ function Add-PodeRouteGroup { if (Test-PodeIsEmpty $Routes) { - if ($PSCmdlet.ParameterSetName -ieq 'file') { + if ($PSCmdlet.ParameterSetName -ieq 'File') { $Routes = Convert-PodeFileToScriptBlock -FilePath $FilePath } else { @@ -1358,9 +1358,12 @@ If supplied, the user will be redirected to the default page if found instead of .EXAMPLE Add-PodeStaticRouteGroup -Path '/static' -Routes { Add-PodeStaticRoute -Path '/images' -Etc } + +.EXAMPLE +Add-PodeStaticRouteGroup -Path '/static' -FilePath '/routes/file.ps1' #> function Add-PodeStaticRouteGroup { - [CmdletBinding()] + [CmdletBinding(DefaultParameterSetName = 'Routes')] param( [Parameter()] [string] @@ -1370,7 +1373,7 @@ function Add-PodeStaticRouteGroup { [string] $Source, - [Parameter(Mandatory = $true)] + [Parameter(Mandatory = $true, ParameterSetName = 'Routes')] [scriptblock] $Routes, @@ -1439,11 +1442,21 @@ function Add-PodeStaticRouteGroup { $DownloadOnly, [switch] - $RedirectToDefault + $RedirectToDefault, + + [Parameter(Mandatory = $true, ParameterSetName = 'File')] + [string] + $FilePath ) if (Test-PodeIsEmpty $Routes) { - throw 'No scriptblock for -Routes passed' + if ($PSCmdlet.ParameterSetName -ieq 'File') { + $Routes = Convert-PodeFileToScriptBlock -FilePath $FilePath + } + else { + + throw 'No scriptblock for -Routes passed or no filepath for -FilePath passed' + } } if ($Path -eq '/') { @@ -1595,13 +1608,13 @@ Add-PodeSignalRouteGroup -Path '/signals' -Routes { Add-PodeSignalRoute -Path '/ Add-PodeSignalRouteGroup -Path '/api' -FilePath '/routes/file.ps1' #> function Add-PodeSignalRouteGroup { - [CmdletBinding()] + [CmdletBinding(DefaultParameterSetName = 'Routes')] param( [Parameter()] [string] $Path, - [Parameter(Mandatory = $true)] + [Parameter(Mandatory = $true, ParameterSetName = 'Routes' )] [scriptblock] $Routes, @@ -1614,19 +1627,18 @@ function Add-PodeSignalRouteGroup { [string] $IfExists = 'Default', - [Parameter(Mandatory = $true, ParameterSetName = 'file')] + [Parameter(Mandatory = $true, ParameterSetName = 'File')] [string] $FilePath ) - i if (Test-PodeIsEmpty $Routes) { - if ($PSCmdlet.ParameterSetName -ieq 'file') { + if ($PSCmdlet.ParameterSetName -ieq 'File') { $Routes = Convert-PodeFileToScriptBlock -FilePath $FilePath } else { - throw 'No scriptblock for -Routes passed' + throw 'No scriptblock for -Routes passed or no filepath for -FilePath passed' } } From 176064274e627500aa909de227ec7376d2513099 Mon Sep 17 00:00:00 2001 From: Tyas Lambrechts <10156034+eltyBelgium@users.noreply.github.com> Date: Sat, 1 Feb 2025 19:14:21 +0000 Subject: [PATCH 5/5] Resolve the comments --- docs/Tutorials/Routes/Utilities/RouteGrouping.md | 4 ++++ src/Public/Routes.ps1 | 3 +++ 2 files changed, 7 insertions(+) diff --git a/docs/Tutorials/Routes/Utilities/RouteGrouping.md b/docs/Tutorials/Routes/Utilities/RouteGrouping.md index af76594ca..dc16d1a08 100644 --- a/docs/Tutorials/Routes/Utilities/RouteGrouping.md +++ b/docs/Tutorials/Routes/Utilities/RouteGrouping.md @@ -55,9 +55,13 @@ Add-PodeRouteGroup -Path '/api' -Authentication Basic -Middleware $mid -Routes { Write-PodeJsonResponse -Value @{ ID = 3 } } } + + Add-PodeRouteGroup -Path '/special' -FilePath './routes/specialRoutes.ps1' } ``` + + ## Static Routes The Groups for Static Routes work in the same manner as normal Routes, but you'll need to use [`Add-PodeStaticRouteGroup`](../../../../Functions/Routes/Add-PodeStaticRouteGroup) instead: diff --git a/src/Public/Routes.ps1 b/src/Public/Routes.ps1 index 1e75e1525..2fa0e7a71 100644 --- a/src/Public/Routes.ps1 +++ b/src/Public/Routes.ps1 @@ -88,6 +88,9 @@ Add-PodeRoute -Method Post -Path '/users/:userId/message' -Middleware (Get-PodeC .EXAMPLE Add-PodeRoute -Method Post -Path '/user' -ContentType 'application/json' -ScriptBlock { /* logic */ } +.EXAMPLE +Add-PodeRoute -Method Post -Path '/user' -ContentType 'application/json' -FilePath '/route.ps1' + .EXAMPLE Add-PodeRoute -Method Post -Path '/user' -ContentType 'application/json' -TransferEncoding gzip -ScriptBlock { /* logic */ }