Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell Servant - is there a way to compose API types?

I know this isn't valid syntax, but is there a way to accomplish something like this in servant?

type StandardAPI = "foo" :> Get '[JSON] Whatever

type CustomAPI = StandardAPI :<|> "customroute" :> Get '[JSON] Blah

in other words, composing APIs. In Spock I could do this with the monadic route construction, but I'm not sure how to do this in servant.

This way I can reuse shared routes across APIs. Another reason to use this is that there are certain types that don't work with client generators, such as Raw.

like image 917
daj Avatar asked Jan 25 '26 10:01

daj


1 Answers

Yes, referencing Servant documentation you can use

type CombinedAPI = "users" :> UsersAPI
          :<|> "products" :> ProductsAPI

server :: Server CombinedAPI
server = usersServer :<|> productsServer

usersServer :: Server UsersAPI
usersServer = -- implementation

productsServer :: Server ProductsAPI
productsServer = -- implementation
like image 144
klappvisor Avatar answered Jan 26 '26 22:01

klappvisor



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!