packages feed

aeson-typescript 0.1.0.0 → 0.1.0.1

raw patch · 3 files changed

+53/−8 lines, 3 filesPVP ok

version bump matches the API change (PVP)

API changes (from Hackage documentation)

Files

README.md view
@@ -1,5 +1,5 @@ -# Welcome to `aeson-typescript` [![Hackage](https://img.shields.io/hackage/v/aeson.svg)](https://hackage.haskell.org/package/aeson-typescript) [![Build Status](https://travis-ci.org/bos/aeson.svg)](https://travis-ci.org/codedownio/aeson-typescript)+# Welcome to `aeson-typescript` [![Hackage](https://img.shields.io/hackage/v/aeson-typescript.svg)](https://hackage.haskell.org/package/aeson-typescript) [![Build Status](https://travis-ci.org/codedownio/aeson-typescript.svg)](https://travis-ci.org/codedownio/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@@ -46,7 +46,7 @@ ```  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:+include the convenience `HasJSONOptions` typeclass, which lets you write the options only once, like this:  ```haskell instance HasJSONOptions MyType where getJSONOptions _ = (defaultOptions {fieldLabelModifier = drop 4})@@ -54,3 +54,49 @@ $(deriveJSON (getJSONOptions (Proxy :: Proxy MyType)) ''MyType) $(deriveTypeScript (getJSONOptions (Proxy :: Proxy MyType)) ''MyType) ```++# 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:++```yaml+executables:+  ...+  tsdef:+    main: Main.hs+    source-dirs: tsdef+    dependencies:+    - my-main-app+    ...+```++And `tsdef/Main.hs` should look like this:++```haskell+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](http://haskell-servant.readthedocs.io/en/stable/). (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](https://github.com/swagger-api/swagger-codegen).
aeson-typescript.cabal view
@@ -2,10 +2,10 @@ -- -- see: https://github.com/sol/hpack ----- hash: b7b8cc9fabc07f52cfa39a27fd83eaaab74f5169d238b970eee90a8f9138f9fa+-- hash: 6b93db57cec1ebbb4a02367edfecba2f5bd0b15ba1e267563fa81587a9e091df  name:           aeson-typescript-version:        0.1.0.0+version:        0.1.0.1 synopsis:       Generate TypeScript definition files from your ADTs description:    Please see the README on Github at <https://github.com/codedownio/aeson-typescript#readme> category:       Text, Web, JSON
test/Util.hs view
@@ -19,7 +19,8 @@ import System.IO.Temp import System.Process -localTSC = "test_assets/node_modules/.bin/tsc"+yarnInstallScript = "test/assets/yarn_install.sh"+localTSC = "test/assets/node_modules/.bin/tsc"  isCI :: IO Bool isCI = lookupEnv "CI" >>= (return . (== (Just "true")))@@ -29,10 +30,8 @@   isCI <- isCI   case isCI of     True -> do-      putStrLn "Using global TSC"       return "tsc" -- Assume it's set up on the path     False -> do-      putStrLn "Using local TSC"       ensureTSCExists       return localTSC @@ -85,7 +84,7 @@ ensureTSCExists = doesFileExist localTSC >>= \exists -> when (not exists) $ void $ do   cwd <- getCurrentDirectory   putStrLn [i|Invoking yarn to install tsc compiler (make sure yarn is installed). CWD is #{cwd}|]-  (exitCode, stdout, stderr) <- readProcessWithExitCode "test_assets/yarn_install.sh" [] ""+  (exitCode, stdout, stderr) <- readProcessWithExitCode yarnInstallScript [] ""   when (exitCode /= ExitSuccess) $ putStrLn [i|Error installing yarn: '#{stderr}', '#{stdout}'|]