1+ import { AmqpExchange , Exchange , ExchangeInfo , ExchangeOptions } from "./exchange.js"
12import { AmqpQueue , Queue , QueueOptions , QueueType } from "./queue.js"
23import {
34 EventContext ,
@@ -10,7 +11,12 @@ import {
1011 SenderOptions ,
1112} from "rhea"
1213import { AmqpEndpoints , AmqpMethods , MessageBuilder , ME } from "./message_builder.js"
13- import { CreateQueueResponseDecoder , DeleteQueueResponseDecoder } from "./response_decoder.js"
14+ import {
15+ CreateExchangeResponseDecoder ,
16+ CreateQueueResponseDecoder ,
17+ DeleteExchangeResponseDecoder ,
18+ DeleteQueueResponseDecoder ,
19+ } from "./response_decoder.js"
1420
1521type LinkOpenEvents = SenderEvents . senderOpen | ReceiverEvents . receiverOpen
1622type LinkErrorEvents = SenderEvents . senderError | ReceiverEvents . receiverError
@@ -30,6 +36,8 @@ const MANAGEMENT_NODE_CONFIGURATION: SenderOptions | ReceiverOptions = {
3036export interface Management {
3137 declareQueue : ( queueName : string , options ?: Partial < QueueOptions > ) => Promise < Queue >
3238 deleteQueue : ( queueName : string ) => Promise < boolean >
39+ declareExchange : ( exchangeName : string , options : Partial < ExchangeOptions > ) => Promise < Exchange >
40+ deleteExchange : ( exchangeName : string ) => Promise < boolean >
3341 close : ( ) => void
3442}
3543
@@ -154,6 +162,67 @@ export class AmqpManagement implements Management {
154162 this . senderLink . send ( message )
155163 } )
156164 }
165+
166+ declareExchange ( exchangeName : string , options : Partial < ExchangeOptions > = { } ) : Promise < Exchange > {
167+ const exchangeInfo : ExchangeInfo = {
168+ type : options . type ?? "direct" ,
169+ arguments : options . arguments ?? { } ,
170+ autoDelete : options . auto_delete ?? false ,
171+ durable : options . durable ?? false ,
172+ name : exchangeName ,
173+ }
174+ return new Promise ( ( res , rej ) => {
175+ this . receiverLink . once ( ReceiverEvents . message , ( context : EventContext ) => {
176+ if ( ! context . message ) {
177+ return rej ( new Error ( "Receiver has not received any message" ) )
178+ }
179+
180+ const response = new CreateExchangeResponseDecoder ( ) . decodeFrom ( context . message , String ( message . message_id ) )
181+ if ( response . status === "error" ) {
182+ return rej ( response . error )
183+ }
184+
185+ return res ( new AmqpExchange ( exchangeInfo ) )
186+ } )
187+
188+ const message = new MessageBuilder ( )
189+ . sendTo ( `/${ AmqpEndpoints . Exchanges } /${ encodeURIComponent ( exchangeName ) } ` )
190+ . setReplyTo ( ME )
191+ . setAmqpMethod ( AmqpMethods . PUT )
192+ . setBody ( {
193+ type : options . type ,
194+ durable : options . durable ?? false ,
195+ auto_delete : options . auto_delete ?? false ,
196+ } )
197+ . build ( )
198+
199+ this . senderLink . send ( message )
200+ } )
201+ }
202+
203+ deleteExchange ( exchangeName : string ) : Promise < boolean > {
204+ return new Promise ( ( res , rej ) => {
205+ this . receiverLink . once ( ReceiverEvents . message , ( context : EventContext ) => {
206+ if ( ! context . message ) {
207+ return rej ( new Error ( "Receiver has not received any message" ) )
208+ }
209+
210+ const response = new DeleteExchangeResponseDecoder ( ) . decodeFrom ( context . message , String ( message . message_id ) )
211+ if ( response . status === "error" ) {
212+ return rej ( response . error )
213+ }
214+
215+ return res ( true )
216+ } )
217+
218+ const message = new MessageBuilder ( )
219+ . sendTo ( `/${ AmqpEndpoints . Exchanges } /${ encodeURIComponent ( exchangeName ) } ` )
220+ . setReplyTo ( ME )
221+ . setAmqpMethod ( AmqpMethods . DELETE )
222+ . build ( )
223+ this . senderLink . send ( message )
224+ } )
225+ }
157226}
158227
159228function buildArgumentsFrom ( queueType ?: QueueType , queueOptions ?: Record < string , string > ) {
0 commit comments