git.iko.soy/rowtype-yoga/purescript-yoga-fetch-om
on main help sign in
Query params accept partial records directly — no opt/Just/Nothing
79ec177Mark
src/Yoga/Fetch/Om.purs+1−3
@@ -97,8 +97,6 @@ instance
97 , Row.Union pathParams queryParams pathQuery
97 , Row.Union pathParams queryParams pathQuery
98 , Row.Nub pathQuery pathQuery
98 , Row.Nub pathQuery pathQuery
99 , RowToList pathQuery pathQueryRL
99 , RowToList pathQuery pathQueryRL
100− , RowToList queryParams queryParamsRL
101− , ReadPartialQuery queryParamsRL queryParams
102 , RowToList headers headersRL
100 , RowToList headers headersRL
103 , ToHeaders headersRL headers
101 , ToHeaders headersRL headers
104 , CheckBodyIsUnit body bodyFlag
102 , CheckBodyIsUnit body bodyFlag
@@ -113,7 +111,6 @@ instance
113 convert = unsafeCoerce
111 convert = unsafeCoerce
114 impl :: Record pathQuery -> Record headers -> body -> Om { | ctx } errRow result
112 impl :: Record pathQuery -> Record headers -> body -> Om { | ctx } errRow result
115 impl pathQueryRec headersRec bodyVal = widenOm do
113 impl pathQueryRec headersRec bodyVal = widenOm do
116− let queryParamsRec = readPartialQuery @queryParamsRL pathQueryRec
117 let url = buildUrl baseUrl (Proxy :: _ segments) pathParamsRec queryParamsRec
114 let url = buildUrl baseUrl (Proxy :: _ segments) pathParamsRec queryParamsRec
118 let hdrs = toHeaders (Proxy :: _ headersRL) headersRec
115 let hdrs = toHeaders (Proxy :: _ headersRL) headersRec
119 let ct = encodingContentType (Proxy :: _ encoding)
116 let ct = encodingContentType (Proxy :: _ encoding)
@@ -122,6 +119,7 @@ instance
122 variantOrValue (Proxy :: _ successRL) variant # pure
119 variantOrValue (Proxy :: _ successRL) variant # pure
123 where
120 where
124 pathParamsRec = unsafeCoerce pathQueryRec :: Record pathParams
121 pathParamsRec = unsafeCoerce pathQueryRec :: Record pathParams
122+ queryParamsRec = unsafeCoerce pathQueryRec :: Record queryParams
125 widenOm :: Om (Record ()) routeErrors ~> Om { | ctx } errRow
123 widenOm :: Om (Record ()) routeErrors ~> Om { | ctx } errRow
126 widenOm = unsafeCoerce
124 widenOm = unsafeCoerce
127
125
src/Yoga/Fetch/Om/BuildUrl.purs+4−2
@@ -28,6 +28,7 @@ import Prim.RowList as RL
28import Record as Record
28import Record as Record
29import Type.Proxy (Proxy(..))
29import Type.Proxy (Proxy(..))
30import Yoga.HTTP.API.Path (class PathPattern, pathPattern)
30import Yoga.HTTP.API.Path (class PathPattern, pathPattern)
31+import Yoga.Fetch.Om.ReadOptional (readOptional)
31
32
32foreign import encodeURIComponent_ :: String -> String
33foreign import encodeURIComponent_ :: String -> String
33
34
@@ -113,19 +114,20 @@ class AppendQueryParamsRL rl r where
113instance AppendQueryParamsRL RL.Nil r where
114instance AppendQueryParamsRL RL.Nil r where
114 appendQueryParamsRL _ _ acc = acc
115 appendQueryParamsRL _ _ acc = acc
115
116
117+-- Maybe ty: read with undefined-awareness — missing field → skip
116instance
118instance
117 ( IsSymbol name
119 ( IsSymbol name
118 , SerializeParam ty
120 , SerializeParam ty
119 , AppendQueryParamsRL tail r
121 , AppendQueryParamsRL tail r
120− , Row.Cons name (Maybe ty) tailRow r
121 ) =>
122 ) =>
122 AppendQueryParamsRL (RL.Cons name (Maybe ty) tail) r where
123 AppendQueryParamsRL (RL.Cons name (Maybe ty) tail) r where
123 appendQueryParamsRL _ rec acc = appendQueryParamsRL (Proxy :: _ tail) rec acc'
124 appendQueryParamsRL _ rec acc = appendQueryParamsRL (Proxy :: _ tail) rec acc'
124 where
125 where
125− acc' = case Record.get (Proxy :: _ name) rec of
126+ acc' = case (readOptional @name rec :: Maybe ty) of
126 Nothing -> acc
127 Nothing -> acc
127 Just value -> Array.snoc acc (encodeURIComponent_ (reflectSymbol (Proxy :: _ name)) <> "=" <> encodeURIComponent_ (serializeParam value))
128 Just value -> Array.snoc acc (encodeURIComponent_ (reflectSymbol (Proxy :: _ name)) <> "=" <> encodeURIComponent_ (serializeParam value))
128
129
130+-- bare ty (Required query param): always present
129else instance
131else instance
130 ( IsSymbol name
132 ( IsSymbol name
131 , SerializeParam ty
133 , SerializeParam ty
test/BuildUrl.Spec.purs+8−7
@@ -2,10 +2,11 @@ module BuildUrl.Spec where
2
2
3import Prelude
3import Prelude
4
4
5−import Data.Maybe (Maybe(..))
5+import Data.Maybe (Maybe)
6import Test.Spec (Spec, describe, it)
6import Test.Spec (Spec, describe, it)
7import Test.Spec.Assertions (shouldEqual)
7import Test.Spec.Assertions (shouldEqual)
8import Type.Proxy (Proxy(..))
8import Type.Proxy (Proxy(..))
9+import Unsafe.Coerce (unsafeCoerce)
9import Yoga.Fetch.Om.BuildUrl (buildUrl, substitutePathParams, appendQueryParams)
10import Yoga.Fetch.Om.BuildUrl (buildUrl, substitutePathParams, appendQueryParams)
10import Yoga.HTTP.API.Path (Path, type (/), type (:))
11import Yoga.HTTP.API.Path (Path, type (/), type (:))
11
12
@@ -50,24 +51,24 @@ spec = do
50 "/users"
51 "/users"
51 result `shouldEqual` "/users?limit=10&offset=20"
52 result `shouldEqual` "/users?limit=10&offset=20"
52
53
53− it "handles Maybe params - Just" do
54+ it "handles optional params - provided via partial record" do
54 let
55 let
55 result = appendQueryParams @(limit :: Maybe Int)
56 result = appendQueryParams @(limit :: Maybe Int)
56− { limit: Just 10 }
57+ (unsafeCoerce { limit: 10 } :: Record (limit :: Maybe Int))
57 "/users"
58 "/users"
58 result `shouldEqual` "/users?limit=10"
59 result `shouldEqual` "/users?limit=10"
59
60
60− it "handles Maybe params - Nothing" do
61+ it "handles optional params - omitted via partial record" do
61 let
62 let
62 result = appendQueryParams @(limit :: Maybe Int)
63 result = appendQueryParams @(limit :: Maybe Int)
63− { limit: Nothing }
64+ (unsafeCoerce {} :: Record (limit :: Maybe Int))
64 "/users"
65 "/users"
65 result `shouldEqual` "/users"
66 result `shouldEqual` "/users"
66
67
67− it "handles mixed Maybe and required params" do
68+ it "handles mixed optional and required params" do
68 let
69 let
69 result = appendQueryParams @(limit :: Maybe Int, offset :: Int)
70 result = appendQueryParams @(limit :: Maybe Int, offset :: Int)
70− { limit: Nothing, offset: 20 }
71+ (unsafeCoerce { offset: 20 } :: Record (limit :: Maybe Int, offset :: Int))
71 "/users"
72 "/users"
72 result `shouldEqual` "/users?offset=20"
73 result `shouldEqual` "/users?offset=20"
73
74