Add preliminary Axios support

This commit is contained in:
Freezeboy 2015-07-27 16:29:08 +02:00
parent 2cf2d08cd3
commit 4362112f31
3 changed files with 94 additions and 0 deletions

View file

@ -36,6 +36,7 @@ flag example
library library
exposed-modules: Servant.JS exposed-modules: Servant.JS
Servant.JS.Angular Servant.JS.Angular
Servant.JS.Axios
Servant.JS.JQuery Servant.JS.JQuery
Servant.JS.Vanilla Servant.JS.Vanilla
Servant.JS.Internal Servant.JS.Internal

View file

@ -95,6 +95,10 @@ module Servant.JS
, AngularOptions(..) , AngularOptions(..)
, defAngularOptions , defAngularOptions
, -- * Axios code generation
axios
, axiosWith
, -- * Misc. , -- * Misc.
listFromAPI listFromAPI
, javascript , javascript
@ -106,6 +110,7 @@ module Servant.JS
import Data.Proxy import Data.Proxy
import Servant.API import Servant.API
import Servant.JS.Angular import Servant.JS.Angular
import Servant.JS.Axios
import Servant.JS.Internal import Servant.JS.Internal
import Servant.JS.JQuery import Servant.JS.JQuery
import Servant.JS.Vanilla import Servant.JS.Vanilla

View file

@ -0,0 +1,88 @@
module Servant.JS.Axios where
import Servant.JS.Internal
import Control.Lens
import Data.Char (toLower)
import Data.List
import Data.Monoid
-- | Generate regular javacript functions that use
-- the axios library, using default values for 'CommonGeneratorOptions'.
axios :: JavaScriptGenerator
axios = axiosWith defCommonGeneratorOptions
-- | Generate regular javascript functions that use the axios library.
axiosWith :: CommonGeneratorOptions -> JavaScriptGenerator
axiosWith opts = intercalate "\n\n" . map (generateAxiosJSWith opts)
-- | js codegen using axios library using default options
generateAxiosJS :: AjaxReq -> String
generateAxiosJS = generateAxiosJSWith defCommonGeneratorOptions
-- | js codegen using axios library
generateAxiosJSWith :: CommonGeneratorOptions -> AjaxReq -> String
generateAxiosJSWith opts req = "\n" <>
fname <> " = function(" <> argsStr <> ")\n"
<> "{\n"
<> " return axios." <> method <> "(" <> url <> ",\n"
<> dataBody
<> reqheaders
<> " });\n"
<> "}\n"
where argsStr = intercalate ", " args
args = captures
++ map (view argName) queryparams
++ body
++ map (toValidFunctionName . (<>) "header" . headerArgName) hs
captures = map captureArg
. filter isCapture
$ req ^. reqUrl.path
hs = req ^. reqHeaders
queryparams = req ^.. reqUrl.queryStr.traverse
body = if req ^. reqBody
then [requestBody opts]
else []
dataBody =
if req ^. reqBody
then " , data: JSON.stringify(body)\n" <>
" , responseType: 'json'\n"
else ""
reqheaders =
if null hs
then ""
else " , headers: { " ++ headersStr ++ " }\n"
where headersStr = intercalate ", " $ map headerStr hs
headerStr header = "\"" ++
headerArgName header ++
"\": " ++ show header
namespace =
if hasNoModule
then "var "
else (moduleName opts) <> "."
where
hasNoModule = null (moduleName opts)
fname = namespace <> (functionRenamer opts $ req ^. funcName)
method = map toLower $ req ^. reqMethod
url = if url' == "'" then "'/'" else url'
url' = "'"
++ urlPrefix opts
++ urlArgs
++ queryArgs
urlArgs = jsSegments
$ req ^.. reqUrl.path.traverse
queryArgs = if null queryparams
then ""
else " + '?" ++ jsParams queryparams