@@ -6,28 +6,53 @@ import Data.Char (toLower)
66import Data.List
77import Data.Monoid
88
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+
931-- | Generate regular javacript functions that use
1032-- the axios library, using default values for 'CommonGeneratorOptions'.
11−axios :: JavaScriptGenerator
12−axios = axiosWith defCommonGeneratorOptions
33+axios :: AxiosOptions -> JavaScriptGenerator
34+axios aopts = axiosWith aopts defCommonGeneratorOptions
1335
1436-- | Generate regular javascript functions that use the axios library.
15−axiosWith :: CommonGeneratorOptions -> JavaScriptGenerator
16−axiosWith opts = intercalate "\n\n" . map (generateAxiosJSWith opts)
37+axiosWith :: AxiosOptions -> CommonGeneratorOptions -> JavaScriptGenerator
38+axiosWith aopts opts = intercalate "\n\n" . map (generateAxiosJSWith aopts opts)
1739
1840-- | js codegen using axios library using default options
19−generateAxiosJS :: AjaxReq -> String
20−generateAxiosJS = generateAxiosJSWith defCommonGeneratorOptions
41+generateAxiosJS :: AxiosOptions -> AjaxReq -> String
42+generateAxiosJS aopts = generateAxiosJSWith aopts defCommonGeneratorOptions
2143
2244-- | js codegen using axios library
23−generateAxiosJSWith :: CommonGeneratorOptions -> AjaxReq -> String
24−generateAxiosJSWith opts req = "\n" <>
45+generateAxiosJSWith :: AxiosOptions -> CommonGeneratorOptions -> AjaxReq -> String
46+generateAxiosJSWith aopts opts req = "\n" <>
2547 fname <> " = function(" <> argsStr <> ")\n"
2648 <> "{\n"
2749 <> " return axios({ url: " <> url <> "\n"
2850 <> " , method: '" <> method <> "'\n"
2951 <> dataBody
3052 <> reqheaders
53+ <> withCreds
54+ <> xsrfCookie
55+ <> xsrfHeader
3156 <> " });\n"
3257 <> "}\n"
3358
@@ -55,10 +80,25 @@ generateAxiosJSWith opts req = "\n" <>
5580 " , responseType: 'json'\n"
5681 else ""
5782
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+
5898 reqheaders =
5999 if null hs
60100 then ""
61− else " , headers: { " ++ headersStr ++ " }\n"
101+ else " , headers: { " <> headersStr <> " }\n"
62102
63103 where headersStr = intercalate ", " $ map headerStr hs
64104 headerStr header = "\"" ++