@@ -19,6 +19,7 @@ module Supabase.Supabase
1919 , class Select
2020 , delete
2121 , equals
22+ , channel
2223 , from
2324 , getSession
2425 , invoke
@@ -26,6 +27,7 @@ module Supabase.Supabase
2627 , onAuthStateChange
2728 , range
2829 , select
30+ , getUser
2931 , signInWithOtp
3032 , signInWithOtpOptions
3133 , signOut
@@ -37,26 +39,28 @@ module Supabase.Supabase
3739import Prelude
3840
3941import Control.Promise (Promise)
40−import Control.Promise as Promise
42+import Data.Either (Either(..))
4143import Data.Function.Uncurried (Fn2, Fn3, runFn2, runFn3)
42−import Data.Maybe (Maybe)
44+import Data.Maybe (Maybe(..))
4345import Data.Nullable (Nullable)
44−import Data.Nullable as Nullable
4546import Data.Tuple (Tuple(..))
4647import Effect (Effect)
4748import Effect.Aff (Aff, Error)
49+import Effect.Exception (error)
4850import Effect.Uncurried (EffectFn1, EffectFn3, mkEffectFn1, runEffectFn3)
49−import Fetch as Fetch
50−import Fetch.Core.Response as YogaJson.Core
51−import Fetch.Internal.Response as FetchInternalResponse
5251import Foreign (Foreign)
5352import Prim.Row (class Union)
54−import Supabase.Types (Client)
55−import Supabase.Util as Util
53+import Supabase.Types (Channel, ChannelName, Client)
5654import Type.Function (type ($))
5755import Type.Row (type (+))
58−import Yoga.JSON (class ReadForeign, class WriteForeign, write) as YogaJson
5956import Yoga.JSON (class ReadForeign, class WriteForeign, writeImpl)
57+import Fetch as Fetch
58+import Fetch.Internal.Response as FetchInternalResponse
59+import Data.Nullable as Nullable
60+import Control.Promise as Promise
61+import Supabase.Util as Util
62+import Yoga.JSON (class ReadForeign, class WriteForeign, write) as YogaJson
63+import Fetch.Core.Response as YogaJson.Core
6064
6165foreign import data QueryBuilder :: Type
6266foreign import data FilterBuilder :: Type
@@ -169,7 +173,7 @@ foreign import onAuthStateChangeImpl :: Client -> (EffectFn1 (Nullable Session)
169173onAuthStateChange ∷ Client → (Maybe Session → Effect Unit) → Effect { data :: { id :: String, unsubscribe :: Effect Unit } }
170174onAuthStateChange client handler = onAuthStateChangeImpl client $ mkEffectFn1 (Nullable.toMaybe >>> handler)
171175
172−type User = { id :: String, email :: String }
176+type User = { id :: String, email :: String, user_metadata :: Foreign }
173177type Session = { user :: User }
174178type SessionData = { session :: Maybe Session }
175179
@@ -200,3 +204,16 @@ invoke client fn body headers = runFn3 (invokeImpl client) fn body headers # Pro
200204 where
201205 convertError { message, context } = { message, context: FetchInternalResponse.convert context }
202206 convert { "data": d, error } = { "data": Nullable.toMaybe d, error: Nullable.toMaybe error <#> convertError }
207+
208+foreign import channel :: ChannelName -> Client -> Effect Channel
209+
210+foreign import getUserImpl :: Client -> Effect (Promise { data :: Nullable User, error :: Nullable Error })
211+
212+getUser :: Client -> Aff (Either Error User)
213+getUser client = do
214+ res <- getUserImpl client # Promise.toAffE
215+ case Nullable.toMaybe res.data, Nullable.toMaybe res.error of
216+ Just user, _ -> pure $ Right user
217+ _, Just error -> pure $ Left error
218+ _, _ -> pure $ Left (error "Unexpected response error")
219+