forked from DefinitelyTyped/DefinitelyTyped
/
index.d.ts
196 lines (167 loc) · 7.99 KB
/
index.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// Type definitions for amqp-connection-manager 2.0
// Project: https://github.com/benbria/node-amqp-connection-manager
// Definitions by: rogierschouten <https://github.com/rogierschouten>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 3.2
import { ConfirmChannel, Connection, Message, Options, Replies } from "amqplib";
import { EventEmitter } from "events";
import { ConnectionOptions } from "tls";
/**
* connect() options
*/
export interface AmqpConnectionManagerOptions {
/**
* Interval to send heartbeats to broker. Defaults to 5 seconds.
*/
heartbeatIntervalInSeconds?: number;
/**
* The time to wait before trying to reconnect. If not specified, defaults to heartbeatIntervalInSeconds
*/
reconnectTimeInSeconds?: number;
/**
* is a function which returns one or more servers to connect to. This should return either a single URL or an array of URLs.
* This is handy when you're using a service discovery mechanism such as Consul or etcd. Instead of taking a callback, this can also
* return a Promise. Note that if this is supplied, then urls is ignored.
*/
findServers?: ((callback: (urls: string | string[]) => void) => void) | (() => Promise<string | string[]>);
/**
* TLS options
*
* These are passed through directly to amqplib (http://www.squaremobius.net/amqp.node/channel_api.html#connect),
* which in turn passes them through to tls.connect (https://nodejs.org/api/tls.html#tls_tls_connect_options_callback)
*/
connectionOptions?: ConnectionOptions;
}
/**
* Creates a new AmqpConnectionManager, which will connect to one of the URLs provided in urls.
* If a broker is unreachable or dies, then AmqpConnectionManager will try the next available broker, round-robin.
* @param urls
* @param options
*/
export function connect(urls: string[], options?: AmqpConnectionManagerOptions): AmqpConnectionManager;
export type SetupFunc = ((channel: ConfirmChannel, callback: (error?: Error) => void) => void) | ((channel: ConfirmChannel) => Promise<void>);
export interface CreateChannelOpts {
/**
* Name for this channel. Used for debugging.
*/
name?: string;
/**
* A function to call whenever we reconnect to the broker (and therefore create a new underlying channel.)
* This function should either accept a callback, or return a Promise. See addSetup below
*/
setup?: SetupFunc;
/**
* if true, then ChannelWrapper assumes all messages passed to publish() and sendToQueue() are plain JSON objects.
* These will be encoded automatically before being sent.
*/
json?: boolean;
}
export interface AmqpConnectionManager extends EventEmitter {
addListener(event: string, listener: (...args: any[]) => void): this;
addListener(event: "connect", listener: (arg: { connection: Connection, url: string }) => void): this;
addListener(event: "disconnect", listener: (arg: {err: Error}) => void): this;
on(event: string, listener: (...args: any[]) => void): this;
on(event: "connect", listener: (arg: { connection: Connection, url: string }) => void): this;
on(event: "disconnect", listener: (arg: {err: Error}) => void): this;
once(event: string, listener: (...args: any[]) => void): this;
once(event: "connect", listener: (arg: { connection: Connection, url: string }) => void): this;
once(event: "disconnect", listener: (arg: {err: Error}) => void): this;
prependListener(event: string, listener: (...args: any[]) => void): this;
prependListener(event: "connect", listener: (arg: { connection: Connection, url: string }) => void): this;
prependListener(event: "disconnect", listener: (arg: {err: Error}) => void): this;
prependOnceListener(event: string, listener: (...args: any[]) => void): this;
prependOnceListener(event: "connect", listener: (arg: { connection: Connection, url: string }) => void): this;
prependOnceListener(event: "disconnect", listener: (arg: {err: Error}) => void): this;
/**
* Create a new ChannelWrapper. This is a proxy for the actual channel (which may or may not exist at any moment, depending on whether or not we are currently connected.)
* @param opts
*/
createChannel(opts: CreateChannelOpts): ChannelWrapper;
/**
* Returns true if the AmqpConnectionManager is connected to a broker, false otherwise.
*/
isConnected(): boolean;
/**
* Close this AmqpConnectionManager and free all associated resources.
*/
close(): Promise<void>;
}
export interface ChannelWrapper extends EventEmitter {
addListener(event: string, listener: (...args: any[]) => void): this;
addListener(event: "connect", listener: () => void): this;
addListener(event: "error", listener: (err: Error, info: { name: string }) => void): this;
addListener(event: "close", listener: () => void): this;
on(event: string, listener: (...args: any[]) => void): this;
on(event: "connect", listener: () => void): this;
on(event: "error", listener: (err: Error, info: { name: string }) => void): this;
on(event: "close", listener: () => void): this;
once(event: string, listener: (...args: any[]) => void): this;
once(event: "connect", listener: () => void): this;
once(event: "error", listener: (err: Error, info: { name: string }) => void): this;
once(event: "close", listener: () => void): this;
prependListener(event: string, listener: (...args: any[]) => void): this;
prependListener(event: "connect", listener: () => void): this;
prependListener(event: "error", listener: (err: Error, info: { name: string }) => void): this;
prependListener(event: "close", listener: () => void): this;
prependOnceListener(event: string, listener: (...args: any[]) => void): this;
prependOnceListener(event: "connect", listener: () => void): this;
prependOnceListener(event: "error", listener: (err: Error, info: { name: string }) => void): this;
prependOnceListener(event: "close", listener: () => void): this;
/**
* Adds a new 'setup handler'. setup(channel, [cb]) is a function to call when a new underlying channel is created -
* handy for asserting exchanges and queues exists, and whatnot. The channel object here is a ConfirmChannel from amqplib.
* The setup function should return a Promise (or optionally take a callback) - no messages will be sent until this Promise resolves.
* If there is a connection, setup() will be run immediately, and the addSetup Promise/callback won't resolve until setup is complete.
* Note that in this case, if the setup throws an error, no 'error' event will be emitted, since you can just handle the error here
* (although the setup will still be added for future reconnects, even if it throws an error.)
* Setup functions should, ideally, not throw errors, but if they do then the ChannelWrapper will emit an 'error' event.
* @param func
*/
addSetup(func: SetupFunc): Promise<void>;
/**
* Remove a setup function added with `addSetup`. If there is currently a
* connection, `teardown(channel, [cb])` will be run immediately, and the
* returned Promise will not resolve until it completes.
* @param func
* @param [tearDown]
*/
removeSetup(func: SetupFunc, tearDown?: SetupFunc): Promise<void>;
/**
* @see amqplib
* @param exchange
* @param routingKey
* @param content
* @param options
* @param callback
*/
publish(exchange: string, routingKey: string, content: Buffer, options?: Options.Publish, callback?: (err: any, ok: Replies.Empty) => void): Promise<void>;
/**
* @see amqplib
* @param queue
* @param content
* @param options
* @param callback
*/
sendToQueue(queue: string, content: Buffer, options?: Options.Publish, callback?: (err: any, ok: Replies.Empty) => void): Promise<void>;
/**
* @see amqplib
* @param message
* @param allUpTo
*/
ack(message: Message, allUpTo?: boolean): void;
/**
* @see amqplib
* @param message
* @param allUpTo
* @param requeue
*/
nack(message: Message, allUpTo?: boolean, requeue?: boolean): void;
/**
* Returns a count of messages currently waiting to be sent to the underlying channel.
*/
queueLength(): number;
/**
* Close a channel, clean up resources associated with it.
*/
close(): Promise<void>;
}