Skip to content

Commit

Permalink
Add Scalar API browser
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaliumhexacyanoferrat committed Dec 16, 2024
1 parent 488e804 commit e53bc74
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 0 deletions.
6 changes: 6 additions & 0 deletions Modules/ApiBrowsing/ApiBrowser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,10 @@ public static class ApiBrowser
/// <returns>The newly created handler</returns>
public static BrowserHandlerBuilder Redoc() => new("Redoc", "Redoc");

/// <summary>
/// Creates a handler that will provide a Scalar app.
/// </summary>
/// <returns>The newly created handler</returns>
public static BrowserHandlerBuilder Scalar() => new("Scalar", "Scalar");

}
16 changes: 16 additions & 0 deletions Modules/ApiBrowsing/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,22 @@ public static LayoutBuilder AddSwaggerUI(this LayoutBuilder layout, string segme
public static LayoutBuilder AddRedoc(this LayoutBuilder layout, string segment = "redoc", string? url = null, string? title = null)
=> AddBrowser(layout, ApiBrowser.Redoc(), segment, url, title);

/// <summary>
/// Creates a Scalar 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 "/scalar/")</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 AddScalar(this LayoutBuilder layout, string segment = "scalar", string? url = null, string? title = null)
=> AddBrowser(layout, ApiBrowser.Scalar(), segment, url, title);

private static LayoutBuilder AddBrowser(this LayoutBuilder layout, BrowserHandlerBuilder builder, string segment, string? url, string? title)
{
if (url != null)
Expand Down
7 changes: 7 additions & 0 deletions Modules/ApiBrowsing/GenHTTP.Modules.ApiBrowsing.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,19 @@
<EmbeddedResource Include="Redoc\Index.html" />
<EmbeddedResource Include="Redoc\Static\redoc-standalone.js" />
<EmbeddedResource Include="Redoc\Static\roboto.css" />
<EmbeddedResource Include="Scalar\Index.html" />
<EmbeddedResource Include="Swagger\Index.html" />
<EmbeddedResource Include="Swagger\Static\swagger-ui-bundle.js" />
<EmbeddedResource Include="Swagger\Static\swagger-ui.css" />
<None Remove="Redoc\Resources\Index.html" />
<None Remove="Redoc\Resources\Static\redoc.standalone.js" />
<None Remove="Redoc\Resources\Static\roboto.css" />
<None Remove="Scalar\Static\scalar.js" />
<EmbeddedResource Include="Scalar\Static\scalar.js" />
</ItemGroup>

<ItemGroup>
<Folder Include="Scalar\Static\" />
</ItemGroup>

</Project>
15 changes: 15 additions & 0 deletions Modules/ApiBrowsing/Scalar/Index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!doctype html>
<html>
<head>
<title>{title}</title>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
</head>
<body>

<script id="api-reference" data-url="{url}"></script>

<script src="./static/scalar.js"></script>

</body>
</html>
45 changes: 45 additions & 0 deletions Modules/ApiBrowsing/Scalar/Static/scalar.js

Large diffs are not rendered by default.

18 changes: 18 additions & 0 deletions Testing/Acceptance/Modules/ApiBrowsing/ApplicationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,22 @@ public async Task TestRedoc(TestEngine engine)
AssertX.Contains("Redoc", await response.GetContentAsync());
}

[TestMethod]
[MultiEngineTest]
public async Task TestScalar(TestEngine engine)
{
var app = Layout.Create()
.Add(Inline.Create().Get(() => 42))
.AddOpenApi()
.AddScalar();

await using var host = await TestHost.RunAsync(app, engine: engine);

using var response = await host.GetResponseAsync("/scalar/");

await response.AssertStatusAsync(HttpStatusCode.OK);

AssertX.Contains("Scalar", await response.GetContentAsync());
}

}

0 comments on commit e53bc74

Please sign in to comment.