@@ -14,7 +14,7 @@ module Servant.JS.Internal where
1414import Control.Applicative
1515#endif
1616import Control.Lens
17−import Data.Char (toLower)
17+import Data.Char (toLower, toUpper)
1818import qualified Data.CharSet as Set
1919import qualified Data.CharSet.Unicode.Category as Set
2020import Data.List
@@ -29,7 +29,7 @@ import Servant.API
2929-- customize the output
3030data CommonGeneratorOptions = CommonGeneratorOptions
3131 {
32− functionRenamer :: String -> String -- ^ function transforming function names
32+ functionRenamer :: FunctionName -> String -- ^ function transforming function names
3333 , requestBody :: String -- ^ name used when a user want to send the request body (to let you redefine it)
3434 , successCallback :: String -- ^ name of the callback parameter when the request was successful
3535 , errorCallback :: String -- ^ name of the callback parameter when the request reported an error
@@ -40,7 +40,7 @@ data CommonGeneratorOptions = CommonGeneratorOptions
4040--
4141-- @
4242-- > defCommonGeneratorOptions = CommonGeneratorOptions
43−-- > { functionRenamer = id
43+-- > { functionRenamer = concatRenamer
4444-- > , requestBody = "body"
4545-- > , successCallback = "onSuccess"
4646-- > , errorCallback = "onError"
@@ -50,12 +50,31 @@ data CommonGeneratorOptions = CommonGeneratorOptions
5050defCommonGeneratorOptions :: CommonGeneratorOptions
5151defCommonGeneratorOptions = CommonGeneratorOptions
5252 {
53− functionRenamer = id
53+ functionRenamer = concatRenamer
5454 , requestBody = "body"
5555 , successCallback = "onSuccess"
5656 , errorCallback = "onError"
5757 , moduleName = ""
5858 }
59+
60+-- | Function renamer that simply concat each part together
61+concatRenamer :: FunctionName -> String
62+concatRenamer = concat
63+
64+-- | Function renamer using the snake_case convention.
65+-- each part is separated by a single underscore character.
66+snakeCaseRenamer :: FunctionName -> String
67+snakeCaseRenamer = intercalate "_"
68+
69+-- | Function renamer using the CamelCase convention.
70+-- each part begins with an upper case character.
71+camelCaseRenamer :: FunctionName -> String
72+camelCaseRenamer [] = ""
73+camelCaseRenamer (p:ps) = concat $ p : camelCaseRenamer' ps
74+ where camelCaseRenamer' [] = []
75+ camelCaseRenamer' (r:rs) = capitalize r : camelCaseRenamer' rs
76+ capitalize [] = []
77+ capitalize (x:xs) = toUpper x : xs
5978
6079type Arg = String
6180
@@ -146,7 +165,7 @@ data Url = Url
146165defUrl :: Url
147166defUrl = Url [] []
148167
149−type FunctionName = String
168+type FunctionName = [String]
150169type Method = String
151170
152171data AjaxReq = AjaxReq
@@ -223,7 +242,7 @@ paramToStr qarg notTheEnd =
223242 where name = qarg ^. argName
224243
225244defReq :: AjaxReq
226−defReq = AjaxReq defUrl "GET" [] False ""
245+defReq = AjaxReq defUrl "GET" [] False []
227246
228247type family Elem (a :: *) (ls::[*]) :: Constraint where
229248 Elem a '[] = 'False ~ 'True
@@ -256,14 +275,14 @@ instance Elem JSON list => HasJS (Delete list a) where
256275 type JS (Delete list a) = AjaxReq
257276
258277 javascriptFor Proxy req =
259− req & funcName %~ ("delete" <>)
278+ req & funcName %~ ("delete" :)
260279 & reqMethod .~ "DELETE"
261280
262281instance Elem JSON list => HasJS (Get list a) where
263282 type JS (Get list a) = AjaxReq
264283
265284 javascriptFor Proxy req =
266− req & funcName %~ ("get" <>)
285+ req & funcName %~ ("get" :)
267286 & reqMethod .~ "GET"
268287
269288instance (KnownSymbol sym, HasJS sublayout)
@@ -280,14 +299,14 @@ instance Elem JSON list => HasJS (Post list a) where
280299 type JS (Post list a) = AjaxReq
281300
282301 javascriptFor Proxy req =
283− req & funcName %~ ("post" <>)
302+ req & funcName %~ ("post" :)
284303 & reqMethod .~ "POST"
285304
286305instance Elem JSON list => HasJS (Put list a) where
287306 type JS (Put list a) = AjaxReq
288307
289308 javascriptFor Proxy req =
290− req & funcName %~ ("put" <>)
309+ req & funcName %~ ("put" :)
291310 & reqMethod .~ "PUT"
292311
293312instance (KnownSymbol sym, HasJS sublayout)
@@ -355,7 +374,7 @@ instance HasJS Raw where
355374 type JS Raw = Method -> AjaxReq
356375
357376 javascriptFor Proxy req method =
358− req & funcName %~ ((toLower <$> method) <>)
377+ req & funcName %~ ((toLower <$> method) :)
359378 & reqMethod .~ method
360379
361380instance (Elem JSON list, HasJS sublayout) => HasJS (ReqBody list a :> sublayout) where
@@ -372,7 +391,7 @@ instance (KnownSymbol path, HasJS sublayout)
372391 javascriptFor Proxy req =
373392 javascriptFor (Proxy :: Proxy sublayout) $
374393 req & reqUrl.path <>~ [Segment (Static str) []]
375− & funcName %~ (str <>)
394+ & funcName %~ (++ [str])
376395
377396 where str = map (\c -> if c == '.' then '_' else c) $ symbolVal (Proxy :: Proxy path)
378397