servant-aeson-generics-typescript: Generates a TypeScript client for Servant APIs

[ bsd3, library, program, web ] [ Propose Tags ]

This project leveratges aeson-generics-typescript to generate type safe API bindings in TypeScript for a given Servant API. Included here are tests that round trip by compling the TypeScript with tsc, running the client in nodejs, and checking that request response round trips with the Servant server.


[Skip to Readme]

Modules

[Index] [Quick Jump]

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

  • No Candidates
Versions [RSS] 0.0.0.1, 0.0.0.2
Dependencies aeson (>=2.1.2 && <2.2), aeson-generics-typescript (>=0.0.0 && <0.1), async (>=2.2.4 && <2.3), base (>=4.17.2 && <4.18), bytestring (>=0.11.5 && <0.12), containers (>=0.6.7 && <0.7), directory (>=1.3.7 && <1.4), filepath (>=1.4.2 && <1.5), hspec (>=2.11.7 && <2.12), hspec-wai (>=0.11.1 && <0.12), http-types (>=0.12.3 && <0.13), jose-jwt (>=0.9.6 && <0.10), process (>=1.6.17 && <1.7), QuickCheck (>=2.14.3 && <2.15), random (>=1.2.1 && <1.3), servant (>=0.20.1 && <0.21), servant-auth (>=0.4.1 && <0.5), servant-server (>=0.20 && <0.21), split (>=0.2.4 && <0.3), string-interpolate (>=0.3.2 && <0.4), text (>=2.0.2 && <2.1), time (>=1.12.2 && <1.13), warp (>=3.3.30 && <3.4) [details]
License BSD-3-Clause
Copyright 2023
Author Platonic.Systems
Maintainer info@Platonic.Systems
Category Web
Source repo head: git clone https://gitlab.com/platonic/servant-aeson-generics-typescript.git
Uploaded by fresheyeball at 2023-12-14T18:45:26Z
Distributions
Executables tests
Downloads 52 total (5 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs uploaded by user
Build status unknown [no reports yet]

Readme for servant-aeson-generics-typescript-0.0.0.1

[back to package description]

Servant Aeson Generics TypeScript

This project leveratges aeson-generics-typescript to generate type safe API bindings in TypeScript for a given Servant API. Included here are tests that round trip by compling the TypeScript with tsc, running the client in nodejs, and checking that request response round trips with the Servant server.

data Foo = ...
  deriving stock (Generic)
  deriving anyclass (ToJSON, FromJSON, TypeScriptDefinition)

type API = "foo" :> Get '[JSON] Foo

Is all it takes to have a TypeScript Definition, and TypeScript client. Now you can obtain the TypeScript client as a string like so.

client :: String
client = gen @API

Example

You can see many examples in the tests. One provided here for documentation purposes:

data Foo = Foo { thang :: String, otherThang :: Int }
  deriving stock (Generic)
  deriving anyclass (ToJSON, FromJSON, TypeScriptDefinition)

type API = "foo" :> Capture "bar" Int :> Post '[JSON] Foo

client = tsClient @'[Foo] @API

will generate

// Defined in Servant.Client.TypeScriptSpec of main
interface Foo {
  // readonly tag: "Foo";
  readonly thang: string;
  readonly otherThang: number;
}
const API = {
  base: "",
  "/foo/:bar": (bar:number): Promise<Foo> => {
    const uri = `${API.base}/foo/${bar}`;
    return fetch(uri, {
      method: "POST"
    }).then(res => res.json());
  }
};