| | | 1 | | <# |
| | | 2 | | .SYNOPSIS |
| | | 3 | | Loads OpenAPI-annotated PowerShell functions into a KestrunHost instance. |
| | | 4 | | .DESCRIPTION |
| | | 5 | | The Get-KrAnnotatedFunctionsLoaded cmdlet scans all loaded PowerShell functions |
| | | 6 | | in the current runspace for OpenAPI annotations and loads them into the specified |
| | | 7 | | KestrunHost instance as API routes. |
| | | 8 | | .PARAMETER Server |
| | | 9 | | The KestrunHost instance to load the annotated functions into. If not specified, |
| | | 10 | | the default KestrunHost instance will be used. |
| | | 11 | | .PARAMETER DocId |
| | | 12 | | The ID of the OpenAPI document to build. Default is 'default'. |
| | | 13 | | .EXAMPLE |
| | | 14 | | Get-KrAnnotatedFunctionsLoaded -Server $myKestrunHost |
| | | 15 | | Loads all OpenAPI-annotated functions into the specified KestrunHost instance. |
| | | 16 | | .NOTES |
| | | 17 | | This cmdlet is designed to be used within the context of a KestrunHost instance. |
| | | 18 | | #> |
| | | 19 | | function Get-KrAnnotatedFunctionsLoaded { |
| | | 20 | | [CmdletBinding()] |
| | | 21 | | param( |
| | | 22 | | [Parameter(Mandatory = $false, ValueFromPipeline = $true)] |
| | | 23 | | [Kestrun.Hosting.KestrunHost]$Server, |
| | | 24 | | [Parameter()] |
| | | 25 | | [string]$DocId = [Kestrun.Authentication.IOpenApiAuthenticationOptions]::DefaultSchemeName |
| | | 26 | | ) |
| | | 27 | | begin { |
| | | 28 | | # Ensure the server instance is resolved |
| | 0 | 29 | | $Server = Resolve-KestrunServer -Server $Server |
| | | 30 | | |
| | | 31 | | # All loaded functions now in the runspace |
| | 0 | 32 | | $funcs = @(Get-Command -CommandType Function | Where-Object { |
| | 0 | 33 | | $null -eq $_.Module -and $null -eq $_.PsDrive |
| | | 34 | | }) |
| | | 35 | | } |
| | | 36 | | process { |
| | 0 | 37 | | if ( -not $Server.OpenApiDocumentDescriptor.ContainsKey($DocId)) { |
| | 0 | 38 | | throw "OpenAPI document with ID '$DocId' does not exist on the server." |
| | | 39 | | } |
| | 0 | 40 | | $doc = $Server.OpenApiDocumentDescriptor[$DocId] |
| | 0 | 41 | | $doc.LoadAnnotatedFunctions( $funcs ) |
| | | 42 | | } |
| | | 43 | | } |