{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE StrictData #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# OPTIONS_GHC -fno-warn-unused-imports #-}
{-# OPTIONS_GHC -fno-warn-unused-matches #-}

-- Derived from AWS service descriptions, licensed under Apache 2.0.

-- |
-- Module      : Amazonka.ApiGatewayV2.Types.Cors
-- Copyright   : (c) 2013-2023 Brendan Hay
-- License     : Mozilla Public License, v. 2.0.
-- Maintainer  : Brendan Hay
-- Stability   : auto-generated
-- Portability : non-portable (GHC extensions)
module Amazonka.ApiGatewayV2.Types.Cors where

import qualified Amazonka.Core as Core
import qualified Amazonka.Core.Lens.Internal as Lens
import qualified Amazonka.Data as Data
import qualified Amazonka.Prelude as Prelude

-- | Represents a CORS configuration. Supported only for HTTP APIs. See
-- <https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-cors.html Configuring CORS>
-- for more information.
--
-- /See:/ 'newCors' smart constructor.
data Cors = Cors'
  { -- | Specifies whether credentials are included in the CORS request.
    -- Supported only for HTTP APIs.
    Cors -> Maybe Bool
allowCredentials :: Prelude.Maybe Prelude.Bool,
    -- | Represents a collection of allowed headers. Supported only for HTTP
    -- APIs.
    Cors -> Maybe [Text]
allowHeaders :: Prelude.Maybe [Prelude.Text],
    -- | Represents a collection of allowed HTTP methods. Supported only for HTTP
    -- APIs.
    Cors -> Maybe [Text]
allowMethods :: Prelude.Maybe [Prelude.Text],
    -- | Represents a collection of allowed origins. Supported only for HTTP
    -- APIs.
    Cors -> Maybe [Text]
allowOrigins :: Prelude.Maybe [Prelude.Text],
    -- | Represents a collection of exposed headers. Supported only for HTTP
    -- APIs.
    Cors -> Maybe [Text]
exposeHeaders :: Prelude.Maybe [Prelude.Text],
    -- | The number of seconds that the browser should cache preflight request
    -- results. Supported only for HTTP APIs.
    Cors -> Maybe Int
maxAge :: Prelude.Maybe Prelude.Int
  }
  deriving (Cors -> Cors -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Cors -> Cors -> Bool
$c/= :: Cors -> Cors -> Bool
== :: Cors -> Cors -> Bool
$c== :: Cors -> Cors -> Bool
Prelude.Eq, ReadPrec [Cors]
ReadPrec Cors
Int -> ReadS Cors
ReadS [Cors]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [Cors]
$creadListPrec :: ReadPrec [Cors]
readPrec :: ReadPrec Cors
$creadPrec :: ReadPrec Cors
readList :: ReadS [Cors]
$creadList :: ReadS [Cors]
readsPrec :: Int -> ReadS Cors
$creadsPrec :: Int -> ReadS Cors
Prelude.Read, Int -> Cors -> ShowS
[Cors] -> ShowS
Cors -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Cors] -> ShowS
$cshowList :: [Cors] -> ShowS
show :: Cors -> String
$cshow :: Cors -> String
showsPrec :: Int -> Cors -> ShowS
$cshowsPrec :: Int -> Cors -> ShowS
Prelude.Show, forall x. Rep Cors x -> Cors
forall x. Cors -> Rep Cors x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep Cors x -> Cors
$cfrom :: forall x. Cors -> Rep Cors x
Prelude.Generic)

-- |
-- Create a value of 'Cors' with all optional fields omitted.
--
-- Use <https://hackage.haskell.org/package/generic-lens generic-lens> or <https://hackage.haskell.org/package/optics optics> to modify other optional fields.
--
-- The following record fields are available, with the corresponding lenses provided
-- for backwards compatibility:
--
-- 'allowCredentials', 'cors_allowCredentials' - Specifies whether credentials are included in the CORS request.
-- Supported only for HTTP APIs.
--
-- 'allowHeaders', 'cors_allowHeaders' - Represents a collection of allowed headers. Supported only for HTTP
-- APIs.
--
-- 'allowMethods', 'cors_allowMethods' - Represents a collection of allowed HTTP methods. Supported only for HTTP
-- APIs.
--
-- 'allowOrigins', 'cors_allowOrigins' - Represents a collection of allowed origins. Supported only for HTTP
-- APIs.
--
-- 'exposeHeaders', 'cors_exposeHeaders' - Represents a collection of exposed headers. Supported only for HTTP
-- APIs.
--
-- 'maxAge', 'cors_maxAge' - The number of seconds that the browser should cache preflight request
-- results. Supported only for HTTP APIs.
newCors ::
  Cors
newCors :: Cors
newCors =
  Cors'
    { $sel:allowCredentials:Cors' :: Maybe Bool
allowCredentials = forall a. Maybe a
Prelude.Nothing,
      $sel:allowHeaders:Cors' :: Maybe [Text]
allowHeaders = forall a. Maybe a
Prelude.Nothing,
      $sel:allowMethods:Cors' :: Maybe [Text]
allowMethods = forall a. Maybe a
Prelude.Nothing,
      $sel:allowOrigins:Cors' :: Maybe [Text]
allowOrigins = forall a. Maybe a
Prelude.Nothing,
      $sel:exposeHeaders:Cors' :: Maybe [Text]
exposeHeaders = forall a. Maybe a
Prelude.Nothing,
      $sel:maxAge:Cors' :: Maybe Int
maxAge = forall a. Maybe a
Prelude.Nothing
    }

-- | Specifies whether credentials are included in the CORS request.
-- Supported only for HTTP APIs.
cors_allowCredentials :: Lens.Lens' Cors (Prelude.Maybe Prelude.Bool)
cors_allowCredentials :: Lens' Cors (Maybe Bool)
cors_allowCredentials = forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\Cors' {Maybe Bool
allowCredentials :: Maybe Bool
$sel:allowCredentials:Cors' :: Cors -> Maybe Bool
allowCredentials} -> Maybe Bool
allowCredentials) (\s :: Cors
s@Cors' {} Maybe Bool
a -> Cors
s {$sel:allowCredentials:Cors' :: Maybe Bool
allowCredentials = Maybe Bool
a} :: Cors)

-- | Represents a collection of allowed headers. Supported only for HTTP
-- APIs.
cors_allowHeaders :: Lens.Lens' Cors (Prelude.Maybe [Prelude.Text])
cors_allowHeaders :: Lens' Cors (Maybe [Text])
cors_allowHeaders = forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\Cors' {Maybe [Text]
allowHeaders :: Maybe [Text]
$sel:allowHeaders:Cors' :: Cors -> Maybe [Text]
allowHeaders} -> Maybe [Text]
allowHeaders) (\s :: Cors
s@Cors' {} Maybe [Text]
a -> Cors
s {$sel:allowHeaders:Cors' :: Maybe [Text]
allowHeaders = Maybe [Text]
a} :: Cors) forall b c a. (b -> c) -> (a -> b) -> a -> c
Prelude.. forall (f :: * -> *) (g :: * -> *) s t a b.
(Functor f, Functor g) =>
AnIso s t a b -> Iso (f s) (g t) (f a) (g b)
Lens.mapping forall s t a b. (Coercible s a, Coercible t b) => Iso s t a b
Lens.coerced

-- | Represents a collection of allowed HTTP methods. Supported only for HTTP
-- APIs.
cors_allowMethods :: Lens.Lens' Cors (Prelude.Maybe [Prelude.Text])
cors_allowMethods :: Lens' Cors (Maybe [Text])
cors_allowMethods = forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\Cors' {Maybe [Text]
allowMethods :: Maybe [Text]
$sel:allowMethods:Cors' :: Cors -> Maybe [Text]
allowMethods} -> Maybe [Text]
allowMethods) (\s :: Cors
s@Cors' {} Maybe [Text]
a -> Cors
s {$sel:allowMethods:Cors' :: Maybe [Text]
allowMethods = Maybe [Text]
a} :: Cors) forall b c a. (b -> c) -> (a -> b) -> a -> c
Prelude.. forall (f :: * -> *) (g :: * -> *) s t a b.
(Functor f, Functor g) =>
AnIso s t a b -> Iso (f s) (g t) (f a) (g b)
Lens.mapping forall s t a b. (Coercible s a, Coercible t b) => Iso s t a b
Lens.coerced

-- | Represents a collection of allowed origins. Supported only for HTTP
-- APIs.
cors_allowOrigins :: Lens.Lens' Cors (Prelude.Maybe [Prelude.Text])
cors_allowOrigins :: Lens' Cors (Maybe [Text])
cors_allowOrigins = forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\Cors' {Maybe [Text]
allowOrigins :: Maybe [Text]
$sel:allowOrigins:Cors' :: Cors -> Maybe [Text]
allowOrigins} -> Maybe [Text]
allowOrigins) (\s :: Cors
s@Cors' {} Maybe [Text]
a -> Cors
s {$sel:allowOrigins:Cors' :: Maybe [Text]
allowOrigins = Maybe [Text]
a} :: Cors) forall b c a. (b -> c) -> (a -> b) -> a -> c
Prelude.. forall (f :: * -> *) (g :: * -> *) s t a b.
(Functor f, Functor g) =>
AnIso s t a b -> Iso (f s) (g t) (f a) (g b)
Lens.mapping forall s t a b. (Coercible s a, Coercible t b) => Iso s t a b
Lens.coerced

-- | Represents a collection of exposed headers. Supported only for HTTP
-- APIs.
cors_exposeHeaders :: Lens.Lens' Cors (Prelude.Maybe [Prelude.Text])
cors_exposeHeaders :: Lens' Cors (Maybe [Text])
cors_exposeHeaders = forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\Cors' {Maybe [Text]
exposeHeaders :: Maybe [Text]
$sel:exposeHeaders:Cors' :: Cors -> Maybe [Text]
exposeHeaders} -> Maybe [Text]
exposeHeaders) (\s :: Cors
s@Cors' {} Maybe [Text]
a -> Cors
s {$sel:exposeHeaders:Cors' :: Maybe [Text]
exposeHeaders = Maybe [Text]
a} :: Cors) forall b c a. (b -> c) -> (a -> b) -> a -> c
Prelude.. forall (f :: * -> *) (g :: * -> *) s t a b.
(Functor f, Functor g) =>
AnIso s t a b -> Iso (f s) (g t) (f a) (g b)
Lens.mapping forall s t a b. (Coercible s a, Coercible t b) => Iso s t a b
Lens.coerced

-- | The number of seconds that the browser should cache preflight request
-- results. Supported only for HTTP APIs.
cors_maxAge :: Lens.Lens' Cors (Prelude.Maybe Prelude.Int)
cors_maxAge :: Lens' Cors (Maybe Int)
cors_maxAge = forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\Cors' {Maybe Int
maxAge :: Maybe Int
$sel:maxAge:Cors' :: Cors -> Maybe Int
maxAge} -> Maybe Int
maxAge) (\s :: Cors
s@Cors' {} Maybe Int
a -> Cors
s {$sel:maxAge:Cors' :: Maybe Int
maxAge = Maybe Int
a} :: Cors)

instance Data.FromJSON Cors where
  parseJSON :: Value -> Parser Cors
parseJSON =
    forall a. String -> (Object -> Parser a) -> Value -> Parser a
Data.withObject
      String
"Cors"
      ( \Object
x ->
          Maybe Bool
-> Maybe [Text]
-> Maybe [Text]
-> Maybe [Text]
-> Maybe [Text]
-> Maybe Int
-> Cors
Cors'
            forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> (Object
x forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
Data..:? Key
"allowCredentials")
            forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
Prelude.<*> (Object
x forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
Data..:? Key
"allowHeaders" forall a. Parser (Maybe a) -> a -> Parser a
Data..!= forall a. Monoid a => a
Prelude.mempty)
            forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
Prelude.<*> (Object
x forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
Data..:? Key
"allowMethods" forall a. Parser (Maybe a) -> a -> Parser a
Data..!= forall a. Monoid a => a
Prelude.mempty)
            forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
Prelude.<*> (Object
x forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
Data..:? Key
"allowOrigins" forall a. Parser (Maybe a) -> a -> Parser a
Data..!= forall a. Monoid a => a
Prelude.mempty)
            forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
Prelude.<*> (Object
x forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
Data..:? Key
"exposeHeaders" forall a. Parser (Maybe a) -> a -> Parser a
Data..!= forall a. Monoid a => a
Prelude.mempty)
            forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
Prelude.<*> (Object
x forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
Data..:? Key
"maxAge")
      )

instance Prelude.Hashable Cors where
  hashWithSalt :: Int -> Cors -> Int
hashWithSalt Int
_salt Cors' {Maybe Bool
Maybe Int
Maybe [Text]
maxAge :: Maybe Int
exposeHeaders :: Maybe [Text]
allowOrigins :: Maybe [Text]
allowMethods :: Maybe [Text]
allowHeaders :: Maybe [Text]
allowCredentials :: Maybe Bool
$sel:maxAge:Cors' :: Cors -> Maybe Int
$sel:exposeHeaders:Cors' :: Cors -> Maybe [Text]
$sel:allowOrigins:Cors' :: Cors -> Maybe [Text]
$sel:allowMethods:Cors' :: Cors -> Maybe [Text]
$sel:allowHeaders:Cors' :: Cors -> Maybe [Text]
$sel:allowCredentials:Cors' :: Cors -> Maybe Bool
..} =
    Int
_salt
      forall a. Hashable a => Int -> a -> Int
`Prelude.hashWithSalt` Maybe Bool
allowCredentials
      forall a. Hashable a => Int -> a -> Int
`Prelude.hashWithSalt` Maybe [Text]
allowHeaders
      forall a. Hashable a => Int -> a -> Int
`Prelude.hashWithSalt` Maybe [Text]
allowMethods
      forall a. Hashable a => Int -> a -> Int
`Prelude.hashWithSalt` Maybe [Text]
allowOrigins
      forall a. Hashable a => Int -> a -> Int
`Prelude.hashWithSalt` Maybe [Text]
exposeHeaders
      forall a. Hashable a => Int -> a -> Int
`Prelude.hashWithSalt` Maybe Int
maxAge

instance Prelude.NFData Cors where
  rnf :: Cors -> ()
rnf Cors' {Maybe Bool
Maybe Int
Maybe [Text]
maxAge :: Maybe Int
exposeHeaders :: Maybe [Text]
allowOrigins :: Maybe [Text]
allowMethods :: Maybe [Text]
allowHeaders :: Maybe [Text]
allowCredentials :: Maybe Bool
$sel:maxAge:Cors' :: Cors -> Maybe Int
$sel:exposeHeaders:Cors' :: Cors -> Maybe [Text]
$sel:allowOrigins:Cors' :: Cors -> Maybe [Text]
$sel:allowMethods:Cors' :: Cors -> Maybe [Text]
$sel:allowHeaders:Cors' :: Cors -> Maybe [Text]
$sel:allowCredentials:Cors' :: Cors -> Maybe Bool
..} =
    forall a. NFData a => a -> ()
Prelude.rnf Maybe Bool
allowCredentials
      seq :: forall a b. a -> b -> b
`Prelude.seq` forall a. NFData a => a -> ()
Prelude.rnf Maybe [Text]
allowHeaders
      seq :: forall a b. a -> b -> b
`Prelude.seq` forall a. NFData a => a -> ()
Prelude.rnf Maybe [Text]
allowMethods
      seq :: forall a b. a -> b -> b
`Prelude.seq` forall a. NFData a => a -> ()
Prelude.rnf Maybe [Text]
allowOrigins
      seq :: forall a b. a -> b -> b
`Prelude.seq` forall a. NFData a => a -> ()
Prelude.rnf Maybe [Text]
exposeHeaders
      seq :: forall a b. a -> b -> b
`Prelude.seq` forall a. NFData a => a -> ()
Prelude.rnf Maybe Int
maxAge

instance Data.ToJSON Cors where
  toJSON :: Cors -> Value
toJSON Cors' {Maybe Bool
Maybe Int
Maybe [Text]
maxAge :: Maybe Int
exposeHeaders :: Maybe [Text]
allowOrigins :: Maybe [Text]
allowMethods :: Maybe [Text]
allowHeaders :: Maybe [Text]
allowCredentials :: Maybe Bool
$sel:maxAge:Cors' :: Cors -> Maybe Int
$sel:exposeHeaders:Cors' :: Cors -> Maybe [Text]
$sel:allowOrigins:Cors' :: Cors -> Maybe [Text]
$sel:allowMethods:Cors' :: Cors -> Maybe [Text]
$sel:allowHeaders:Cors' :: Cors -> Maybe [Text]
$sel:allowCredentials:Cors' :: Cors -> Maybe Bool
..} =
    [Pair] -> Value
Data.object
      ( forall a. [Maybe a] -> [a]
Prelude.catMaybes
          [ (Key
"allowCredentials" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
Data..=)
              forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> Maybe Bool
allowCredentials,
            (Key
"allowHeaders" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
Data..=) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> Maybe [Text]
allowHeaders,
            (Key
"allowMethods" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
Data..=) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> Maybe [Text]
allowMethods,
            (Key
"allowOrigins" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
Data..=) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> Maybe [Text]
allowOrigins,
            (Key
"exposeHeaders" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
Data..=) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> Maybe [Text]
exposeHeaders,
            (Key
"maxAge" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
Data..=) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> Maybe Int
maxAge
          ]
      )