@@ -14,7 +14,7 @@ module Servant.JS.Internal where
14import Control.Applicative
14import Control.Applicative
16import Control.Lens
16import Control.Lens
17−import Data.Char (toLower)
17+import Data.Char (toLower, toUpper)
18import qualified Data.CharSet as Set
18import qualified Data.CharSet as Set
19import qualified Data.CharSet.Unicode.Category as Set
19import qualified Data.CharSet.Unicode.Category as Set
20import Data.List
20import Data.List
@@ -29,7 +29,7 @@ import Servant.API
29-- customize the output
29-- customize the output
30data CommonGeneratorOptions = CommonGeneratorOptions
30data CommonGeneratorOptions = CommonGeneratorOptions
32− functionRenamer :: String -> String -- ^ function transforming function names
32+ functionNameBuilder :: FunctionName -> String -- ^ function generating function names
33 , requestBody :: String -- ^ name used when a user want to send the request body (to let you redefine it)
33 , requestBody :: String -- ^ name used when a user want to send the request body (to let you redefine it)
34 , successCallback :: String -- ^ name of the callback parameter when the request was successful
34 , successCallback :: String -- ^ name of the callback parameter when the request was successful
35 , errorCallback :: String -- ^ name of the callback parameter when the request reported an error
35 , errorCallback :: String -- ^ name of the callback parameter when the request reported an error
@@ -41,7 +41,7 @@ data CommonGeneratorOptions = CommonGeneratorOptions
43-- > defCommonGeneratorOptions = CommonGeneratorOptions
43-- > defCommonGeneratorOptions = CommonGeneratorOptions
44−-- > { functionRenamer = id
44+-- > { functionNameBuilder = camelCase
45-- > , requestBody = "body"
45-- > , requestBody = "body"
46-- > , successCallback = "onSuccess"
46-- > , successCallback = "onSuccess"
47-- > , errorCallback = "onError"
47-- > , errorCallback = "onError"
@@ -52,13 +52,32 @@ data CommonGeneratorOptions = CommonGeneratorOptions
52defCommonGeneratorOptions :: CommonGeneratorOptions
52defCommonGeneratorOptions :: CommonGeneratorOptions
53defCommonGeneratorOptions = CommonGeneratorOptions
53defCommonGeneratorOptions = CommonGeneratorOptions
55− functionRenamer = id
55+ functionNameBuilder = camelCase
56 , requestBody = "body"
56 , requestBody = "body"
57 , successCallback = "onSuccess"
57 , successCallback = "onSuccess"
58 , errorCallback = "onError"
58 , errorCallback = "onError"
59 , moduleName = ""
59 , moduleName = ""
60 , urlPrefix = ""
60 , urlPrefix = ""
63+-- | Function name builder that simply concat each part together
64+concatCase :: FunctionName -> String
67+-- | Function name builder using the snake_case convention.
68+-- each part is separated by a single underscore character.
69+snakeCase :: FunctionName -> String
70+snakeCase = intercalate "_"
72+-- | Function name builder using the CamelCase convention.
73+-- each part begins with an upper case character.
74+camelCase :: FunctionName -> String
76+camelCase (p:ps) = concat $ p : camelCase' ps
77+ where camelCase' [] = []
78+ camelCase' (r:rs) = capitalize r : camelCase' rs
80+ capitalize (x:xs) = toUpper x : xs
63type Arg = String
82type Arg = String
@@ -149,7 +168,7 @@ data Url = Url
149defUrl :: Url
168defUrl :: Url
150defUrl = Url [] []
169defUrl = Url [] []
152−type FunctionName = String
171+type FunctionName = [String]
153type Method = String
172type Method = String
155data AjaxReq = AjaxReq
174data AjaxReq = AjaxReq
@@ -226,7 +245,7 @@ paramToStr qarg notTheEnd =
226 where name = qarg ^. argName
245 where name = qarg ^. argName
228defReq :: AjaxReq
247defReq :: AjaxReq
229−defReq = AjaxReq defUrl "GET" [] False ""
248+defReq = AjaxReq defUrl "GET" [] False []
231type family Elem (a :: *) (ls::[*]) :: Constraint where
250type family Elem (a :: *) (ls::[*]) :: Constraint where
232 Elem a '[] = 'False ~ 'True
251 Elem a '[] = 'False ~ 'True
@@ -252,6 +271,7 @@ instance (KnownSymbol sym, HasJS sublayout)
252 javascriptFor Proxy req =
271 javascriptFor Proxy req =
253 javascriptFor (Proxy :: Proxy sublayout) $
272 javascriptFor (Proxy :: Proxy sublayout) $
254 req & reqUrl.path <>~ [Segment (Cap str) []]
273 req & reqUrl.path <>~ [Segment (Cap str) []]
274+ & funcName %~ (++ ["by", str])
256 where str = symbolVal (Proxy :: Proxy sym)
276 where str = symbolVal (Proxy :: Proxy sym)
@@ -259,14 +279,14 @@ instance Elem JSON list => HasJS (Delete list a) where
259 type JS (Delete list a) = AjaxReq
279 type JS (Delete list a) = AjaxReq
261 javascriptFor Proxy req =
281 javascriptFor Proxy req =
262− req & funcName %~ ("delete" <>)
282+ req & funcName %~ ("delete" :)
263 & reqMethod .~ "DELETE"
283 & reqMethod .~ "DELETE"
265instance Elem JSON list => HasJS (Get list a) where
285instance Elem JSON list => HasJS (Get list a) where
266 type JS (Get list a) = AjaxReq
286 type JS (Get list a) = AjaxReq
268 javascriptFor Proxy req =
288 javascriptFor Proxy req =
269− req & funcName %~ ("get" <>)
289+ req & funcName %~ ("get" :)
270 & reqMethod .~ "GET"
290 & reqMethod .~ "GET"
272instance (KnownSymbol sym, HasJS sublayout)
292instance (KnownSymbol sym, HasJS sublayout)
@@ -283,14 +303,14 @@ instance Elem JSON list => HasJS (Post list a) where
283 type JS (Post list a) = AjaxReq
303 type JS (Post list a) = AjaxReq
285 javascriptFor Proxy req =
305 javascriptFor Proxy req =
286− req & funcName %~ ("post" <>)
306+ req & funcName %~ ("post" :)
287 & reqMethod .~ "POST"
307 & reqMethod .~ "POST"
289instance Elem JSON list => HasJS (Put list a) where
309instance Elem JSON list => HasJS (Put list a) where
290 type JS (Put list a) = AjaxReq
310 type JS (Put list a) = AjaxReq
292 javascriptFor Proxy req =
312 javascriptFor Proxy req =
293− req & funcName %~ ("put" <>)
313+ req & funcName %~ ("put" :)
294 & reqMethod .~ "PUT"
314 & reqMethod .~ "PUT"
296instance (KnownSymbol sym, HasJS sublayout)
316instance (KnownSymbol sym, HasJS sublayout)
@@ -358,7 +378,7 @@ instance HasJS Raw where
358 type JS Raw = Method -> AjaxReq
378 type JS Raw = Method -> AjaxReq
360 javascriptFor Proxy req method =
380 javascriptFor Proxy req method =
361− req & funcName %~ ((toLower <$> method) <>)
381+ req & funcName %~ ((toLower <$> method) :)
362 & reqMethod .~ method
382 & reqMethod .~ method
364instance (Elem JSON list, HasJS sublayout) => HasJS (ReqBody list a :> sublayout) where
384instance (Elem JSON list, HasJS sublayout) => HasJS (ReqBody list a :> sublayout) where
@@ -375,7 +395,7 @@ instance (KnownSymbol path, HasJS sublayout)
375 javascriptFor Proxy req =
395 javascriptFor Proxy req =
376 javascriptFor (Proxy :: Proxy sublayout) $
396 javascriptFor (Proxy :: Proxy sublayout) $
377 req & reqUrl.path <>~ [Segment (Static str) []]
397 req & reqUrl.path <>~ [Segment (Static str) []]
378− & funcName %~ (str <>)
398+ & funcName %~ (++ [str])
380 where str = map (\c -> if c == '.' then '_' else c) $ symbolVal (Proxy :: Proxy path)
400 where str = map (\c -> if c == '.' then '_' else c) $ symbolVal (Proxy :: Proxy path)