| | | 1 | | <# |
| | | 2 | | .SYNOPSIS |
| | | 3 | | Creates a new OpenAPI server. |
| | | 4 | | .DESCRIPTION |
| | | 5 | | This function creates a new OpenAPI server using the provided parameters. |
| | | 6 | | .PARAMETER Server |
| | | 7 | | The Kestrun server instance to which the OpenAPI server will be added. |
| | | 8 | | If not specified, the function will attempt to resolve the current server context. |
| | | 9 | | .PARAMETER Description |
| | | 10 | | A description of the server. |
| | | 11 | | .PARAMETER DocId |
| | | 12 | | The documentation IDs to which this server will be added. |
| | | 13 | | .PARAMETER Url |
| | | 14 | | The URL of the server. |
| | | 15 | | .PARAMETER Variables |
| | | 16 | | A dictionary of server variables. |
| | | 17 | | .EXAMPLE |
| | | 18 | | $variables = @{ |
| | | 19 | | env = New-KrOpenApiServerVariable -Default 'dev' -Enum @('dev', 'staging', 'prod') -Description 'Environment nam |
| | | 20 | | } |
| | | 21 | | $oaServer = New-KrOpenApiServer -Description 'My API Server' -Url 'https://api.example.com' -Variables $variables |
| | | 22 | | .NOTES |
| | | 23 | | This cmdlet is part of the Kestrun OpenAPI module. |
| | | 24 | | #> |
| | | 25 | | function Add-KrOpenApiServer { |
| | | 26 | | [KestrunRuntimeApi('Everywhere')] |
| | | 27 | | param( |
| | | 28 | | [Parameter(Mandatory = $false, ValueFromPipeline = $true)] |
| | | 29 | | [Kestrun.Hosting.KestrunHost]$Server, |
| | | 30 | | [Parameter()] |
| | | 31 | | [string[]]$DocId = [Kestrun.Authentication.IOpenApiAuthenticationOptions]::DefaultDocumentationIds, |
| | | 32 | | [Parameter(Mandatory)] |
| | | 33 | | [string]$Url, |
| | | 34 | | [string]$Description, |
| | | 35 | | [System.Collections.Specialized.OrderedDictionary]$Variables |
| | | 36 | | ) |
| | | 37 | | begin { |
| | | 38 | | # Ensure the server instance is resolved |
| | 0 | 39 | | $Server = Resolve-KestrunServer -Server $Server |
| | | 40 | | } |
| | | 41 | | process { |
| | 0 | 42 | | $oaServer = [Microsoft.OpenApi.OpenApiServer]::new() |
| | 0 | 43 | | if ($PsBoundParameters.ContainsKey('Description')) { |
| | 0 | 44 | | $oaServer.Description = $Description |
| | | 45 | | } |
| | 0 | 46 | | $oaServer.Url = $Url |
| | 0 | 47 | | if ($PsBoundParameters.ContainsKey('Variables')) { |
| | 0 | 48 | | $oaServer.Variables = [System.Collections.Generic.Dictionary[string, Microsoft.OpenApi.OpenApiServerVariable |
| | 0 | 49 | | foreach ($key in $Variables.Keys) { |
| | 0 | 50 | | $value = $Variables[$key] |
| | 0 | 51 | | $oaServer.Variables.Add($key, $value) |
| | | 52 | | } |
| | | 53 | | } |
| | | 54 | | # Add the server to the specified OpenAPI documents |
| | 0 | 55 | | foreach ($doc in $DocId) { |
| | 0 | 56 | | $docDescriptor = $Server.GetOrCreateOpenApiDocument($doc) |
| | 0 | 57 | | if ($null -eq $docDescriptor.Document.Servers) { |
| | | 58 | | # Initialize the Servers list if null |
| | 0 | 59 | | $docDescriptor.Document.Servers = [System.Collections.Generic.List[Microsoft.OpenApi.OpenApiServer]]::ne |
| | | 60 | | } |
| | | 61 | | # Add the server to the Servers list |
| | 0 | 62 | | $docDescriptor.Document.Servers.Add($oaServer) |
| | | 63 | | } |
| | | 64 | | } |
| | | 65 | | } |