@@ -6,28 +6,53 @@ import Data.Char (toLower)
6import Data.List
6import Data.List
7import Data.Monoid
7import Data.Monoid
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)
21+-- | Default instance of the AxiosOptions
22+-- Defines the settings as they are in the Axios documentation
24+defAxiosOptions :: AxiosOptions
25+defAxiosOptions = AxiosOptions
26+ { withCredentials = False
27+ , xsrfCookieName = Nothing
28+ , xsrfHeaderName = Nothing
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
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)
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
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"
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
31 <> " });\n"
56 <> " });\n"
@@ -55,10 +80,25 @@ generateAxiosJSWith opts req = "\n" <>
55 " , responseType: 'json'\n"
80 " , responseType: 'json'\n"
84+ if withCredentials aopts
85+ then " , withCredentials: true\n"
89+ case xsrfCookieName aopts of
90+ Just name -> " , xsrfCookieName: '" <> name <> "'\n"
94+ case xsrfHeaderName aopts of
95+ Just name -> " , xsrfHeaderName: '" <> name <> "'\n"
58 reqheaders =
98 reqheaders =
59 if null hs
99 if null hs
61− else " , headers: { " ++ headersStr ++ " }\n"
101+ else " , headers: { " <> headersStr <> " }\n"
63 where headersStr = intercalate ", " $ map headerStr hs
103 where headersStr = intercalate ", " $ map headerStr hs
64 headerStr header = "\"" ++
104 headerStr header = "\"" ++