@@ -19,7 +19,8 @@ module Yoga.Fetch.Om
1919
2020import Prelude
2121
22−import Data.Symbol (class IsSymbol)
22+import Data.Symbol (class IsSymbol, reflectSymbol)
23+import Data.Tuple.Nested ((/\))
2324import Data.Variant (Variant)
2425import Prim.Row as Row
2526import Prim.RowList (class RowToList, RowList)
@@ -29,7 +30,6 @@ import Type.Proxy (Proxy(..))
2930import Unsafe.Coerce (unsafeCoerce)
3031import JS.Fetch.Headers (Headers)
3132import JS.Fetch.Headers as Headers
32−import Type.Row.Homogeneous (class Homogeneous)
3333import Yoga.Fetch.Om.BuildUrl (class BuildUrl, buildUrl)
3434import Yoga.Fetch.Om.Simple (class DecodeResponse, decodeResponse, FetchError, FetchResponse, get, getWithHeaders, delete, deleteWithHeaders, delete_, post, postWithHeaders, post_, put, putWithHeaders, put_, patch, patchWithHeaders, patch_)
3535import Yoga.Fetch.Om.StreamDecode (class StreamDecode, decodeStream)
@@ -40,7 +40,7 @@ import Yoga.Fetch.Om.ParseResponse (class ParseResponse, parseResponse)
4040import Yoga.Fetch.Om.SplitResponses (class SplitResponses)
4141import Yoga.Fetch.Om.Variant (class VariantOrValue, variantOrValue)
4242import Yoga.HTTP.API.Path (Path, Root, Lit, Capture, PathCons, Param, QueryParams, Required, type (/), type (:), type (:?), class PathPattern)
43−import Yoga.HTTP.API.Route (Route(..), GET, POST, PUT, DELETE, PATCH, Response(..), JSON, FormData, PlainText, Streaming, NoBody)
43+import Yoga.HTTP.API.Route (Route(..), GET, POST, PUT, DELETE, PATCH, Response(..), JSON, FormData, PlainText, Streaming, NoBody, class HeaderValue, printHeader)
4444import Yoga.HTTP.API.Route.Handler (class SegmentPathParams, class SegmentQueryParams)
4545import Yoga.Om (class ToOm, Om, toOm)
4646
@@ -57,8 +57,19 @@ class ToHeaders headersRL headers where
5757instance ToHeaders RL.Nil headers where
5858 toHeaders _ _ = Headers.empty
5959
60−else instance Homogeneous headers String => ToHeaders headersRL headers where
61− toHeaders _ = Headers.fromRecord
60+else instance
61+ ( IsSymbol name
62+ , HeaderValue ty
63+ , Row.Cons name ty tailRow headers
64+ , Row.Lacks name tailRow
65+ , ToHeaders tail tailRow
66+ ) =>
67+ ToHeaders (RL.Cons name ty tail) headers where
68+ toHeaders _ headers = Headers.fromFoldable ([ headerName /\ headerValue ] <> Headers.toArray rest)
69+ where
70+ headerName = reflectSymbol (Proxy :: Proxy name)
71+ headerValue = printHeader (Record.get (Proxy :: Proxy name) headers)
72+ rest = toHeaders (Proxy :: Proxy tail) (unsafeCoerce headers :: Record tailRow)
6273
6374class DeriveClientFn :: forall k1. Type -> k1 -> Type -> Row Type -> Row Type -> Row Type -> Type -> Type -> Constraint
6475class
@@ -114,23 +125,20 @@ class DeriveClient routesRow clientsRow | routesRow -> clientsRow where
114125-- | type UserAPI = { getUser :: Route ... }
115126-- | api = client @UserAPI "https://api.example.com"
116127-- | ```
117−client :: forall @routes routesRow clientsRow polyClientsRow. RecordRow routes routesRow => DeriveClient routesRow clientsRow => String -> Record polyClientsRow
118−client baseUrl = polymorphic (deriveClientImpl baseUrl (Proxy :: _ { | routesRow }) :: Record clientsRow)
128+client :: forall @routes routesRow clientsRow. RecordRow routes routesRow => DeriveClient routesRow clientsRow => String -> Record clientsRow
129+client baseUrl = deriveClientImpl baseUrl (Proxy :: _ { | routesRow })
119130
120131-- | Deprecated: Use `client` with VTA instead
121132-- |
122133-- | ```purescript
123134-- | api = deriveClient @UserAPI "https://api.example.com"
124135-- | ```
125−deriveClient :: forall @routesRow clientsRow polyClientsRow. DeriveClient routesRow clientsRow => String -> Record polyClientsRow
126−deriveClient baseUrl = polymorphic (deriveClientImpl baseUrl (Proxy :: _ { | routesRow }) :: Record clientsRow)
136+deriveClient :: forall @routesRow clientsRow. DeriveClient routesRow clientsRow => String -> Record clientsRow
137+deriveClient baseUrl = deriveClientImpl baseUrl (Proxy :: _ { | routesRow })
127138
128139plainText :: PlainText -> String
129140plainText = unsafeCoerce
130141
131−polymorphic :: forall a b. a -> b
132−polymorphic = unsafeCoerce
133−
134142instance
135143 ( RowToList routesRow rl
136144 , DeriveClientRL rl () clientsRow