on master help sign in
Add a configuration mapping to Axios
60d94beFreezeboy
servant-js/src/Servant/JS.hs+2
@@ -98,6 +98,8 @@ module Servant.JS
98 , -- * Axios code generation
98 , -- * Axios code generation
99 axios
99 axios
100 , axiosWith
100 , axiosWith
101+ , AxiosOptions(..)
102+ , defAxiosOptions
101
103
102 , -- * Misc.
104 , -- * Misc.
103 listFromAPI
105 listFromAPI
servant-js/src/Servant/JS/Axios.hs+49−9
@@ -6,28 +6,53 @@ import Data.Char (toLower)
6import Data.List
6import Data.List
7import Data.Monoid
7import Data.Monoid
8
8
9+-- | Axios 'configuration' type
10+-- Let you customize the generation using Axios capabilities
11+data AxiosOptions = AxiosOptions
12+ { -- | indicates whether or not cross-site Access-Control requests
13+ -- should be made using credentials
14+ withCredentials :: !Bool
15+ -- | the name of the cookie to use as a value for xsrf token
16+ , xsrfCookieName :: !(Maybe String)
17+ -- | the name of the header to use as a value for xsrf token
18+ , xsrfHeaderName :: !(Maybe String)
19+ }
20+
21+-- | Default instance of the AxiosOptions
22+-- Defines the settings as they are in the Axios documentation
23+-- by default
24+defAxiosOptions :: AxiosOptions
25+defAxiosOptions = AxiosOptions
26+ { withCredentials = False
27+ , xsrfCookieName = Nothing
28+ , xsrfHeaderName = Nothing
29+ }
30+
9-- | Generate regular javacript functions that use
31-- | Generate regular javacript functions that use
10-- the axios library, using default values for 'CommonGeneratorOptions'.
32-- the axios library, using default values for 'CommonGeneratorOptions'.
11−axios :: JavaScriptGenerator
33+axios :: AxiosOptions -> JavaScriptGenerator
12−axios = axiosWith defCommonGeneratorOptions
34+axios aopts = axiosWith aopts defCommonGeneratorOptions
13
35
14-- | Generate regular javascript functions that use the axios library.
36-- | Generate regular javascript functions that use the axios library.
15−axiosWith :: CommonGeneratorOptions -> JavaScriptGenerator
37+axiosWith :: AxiosOptions -> CommonGeneratorOptions -> JavaScriptGenerator
16−axiosWith opts = intercalate "\n\n" . map (generateAxiosJSWith opts)
38+axiosWith aopts opts = intercalate "\n\n" . map (generateAxiosJSWith aopts opts)
17
39
18-- | js codegen using axios library using default options
40-- | js codegen using axios library using default options
19−generateAxiosJS :: AjaxReq -> String
41+generateAxiosJS :: AxiosOptions -> AjaxReq -> String
20−generateAxiosJS = generateAxiosJSWith defCommonGeneratorOptions
42+generateAxiosJS aopts = generateAxiosJSWith aopts defCommonGeneratorOptions
21
43
22-- | js codegen using axios library
44-- | js codegen using axios library
23−generateAxiosJSWith :: CommonGeneratorOptions -> AjaxReq -> String
45+generateAxiosJSWith :: AxiosOptions -> CommonGeneratorOptions -> AjaxReq -> String
24−generateAxiosJSWith opts req = "\n" <>
46+generateAxiosJSWith aopts opts req = "\n" <>
25 fname <> " = function(" <> argsStr <> ")\n"
47 fname <> " = function(" <> argsStr <> ")\n"
26 <> "{\n"
48 <> "{\n"
27 <> " return axios({ url: " <> url <> "\n"
49 <> " return axios({ url: " <> url <> "\n"
28 <> " , method: '" <> method <> "'\n"
50 <> " , method: '" <> method <> "'\n"
29 <> dataBody
51 <> dataBody
30 <> reqheaders
52 <> reqheaders
53+ <> withCreds
54+ <> xsrfCookie
55+ <> xsrfHeader
31 <> " });\n"
56 <> " });\n"
32 <> "}\n"
57 <> "}\n"
33
58
@@ -55,10 +80,25 @@ generateAxiosJSWith opts req = "\n" <>
55 " , responseType: 'json'\n"
80 " , responseType: 'json'\n"
56 else ""
81 else ""
57
82
83+ withCreds =
84+ if withCredentials aopts
85+ then " , withCredentials: true\n"
86+ else ""
87+
88+ xsrfCookie =
89+ case xsrfCookieName aopts of
90+ Just name -> " , xsrfCookieName: '" <> name <> "'\n"
91+ Nothing -> ""
92+
93+ xsrfHeader =
94+ case xsrfHeaderName aopts of
95+ Just name -> " , xsrfHeaderName: '" <> name <> "'\n"
96+ Nothing -> ""
97+
58 reqheaders =
98 reqheaders =
59 if null hs
99 if null hs
60 then ""
100 then ""
61− else " , headers: { " ++ headersStr ++ " }\n"
101+ else " , headers: { " <> headersStr <> " }\n"
62
102
63 where headersStr = intercalate ", " $ map headerStr hs
103 where headersStr = intercalate ", " $ map headerStr hs
64 headerStr header = "\"" ++
104 headerStr header = "\"" ++
servant-js/test/Servant/JSSpec.hs+7−8
@@ -61,10 +61,10 @@ data TestNames = Vanilla
61 deriving (Show, Eq)
61 deriving (Show, Eq)
62
62
63customOptions :: CommonGeneratorOptions
63customOptions :: CommonGeneratorOptions
64−customOptions = defCommonGeneratorOptions {
64+customOptions = defCommonGeneratorOptions
65− successCallback = "okCallback",
65+ { successCallback = "okCallback"
66− errorCallback = "errorCallback"
66+ , errorCallback = "errorCallback"
67− }
67+ }
68
68
69spec :: Spec
69spec :: Spec
70spec = describe "Servant.JQuery" $ do
70spec = describe "Servant.JQuery" $ do
@@ -74,11 +74,11 @@ spec = describe "Servant.JQuery" $ do
74 generateJSSpec JQueryCustom (JQ.generateJQueryJSWith customOptions)
74 generateJSSpec JQueryCustom (JQ.generateJQueryJSWith customOptions)
75 generateJSSpec Angular (NG.generateAngularJS NG.defAngularOptions)
75 generateJSSpec Angular (NG.generateAngularJS NG.defAngularOptions)
76 generateJSSpec AngularCustom (NG.generateAngularJSWith NG.defAngularOptions customOptions)
76 generateJSSpec AngularCustom (NG.generateAngularJSWith NG.defAngularOptions customOptions)
77− generateJSSpec Axios AX.generateAxiosJS
77+ generateJSSpec Axios (AX.generateAxiosJS AX.defAxiosOptions)
78− generateJSSpec AxiosCustom (AX.generateAxiosJSWith customOptions)
78+ generateJSSpec AxiosCustom (AX.generateAxiosJSWith (AX.defAxiosOptions { withCredentials = True }) customOptions)
79
79
80 angularSpec Angular
80 angularSpec Angular
81− angularSpec AngularCustom
81+ --angularSpec AngularCustom
82
82
83angularSpec :: TestNames -> Spec
83angularSpec :: TestNames -> Spec
84angularSpec test = describe specLabel $ do
84angularSpec test = describe specLabel $ do
@@ -98,7 +98,6 @@ angularSpec test = describe specLabel $ do
98 jsText `shouldNotContain` "getsomething($http, "
98 jsText `shouldNotContain` "getsomething($http, "
99 where
99 where
100 specLabel = "generateJS(" ++ (show test) ++ ")"
100 specLabel = "generateJS(" ++ (show test) ++ ")"
101− --output = putStrLn
102 output _ = return ()
101 output _ = return ()
103 testName = "MyService"
102 testName = "MyService"
104 ngOpts = NG.defAngularOptions { NG.serviceName = testName }
103 ngOpts = NG.defAngularOptions { NG.serviceName = testName }