2015-09-21 12:31:00 +02:00
|
|
|
module Servant.JS.Internal
|
|
|
|
( JavaScriptGenerator
|
|
|
|
, AjaxReq
|
|
|
|
, jsSegments
|
|
|
|
, segmentToStr
|
|
|
|
, segmentTypeToStr
|
|
|
|
, jsParams
|
|
|
|
, jsGParams
|
|
|
|
, jsMParams
|
|
|
|
, paramToStr
|
|
|
|
-- re-exports
|
|
|
|
, (:<|>)(..)
|
|
|
|
, (:>)
|
|
|
|
, defReq
|
|
|
|
, defCommonGeneratorOptions
|
|
|
|
, reqHeaders
|
|
|
|
, CommonGeneratorOptions(..)
|
|
|
|
, HasForeign(..)
|
|
|
|
, HeaderArg(..)
|
|
|
|
, concatCase
|
|
|
|
, snakeCase
|
|
|
|
, camelCase
|
|
|
|
, ReqBody
|
|
|
|
, JSON
|
|
|
|
, FormUrlEncoded
|
|
|
|
, Post
|
|
|
|
, Get
|
|
|
|
, Raw
|
|
|
|
, Header
|
|
|
|
) where
|
2014-11-25 01:36:34 +01:00
|
|
|
|
2015-09-15 15:44:55 +02:00
|
|
|
import Control.Lens hiding (List)
|
2015-09-21 12:31:00 +02:00
|
|
|
import Servant.Foreign
|
2014-11-25 01:36:34 +01:00
|
|
|
|
2015-09-21 12:31:00 +02:00
|
|
|
type AjaxReq = Req
|
2014-11-25 01:36:34 +01:00
|
|
|
|
2015-07-22 19:23:31 +02:00
|
|
|
-- A 'JavascriptGenerator' just takes the data found in the API type
|
|
|
|
-- for each endpoint and generates Javascript code in a String. Several
|
|
|
|
-- generators are available in this package.
|
2015-09-21 12:31:00 +02:00
|
|
|
type JavaScriptGenerator = [Req] -> String
|
2015-01-03 18:52:18 +01:00
|
|
|
|
|
|
|
jsSegments :: [Segment] -> String
|
|
|
|
jsSegments [] = ""
|
|
|
|
jsSegments [x] = "/" ++ segmentToStr x False
|
|
|
|
jsSegments (x:xs) = "/" ++ segmentToStr x True ++ jsSegments xs
|
|
|
|
|
|
|
|
segmentToStr :: Segment -> Bool -> String
|
|
|
|
segmentToStr (Segment st ms) notTheEnd =
|
|
|
|
segmentTypeToStr st ++ jsMParams ms ++ if notTheEnd then "" else "'"
|
|
|
|
|
|
|
|
segmentTypeToStr :: SegmentType -> String
|
|
|
|
segmentTypeToStr (Static s) = s
|
|
|
|
segmentTypeToStr (Cap s) = "' + encodeURIComponent(" ++ s ++ ") + '"
|
|
|
|
|
|
|
|
jsGParams :: String -> [QueryArg] -> String
|
2015-09-21 12:31:00 +02:00
|
|
|
jsGParams _ [] = ""
|
|
|
|
jsGParams _ [x] = paramToStr x False
|
2015-01-03 18:52:18 +01:00
|
|
|
jsGParams s (x:xs) = paramToStr x True ++ s ++ jsGParams s xs
|
|
|
|
|
2014-11-25 01:36:34 +01:00
|
|
|
jsParams :: [QueryArg] -> String
|
2015-01-03 18:52:18 +01:00
|
|
|
jsParams = jsGParams "&"
|
|
|
|
|
|
|
|
jsMParams :: [MatrixArg] -> String
|
|
|
|
jsMParams [] = ""
|
|
|
|
jsMParams xs = ";" ++ jsGParams ";" xs
|
2014-11-25 01:36:34 +01:00
|
|
|
|
|
|
|
paramToStr :: QueryArg -> Bool -> String
|
|
|
|
paramToStr qarg notTheEnd =
|
|
|
|
case qarg ^. argType of
|
|
|
|
Normal -> name
|
|
|
|
++ "=' + encodeURIComponent("
|
|
|
|
++ name
|
|
|
|
++ if notTheEnd then ") + '" else ")"
|
|
|
|
Flag -> name ++ "="
|
|
|
|
List -> name
|
|
|
|
++ "[]=' + encodeURIComponent("
|
|
|
|
++ name
|
|
|
|
++ if notTheEnd then ") + '" else ")"
|
|
|
|
where name = qarg ^. argName
|