-
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ff2e864
commit 1330ea2
Showing
3 changed files
with
77 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,24 @@ | ||
using GenHTTP.Modules.ApiBrowsing.Common; | ||
using GenHTTP.Modules.Layouting.Provider; | ||
|
||
namespace GenHTTP.Modules.ApiBrowsing; | ||
|
||
/// <summary> | ||
/// Provides graphical, JavaScript based web applications that render an Open API | ||
/// definition so that the API can be explored by users. | ||
/// </summary> | ||
public static class ApiBrowser | ||
{ | ||
|
||
#region Factories | ||
|
||
/// <summary> | ||
/// Creates a handler that will provide a Swagger UI app. | ||
/// </summary> | ||
/// <returns>The newly created handler</returns> | ||
public static BrowserHandlerBuilder SwaggerUI() => new("Swagger", "Swagger UI"); | ||
|
||
/// <summary> | ||
/// Creates a handler that will provide a Redoc app. | ||
/// </summary> | ||
/// <returns>The newly created handler</returns> | ||
public static BrowserHandlerBuilder Redoc() => new("Redoc", "Redoc"); | ||
|
||
#endregion | ||
|
||
#region Layout extensions | ||
|
||
public static LayoutBuilder AddSwaggerUI(this LayoutBuilder layout, string segment = "swagger", string? url = null, string? title = null) | ||
=> AddBrowser(layout, SwaggerUI(), segment, url, title); | ||
|
||
public static LayoutBuilder AddRedoc(this LayoutBuilder layout, string segment = "redoc", string? url = null, string? title = null) | ||
=> AddBrowser(layout, Redoc(), segment, url, title); | ||
|
||
private static LayoutBuilder AddBrowser(this LayoutBuilder layout, BrowserHandlerBuilder builder, string segment, string? url, string? title) | ||
{ | ||
if (url != null) | ||
{ | ||
builder.Url(url); | ||
} | ||
|
||
if (title != null) | ||
{ | ||
builder.Title(title); | ||
} | ||
|
||
return layout.Add(segment, builder); | ||
} | ||
|
||
#endregion | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
using GenHTTP.Modules.ApiBrowsing.Common; | ||
using GenHTTP.Modules.Layouting.Provider; | ||
|
||
namespace GenHTTP.Modules.ApiBrowsing; | ||
|
||
public static class Extensions | ||
{ | ||
|
||
/// <summary> | ||
/// Creates a Swagger UI application and registers it at the layout. | ||
/// </summary> | ||
/// <param name="layout">The layout to add the application to</param> | ||
/// <param name="segment">The path to make the application available from (defaults to "/swagger/")</param> | ||
/// <param name="url">The URL of the Open API definition to be rendered (defaults to "../openapi.json")</param> | ||
/// <param name="title">The title of the rendered application</param> | ||
/// <returns>The layout once again (builder pattern)</returns> | ||
/// <remarks> | ||
/// There is no auto-detection of Open API definitions provided by the server | ||
/// so the URL provided needs to point to the correct definition to be consumed. | ||
/// Use relative paths to avoid issues with CORS, proxies etc. | ||
/// </remarks> | ||
public static LayoutBuilder AddSwaggerUI(this LayoutBuilder layout, string segment = "swagger", string? url = null, string? title = null) | ||
=> AddBrowser(layout, ApiBrowser.SwaggerUI(), segment, url, title); | ||
|
||
/// <summary> | ||
/// Creates a Redoc application and registers it at the layout. | ||
/// </summary> | ||
/// <param name="layout">The layout to add the application to</param> | ||
/// <param name="segment">The path to make the application available from (defaults to "/redoc/")</param> | ||
/// <param name="url">The URL of the Open API definition to be rendered (defaults to "../openapi.json")</param> | ||
/// <param name="title">The title of the rendered application</param> | ||
/// <returns>The layout once again (builder pattern)</returns> | ||
/// <remarks> | ||
/// There is no auto-detection of Open API definitions provided by the server | ||
/// so the URL provided needs to point to the correct definition to be consumed. | ||
/// Use relative paths to avoid issues with CORS, proxies etc. | ||
/// </remarks> | ||
public static LayoutBuilder AddRedoc(this LayoutBuilder layout, string segment = "redoc", string? url = null, string? title = null) | ||
=> AddBrowser(layout, ApiBrowser.Redoc(), segment, url, title); | ||
|
||
private static LayoutBuilder AddBrowser(this LayoutBuilder layout, BrowserHandlerBuilder builder, string segment, string? url, string? title) | ||
{ | ||
if (url != null) | ||
{ | ||
builder.Url(url); | ||
} | ||
|
||
if (title != null) | ||
{ | ||
builder.Title(title); | ||
} | ||
|
||
return layout.Add(segment, builder); | ||
} | ||
|
||
} |