All Projects → codedownio → aeson-typescript

codedownio / aeson-typescript

Licence: BSD-3-Clause license
Generate TypeScript definition files from your ADTs

Programming Languages

haskell
3896 projects

Labels

Projects that are alternatives of or similar to aeson-typescript

browser-apis
🦄 Cool & Fun Browser Web APIs 🥳
Stars: ✭ 21 (-54.35%)
Mutual labels:  apis
codetabs
Free Online Services. Github/GitLab star history. Count Lines of Code. CORS proxy server. IP GeoLocation. Convert Video to Gif. HTTP Headers. Api weather temp. Alexa ranking.
Stars: ✭ 114 (+147.83%)
Mutual labels:  apis
apis
The API (CRD) of Volcano
Stars: ✭ 16 (-65.22%)
Mutual labels:  apis
toolbox
A collection of tools, APIs and other resources to use in creative coding web projects.
Stars: ✭ 71 (+54.35%)
Mutual labels:  apis
wiremock
A tool for mocking HTTP services
Stars: ✭ 5,239 (+11289.13%)
Mutual labels:  apis
Open-APIs
Collection of Unlimited APIs
Stars: ✭ 55 (+19.57%)
Mutual labels:  apis
MyAPI
A template to create awesome APIs easily ⚡️
Stars: ✭ 117 (+154.35%)
Mutual labels:  apis
maikai
RFC-compliant, Kubernetes-ready, intelligent health check middleware for HTTP APIs and microservices written in Node
Stars: ✭ 23 (-50%)
Mutual labels:  apis
awesome.gl
棒棒哒攻略:Developer's Technical Documents, API References, Code Examples, Quick Starts, Programming minutebooks, and Tutorials. https://awesome.gl
Stars: ✭ 12 (-73.91%)
Mutual labels:  apis
node-freshdesk-api
Node wrapper for Freshdesk v2 API
Stars: ✭ 24 (-47.83%)
Mutual labels:  apis
aioapi
Yet another way to build APIs using AIOHTTP framework
Stars: ✭ 14 (-69.57%)
Mutual labels:  apis
acme-freight
Acme Freight's Logistics Wizard application is composed of several microservices, including three Cloud Foundry applications, LoopBack, API Connect, and multiple Cloud Function actions.
Stars: ✭ 43 (-6.52%)
Mutual labels:  apis
Apihouse
Get the collective list of free APIs for your next side project
Stars: ✭ 125 (+171.74%)
Mutual labels:  apis
api-principles
The applicable SBB API Principles
Stars: ✭ 31 (-32.61%)
Mutual labels:  apis
Indian-government-API-List
A curated list of official APIs owned by government of India.
Stars: ✭ 65 (+41.3%)
Mutual labels:  apis
altinn-studio
Next generation open source Altinn platform and applications.
Stars: ✭ 91 (+97.83%)
Mutual labels:  apis
Bundli-Frontend
Bundli-Frontend 🚩 is a repo which contains 🚀different code of full stack Development 👨‍💻🚀
Stars: ✭ 133 (+189.13%)
Mutual labels:  apis
KotlinReactiveMS
An educational project to learn reactive programming with Spring 5 and Kotlin
Stars: ✭ 33 (-28.26%)
Mutual labels:  apis
Beef
Business Entity Execution Framework
Stars: ✭ 95 (+106.52%)
Mutual labels:  apis
option chain analysis
NSE Nifty Option chain analysis on the web page.
Stars: ✭ 63 (+36.96%)
Mutual labels:  apis

Welcome to aeson-typescript Hackage aeson-typescript

This library provides a way to generate TypeScript .d.ts files that match your existing Aeson ToJSON instances. If you already use Aeson's Template Haskell support to derive your instances, then deriving TypeScript is as simple as

$(deriveTypeScript myAesonOptions ''MyType)

For example,

data D a = Nullary
         | Unary Int
         | Product String Char a
         | Record { testOne   :: Double
                  , testTwo   :: Bool
                  , testThree :: D a
                  } deriving Eq

Next we derive the necessary instances.

$(deriveTypeScript (defaultOptions {fieldLabelModifier = drop 4, constructorTagModifier = map toLower}) ''D)

Now we can use the newly created instances.

>>> putStrLn $ formatTSDeclarations $ getTypeScriptDeclaration (Proxy :: Proxy (D T))

type D<T> = "nullary" | IUnary<T> | IProduct<T> | IRecord<T>;

type IUnary<T> = number;

type IProduct<T> = [string, string, T];

interface IRecord<T> {
  tag: "record";
  One: number;
  Two: boolean;
  Three: D<T>;
}

It's important to make sure your JSON and TypeScript are being derived with the same options. For this reason, we include the convenience HasJSONOptions typeclass, which lets you write the options only once, like this:

instance HasJSONOptions MyType where getJSONOptions _ = (defaultOptions {fieldLabelModifier = drop 4})

$(deriveJSON (getJSONOptions (Proxy :: Proxy MyType)) ''MyType)
$(deriveTypeScript (getJSONOptions (Proxy :: Proxy MyType)) ''MyType)

Or, if you want to be even more concise and don't mind defining the instances in the same file,

myOptions = defaultOptions {fieldLabelModifier = drop 4}

$(deriveJSONAndTypeScript myOptions ''MyType)

Remembering that the Template Haskell Q monad is an ordinary monad, you can derive instances for several types at once like this:

$(mconcat <$> traverse (deriveJSONAndTypeScript myOptions) [''MyType1, ''MyType2, ''MyType3])

Suggestions for use

This library was written to make it easier to typecheck your TypeScript frontend against your Haskell backend. Here's how I like to integrate it into my workflow:

The idea is to set up a separate Haskell executable in your Cabal file whose sole purpose is to generate types. For example, in your hpack package.yaml file add a new executable like this:

executables:
  ...
  tsdef:
    main: Main.hs
    source-dirs: tsdef
    dependencies:
    - my-main-app
    ...

And tsdef/Main.hs should look like this:

module Main where

import Data.Proxy
import Data.Monoid
import MyLibraries

$(deriveTypeScript (getJSONOptions (Proxy :: Proxy MyType1)) ''MyType1)
$(deriveTypeScript (getJSONOptions (Proxy :: Proxy MyType2)) ''MyType2)
...

main = putStrLn $ formatTSDeclarations (
  (getTypeScriptDeclaration (Proxy :: Proxy MyType1)) <>
  (getTypeScriptDeclaration (Proxy :: Proxy MyType2)) <>
  ...
)

Now you can generate the types by running stack runhaskell tsdef/Main.hs > types.d.ts. I like to make this an automatic step in my Gulpfile, Webpack config, etc.

See also

If you want a much more opinionated web framework for generating APIs, check out servant. (Although it doesn't seem to support TypeScript client generation at the moment.)

For another very powerful framework that can generate TypeScript client code based on an API specification, see Swagger/OpenAPI.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].