git.iko.soy/typeable/servant
on master help sign in
Add some haddock, rename 'functionName'
ce43317Freezeboy
servant-jquery/src/Servant/JQuery/Angular.hs+20−13
@@ -5,23 +5,29 @@ import Control.Lens
5import Data.List
5import Data.List
6import Data.Monoid
6import Data.Monoid
7
7
8−data AngularOptions = AngularOptions {
8+data AngularOptions = AngularOptions
9− serviceName :: String,
9+ { serviceName :: String -- ^ When generating code with wrapInService,
10+ -- ^ name of the service to generate
10− prologue :: String -> String -> String,
11+ , prologue :: String -> String -> String -- ^ beginning of the service definition
11− epilogue :: String
12+ , epilogue :: String -- ^ end of the service definition
12− }
13+ }
13
14
14defAngularOptions :: AngularOptions
15defAngularOptions :: AngularOptions
15−defAngularOptions = AngularOptions {
16+defAngularOptions = AngularOptions
16− serviceName = "",
17+ { serviceName = ""
17− prologue = \svc m -> m <> "service('" <> svc <> "', function($http) {\n"
18+ , prologue = \svc m -> m <> "service('" <> svc <> "', function($http) {\n"
18− <> " return ({",
19+ <> " return ({"
19− epilogue = "});\n});\n"
20+ , epilogue = "});\n});\n"
20 }
21 }
21
22
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
22wrapInService :: AngularOptions -> [AjaxReq] -> String
26wrapInService :: AngularOptions -> [AjaxReq] -> String
23wrapInService ngOpts reqs = wrapInServiceWith ngOpts defCommonGeneratorOptions reqs
27wrapInService ngOpts reqs = wrapInServiceWith ngOpts defCommonGeneratorOptions reqs
24
28
29+-- | Instead of simply generating top level functions, generates a service instance
30+-- on which your controllers can depend to access your API
25wrapInServiceWith :: AngularOptions -> CommonGeneratorOptions -> [AjaxReq] -> String
31wrapInServiceWith :: AngularOptions -> CommonGeneratorOptions -> [AjaxReq] -> String
26wrapInServiceWith ngOpts opts reqs =
32wrapInServiceWith ngOpts opts reqs =
27 ((prologue ngOpts) svc mName)
33 ((prologue ngOpts) svc mName)
@@ -34,10 +40,11 @@ wrapInServiceWith ngOpts opts reqs =
34 then "app."
40 then "app."
35 else (moduleName opts) <> "."
41 else (moduleName opts) <> "."
36
42
43+-- js codegen using $http service from Angular using default options
37generateAngularJS :: AngularOptions -> AjaxReq -> String
44generateAngularJS :: AngularOptions -> AjaxReq -> String
38generateAngularJS ngOpts = generateAngularJSWith ngOpts defCommonGeneratorOptions
45generateAngularJS ngOpts = generateAngularJSWith ngOpts defCommonGeneratorOptions
39
46
40−-- js codegen using $http
47+-- js codegen using $http service from Angular
41generateAngularJSWith :: AngularOptions -> CommonGeneratorOptions -> AjaxReq -> String
48generateAngularJSWith :: AngularOptions -> CommonGeneratorOptions -> AjaxReq -> String
42generateAngularJSWith ngOptions opts req = "\n" <>
49generateAngularJSWith ngOptions opts req = "\n" <>
43 fname <> fsep <> " function(" <> argsStr <> ")\n"
50 fname <> fsep <> " function(" <> argsStr <> ")\n"
@@ -105,7 +112,7 @@ generateAngularJSWith ngOptions opts req = "\n" <>
105
112
106 fsep = if hasService then ":" else " ="
113 fsep = if hasService then ":" else " ="
107
114
108− fname = namespace <> (functionName opts $ req ^. funcName)
115+ fname = namespace <> (functionRenamer opts $ req ^. funcName)
109
116
110 method = req ^. reqMethod
117 method = req ^. reqMethod
111 url = if url' == "'" then "'/'" else url'
118 url = if url' == "'" then "'/'" else url'
@@ -118,4 +125,4 @@ generateAngularJSWith ngOptions opts req = "\n" <>
118
125
119 queryArgs = if null queryparams
126 queryArgs = if null queryparams
120 then ""
127 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)
25import GHC.TypeLits
25import GHC.TypeLits
26import Servant.API
26import Servant.API
27
27
28+-- | this structure is used by JavaScriptGenerator implementations to let you
29+-- customize the output
28−data CommonGeneratorOptions = CommonGeneratorOptions {
30+data CommonGeneratorOptions = CommonGeneratorOptions
31+ {
29− -- Function transforming function names
32+ functionRenamer :: String -> String -- ^ 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)
33+ , requestBody :: String -- ^ name used when a user want to send the request body (to let you redefine it)
32− requestBody :: String,
33− successCallback :: String,
34+ , successCallback :: String -- ^ name of the callback parameter when the request was successful
34− errorCallback :: String,
35+ , errorCallback :: String -- ^ name of the callback parameter when the request reported an error
35− moduleName :: String
36+ , moduleName :: String -- ^ namespace on which we define the js function (empty mean local var)
36− }
37+ }
37
38
38defCommonGeneratorOptions :: CommonGeneratorOptions
39defCommonGeneratorOptions :: CommonGeneratorOptions
39−defCommonGeneratorOptions = CommonGeneratorOptions {
40+defCommonGeneratorOptions = CommonGeneratorOptions
40− functionName = id,
41+ {
42+ functionRenamer = id
41− requestBody = "body",
43+ , requestBody = "body"
42− successCallback = "onSuccess",
44+ , successCallback = "onSuccess"
43− errorCallback = "onError",
45+ , errorCallback = "onError"
44− moduleName = ""
46+ , moduleName = ""
45− }
47+ }
46
48
47type Arg = String
49type Arg = String
48
50
servant-jquery/src/Servant/JQuery/JQuery.hs+4−3
@@ -5,10 +5,11 @@ import Control.Lens
5import Data.List
5import Data.List
6import Data.Monoid
6import Data.Monoid
7
7
8+-- | js codegen using JQuery using default options
8generateJQueryJS :: AjaxReq -> String
9generateJQueryJS :: AjaxReq -> String
9generateJQueryJS = generateJQueryJSWith defCommonGeneratorOptions
10generateJQueryJS = generateJQueryJSWith defCommonGeneratorOptions
10
11
11−-- js codegen using JQuery
12+-- | js codegen using JQuery
12generateJQueryJSWith :: CommonGeneratorOptions -> AjaxReq -> String
13generateJQueryJSWith :: CommonGeneratorOptions -> AjaxReq -> String
13generateJQueryJSWith opts req = "\n" <>
14generateJQueryJSWith opts req = "\n" <>
14 fname <> " = function(" <> argsStr <> ")\n"
15 fname <> " = function(" <> argsStr <> ")\n"
@@ -64,7 +65,7 @@ generateJQueryJSWith opts req = "\n" <>
64 namespace = if null (moduleName opts)
65 namespace = if null (moduleName opts)
65 then "var "
66 then "var "
66 else (moduleName opts) <> "."
67 else (moduleName opts) <> "."
67− fname = namespace <> (functionName opts $ req ^. funcName)
68+ fname = namespace <> (functionRenamer opts $ req ^. funcName)
68
69
69 method = req ^. reqMethod
70 method = req ^. reqMethod
70 url = if url' == "'" then "'/'" else url'
71 url = if url' == "'" then "'/'" else url'
@@ -77,4 +78,4 @@ generateJQueryJSWith opts req = "\n" <>
77
78
78 queryArgs = if null queryparams
79 queryArgs = if null queryparams
79 then ""
80 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
5import Data.List
5import Data.List
6import Data.Monoid
6import Data.Monoid
7
7
8+-- | js codegen using XmlHttpRequest using default generation options
8generateVanillaJS :: AjaxReq -> String
9generateVanillaJS :: AjaxReq -> String
9generateVanillaJS = generateVanillaJSWith defCommonGeneratorOptions
10generateVanillaJS = generateVanillaJSWith defCommonGeneratorOptions
10
11
11−-- js codegen using XmlHttpRequest
12+-- | js codegen using XmlHttpRequest
12generateVanillaJSWith :: CommonGeneratorOptions -> AjaxReq -> String
13generateVanillaJSWith :: CommonGeneratorOptions -> AjaxReq -> String
13generateVanillaJSWith opts req = "\n" <>
14generateVanillaJSWith opts req = "\n" <>
14 fname <> " = function(" <> argsStr <> ")\n"
15 fname <> " = function(" <> argsStr <> ")\n"
@@ -70,7 +71,7 @@ generateVanillaJSWith opts req = "\n" <>
70 namespace = if null (moduleName opts)
71 namespace = if null (moduleName opts)
71 then "var "
72 then "var "
72 else (moduleName opts) <> "."
73 else (moduleName opts) <> "."
73− fname = namespace <> (functionName opts $ req ^. funcName)
74+ fname = namespace <> (functionRenamer opts $ req ^. funcName)
74
75
75 method = req ^. reqMethod
76 method = req ^. reqMethod
76 url = if url' == "'" then "'/'" else url'
77 url = if url' == "'" then "'/'" else url'