Skip to content

Commit

Permalink
Merge pull request #1529 from purefunsolutions/fix-servant-client-ghc…
Browse files Browse the repository at this point in the history
…js-for-servant-0.19

Fix servant-client-ghcjs for servant 0.19
  • Loading branch information
Gaël Deest committed Feb 14, 2022
2 parents 002fa21 + 17b5563 commit d35b3e9
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
10 changes: 10 additions & 0 deletions changelog.d/1529
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
synopsis: Fix performRequest in servant-client-ghcjs
prs: #1529

description: {

performRequest function in servant-client-ghcjs was not compatible with the
latest RunClient typeclass. Added the acceptStatus parameter and fixed the
functionality to match what servant-client provides.

}
2 changes: 1 addition & 1 deletion servant-client-ghcjs/servant-client-ghcjs.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ library
Servant.Client.Internal.XhrClient

build-depends:
base >=4.11 && <4.12
base >=4.11 && <5
, bytestring >=0.10 && <0.12
, case-insensitive >=1.2.0.0 && <1.3.0.0
, containers >=0.5 && <0.7
Expand Down
13 changes: 8 additions & 5 deletions servant-client-ghcjs/src/Servant/Client/Internal/XhrClient.hs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ instance Exception StreamingNotSupportedException where
displayException _ = "streamingRequest: streaming is not supported!"

instance RunClient ClientM where
runRequest = performRequest
runRequestAcceptStatus = performRequest
throwClientError = throwError

runClientMOrigin :: ClientM a -> ClientEnv -> IO (Either ClientError a)
Expand Down Expand Up @@ -152,15 +152,18 @@ runClientM m = do

runClientMOrigin m (ClientEnv (BaseUrl protocol hostname port ""))

performRequest :: Request -> ClientM Response
performRequest req = do
performRequest :: Maybe [Status] -> Request -> ClientM Response
performRequest acceptStatus req = do
xhr <- liftIO initXhr
burl <- asks baseUrl
liftIO $ performXhr xhr burl req
resp <- toResponse xhr

let status = statusCode (responseStatusCode resp)
unless (status >= 200 && status < 300) $ do
let status = responseStatusCode resp
goodStatus = case acceptStatus of
Nothing -> statusIsSuccessful status
Just good -> status `elem` good
unless goodStatus $ do
let f b = (burl, BL.toStrict $ toLazyByteString b)
throwError $ FailureResponse (bimap (const ()) f req) resp

Expand Down

0 comments on commit d35b3e9

Please sign in to comment.