| | | 1 | | <# |
| | | 2 | | .SYNOPSIS |
| | | 3 | | Adds a new OpenAPI map route to the Kestrun server. |
| | | 4 | | .DESCRIPTION |
| | | 5 | | This function allows you to add a new OpenAPI map route to the Kestrun server by specifying the route path, Open |
| | | 6 | | .PARAMETER Server |
| | | 7 | | The Kestrun server instance to which the route will be added. |
| | | 8 | | If not specified, the function will attempt to resolve the current server context. |
| | | 9 | | .PARAMETER Options |
| | | 10 | | The MapRouteOptions object to configure the route. |
| | | 11 | | .PARAMETER Pattern |
| | | 12 | | The URL path for the new OpenAPI map route. |
| | | 13 | | Default is '/openapi/{version}/openapi.{format}'. |
| | | 14 | | .PARAMETER SpecVersion |
| | | 15 | | An array of OpenAPI specification versions to support (e.g., OpenApi2_0 or OpenApi3_0). |
| | | 16 | | .PARAMETER VersionVarName |
| | | 17 | | The name of the route variable used to specify the OpenAPI version. |
| | | 18 | | .PARAMETER FormatVarName |
| | | 19 | | The name of the route variable used to specify the OpenAPI format (e.g., json or yaml). |
| | | 20 | | .PARAMETER RefreshVarName |
| | | 21 | | The name of the route variable used to trigger a refresh of the OpenAPI document. |
| | | 22 | | .PARAMETER DefaultFormat |
| | | 23 | | The default format for the OpenAPI document if not specified in the route. |
| | | 24 | | .PARAMETER DefaultVersion |
| | | 25 | | The default version for the OpenAPI document if not specified in the route. |
| | | 26 | | .PARAMETER PassThru |
| | | 27 | | If specified, the function will return the created route object. |
| | | 28 | | .OUTPUTS |
| | | 29 | | [Kestrun.Hosting.KestrunHost] representing the created route. |
| | | 30 | | .EXAMPLE |
| | | 31 | | Add-KrOpenApiRoute -Server $myServer -Pattern "/openapi/{version}/{format}" -SpecVersion @('OpenApi3_0') ` |
| | | 32 | | -VersionVarName "version" -FormatVarName "format" -DefaultFormat "json" -DefaultVersion "v3.0" |
| | | 33 | | Adds a new OpenAPI map route to the specified Kestrun server with the given pattern and options. |
| | | 34 | | .EXAMPLE |
| | | 35 | | Get-KrServer | Add-KrOpenApiRoute -Pattern "/openapi/{version}/{format}" -SpecVersion @('OpenApi3_0') ` |
| | | 36 | | -VersionVarName "version" -FormatVarName "format" -DefaultFormat "json" -DefaultVersion "v3.0" -PassThru |
| | | 37 | | Adds a new OpenAPI map route to the specified Kestrun server with the given pattern and options. |
| | | 38 | | .NOTES |
| | | 39 | | This function is part of the Kestrun PowerShell module and is used to manage routes |
| | | 40 | | #> |
| | | 41 | | function Add-KrOpenApiRoute { |
| | | 42 | | [KestrunRuntimeApi('Definition')] |
| | | 43 | | [CmdletBinding()] |
| | | 44 | | [OutputType([Kestrun.Hosting.KestrunHost])] |
| | | 45 | | param( |
| | | 46 | | [Parameter(Mandatory = $false, ValueFromPipeline = $true)] |
| | | 47 | | [Kestrun.Hosting.KestrunHost]$Server, |
| | | 48 | | [Parameter()] |
| | | 49 | | [Kestrun.Hosting.Options.MapRouteOptions]$Options, |
| | | 50 | | [Parameter()] |
| | | 51 | | [alias('Path')] |
| | | 52 | | [string]$Pattern, |
| | | 53 | | [Parameter()] |
| | | 54 | | [Microsoft.OpenApi.OpenApiSpecVersion[]]$SpecVersion, |
| | | 55 | | [Parameter()] |
| | | 56 | | [string]$VersionVarName, |
| | | 57 | | [Parameter()] |
| | | 58 | | [string]$FormatVarName, |
| | | 59 | | [Parameter()] |
| | | 60 | | [string]$RefreshVarName, |
| | | 61 | | [Parameter()] |
| | | 62 | | [string]$DefaultFormat, |
| | | 63 | | [Parameter()] |
| | | 64 | | [string]$DefaultVersion, |
| | | 65 | | [Parameter()] |
| | | 66 | | [switch]$PassThru |
| | | 67 | | ) |
| | | 68 | | begin { |
| | | 69 | | # Ensure the server instance is resolved |
| | 0 | 70 | | $Server = Resolve-KestrunServer -Server $Server |
| | | 71 | | } |
| | | 72 | | process { |
| | 0 | 73 | | if ($null -eq $Options) { |
| | 0 | 74 | | $Options = [Kestrun.Hosting.Options.MapRouteOptions]::new() |
| | | 75 | | } |
| | 0 | 76 | | if (-not [string]::IsNullOrEmpty($Pattern)) { |
| | 0 | 77 | | $Options.Pattern = $Pattern |
| | | 78 | | } |
| | 0 | 79 | | $Options.HttpVerbs = @('GET') |
| | 0 | 80 | | $OpenApiMapRouteOptions = [Kestrun.Hosting.Options.OpenApiMapRouteOptions]::new($Options) |
| | 0 | 81 | | if ($PsBoundParameters.ContainsKey('DefaultFormat')) { |
| | 0 | 82 | | $OpenApiMapRouteOptions.DefaultFormat = $DefaultFormat |
| | | 83 | | } |
| | 0 | 84 | | if ($PsBoundParameters.ContainsKey('DefaultVersion')) { |
| | 0 | 85 | | $OpenApiMapRouteOptions.DefaultVersion = $DefaultVersion |
| | | 86 | | } |
| | 0 | 87 | | if ($PsBoundParameters.ContainsKey('FormatVarName')) { |
| | 0 | 88 | | $OpenApiMapRouteOptions.FormatVarName = $FormatVarName |
| | | 89 | | } |
| | 0 | 90 | | if ($PsBoundParameters.ContainsKey('VersionVarName')) { |
| | 0 | 91 | | $OpenApiMapRouteOptions.VersionVarName = $VersionVarName |
| | | 92 | | } |
| | 0 | 93 | | if ($PsBoundParameters.ContainsKey('RefreshVarName')) { |
| | 0 | 94 | | $OpenApiMapRouteOptions.RefreshVarName = $RefreshVarName |
| | | 95 | | } |
| | 0 | 96 | | if ($PsBoundParameters.ContainsKey('SpecVersion')) { |
| | 0 | 97 | | $OpenApiMapRouteOptions.SpecVersions = $SpecVersion |
| | | 98 | | } |
| | | 99 | | |
| | | 100 | | # Call the C# extension method to add the OpenAPI map route |
| | 0 | 101 | | [Kestrun.Hosting.KestrunHostMapExtensions]::AddOpenApiMapRoute($Server, $OpenApiMapRouteOptions) | Out-Null |
| | | 102 | | |
| | | 103 | | # Return the server if PassThru is specified |
| | 0 | 104 | | if ($PassThru) { |
| | 0 | 105 | | return $Server |
| | | 106 | | } |
| | | 107 | | } |
| | | 108 | | } |