From 7aadaa44e8952f59c6eddfc7e9ab588bb36b300a Mon Sep 17 00:00:00 2001 From: steve-chavez Date: Sun, 29 Jan 2023 23:28:20 -0500 Subject: [PATCH] refactor: remove Target from infoResponse --- src/PostgREST/App.hs | 10 ++++++++-- src/PostgREST/Response.hs | 37 +++++++++++++++++++++---------------- 2 files changed, 29 insertions(+), 18 deletions(-) diff --git a/src/PostgREST/App.hs b/src/PostgREST/App.hs index 5b5ea87a3..eb47231ff 100644 --- a/src/PostgREST/App.hs +++ b/src/PostgREST/App.hs @@ -200,8 +200,14 @@ handleRequest AuthResult{..} conf appState authenticated prepared jsonDbS pgVer oaiResult <- runQuery Plan.inspectPlanTxMode $ Query.openApiQuery sCache pgVer conf tSchema return $ Response.openApiResponse headersOnly oaiResult conf sCache iSchema iNegotiatedByProfile - (ActionInfo, _) -> - return $ Response.infoResponse iTarget sCache + (ActionInfo, TargetIdent identifier) -> + return $ Response.infoIdentResponse identifier sCache + + (ActionInfo, TargetProc proc _) -> + return $ Response.infoProcResponse proc + + (ActionInfo, TargetDefaultSpec _) -> + return Response.infoRootResponse _ -> -- This is unreachable as the ApiRequest.hs rejects it before diff --git a/src/PostgREST/Response.hs b/src/PostgREST/Response.hs index 9f710b660..ae72cfcfa 100644 --- a/src/PostgREST/Response.hs +++ b/src/PostgREST/Response.hs @@ -3,7 +3,9 @@ module PostgREST.Response ( createResponse , deleteResponse - , infoResponse + , infoIdentResponse + , infoProcResponse + , infoRootResponse , invokeResponse , openApiResponse , readResponse @@ -30,8 +32,7 @@ import qualified PostgREST.RangeQuery as RangeQuery import qualified PostgREST.Response.OpenAPI as OpenAPI import PostgREST.ApiRequest (ApiRequest (..), - InvokeMethod (..), - Target (..)) + InvokeMethod (..)) import PostgREST.ApiRequest.Preferences (PreferRepresentation (..), PreferTransaction (..), shouldCount, @@ -169,20 +170,12 @@ deleteResponse ctxApiRequest@ApiRequest{..} resultSet = case resultSet of RSPlan plan -> Wai.responseLBS HTTP.status200 (contentTypeHeaders ctxApiRequest) $ LBS.fromStrict plan -infoResponse :: Target -> SchemaCache -> Wai.Response -infoResponse target sCache = - case target of - TargetIdent identifier -> - case HM.lookup identifier (dbTables sCache) of - Just tbl -> respondInfo $ allowH tbl - Nothing -> Error.errorResponseFor $ Error.ApiRequestError ApiRequestTypes.NotFound - TargetProc pd _ - | pdVolatility pd == Volatile -> respondInfo "OPTIONS,POST" - | otherwise -> respondInfo "OPTIONS,GET,HEAD,POST" - TargetDefaultSpec _ -> respondInfo "OPTIONS,GET,HEAD" +infoIdentResponse :: QualifiedIdentifier -> SchemaCache -> Wai.Response +infoIdentResponse identifier sCache = + case HM.lookup identifier (dbTables sCache) of + Just tbl -> respondInfo $ allowH tbl + Nothing -> Error.errorResponseFor $ Error.ApiRequestError ApiRequestTypes.NotFound where - respondInfo allowHeader = Wai.responseLBS HTTP.status200 [allOrigins, (HTTP.hAllow, allowHeader)] mempty - allOrigins = ("Access-Control-Allow-Origin", "*") allowH table = let hasPK = not . null $ tablePKCols table in BS.intercalate "," $ @@ -192,6 +185,18 @@ infoResponse target sCache = ["PATCH" | tableUpdatable table] ++ ["DELETE" | tableDeletable table] +infoProcResponse :: ProcDescription -> Wai.Response +infoProcResponse proc | pdVolatility proc == Volatile = respondInfo "OPTIONS,POST" + | otherwise = respondInfo "OPTIONS,GET,HEAD,POST" + +infoRootResponse :: Wai.Response +infoRootResponse = respondInfo "OPTIONS,GET,HEAD" + +respondInfo :: ByteString -> Wai.Response +respondInfo allowHeader = + let allOrigins = ("Access-Control-Allow-Origin", "*") in + Wai.responseLBS HTTP.status200 [allOrigins, (HTTP.hAllow, allowHeader)] mempty + invokeResponse :: InvokeMethod -> ProcDescription -> ApiRequest -> ResultSet -> Wai.Response invokeResponse invMethod proc ctxApiRequest@ApiRequest{..} resultSet = case resultSet of RSStandard {..} -> do