| | | 1 | | <# |
| | | 2 | | .SYNOPSIS |
| | | 3 | | Adds a new API documentation route (Redoc or Swagger UI) to the Kestrun server. |
| | | 4 | | .DESCRIPTION |
| | | 5 | | This function allows you to add a new API documentation route to the Kestrun server by specifying the route path |
| | | 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 API documentation route. |
| | | 13 | | Default is '/docs/redoc' for Redoc and '/docs/swagger' for Swagger UI. |
| | | 14 | | .PARAMETER DocumentType |
| | | 15 | | The type of API documentation to add. Valid values are 'Redoc', 'Swagger', 'Scalar', 'Rapidoc', and 'Elements'. |
| | | 16 | | .PARAMETER OpenApiEndpoint |
| | | 17 | | The OpenAPI endpoint URI that the documentation UI will use to fetch the API documentation. Default is '/openapi |
| | | 18 | | .PARAMETER PassThru |
| | | 19 | | If specified, the function will return the created route object. |
| | | 20 | | .OUTPUTS |
| | | 21 | | [Microsoft.AspNetCore.Builder.IEndpointConventionBuilder] representing the created route. |
| | | 22 | | .EXAMPLE |
| | | 23 | | Add-KrApiDocumentationRoute -Server $myServer -Pattern "/docs/redoc" -DocumentType "Redoc" -OpenApiEndpoint "/op |
| | | 24 | | Adds a new Redoc UI route to the specified Kestrun server with the given pattern and OpenAPI endpoint. |
| | | 25 | | .EXAMPLE |
| | | 26 | | Get-KrServer | Add-KrApiDocumentationRoute -Pattern "/docs/swagger" -DocumentType "Swagger" -OpenApiEndpoint "/o |
| | | 27 | | .NOTES |
| | | 28 | | This function is part of the Kestrun PowerShell module and is used to manage routes |
| | | 29 | | #> |
| | | 30 | | function Add-KrApiDocumentationRoute { |
| | | 31 | | [KestrunRuntimeApi('Definition')] |
| | | 32 | | [CmdletBinding()] |
| | | 33 | | [OutputType([Kestrun.Hosting.KestrunHost])] |
| | | 34 | | param( |
| | | 35 | | [Parameter(Mandatory = $false, ValueFromPipeline = $true)] |
| | | 36 | | [Kestrun.Hosting.KestrunHost]$Server, |
| | | 37 | | [Parameter()] |
| | | 38 | | [Kestrun.Hosting.Options.MapRouteOptions]$Options, |
| | | 39 | | [Parameter()] |
| | | 40 | | [alias('Path')] |
| | | 41 | | [string]$Pattern, |
| | | 42 | | [Parameter()] |
| | | 43 | | [ValidateSet('Redoc', 'Swagger', 'Scalar', 'Rapidoc', 'Elements')] |
| | | 44 | | [string]$DocumentType = 'Swagger', |
| | | 45 | | [Parameter()] |
| | | 46 | | [Uri]$OpenApiEndpoint = [Uri]::new('/openapi/v3.0/openapi.json', [UriKind]::Relative), |
| | | 47 | | [Parameter()] |
| | | 48 | | [switch]$PassThru |
| | | 49 | | ) |
| | | 50 | | begin { |
| | | 51 | | # Ensure the server instance is resolved |
| | 0 | 52 | | $Server = Resolve-KestrunServer -Server $Server |
| | | 53 | | } |
| | | 54 | | process { |
| | 0 | 55 | | if ($null -eq $Options) { |
| | 0 | 56 | | $Options = [Kestrun.Hosting.Options.MapRouteOptions]::new() |
| | | 57 | | } |
| | | 58 | | |
| | 0 | 59 | | $Options.Pattern = $Pattern |
| | 0 | 60 | | switch ($DocumentType) { |
| | | 61 | | 'Swagger' { |
| | | 62 | | # Call the C# extension method to add the Swagger UI route |
| | 0 | 63 | | [Kestrun.Hosting.KestrunHostMapExtensions]::AddSwaggerUiRoute($Server, $Options, $OpenApiEndpoint) | Out |
| | | 64 | | } |
| | | 65 | | 'Redoc' { |
| | | 66 | | # Call the C# extension method to add the Redoc UI route |
| | 0 | 67 | | [Kestrun.Hosting.KestrunHostMapExtensions]::AddRedocUiRoute($Server, $Options, $OpenApiEndpoint) | Out-N |
| | | 68 | | } |
| | | 69 | | 'Scalar' { |
| | | 70 | | # Call the C# extension method to add the Scalar UI route |
| | 0 | 71 | | [Kestrun.Hosting.KestrunHostMapExtensions]::AddScalarUiRoute($Server, $Options, $OpenApiEndpoint) | Out- |
| | | 72 | | } |
| | | 73 | | 'Rapidoc' { |
| | | 74 | | # Call the C# extension method to add the Rapidoc UI route |
| | 0 | 75 | | [Kestrun.Hosting.KestrunHostMapExtensions]::AddRapidocUiRoute($Server, $Options, $OpenApiEndpoint) | Out |
| | | 76 | | } |
| | | 77 | | 'Elements' { |
| | | 78 | | # Call the C# extension method to add the Elements UI route |
| | 0 | 79 | | [Kestrun.Hosting.KestrunHostMapExtensions]::AddElementsUiRoute($Server, $Options, $OpenApiEndpoint) | Ou |
| | | 80 | | } |
| | | 81 | | default { |
| | 0 | 82 | | throw "Unsupported DocumentType: $DocumentType. Supported types are 'Swagger', 'Redoc', 'Scalar', 'Rapid |
| | | 83 | | } |
| | | 84 | | } |
| | 0 | 85 | | if ($PassThru) { |
| | 0 | 86 | | return $Server |
| | | 87 | | } |
| | | 88 | | } |
| | | 89 | | } |