在带有Servant的内部Api之前添加公共根路径

时间:2018-12-13 15:22:01

标签: haskell servant

我有以下API:

type GSDMonitoringApi =   FetchWorkspaceIdsCreated
                    :<|>  FetchGsdCommandsByWorkspaceId

type FetchWorkspaceIdsCreated =      "gsd" :> "monitoring" :> "workspaceIds" :> Get '[JSON] [Persisted WorkspaceId]
type FetchGsdCommandsByWorkspaceId = "gsd" :> "monitoring" :> "commands" :> Capture "workspaceId" WorkspaceId :> Get '[JSON] [Persisted GsdCommand]

是否可以排除"gsd" :> "monitoring" :>并将其放入GSDMonitoringApi一次?

P.S:仆人做得好极了!

1 个答案:

答案 0 :(得分:1)

是的,您可以在合并两个分支后将前缀添加到这两个分支:

type GSDMonitoringApi = 
    "gsd" :> "monitoring" :> 
    ( FetchWorkspaceIdsCreated :<|> FetchGsdCommandsByWorkspaceId )

type FetchWorkspaceIdsCreated = "workspaceIds" :> Get '[JSON] [Persisted WorkspaceId]
type FetchGsdCommandsByWorkspaceId = "commands" :> Capture "workspaceId" WorkspaceId :> Get '[JSON] [Persisted GsdCommand]

作为半关联点,我建议您签出Servant.API.Generic。像这样将API与:<|>组合在一起非常难看。 Servant.API.Generic在某种程度上解决了这个问题。