git.iko.soy/typeable/servant
on master help sign in
Merge pull request #11 from haskell-servant/pr5
8c6f327Julian Arni
.gitignorenew file+17
@@ -0,0 +1,17 @@
1+dist
2+cabal-dev
3+*.o
4+*.hi
5+*.chi
6+*.chs.h
7+*.dyn_o
8+*.dyn_hi
9+.virtualenv
10+.hpc
11+.hsenv
12+.cabal-sandbox/
13+cabal.sandbox.config
14+cabal.config
15+*.prof
16+*.aux
17+*.hp
CHANGELOG.md+1
@@ -2,6 +2,7 @@
2---
2---
3* Extend `HeaderArg` to support more advanced HTTP header handling (https://github.com/haskell-servant/servant-jquery/pull/6)
3* Extend `HeaderArg` to support more advanced HTTP header handling (https://github.com/haskell-servant/servant-jquery/pull/6)
4* Support content-type aware combinators (but require that endpoints support JSON)
4* Support content-type aware combinators (but require that endpoints support JSON)
5+* Add support for Matrix params (https://github.com/haskell-servant/servant-jquery/pull/11)
5
6
60.2.2
70.2.2
7-----
8-----
servant-jquery.cabal+4−4
@@ -34,7 +34,7 @@ library
34 build-depends: base >=4.5 && <5
34 build-depends: base >=4.5 && <5
35 , charset
35 , charset
36 , lens >= 4
36 , lens >= 4
37− , servant >= 0.2.1
37+ , servant >= 0.2.2
38 , text
38 , text
39 hs-source-dirs: src
39 hs-source-dirs: src
40 default-language: Haskell2010
40 default-language: Haskell2010
@@ -54,9 +54,9 @@ executable counter
54 aeson
54 aeson
55 , base
55 , base
56 , filepath
56 , filepath
57− , servant >= 0.2.1
57+ , servant >= 0.2.2
58− , servant-server >= 0.2.1
58+ , servant-server >= 0.2.3
59− , servant-jquery >= 0.2.1
59+ , servant-jquery >= 0.2.2
60 , stm
60 , stm
61 , transformers
61 , transformers
62 , warp
62 , warp
src/Servant/JQuery.hs+3−2
@@ -46,7 +46,7 @@ generateJS req = "\n" <>
46 ++ body
46 ++ body
47 ++ map (toValidFunctionName . (<>) "header" . headerArgName) hs
47 ++ map (toValidFunctionName . (<>) "header" . headerArgName) hs
48 ++ ["onSuccess", "onError"]
48 ++ ["onSuccess", "onError"]
49−
49+
50 captures = map captureArg
50 captures = map captureArg
51 . filter isCapture
51 . filter isCapture
52 $ req ^. reqUrl.path
52 $ req ^. reqUrl.path
@@ -76,7 +76,8 @@ generateJS req = "\n" <>
76
76
77 fname = req ^. funcName
77 fname = req ^. funcName
78 method = req ^. reqMethod
78 method = req ^. reqMethod
79− url = "'"
79+ url = if url' == "'" then "'/'" else url'
80+ url' = "'"
80 ++ urlArgs
81 ++ urlArgs
81 ++ queryArgs
82 ++ queryArgs
82
83
src/Servant/JQuery/Internal.hs+81−26
@@ -24,29 +24,12 @@ import Servant.API
24
24
25type Arg = String
25type Arg = String
26
26
27−data Segment = Static String -- ^ a static path segment. like "/foo"
27+data Segment = Segment { _segment :: SegmentType, _matrix :: [MatrixArg] }
28− | Cap Arg -- ^ a capture. like "/:userid"
29 deriving (Eq, Show)
28 deriving (Eq, Show)
30
29
31−
30+data SegmentType = Static String -- ^ a static path segment. like "/foo"
32−isCapture :: Segment -> Bool
31+ | Cap Arg -- ^ a capture. like "/:userid"
33−isCapture (Cap _) = True
32+ deriving (Eq, Show)
34−isCapture _ = False
35−
36−captureArg :: Segment -> Arg
37−captureArg (Cap s) = s
38−captureArg _ = error "captureArg called on non capture"
39−
40−jsSegments :: [Segment] -> String
41−jsSegments [] = "/'"
42−jsSegments [x] = "/" ++ segmentToStr x False
43−jsSegments (x:xs) = "/" ++ segmentToStr x True ++ jsSegments xs
44−
45−segmentToStr :: Segment -> Bool -> String
46−segmentToStr (Static s) notTheEnd =
47− if notTheEnd then s else s ++ "'"
48−segmentToStr (Cap s) notTheEnd =
49− "' + encodeURIComponent(" ++ s ++ if notTheEnd then ") + '" else ")"
50
33
51type Path = [Segment]
34type Path = [Segment]
52
35
@@ -113,6 +96,8 @@ toValidFunctionName (x:xs) = [setFirstChar x] <> filter remainder xs
113 , Set.connectorPunctuation ]
96 , Set.connectorPunctuation ]
114toValidFunctionName [] = "_"
97toValidFunctionName [] = "_"
115
98
99+type MatrixArg = QueryArg
100+
116data Url = Url
101data Url = Url
117 { _path :: Path
102 { _path :: Path
118 , _queryStr :: [QueryArg]
103 , _queryStr :: [QueryArg]
@@ -133,13 +118,52 @@ data AjaxReq = AjaxReq
133 } deriving (Eq, Show)
118 } deriving (Eq, Show)
134
119
135makeLenses ''QueryArg
120makeLenses ''QueryArg
121+makeLenses ''Segment
136makeLenses ''Url
122makeLenses ''Url
137makeLenses ''AjaxReq
123makeLenses ''AjaxReq
138
124
125+isCapture :: Segment -> Bool
126+isCapture (Segment (Cap _) _) = True
127+isCapture _ = False
128+
129+hasMatrixArgs :: Segment -> Bool
130+hasMatrixArgs (Segment _ (_:_)) = True
131+hasMatrixArgs _ = False
132+
133+hasArgs :: Segment -> Bool
134+hasArgs s = isCapture s || hasMatrixArgs s
135+
136+matrixArgs :: Segment -> [MatrixArg]
137+matrixArgs (Segment _ ms) = ms
138+
139+captureArg :: Segment -> Arg
140+captureArg (Segment (Cap s) _) = s
141+captureArg _ = error "captureArg called on non capture"
142+
143+jsSegments :: [Segment] -> String
144+jsSegments [] = ""
145+jsSegments [x] = "/" ++ segmentToStr x False
146+jsSegments (x:xs) = "/" ++ segmentToStr x True ++ jsSegments xs
147+
148+segmentToStr :: Segment -> Bool -> String
149+segmentToStr (Segment st ms) notTheEnd =
150+ segmentTypeToStr st ++ jsMParams ms ++ if notTheEnd then "" else "'"
151+
152+segmentTypeToStr :: SegmentType -> String
153+segmentTypeToStr (Static s) = s
154+segmentTypeToStr (Cap s) = "' + encodeURIComponent(" ++ s ++ ") + '"
155+
156+jsGParams :: String -> [QueryArg] -> String
157+jsGParams _ [] = ""
158+jsGParams _ [x] = paramToStr x False
159+jsGParams s (x:xs) = paramToStr x True ++ s ++ jsGParams s xs
160+
139jsParams :: [QueryArg] -> String
161jsParams :: [QueryArg] -> String
140−jsParams [] = ""
162+jsParams = jsGParams "&"
141−jsParams [x] = paramToStr x False
163+
142−jsParams (x:xs) = paramToStr x True ++ "&" ++ jsParams xs
164+jsMParams :: [MatrixArg] -> String
165+jsMParams [] = ""
166+jsMParams xs = ";" ++ jsGParams ";" xs
143
167
144paramToStr :: QueryArg -> Bool -> String
168paramToStr :: QueryArg -> Bool -> String
145paramToStr qarg notTheEnd =
169paramToStr qarg notTheEnd =
@@ -184,7 +208,7 @@ instance (KnownSymbol sym, HasJQ sublayout)
184
208
185 jqueryFor Proxy req =
209 jqueryFor Proxy req =
186 jqueryFor (Proxy :: Proxy sublayout) $
210 jqueryFor (Proxy :: Proxy sublayout) $
187− req & reqUrl.path <>~ [Cap str]
211+ req & reqUrl.path <>~ [Segment (Cap str) []]
188
212
189 where str = symbolVal (Proxy :: Proxy sym)
213 where str = symbolVal (Proxy :: Proxy sym)
190
214
@@ -256,6 +280,37 @@ instance (KnownSymbol sym, HasJQ sublayout)
256
280
257 where str = symbolVal (Proxy :: Proxy sym)
281 where str = symbolVal (Proxy :: Proxy sym)
258
282
283+instance (KnownSymbol sym, HasJQ sublayout)
284+ => HasJQ (MatrixParam sym a :> sublayout) where
285+ type JQ (MatrixParam sym a :> sublayout) = JQ sublayout
286+
287+ jqueryFor Proxy req =
288+ jqueryFor (Proxy :: Proxy sublayout) $
289+ req & reqUrl.path._last.matrix <>~ [QueryArg strArg Normal]
290+
291+ where str = symbolVal (Proxy :: Proxy sym)
292+ strArg = str ++ "Value"
293+
294+instance (KnownSymbol sym, HasJQ sublayout)
295+ => HasJQ (MatrixParams sym a :> sublayout) where
296+ type JQ (MatrixParams sym a :> sublayout) = JQ sublayout
297+
298+ jqueryFor Proxy req =
299+ jqueryFor (Proxy :: Proxy sublayout) $
300+ req & reqUrl.path._last.matrix <>~ [QueryArg str List]
301+
302+ where str = symbolVal (Proxy :: Proxy sym)
303+
304+instance (KnownSymbol sym, HasJQ sublayout)
305+ => HasJQ (MatrixFlag sym :> sublayout) where
306+ type JQ (MatrixFlag sym :> sublayout) = JQ sublayout
307+
308+ jqueryFor Proxy req =
309+ jqueryFor (Proxy :: Proxy sublayout) $
310+ req & reqUrl.path._last.matrix <>~ [QueryArg str Flag]
311+
312+ where str = symbolVal (Proxy :: Proxy sym)
313+
259instance HasJQ Raw where
314instance HasJQ Raw where
260 type JQ Raw = Method -> AjaxReq
315 type JQ Raw = Method -> AjaxReq
261
316
@@ -276,7 +331,7 @@ instance (KnownSymbol path, HasJQ sublayout)
276
331
277 jqueryFor Proxy req =
332 jqueryFor Proxy req =
278 jqueryFor (Proxy :: Proxy sublayout) $
333 jqueryFor (Proxy :: Proxy sublayout) $
279− req & reqUrl.path <>~ [Static str]
334+ req & reqUrl.path <>~ [Segment (Static str) []]
280 & funcName %~ (str <>)
335 & funcName %~ (str <>)
281
336
282 where str = map (\c -> if c == '.' then '_' else c) $ symbolVal (Proxy :: Proxy path)
337 where str = map (\c -> if c == '.' then '_' else c) $ symbolVal (Proxy :: Proxy path)
test/Servant/JQuerySpec.hs−1
@@ -13,7 +13,6 @@ import Language.ECMAScript3.Parser (parseFromString)
13import Test.Hspec
13import Test.Hspec
14
14
15import Servant.API
15import Servant.API
16−import Servant.API.ContentTypes
17import Servant.JQuery
16import Servant.JQuery
18import Servant.JQuerySpec.CustomHeaders
17import Servant.JQuerySpec.CustomHeaders
19
18