https://github.com/ramtype0/krokidotnet
Kroki client for .NET
https://github.com/ramtype0/krokidotnet
Last synced: about 1 year ago
JSON representation
Kroki client for .NET
- Host: GitHub
- URL: https://github.com/ramtype0/krokidotnet
- Owner: RamType0
- License: mit
- Created: 2025-04-28T18:35:48.000Z (about 1 year ago)
- Default Branch: master
- Last Pushed: 2025-05-10T13:07:02.000Z (about 1 year ago)
- Last Synced: 2025-05-10T14:19:03.573Z (about 1 year ago)
- Language: C#
- Size: 47.9 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
[](https://www.nuget.org/packages/Kroki/)
[](https://www.nuget.org/packages/Kroki/)
# [Kroki](https://github.com/yuzutech/kroki) integration for .NET
## Kroki Client
### Getting started
Add `KrokiHttpRequestFactory` to services.
```C#
builder.Services.AddKrokiHttpRequestFactory();
```
Or just create `KrokiHttpRequestFactory` directly.
```C#
KrokiHttpRequestFactory krokiHttpRequestFactory = new();
KrokiHttpRequestFactory myHostedKrokiHttpRequestFactory = new(new Uri("https://my-hosted-kroki.io"));
```
### How to use
```razor
@using global::Kroki
@using System.IO.Compression
@inject KrokiHttpRequestFactory KrokiHttpRequestFactory

@code {
[Parameter, EditorRequired]
public required string DiagramType { get; set; }
[Parameter, EditorRequired]
public required string DiagramSource { get; set; }
string imageSrc = "";
protected override void OnParametersSet()
{
imageSrc = KrokiHttpRequestFactory.CreateGetRequestUri(new()
{
DiagramType = DiagramType,
OutputFormat = FileFormat.Svg,
DiagramSource = DiagramSource
}, CompressionLevel.Optimal).AbsoluteUri;
}
}
```
## Kroki Server Hosting
### Getting started
```C#
var kroki = builder.AddKrokiServer("kroki")
.WithKrokiMermaidServer()
.WithKrokiBpmnServer()
.WithKrokiExcalidrawServer()
.WithKrokiDiagramsNetServer();
var webFrontend = builder.AddProject("web-frontend")
.WithExternalHttpEndpoints()
.WithReference(kroki)
```
### How to use
Forward to Kroki server in your frontend project.
```C#
using Yarp.ReverseProxy.Forwarder;
using Yarp.ReverseProxy.Transforms;
public static IEndpointConventionBuilder MapKrokiForwarder(this IEndpointRouteBuilder endpoints)
{
return endpoints.MapForwarder("/Kroki/{**apiPath}", "http://kroki", ForwarderRequestConfig.Empty,
transformBuilderContext
=> transformBuilderContext
.AddPathRemovePrefix("/Kroki")
// Custom authorization logic!
.AddRequestTransform(transformContext =>
{
HttpContext httpContext = transformContext.HttpContext;
if(httpContext.User.Identity?.IsAuthenticated != true)
{
httpContext.Response.StatusCode = StatusCodes.Status401Unauthorized;
}
return new();
}));
}
```