refactor: remove Target from infoResponse

This commit is contained in:
steve-chavez
2023-01-30 00:23:24 -05:00
committed by Steve Chavez
parent 0139cd8261
commit 7aadaa44e8
2 changed files with 29 additions and 18 deletions
+8 -2
View File
@@ -200,8 +200,14 @@ handleRequest AuthResult{..} conf appState authenticated prepared jsonDbS pgVer
oaiResult <- runQuery Plan.inspectPlanTxMode $ Query.openApiQuery sCache pgVer conf tSchema oaiResult <- runQuery Plan.inspectPlanTxMode $ Query.openApiQuery sCache pgVer conf tSchema
return $ Response.openApiResponse headersOnly oaiResult conf sCache iSchema iNegotiatedByProfile return $ Response.openApiResponse headersOnly oaiResult conf sCache iSchema iNegotiatedByProfile
(ActionInfo, _) -> (ActionInfo, TargetIdent identifier) ->
return $ Response.infoResponse iTarget sCache 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 -- This is unreachable as the ApiRequest.hs rejects it before
+21 -16
View File
@@ -3,7 +3,9 @@
module PostgREST.Response module PostgREST.Response
( createResponse ( createResponse
, deleteResponse , deleteResponse
, infoResponse , infoIdentResponse
, infoProcResponse
, infoRootResponse
, invokeResponse , invokeResponse
, openApiResponse , openApiResponse
, readResponse , readResponse
@@ -30,8 +32,7 @@ import qualified PostgREST.RangeQuery as RangeQuery
import qualified PostgREST.Response.OpenAPI as OpenAPI import qualified PostgREST.Response.OpenAPI as OpenAPI
import PostgREST.ApiRequest (ApiRequest (..), import PostgREST.ApiRequest (ApiRequest (..),
InvokeMethod (..), InvokeMethod (..))
Target (..))
import PostgREST.ApiRequest.Preferences (PreferRepresentation (..), import PostgREST.ApiRequest.Preferences (PreferRepresentation (..),
PreferTransaction (..), PreferTransaction (..),
shouldCount, shouldCount,
@@ -169,20 +170,12 @@ deleteResponse ctxApiRequest@ApiRequest{..} resultSet = case resultSet of
RSPlan plan -> RSPlan plan ->
Wai.responseLBS HTTP.status200 (contentTypeHeaders ctxApiRequest) $ LBS.fromStrict plan Wai.responseLBS HTTP.status200 (contentTypeHeaders ctxApiRequest) $ LBS.fromStrict plan
infoResponse :: Target -> SchemaCache -> Wai.Response infoIdentResponse :: QualifiedIdentifier -> SchemaCache -> Wai.Response
infoResponse target sCache = infoIdentResponse identifier sCache =
case target of case HM.lookup identifier (dbTables sCache) of
TargetIdent identifier -> Just tbl -> respondInfo $ allowH tbl
case HM.lookup identifier (dbTables sCache) of Nothing -> Error.errorResponseFor $ Error.ApiRequestError ApiRequestTypes.NotFound
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"
where where
respondInfo allowHeader = Wai.responseLBS HTTP.status200 [allOrigins, (HTTP.hAllow, allowHeader)] mempty
allOrigins = ("Access-Control-Allow-Origin", "*")
allowH table = allowH table =
let hasPK = not . null $ tablePKCols table in let hasPK = not . null $ tablePKCols table in
BS.intercalate "," $ BS.intercalate "," $
@@ -192,6 +185,18 @@ infoResponse target sCache =
["PATCH" | tableUpdatable table] ++ ["PATCH" | tableUpdatable table] ++
["DELETE" | tableDeletable 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 :: InvokeMethod -> ProcDescription -> ApiRequest -> ResultSet -> Wai.Response
invokeResponse invMethod proc ctxApiRequest@ApiRequest{..} resultSet = case resultSet of invokeResponse invMethod proc ctxApiRequest@ApiRequest{..} resultSet = case resultSet of
RSStandard {..} -> do RSStandard {..} -> do