< Summary - Kestrun — Combined Coverage

Information
Class: Public.Route.Add-KrRouteGroup
Assembly: Kestrun.PowerShell.Public
File(s): /home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Route/Add-KrRouteGroup.ps1
Tag: Kestrun/Kestrun@9d3a582b2d63930269564a7591aa77ef297cadeb
Line coverage
66%
Covered lines: 24
Uncovered lines: 12
Coverable lines: 36
Total lines: 165
Line coverage: 66.6%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100

Metrics

File(s)

/home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Route/Add-KrRouteGroup.ps1

#LineLine coverage
 1<#
 2    .SYNOPSIS
 3        Creates a grouped route context (prefix + shared options) for nested Add-KrMapRoute calls.
 4    .DESCRIPTION
 5        While the ScriptBlock runs, all Add-KrMapRoute calls inherit:
 6          - Prefix (prepended to -Path)
 7          - AuthorizationSchema / AuthorizationPolicy
 8          - ExtraImports / ExtraRefs
 9          - Arguments (merged; child overrides keys)
 10        Supports nesting; inner groups inherit and can override unless -NoInherit is used.
 11    .PARAMETER Server
 12        The Kestrun server instance to which the route will be added.
 13        If not specified, the function will attempt to resolve the current server context.
 14    .PARAMETER Options
 15        The options to apply to all routes in the group.
 16    .PARAMETER Prefix
 17        The path prefix for the group (e.g. '/todoitems').
 18    .PARAMETER AuthorizationSchema
 19        Authorization schemes required by all routes in the group.
 20    .PARAMETER AuthorizationPolicy
 21        Authorization policies required by all routes in the group.
 22    .PARAMETER ExtraImports
 23        Extra namespaces added to all routes in the group.
 24    .PARAMETER ExtraRefs
 25        Extra assemblies referenced by all routes in the group.
 26    .PARAMETER Arguments
 27        Extra arguments injected into all routes in the group.
 28    .PARAMETER ScriptBlock
 29        ScriptBlock within which you call Add-KrMapRoute for relative paths.
 30    .PARAMETER FileName
 31        Path to a script file containing the scriptblock to execute.
 32    .PARAMETER NoInherit
 33        If set, do not inherit options from the parent group; only apply the current parameters.
 34    .PARAMETER PassThru
 35        If specified, the function will return the created route object.
 36    .EXAMPLE
 37        Add-KrRouteGroup -Prefix '/todoitems' -AuthorizationPolicy 'RequireUser' -ScriptBlock {
 38            Add-KrMapRoute -Verbs Get  -Path '/'      -ScriptBlock { 'all todos' }
 39            Add-KrMapRoute -Verbs Get  -Path '/{id}'  -ScriptBlock { "todo $($Context.Request.RouteValues['id'])" }
 40            Add-KrMapRoute -Verbs Post -Path '/'      -ScriptBlock { write-KrResponse -InputObject 'create' }
 41        }
 42        Adds a new route group to the specified Kestrun server with the given prefix and options.
 43    .EXAMPLE
 44        Add-KrRouteGroup -Prefix '/todoitems' -FileName 'C:\Scripts\TodoItems.ps1'
 45        Add the new route group defined in the specified file.
 46    .NOTES
 47        This function is part of the Kestrun PowerShell module and is used to manage routes
 48#>
 49function Add-KrRouteGroup {
 50    [KestrunRuntimeApi('Definition')]
 51    [CmdletBinding(DefaultParameterSetName = 'ScriptBlock', PositionalBinding = $true)]
 52    param(
 53        [Parameter(Mandatory = $false, ValueFromPipeline = $true)]
 54        [Kestrun.Hosting.KestrunHost]$Server,
 55
 56        [Parameter(Mandatory, ParameterSetName = 'ScriptBlockWithOptions')]
 57        [Parameter(Mandatory, ParameterSetName = 'FileNameWithOptions')]
 58        [Kestrun.Hosting.Options.MapRouteOptions]$Options,
 59
 60        [Parameter(ParameterSetName = 'ScriptBlock')]
 61        [Parameter(ParameterSetName = 'FileName')]
 62        [string]$Prefix,
 63
 64        [Parameter(ParameterSetName = 'ScriptBlock')]
 65        [Parameter(ParameterSetName = 'FileName')]
 66        [string[]]$AuthorizationSchema,
 67
 68        [Parameter(ParameterSetName = 'ScriptBlock')]
 69        [Parameter(ParameterSetName = 'FileName')]
 70        [string[]]$AuthorizationPolicy,
 71
 72        [Parameter(ParameterSetName = 'ScriptBlock')]
 73        [Parameter(ParameterSetName = 'FileName')]
 74        [string[]]$ExtraImports,
 75
 76        [Parameter(ParameterSetName = 'ScriptBlock')]
 77        [Parameter(ParameterSetName = 'FileName')]
 78        [System.Reflection.Assembly[]]$ExtraRefs,
 79
 80        [Parameter(ParameterSetName = 'ScriptBlock')]
 81        [Parameter(ParameterSetName = 'FileName')]
 82        [hashtable]$Arguments,
 83
 84        [Parameter(Mandatory, Position = 0, ParameterSetName = 'ScriptBlock')]
 85        [Parameter(Mandatory, Position = 0, ParameterSetName = 'ScriptBlockWithOptions')]
 86        [scriptblock]$ScriptBlock,
 87
 88        [Parameter(Mandatory, ParameterSetName = 'FileName')]
 89        [Parameter(Mandatory, ParameterSetName = 'FileNameWithOptions')]
 90        [string]$FileName,
 91
 92        [Parameter()]
 93        [switch]$NoInherit,
 94
 95        [Parameter()]
 96        [switch]$PassThru
 97    )
 98    begin {
 99        # Ensure the server instance is resolved
 1100        $Server = Resolve-KestrunServer -Server $Server
 1101        if ($null -eq $Server) {
 0102            throw 'Server is not initialized. Please ensure the server is configured before setting options.'
 103        }
 104    }
 105    process {
 106
 1107        if ($PSCmdlet.ParameterSetName -like 'FileName*') {
 0108            if (-not (Test-Path -Path $FileName)) {
 0109                throw "The specified file path does not exist: $FileName"
 110            }
 0111            $code = Get-Content -Path $FileName -Raw
 0112            $ScriptBlock = [scriptblock]::Create($code)
 113        }
 114
 115        # Normalize prefix: allow "todoitems" or "/todoitems"
 1116        if (-not [string]::IsNullOrWhiteSpace($Prefix) -and -not $Prefix.StartsWith('/')) {
 0117            $Prefix = "/$Prefix"
 118        }
 119        # Discover parent template (stack always aims to hold MapRouteOptions)
 1120        $parentOpts = $null
 1121        if (-not $NoInherit -and $server.RouteGroupStack.Count) {
 1122            $parentOpts = $server.RouteGroupStack.Peek()
 123        }
 124
 1125        $curr = if ($PSCmdlet.ParameterSetName -like '*WithOptions') {
 0126            $Options
 127        } else {
 1128            $dict = $null
 1129            if ($Arguments) {
 0130                $dict = [System.Collections.Generic.Dictionary[string, object]]::new()
 0131                foreach ($k in $Arguments.Keys) { $dict[$k] = $Arguments[$k] }
 132            }
 1133            New-KrMapRouteOption -Property @{
 1134                RequireSchemes = $AuthorizationSchema
 1135                RequirePolicies = $AuthorizationPolicy
 1136                ExtraImports = $ExtraImports
 1137                ExtraRefs = $ExtraRefs
 1138                Arguments = $dict
 1139                Pattern = $Prefix
 140            }
 141        }
 142        # Merge with parent (parent first, then current overrides)
 1143        if ($parentOpts) {
 1144            $curr = _KrMerge-MRO -Parent $parentOpts -Child $curr
 145        }
 146
 1147        $Server.RouteGroupStack.Push($curr)
 148        try {
 1149            & $ScriptBlock
 150        } catch {
 0151            $msg = "Error inside route group '$($current.Prefix)': $($_.Exception.Message)"
 0152            throw [System.Exception]::new($msg, $_.Exception)
 153        } finally {
 1154            $null = $Server.RouteGroupStack.Pop()
 1155            if ($restorePath) { Set-Location $restorePath }
 156        }
 157
 1158        if ($PassThru.IsPresent) {
 159            # if the PassThru switch is specified, return the server instance
 160            # Return the modified server instance
 0161            return $Server
 162        }
 163    }
 164}
 165

Methods/Properties

Add-KrRouteGroup()