@@ -1,14 +1,29 @@
1−module Kafka.Producer where
1+module Kafka.Producer
2+ ( Producer
3+ , ProducerConfig
4+ , makeProducer
5+ , connect
6+ , ProducerMessage
7+ , ProducerBatch
8+ , InternalProducerMessage
9+ , InternalProducerBatch
10+ , toInternalProducerMessage
11+ , toInternalProducerBatch
12+ , RecordMetadata
13+ , send
14+ , disconnect
15+ ) where
216
3−import Prelude (Unit, (#), (>>>))
17+import Prelude (Unit, (#), (>>>), (<#>))
418import Control.Promise (Promise, toAffE)
519import Data.Function.Uncurried (Fn2, runFn2)
620import Data.Maybe (Maybe)
721import Data.Nullable (Nullable, toNullable)
822import Effect (Effect)
923import Effect.Aff (Aff)
10−import Kafka.Kafka (Kafka, Payload, RecordMetadata)
11−import Kafka.Internal.Internal (InternalPayload, convert)
24+import Kafka.Kafka (Kafka)
25+import Kafka.Types (Partition(..), Topic(..))
26+import Data.Newtype (un)
1227
1328foreign import data Producer :: Type
1429
@@ -32,7 +47,7 @@ makeProducer k pc = runFn2 makeProducerImpl k ipc
3247 ipc =
3348 { idempotent: toNullable pc.idempotent
3449 , transactionalId: toNullable pc.transactionalId
35− , maxInFlightRequests : toNullable pc.maxInFlightRequests
50+ , maxInFlightRequests: toNullable pc.maxInFlightRequests
3651 }
3752
3853foreign import connectImpl :: Producer -> Effect (Promise Unit)
@@ -40,10 +55,40 @@ foreign import connectImpl :: Producer -> Effect (Promise Unit)
4055connect :: Producer -> Aff Unit
4156connect = connectImpl >>> toAffE
4257
43−foreign import sendImpl :: Fn2 Producer InternalPayload (Effect (Promise (Array RecordMetadata)))
58+type ProducerMessage
59+ = { key :: Maybe String
60+ , value :: String
61+ , partition :: Maybe Partition
62+ }
63+
64+type ProducerBatch
65+ = { topic :: Topic
66+ , messages :: Array (ProducerMessage)
67+ }
68+
69+foreign import data RecordMetadata :: Type
70+
71+type InternalProducerMessage
72+ = { key :: Nullable String
73+ , value :: String
74+ , partition :: Nullable Int
75+ }
76+
77+type InternalProducerBatch
78+ = { topic :: String
79+ , messages :: Array (InternalProducerMessage)
80+ }
81+
82+toInternalProducerMessage :: ProducerMessage -> InternalProducerMessage
83+toInternalProducerMessage { key, value, partition: partition } = { key: toNullable key, value: value, partition: partition <#> un Partition # toNullable }
84+
85+toInternalProducerBatch :: ProducerBatch -> InternalProducerBatch
86+toInternalProducerBatch { topic: Topic topic, messages } = { topic, messages: messages <#> toInternalProducerMessage }
87+
88+foreign import sendImpl :: Fn2 Producer InternalProducerBatch (Effect (Promise (Array RecordMetadata)))
4489
45−send :: Producer -> Payload -> Aff (Array RecordMetadata)
46−send p pl = runFn2 sendImpl p (convert pl) # toAffE
90+send :: Producer -> ProducerBatch -> Aff (Array RecordMetadata)
91+send p pl = runFn2 sendImpl p (toInternalProducerBatch pl) # toAffE
4792
4893foreign import disconnectImpl :: Producer -> Effect (Promise Unit)
4994