fable-compiler / Fable

F# to JavaScript, TypeScript, Python, Rust and Dart Compiler
http://fable.io/
MIT License
2.92k stars 300 forks source link

Theoretical question: Client Library #3876

Closed dcook-net closed 2 months ago

dcook-net commented 2 months ago

Description

I have a web service that my team built and maintains, with various different endpoints. We've also build a client library to encapsulate logic for talking to this service, which encapsulates things like authentication, caching, retries, etc....because we have serveral consuming services and we didn't want to duplicate that logic.

The library is built in .net and distributed over github.

Now we need to provide another client to our javascript and typescript consumers (including node).

I've been pondering the idea of re-writing the original .net client (originally c#) in F# and then generating the TypesScript and js with Fable, rather than having to build and maintain 2 versions.

I've only ever done very basic proof of concepts with Fable. So my question, before I'm embark on such as task, is: do you think this is possible?

What challenges are we likely to encounter?

MangelMaxime commented 2 months ago

Hello,

It should be possible to do. The main difficulty you can face is if you are using API not supported by Fable. You can see a summary of it here.

I also think the main difficulty you will face is if your library does HTTP call there is no support HttpClient library in Fable you will need to use compiler directives to make you .NET version use HttpClient and Fable/JavaScript version use fetch for example.

let getData () =
    async {
        #if FABLE_COMPILER
        // use fetch
        #else
        // use HttpClient
        #endif
    }

In your project you will probably create a small helper/library to make a common API between HttpClient/Fetch and don't have to worry about it in the rest of your project.

This blog post can also interest you I believe, it explains how they use Fable to support several targets like .NET, JavaScript, Python, etc.

dcook-net commented 2 months ago

Thanks, this is great info, thanks so much