git.iko.soy/rowtype-yoga/purescript-yoga-fetch-om
on main help sign in
Add polymorphic helper to enable forall at record level
9df43d1Mark
src/Yoga/Fetch/Om.purs+13
@@ -1,6 +1,7 @@
1module Yoga.Fetch.Om
1module Yoga.Fetch.Om
2 ( client
2 ( client
3 , deriveClient
3 , deriveClient
4+ , polymorphic
4 , class DeriveClient
5 , class DeriveClient
5 , deriveClientImpl
6 , deriveClientImpl
6 , class DeriveClientRL
7 , class DeriveClientRL
@@ -120,6 +121,18 @@ client baseUrl = deriveClientImpl baseUrl (Proxy :: _ { | routesRow })
120deriveClient :: forall @routesRow clientsRow. DeriveClient routesRow clientsRow => String -> Record clientsRow
121deriveClient :: forall @routesRow clientsRow. DeriveClient routesRow clientsRow => String -> Record clientsRow
121deriveClient baseUrl = deriveClientImpl baseUrl (Proxy :: _ { | routesRow })
122deriveClient baseUrl = deriveClientImpl baseUrl (Proxy :: _ { | routesRow })
122
123
124+-- | Cast a generated client to a polymorphic type signature
125+-- | This allows you to add explicit forall quantification for better documentation
126+-- |
127+-- | ```purescript
128+-- | api :: forall ctx err.
129+-- | { getUser :: { id :: Int } -> Om ctx (notFound :: ErrorMessage | err) User
130+-- | }
131+-- | api = polymorphic $ client @UserAPI "https://api.example.com"
132+-- | ```
133+polymorphic :: forall a b. a -> b
134+polymorphic = unsafeCoerce
135+
123instance
136instance
124 ( RowToList routesRow rl
137 ( RowToList routesRow rl
125 , DeriveClientRL rl () clientsRow
138 , DeriveClientRL rl () clientsRow
test/Complete.Example.purs+10−8
@@ -5,7 +5,7 @@ import Prelude
5import Data.Array (length)
5import Data.Array (length)
6import Data.Maybe (Maybe(..))
6import Data.Maybe (Maybe(..))
7import Effect.Class.Console (log)
7import Effect.Class.Console (log)
8−import Yoga.Fetch.Om (GET, POST, PUT, DELETE, Route, JSON, Path, type (/), type (:), type (:?), client)
8+import Yoga.Fetch.Om (GET, POST, PUT, DELETE, Route, JSON, Path, type (/), type (:), type (:?), client, polymorphic)
9import Yoga.Om (Om, handleErrors)
9import Yoga.Om (Om, handleErrors)
10
10
11type User =
11type User =
@@ -73,15 +73,17 @@ type UserAPI =
73 )
73 )
74 }
74 }
75
75
76+-- Generated client with polymorphic type annotation
76api
77api
78+ :: forall ctx err
77− :: { createUser :: CreateUserRequest -> Om {} (badRequest :: ErrorMessage) User
79+ . { createUser :: CreateUserRequest -> Om ctx (badRequest :: ErrorMessage | err) User
78− , createUserAuth :: { authorization :: String } -> CreateUserRequest -> Om {} (badRequest :: ErrorMessage) User
80+ , createUserAuth :: { authorization :: String } -> CreateUserRequest -> Om ctx (badRequest :: ErrorMessage | err) User
79− , deleteUser :: { id :: Int } -> Om {} (notFound :: ErrorMessage) {}
81+ , deleteUser :: { id :: Int } -> Om ctx (notFound :: ErrorMessage | err) {}
80− , getUser :: { id :: Int } -> Om {} (notFound :: ErrorMessage) User
82+ , getUser :: { id :: Int } -> Om ctx (notFound :: ErrorMessage | err) User
81− , listUsers :: { limit :: Maybe Int, offset :: Maybe Int } -> Om {} () (Array User)
83+ , listUsers :: { limit :: Maybe Int, offset :: Maybe Int } -> Om ctx err (Array User)
82− , updateUser :: { id :: Int } -> UpdateUserRequest -> Om {} (badRequest :: ErrorMessage, notFound :: ErrorMessage) User
84+ , updateUser :: { id :: Int } -> UpdateUserRequest -> Om ctx (badRequest :: ErrorMessage, notFound :: ErrorMessage | err) User
83 }
85 }
84−api = client @UserAPI "https://api.example.com"
86+api = polymorphic $ client @UserAPI "https://api.example.com"
85
87
86exampleGetUser :: Om {} (notFound :: ErrorMessage) Unit
88exampleGetUser :: Om {} (notFound :: ErrorMessage) Unit
87exampleGetUser = do
89exampleGetUser = do