Created
June 10, 2020 10:06
-
-
Save Woody88/562b52c731e63610251bed20b5b360ea to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(Right { body: "{\"message\":\"Successfully retrieved\",\"data\":[{\"particular\":{\"netTonnage\":123.45,\"freeBoard\":456.34,\"bowThruster\":true},\"name\":\"ABC Ship\",\"maintainer\":\"Mr. X\",\"imoNumber\":12345,\"id\":\"f244a03f-e5d7-44c7-bec3-7d161acaf41c\"},{\"particular\":{\"netTonnage\":123.45,\"freeBoard\":456.34,\"bowThruster\":true},\"name\":\"ABC Ship Test\",\"maintainer\":\"Mr. X\",\"imoNumber\":123456,\"id\":\"bea2ee4d-e923-4ae3-9e8b-da165d8758fc\"}]}", headers: [(ResponseHeader "content-type" "application/json; charset=utf-8")], status: (StatusCode 200), statusText: "OK" }) foreign.js:5:12 | |
(Left "Error at property \"data\": Error at array index 0: Error at property \"netTonnage\": Type mismatch: expected Number, found Undefined\nError at property \"data\": Error at array index 0: Error at property \"freeBoard\": Type mismatch: expected Number, found Undefined\nError at property \"data\": Error at array index 0: Error at property \"bowThruster\": Type mismatch: expected Boolean, found Undefined\n") foreign.js:5:12 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type ShipApplication = | |
{ id :: String | |
| CoverRow | |
+ ParticularRow | |
+ () | |
} | |
type ParticularRow r | |
= ( netTonnage :: Number | |
, freeBoard :: Number | |
, bowThruster :: Boolean | |
) | |
type CoverRow r | |
= ( imoNumber :: Int | |
, name :: String | |
, maintainer :: String | |
| r | |
) | |
type ServiceResponse = { message :: String, data :: Array ShipApplication } | |
fetchShipApplication :: ShipApplicationQueryParam -> Aff (Either String (Array ShipApplication)) | |
fetchShipApplication query = do | |
let urlquery = append "?" <<< String.joinWith "&" $ | |
catMaybes | |
[ map (append "imoNumber=" <<< show ) query.imoNumber | |
, map ("maintainer=" <> _) query.maintainer | |
, map ("name=" <> _) query.name | |
] | |
endpoint = "http://localhost:3000/ship-application/list" <> if String.null urlquery then mempty else urlquery | |
requestError = append "Request failed: " <<< AX.printError | |
err e = foldl (\l e' -> Foreign.renderForeignError e' <> "\n" <> l) mempty e | |
eResult <- lmap requestError <$> AX.get ResponseFormat.string endpoint | |
Console.logShow eResult | |
(r :: Either String ServiceResponse) <- case eResult of | |
Left e -> pure $ Left "error" | |
Right result -> pure $ lmap err $ readJSON result.body | |
Console.logShow r | |
pure $ Right [] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment