From ce43317eeb4445468850d9aa80117dfed265ed4f Mon Sep 17 00:00:00 2001 From: Freezeboy Date: Sat, 18 Jul 2015 10:51:49 +0200 Subject: [PATCH] Add some haddock, rename 'functionName' --- servant-jquery/src/Servant/JQuery/Angular.hs | 33 +++++++++++------- servant-jquery/src/Servant/JQuery/Internal.hs | 34 ++++++++++--------- servant-jquery/src/Servant/JQuery/JQuery.hs | 7 ++-- servant-jquery/src/Servant/JQuery/Vanilla.hs | 5 +-- 4 files changed, 45 insertions(+), 34 deletions(-) diff --git a/servant-jquery/src/Servant/JQuery/Angular.hs b/servant-jquery/src/Servant/JQuery/Angular.hs index ff93a6da..409a306f 100644 --- a/servant-jquery/src/Servant/JQuery/Angular.hs +++ b/servant-jquery/src/Servant/JQuery/Angular.hs @@ -5,23 +5,29 @@ import Control.Lens import Data.List import Data.Monoid -data AngularOptions = AngularOptions { - serviceName :: String, - prologue :: String -> String -> String, - epilogue :: String - } +data AngularOptions = AngularOptions + { serviceName :: String -- ^ When generating code with wrapInService, + -- ^ name of the service to generate + , prologue :: String -> String -> String -- ^ beginning of the service definition + , epilogue :: String -- ^ end of the service definition + } defAngularOptions :: AngularOptions -defAngularOptions = AngularOptions { - serviceName = "", - prologue = \svc m -> m <> "service('" <> svc <> "', function($http) {\n" - <> " return ({", - epilogue = "});\n});\n" +defAngularOptions = AngularOptions + { serviceName = "" + , prologue = \svc m -> m <> "service('" <> svc <> "', function($http) {\n" + <> " return ({" + , epilogue = "});\n});\n" } +-- | Instead of simply generating top level functions, generates a service instance +-- on which your controllers can depend to access your API +-- This variant uses default AngularOptions wrapInService :: AngularOptions -> [AjaxReq] -> String wrapInService ngOpts reqs = wrapInServiceWith ngOpts defCommonGeneratorOptions reqs +-- | Instead of simply generating top level functions, generates a service instance +-- on which your controllers can depend to access your API wrapInServiceWith :: AngularOptions -> CommonGeneratorOptions -> [AjaxReq] -> String wrapInServiceWith ngOpts opts reqs = ((prologue ngOpts) svc mName) @@ -34,10 +40,11 @@ wrapInServiceWith ngOpts opts reqs = then "app." else (moduleName opts) <> "." +-- js codegen using $http service from Angular using default options generateAngularJS :: AngularOptions -> AjaxReq -> String generateAngularJS ngOpts = generateAngularJSWith ngOpts defCommonGeneratorOptions --- js codegen using $http +-- js codegen using $http service from Angular generateAngularJSWith :: AngularOptions -> CommonGeneratorOptions -> AjaxReq -> String generateAngularJSWith ngOptions opts req = "\n" <> fname <> fsep <> " function(" <> argsStr <> ")\n" @@ -105,7 +112,7 @@ generateAngularJSWith ngOptions opts req = "\n" <> fsep = if hasService then ":" else " =" - fname = namespace <> (functionName opts $ req ^. funcName) + fname = namespace <> (functionRenamer opts $ req ^. funcName) method = req ^. reqMethod url = if url' == "'" then "'/'" else url' @@ -118,4 +125,4 @@ generateAngularJSWith ngOptions opts req = "\n" <> queryArgs = if null queryparams then "" - else " + '?" ++ jsParams queryparams \ No newline at end of file + else " + '?" ++ jsParams queryparams diff --git a/servant-jquery/src/Servant/JQuery/Internal.hs b/servant-jquery/src/Servant/JQuery/Internal.hs index baa718ae..a9ff5e10 100644 --- a/servant-jquery/src/Servant/JQuery/Internal.hs +++ b/servant-jquery/src/Servant/JQuery/Internal.hs @@ -25,24 +25,26 @@ import GHC.Exts (Constraint) import GHC.TypeLits import Servant.API -data CommonGeneratorOptions = CommonGeneratorOptions { - -- Function transforming function names - functionName :: String -> String, - -- Name used when a user want to send the request body (to let you redefine it) - requestBody :: String, - successCallback :: String, - errorCallback :: String, - moduleName :: String - } +-- | this structure is used by JavaScriptGenerator implementations to let you +-- customize the output +data CommonGeneratorOptions = CommonGeneratorOptions + { + functionRenamer :: String -> String -- ^ function transforming function names + , requestBody :: String -- ^ name used when a user want to send the request body (to let you redefine it) + , successCallback :: String -- ^ name of the callback parameter when the request was successful + , errorCallback :: String -- ^ name of the callback parameter when the request reported an error + , moduleName :: String -- ^ namespace on which we define the js function (empty mean local var) + } defCommonGeneratorOptions :: CommonGeneratorOptions -defCommonGeneratorOptions = CommonGeneratorOptions { - functionName = id, - requestBody = "body", - successCallback = "onSuccess", - errorCallback = "onError", - moduleName = "" - } +defCommonGeneratorOptions = CommonGeneratorOptions + { + functionRenamer = id + , requestBody = "body" + , successCallback = "onSuccess" + , errorCallback = "onError" + , moduleName = "" + } type Arg = String diff --git a/servant-jquery/src/Servant/JQuery/JQuery.hs b/servant-jquery/src/Servant/JQuery/JQuery.hs index 274f8021..01fabfda 100644 --- a/servant-jquery/src/Servant/JQuery/JQuery.hs +++ b/servant-jquery/src/Servant/JQuery/JQuery.hs @@ -5,10 +5,11 @@ import Control.Lens import Data.List import Data.Monoid +-- | js codegen using JQuery using default options generateJQueryJS :: AjaxReq -> String generateJQueryJS = generateJQueryJSWith defCommonGeneratorOptions --- js codegen using JQuery +-- | js codegen using JQuery generateJQueryJSWith :: CommonGeneratorOptions -> AjaxReq -> String generateJQueryJSWith opts req = "\n" <> fname <> " = function(" <> argsStr <> ")\n" @@ -64,7 +65,7 @@ generateJQueryJSWith opts req = "\n" <> namespace = if null (moduleName opts) then "var " else (moduleName opts) <> "." - fname = namespace <> (functionName opts $ req ^. funcName) + fname = namespace <> (functionRenamer opts $ req ^. funcName) method = req ^. reqMethod url = if url' == "'" then "'/'" else url' @@ -77,4 +78,4 @@ generateJQueryJSWith opts req = "\n" <> queryArgs = if null queryparams then "" - else " + '?" ++ jsParams queryparams \ No newline at end of file + else " + '?" ++ jsParams queryparams diff --git a/servant-jquery/src/Servant/JQuery/Vanilla.hs b/servant-jquery/src/Servant/JQuery/Vanilla.hs index 54bfcf2a..919cb97f 100644 --- a/servant-jquery/src/Servant/JQuery/Vanilla.hs +++ b/servant-jquery/src/Servant/JQuery/Vanilla.hs @@ -5,10 +5,11 @@ import Control.Lens import Data.List import Data.Monoid +-- | js codegen using XmlHttpRequest using default generation options generateVanillaJS :: AjaxReq -> String generateVanillaJS = generateVanillaJSWith defCommonGeneratorOptions --- js codegen using XmlHttpRequest +-- | js codegen using XmlHttpRequest generateVanillaJSWith :: CommonGeneratorOptions -> AjaxReq -> String generateVanillaJSWith opts req = "\n" <> fname <> " = function(" <> argsStr <> ")\n" @@ -70,7 +71,7 @@ generateVanillaJSWith opts req = "\n" <> namespace = if null (moduleName opts) then "var " else (moduleName opts) <> "." - fname = namespace <> (functionName opts $ req ^. funcName) + fname = namespace <> (functionRenamer opts $ req ^. funcName) method = req ^. reqMethod url = if url' == "'" then "'/'" else url'