git.iko.soy/typeable/servant
on master help sign in
Add some haddock, rename 'functionName'
commitce43317eeb4445468850d9aa80117dfed265ed4f authorFreezeboy <freezeboy@users.noreply.github.com> dateSat 18 Jul 2015, 10:51 (11 years ago) parent294143a
4 files changed+45−34
servant-jquery/src/Servant/JQuery/Angular.hs+20−13 servant-jquery/src/Servant/JQuery/Internal.hs+18−16 servant-jquery/src/Servant/JQuery/JQuery.hs+4−3 servant-jquery/src/Servant/JQuery/Vanilla.hs+3−2
servant-jquery/src/Servant/JQuery/Angular.hs+20−13
@@ -5,23 +5,29 @@ import Control.Lens
55import Data.List
66import Data.Monoid
77
8−data AngularOptions = AngularOptions {
9− serviceName :: String,
10− prologue :: String -> String -> String,
11− epilogue :: String
12− }
8+data AngularOptions = AngularOptions
9+ { serviceName :: String -- ^ When generating code with wrapInService,
10+ -- ^ name of the service to generate
11+ , prologue :: String -> String -> String -- ^ beginning of the service definition
12+ , epilogue :: String -- ^ end of the service definition
13+ }
1314
1415defAngularOptions :: AngularOptions
15−defAngularOptions = AngularOptions {
16− serviceName = "",
17− prologue = \svc m -> m <> "service('" <> svc <> "', function($http) {\n"
18− <> " return ({",
19− epilogue = "});\n});\n"
16+defAngularOptions = AngularOptions
17+ { serviceName = ""
18+ , prologue = \svc m -> m <> "service('" <> svc <> "', function($http) {\n"
19+ <> " return ({"
20+ , epilogue = "});\n});\n"
2021 }
2122
23+-- | Instead of simply generating top level functions, generates a service instance
24+-- on which your controllers can depend to access your API
25+-- This variant uses default AngularOptions
2226wrapInService :: AngularOptions -> [AjaxReq] -> String
2327wrapInService ngOpts reqs = wrapInServiceWith ngOpts defCommonGeneratorOptions reqs
2428
29+-- | Instead of simply generating top level functions, generates a service instance
30+-- on which your controllers can depend to access your API
2531wrapInServiceWith :: AngularOptions -> CommonGeneratorOptions -> [AjaxReq] -> String
2632wrapInServiceWith ngOpts opts reqs =
2733 ((prologue ngOpts) svc mName)
@@ -34,10 +40,11 @@ wrapInServiceWith ngOpts opts reqs =
3440 then "app."
3541 else (moduleName opts) <> "."
3642
43+-- js codegen using $http service from Angular using default options
3744generateAngularJS :: AngularOptions -> AjaxReq -> String
3845generateAngularJS ngOpts = generateAngularJSWith ngOpts defCommonGeneratorOptions
3946
40−-- js codegen using $http
47+-- js codegen using $http service from Angular
4148generateAngularJSWith :: AngularOptions -> CommonGeneratorOptions -> AjaxReq -> String
4249generateAngularJSWith ngOptions opts req = "\n" <>
4350 fname <> fsep <> " function(" <> argsStr <> ")\n"
@@ -105,7 +112,7 @@ generateAngularJSWith ngOptions opts req = "\n" <>
105112
106113 fsep = if hasService then ":" else " ="
107114
108− fname = namespace <> (functionName opts $ req ^. funcName)
115+ fname = namespace <> (functionRenamer opts $ req ^. funcName)
109116
110117 method = req ^. reqMethod
111118 url = if url' == "'" then "'/'" else url'
@@ -118,4 +125,4 @@ generateAngularJSWith ngOptions opts req = "\n" <>
118125
119126 queryArgs = if null queryparams
120127 then ""
121− else " + '?" ++ jsParams queryparams
128+ else " + '?" ++ jsParams queryparams
servant-jquery/src/Servant/JQuery/Internal.hs+18−16
@@ -25,24 +25,26 @@ import GHC.Exts (Constraint)
2525import GHC.TypeLits
2626import Servant.API
2727
28−data CommonGeneratorOptions = CommonGeneratorOptions {
29− -- Function transforming function names
30− functionName :: String -> String,
31− -- Name used when a user want to send the request body (to let you redefine it)
32− requestBody :: String,
33− successCallback :: String,
34− errorCallback :: String,
35− moduleName :: String
36− }
28+-- | this structure is used by JavaScriptGenerator implementations to let you
29+-- customize the output
30+data CommonGeneratorOptions = CommonGeneratorOptions
31+ {
32+ functionRenamer :: String -> String -- ^ function transforming function names
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
35+ , errorCallback :: String -- ^ name of the callback parameter when the request reported an error
36+ , moduleName :: String -- ^ namespace on which we define the js function (empty mean local var)
37+ }
3738
3839defCommonGeneratorOptions :: CommonGeneratorOptions
39−defCommonGeneratorOptions = CommonGeneratorOptions {
40− functionName = id,
41− requestBody = "body",
42− successCallback = "onSuccess",
43− errorCallback = "onError",
44− moduleName = ""
45− }
40+defCommonGeneratorOptions = CommonGeneratorOptions
41+ {
42+ functionRenamer = id
43+ , requestBody = "body"
44+ , successCallback = "onSuccess"
45+ , errorCallback = "onError"
46+ , moduleName = ""
47+ }
4648
4749type Arg = String
4850
servant-jquery/src/Servant/JQuery/JQuery.hs+4−3
@@ -5,10 +5,11 @@ import Control.Lens
55import Data.List
66import Data.Monoid
77
8+-- | js codegen using JQuery using default options
89generateJQueryJS :: AjaxReq -> String
910generateJQueryJS = generateJQueryJSWith defCommonGeneratorOptions
1011
11−-- js codegen using JQuery
12+-- | js codegen using JQuery
1213generateJQueryJSWith :: CommonGeneratorOptions -> AjaxReq -> String
1314generateJQueryJSWith opts req = "\n" <>
1415 fname <> " = function(" <> argsStr <> ")\n"
@@ -64,7 +65,7 @@ generateJQueryJSWith opts req = "\n" <>
6465 namespace = if null (moduleName opts)
6566 then "var "
6667 else (moduleName opts) <> "."
67− fname = namespace <> (functionName opts $ req ^. funcName)
68+ fname = namespace <> (functionRenamer opts $ req ^. funcName)
6869
6970 method = req ^. reqMethod
7071 url = if url' == "'" then "'/'" else url'
@@ -77,4 +78,4 @@ generateJQueryJSWith opts req = "\n" <>
7778
7879 queryArgs = if null queryparams
7980 then ""
80− else " + '?" ++ jsParams queryparams
81+ else " + '?" ++ jsParams queryparams
servant-jquery/src/Servant/JQuery/Vanilla.hs+3−2
@@ -5,10 +5,11 @@ import Control.Lens
55import Data.List
66import Data.Monoid
77
8+-- | js codegen using XmlHttpRequest using default generation options
89generateVanillaJS :: AjaxReq -> String
910generateVanillaJS = generateVanillaJSWith defCommonGeneratorOptions
1011
11−-- js codegen using XmlHttpRequest
12+-- | js codegen using XmlHttpRequest
1213generateVanillaJSWith :: CommonGeneratorOptions -> AjaxReq -> String
1314generateVanillaJSWith opts req = "\n" <>
1415 fname <> " = function(" <> argsStr <> ")\n"
@@ -70,7 +71,7 @@ generateVanillaJSWith opts req = "\n" <>
7071 namespace = if null (moduleName opts)
7172 then "var "
7273 else (moduleName opts) <> "."
73− fname = namespace <> (functionName opts $ req ^. funcName)
74+ fname = namespace <> (functionRenamer opts $ req ^. funcName)
7475
7576 method = req ^. reqMethod
7677 url = if url' == "'" then "'/'" else url'