I’m trying to work with fetch
:
fetch
:: forall input output thruIn thruOut headers
. Union input thruIn (HighlevelRequestOptions headers String)
=> Union output thruOut UnsafeRequestOptions
=> ToCoreRequestOptions input output
=> String
-> { | input }
-> Aff Response
and I’m trying to make a helper function that can expand the functionality (by e.g., making the Aff
fail if not response.ok
). So I’m trying to make a function that still has the generic input
, output
, headers
etc., and call fetch
but the compiler isn’t letting me. To illustrate the problem I can say:
fetch2
:: forall input output thruIn thruOut headers
. Union input thruIn (HighlevelRequestOptions headers String)
=> Union output thruOut UnsafeRequestOptions
=> ToCoreRequestOptions input output
=> String
-> { | input }
-> Aff Response
fetch2 = fetch
and this won’t compile even though the annotation for fetch2
is identical to fetch
, because it’ll say that the first Union
constraint is not available and it contains unknown type variables and needs an annotation. I’m pretty sure this is related to the fact that the implementation of fetch
still compiles if you completely remove the first constraint - it’s not needed by the implementation at all.
I’ve found out that I can work around the problem by copying the implementation of fetch
and adding a parameter of type Proxy thruIn
. So I could have:
fetchWithProxy
:: forall input output thruIn thruOut headers
. Union input thruIn (HighlevelRequestOptions headers String)
=> Union output thruOut CoreRequest.UnsafeRequestOptions
=> ToCoreRequestOptions input output
=> Proxy thruIn
-> String
-> { | input }
-> Aff Response
fetchWithProxy _ url r = do
-- this implementation just copied directly from fetch
request <- liftEffect $ new url $ Request.convert r
cResponse <- Promise.toAffE $ Response.promiseToPromise <$> Core.fetch request
pure $ Response.convert cResponse
fetch2
:: forall input output thruIn thruOut headers
. Union input thruIn (HighlevelRequestOptions headers String)
=> Union output thruOut UnsafeRequestOptions
=> ToCoreRequestOptions input output
=> String
-> { | input }
-> Aff Response
fetch2 = fetchWithProxy (Proxy :: _ thruIn)
Anyway I’d really like to be able to do this without copying the implementation of fetch
and just somehow tell the compiler that my thruIn
needs to be the same as fetch
’s thruIn
. So maybe something like
fetch2
:: forall input output thruIn thruOut headers
. Union input thruIn (HighlevelRequestOptions headers String)
=> Union output thruOut UnsafeRequestOptions
=> ToCoreRequestOptions input output
=> String
-> { | input }
-> Aff Response
fetch2 = (fetch :: ?_)
But I can’t figure out a syntax that will let me do this. thruIn
only shows up in the constraints, and not at all in any of the input/output types.
Any advice where to go with this to avoid having to copy the source of fetch
just to get that proxy in there?