-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utils.purs
29 lines (23 loc) · 1.01 KB
/
Utils.purs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
module MeowClient.Utils where
import Prelude
import Data.Argonaut (class DecodeJson, Json, decodeJson)
import Data.Either (Either(..))
import Data.Tuple (Tuple)
import Effect (Effect)
import Effect.Aff (Aff)
import Effect.Exception (Error)
import MeowClient.JsonOrJsError as JsonOrJsError
import Promise (Promise)
import Promise.Aff (toAffE)
type RightF = forall l r . r -> Either l r
type LeftF = forall l r . l -> Either l r
type TupleF = forall l r . l -> r -> Tuple l r
type EffPromise v = Effect (Promise v)
mapLeft :: forall l r l2 . (l -> l2) -> Either l r -> Either l2 r
mapLeft f = case _ of
Left l -> Left $ f l
Right r -> Right r
decodeJsErrorOrJson :: forall r . DecodeJson r => Either Error Json -> Either JsonOrJsError.Value r
decodeJsErrorOrJson res = mapLeft JsonOrJsError.Other res >>= mapLeft JsonOrJsError.Json <<< decodeJson
toAffDecodeResult :: forall r . DecodeJson r => EffPromise (Either Error Json) -> Aff (Either JsonOrJsError.Value r)
toAffDecodeResult v = decodeJsErrorOrJson <$> toAffE v