Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C#-friendly api #318

Merged
merged 3 commits into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions src/Bolero.Server/Extensions.fs
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,11 @@ type ServerComponentsExtensions =
/// can be passed to use the given mode.
/// </param>
[<Extension>]
static member AddBoleroHost(this: IServiceCollection, ?server: bool, ?prerendered: bool, ?devToggle: bool) =
let server = defaultArg server false
let prerendered = defaultArg prerendered true
let devToggle = defaultArg devToggle true
static member AddBoleroHost(
this: IServiceCollection,
[<Optional; DefaultParameterValue false>] server: bool,
[<Optional; DefaultParameterValue true>] prerendered: bool,
[<Optional; DefaultParameterValue true>] devToggle: bool) =
if devToggle then
this.AddSingleton(
{ new IBoleroHostBaseConfig with
Expand Down Expand Up @@ -119,9 +120,9 @@ type ServerComponentsExtensions =
/// </summary>
/// <param name="page">A function that generates the page to serve.</param>
[<Extension>]
static member MapFallbackToBolero(this: IEndpointRouteBuilder, page: HttpContext -> Bolero.Node) =
static member MapFallbackToBolero(this: IEndpointRouteBuilder, page: Func<HttpContext, Bolero.Node>) =
this.MapFallback(fun ctx ->
let page = page ctx
let page = page.Invoke(ctx)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let page = page.Invoke(ctx)
let page = page.Invoke ctx

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the suggestions! I actually tend to prefer parenthesized syntax for methods though, even if they're single-argument.

if isNull ctx.Response.ContentType then
ctx.Response.ContentType <- "text/html; charset=UTF-8"
ctx.RenderPage(page))
Expand Down
29 changes: 15 additions & 14 deletions src/Bolero/Remoting.Client.fs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ open System
open System.Net
open System.Net.Http
open System.Runtime.CompilerServices
open System.Runtime.InteropServices
open System.Text
open System.Text.Json
open System.Text.Json.Serialization
Expand Down Expand Up @@ -134,12 +135,12 @@ type ClientRemotingExtensions =
static member private ConfigureHttpClientFromEnv(env: IWebAssemblyHostEnvironment) =
fun (httpClient: HttpClient) -> httpClient.BaseAddress <- Uri(env.BaseAddress)

static member private ConfigureSerialization(configureSerialization: option<JsonSerializerOptions -> unit>) =
static member private ConfigureSerialization(configureSerialization: Action<JsonSerializerOptions>) =
{ new IConfigureSerialization with
member _.ConfigureSerialization(serOptions) =
match configureSerialization with
| None -> serOptions.Converters.Add(JsonFSharpConverter())
| Some f -> f serOptions }
| null -> serOptions.Converters.Add(JsonFSharpConverter())
| f -> f.Invoke(serOptions) }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
| f -> f.Invoke(serOptions) }
| f -> f.Invoke serOptions }


/// <summary>Enable support for remoting in ProgramComponent when running in WebAssembly.</summary>
/// <param name="services">The DI service collection.</param>
Expand All @@ -149,10 +150,10 @@ type ClientRemotingExtensions =
/// </param>
/// <returns>The HttpClient builder for remote calls.</returns>
[<Extension>]
static member AddBoleroRemoting(services: IServiceCollection, env: IWebAssemblyHostEnvironment, ?configureSerialization: JsonSerializerOptions -> unit) =
ClientRemotingExtensions.AddRemoting(services,
static member AddBoleroRemoting(services: IServiceCollection, env: IWebAssemblyHostEnvironment, [<Optional>] configureSerialization: Action<JsonSerializerOptions>) =
ClientRemotingExtensions.AddBoleroRemoting(services,
ClientRemotingExtensions.ConfigureHttpClientFromEnv(env),
?configureSerialization = configureSerialization)
configureSerialization)

/// <summary>Enable support for remoting in ProgramComponent when running in WebAssembly.</summary>
/// <param name="services">The DI service collection.</param>
Expand All @@ -163,7 +164,7 @@ type ClientRemotingExtensions =
/// <returns>The HttpClient builder for remote calls.</returns>
[<Extension; Obsolete "Use AddBoleroRemoting">]
static member AddRemoting(services: IServiceCollection, env: IWebAssemblyHostEnvironment, ?configureSerialization: JsonSerializerOptions -> unit) =
services.AddBoleroRemoting(env, ?configureSerialization = configureSerialization)
services.AddBoleroRemoting(env, match configureSerialization with None -> null | Some f -> Action<_> f)

/// <summary>Enable support for remoting in ProgramComponent when running in WebAssembly.</summary>
/// <param name="services">The DI service collection.</param>
Expand All @@ -173,10 +174,10 @@ type ClientRemotingExtensions =
/// </param>
/// <returns>The HttpClient builder for remote calls.</returns>
[<Extension>]
static member AddBoleroRemoting(services: IServiceCollection, configureHttpClient: HttpClient -> unit, ?configureSerialization: JsonSerializerOptions -> unit) : IHttpClientBuilder =
static member AddBoleroRemoting(services: IServiceCollection, configureHttpClient: Action<HttpClient>, [<Optional>] configureSerialization: Action<JsonSerializerOptions>) : IHttpClientBuilder =
services.AddSingleton(ClientRemotingExtensions.ConfigureSerialization(configureSerialization)) |> ignore
services.Add(ServiceDescriptor(typedefof<IRemoteProvider<_>>, typedefof<ClientRemoteProvider<_>>, ServiceLifetime.Singleton))
services.AddHttpClient(ClientRemoteProvider.HttpClientName, configureClient = configureHttpClient)
services.AddHttpClient(ClientRemoteProvider.HttpClientName, configureHttpClient)

/// <summary>Enable support for remoting in ProgramComponent when running in WebAssembly.</summary>
/// <param name="services">The DI service collection.</param>
Expand All @@ -187,7 +188,7 @@ type ClientRemotingExtensions =
/// <returns>The HttpClient builder for remote calls.</returns>
[<Extension; Obsolete "Use AddBoleroRemoting">]
static member AddRemoting(services: IServiceCollection, configureHttpClient: HttpClient -> unit, ?configureSerialization: JsonSerializerOptions -> unit) : IHttpClientBuilder =
services.AddBoleroRemoting(configureHttpClient, ?configureSerialization = configureSerialization)
services.AddBoleroRemoting(configureHttpClient, match configureSerialization with None -> null | Some f -> Action<_> f)

/// <summary>Enable support for the given remote service in ProgramComponent when running in WebAssembly.</summary>
/// <param name="services">The DI service collection.</param>
Expand All @@ -198,10 +199,10 @@ type ClientRemotingExtensions =
/// <typeparam name="Service">The remote service.</typeparam>
/// <returns>The HttpClient builder for remote calls.</returns>
[<Extension>]
static member AddBoleroRemoting<'Service>(services: IServiceCollection, env: IWebAssemblyHostEnvironment, ?configureSerialization: JsonSerializerOptions -> unit) : IHttpClientBuilder =
static member AddBoleroRemoting<'Service>(services: IServiceCollection, env: IWebAssemblyHostEnvironment, [<Optional>] configureSerialization: Action<JsonSerializerOptions>) : IHttpClientBuilder =
ClientRemotingExtensions.AddBoleroRemoting<'Service>(services,
ClientRemotingExtensions.ConfigureHttpClientFromEnv(env),
?configureSerialization = configureSerialization)
configureSerialization)

/// <summary>Enable support for the given remote service in ProgramComponent when running in WebAssembly.</summary>
/// <param name="services">The DI service collection.</param>
Expand All @@ -212,7 +213,7 @@ type ClientRemotingExtensions =
/// <typeparam name="Service">The remote service.</typeparam>
/// <returns>The HttpClient builder for remote calls.</returns>
[<Extension>]
static member AddBoleroRemoting<'Service>(services: IServiceCollection, configureHttpClient: HttpClient -> unit, ?configureSerialization: JsonSerializerOptions -> unit) : IHttpClientBuilder =
static member AddBoleroRemoting<'Service>(services: IServiceCollection, configureHttpClient: Action<HttpClient>, [<Optional>] configureSerialization: Action<JsonSerializerOptions>) : IHttpClientBuilder =
services.AddHttpClient<IRemoteProvider<'Service>, ClientRemoteProvider<'Service>>(factory = fun httpClient services ->
configureHttpClient httpClient
configureHttpClient.Invoke(httpClient)
ClientRemoteProvider<'Service>.Typed(httpClient, ClientRemotingExtensions.ConfigureSerialization(configureSerialization)))
Loading