首页 > 解决方案 > 在带有 Servant 的内部 Api 之前添加公共根路径

问题描述

我有以下 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 一次?

PS:仆人做的真好!

标签: haskellservant

解决方案


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

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在一定程度上解决了这个问题。


推荐阅读