git.iko.soy/typeable/servant
on master help sign in
Add preliminary Axios support
commit4362112f31ef5423e4683b0c8244ecdff9e49678 authorFreezeboy <freezeboy@users.noreply.github.com> dateMon 27 Jul 2015, 16:29 (11 years ago) parent2cf2d08
3 files changed+94
servant-js/servant-js.cabal+1 servant-js/src/Servant/JS.hs+5 servant-js/src/Servant/JS/Axios.hs+88
servant-js/servant-js.cabal+1
@@ -36,6 +36,7 @@ flag example
3636library
3737 exposed-modules: Servant.JS
3838 Servant.JS.Angular
39+ Servant.JS.Axios
3940 Servant.JS.JQuery
4041 Servant.JS.Vanilla
4142 Servant.JS.Internal
servant-js/src/Servant/JS.hs+5
@@ -95,6 +95,10 @@ module Servant.JS
9595 , AngularOptions(..)
9696 , defAngularOptions
9797
98+ , -- * Axios code generation
99+ axios
100+ , axiosWith
101+
98102 , -- * Misc.
99103 listFromAPI
100104 , javascript
@@ -106,6 +110,7 @@ module Servant.JS
106110import Data.Proxy
107111import Servant.API
108112import Servant.JS.Angular
113+import Servant.JS.Axios
109114import Servant.JS.Internal
110115import Servant.JS.JQuery
111116import Servant.JS.Vanilla
servant-js/src/Servant/JS/Axios.hsnew file+88
@@ -0,0 +1,88 @@
1+module Servant.JS.Axios where
2+
3+import Servant.JS.Internal
4+import Control.Lens
5+import Data.Char (toLower)
6+import Data.List
7+import Data.Monoid
8+
9+-- | Generate regular javacript functions that use
10+-- the axios library, using default values for 'CommonGeneratorOptions'.
11+axios :: JavaScriptGenerator
12+axios = axiosWith defCommonGeneratorOptions
13+
14+-- | Generate regular javascript functions that use the axios library.
15+axiosWith :: CommonGeneratorOptions -> JavaScriptGenerator
16+axiosWith opts = intercalate "\n\n" . map (generateAxiosJSWith opts)
17+
18+-- | js codegen using axios library using default options
19+generateAxiosJS :: AjaxReq -> String
20+generateAxiosJS = generateAxiosJSWith defCommonGeneratorOptions
21+
22+-- | js codegen using axios library
23+generateAxiosJSWith :: CommonGeneratorOptions -> AjaxReq -> String
24+generateAxiosJSWith opts req = "\n" <>
25+ fname <> " = function(" <> argsStr <> ")\n"
26+ <> "{\n"
27+ <> " return axios." <> method <> "(" <> url <> ",\n"
28+ <> dataBody
29+ <> reqheaders
30+ <> " });\n"
31+ <> "}\n"
32+
33+ where argsStr = intercalate ", " args
34+ args = captures
35+ ++ map (view argName) queryparams
36+ ++ body
37+ ++ map (toValidFunctionName . (<>) "header" . headerArgName) hs
38+
39+ captures = map captureArg
40+ . filter isCapture
41+ $ req ^. reqUrl.path
42+
43+ hs = req ^. reqHeaders
44+
45+ queryparams = req ^.. reqUrl.queryStr.traverse
46+
47+ body = if req ^. reqBody
48+ then [requestBody opts]
49+ else []
50+
51+ dataBody =
52+ if req ^. reqBody
53+ then " , data: JSON.stringify(body)\n" <>
54+ " , responseType: 'json'\n"
55+ else ""
56+
57+ reqheaders =
58+ if null hs
59+ then ""
60+ else " , headers: { " ++ headersStr ++ " }\n"
61+
62+ where headersStr = intercalate ", " $ map headerStr hs
63+ headerStr header = "\"" ++
64+ headerArgName header ++
65+ "\": " ++ show header
66+
67+ namespace =
68+ if hasNoModule
69+ then "var "
70+ else (moduleName opts) <> "."
71+ where
72+ hasNoModule = null (moduleName opts)
73+
74+ fname = namespace <> (functionRenamer opts $ req ^. funcName)
75+
76+ method = map toLower $ req ^. reqMethod
77+ url = if url' == "'" then "'/'" else url'
78+ url' = "'"
79+ ++ urlPrefix opts
80+ ++ urlArgs
81+ ++ queryArgs
82+
83+ urlArgs = jsSegments
84+ $ req ^.. reqUrl.path.traverse
85+
86+ queryArgs = if null queryparams
87+ then ""
88+ else " + '?" ++ jsParams queryparams