Add some haddock, rename 'functionName'

This commit is contained in:
Freezeboy 2015-07-18 10:51:49 +02:00
parent e8c457ed1f
commit d8740ff0e5
4 changed files with 45 additions and 34 deletions

View file

@ -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
else " + '?" ++ jsParams queryparams

View file

@ -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

View file

@ -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
else " + '?" ++ jsParams queryparams

View file

@ -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'