Merge pull request #17115 from cyrilschumacher/master

Update bunnymq definition
This commit is contained in:
Mine Starks
2017-06-15 16:01:59 -07:00
committed by GitHub
3 changed files with 105 additions and 131 deletions
+12 -19
View File
@@ -1,34 +1,27 @@
import * as bunnymq from "bunnymq";
// Basic usage
var instance = bunnymq({ host: 'amqp://localhost' });
const instance = bunnymq({ host: "amqp://localhost" });
// Publisher
instance.producer.produce('queue:name', 'Hello World!');
instance.publish("queue:name", "message");
instance.producer.produce("queue:name", "message");
// Subscriber
instance.consumer.consume('queue:name', message => { });
instance.subscribe("queue:name", (message: string) => "response");
instance.consumer.consume("queue:name", (message: string) => "response");
// RPC Support
instance.producer.produce('queue:name', { message: 'content' }, { rpc: true })
.then(function (consumerResponse) {
console.log(consumerResponse);
});
// Routing keys
instance.producer.produce('queue:name', { message: 'content' }, { routingKey: 'my-routing-key' });
instance.publish("queue:name", { message: "content" }, { routingKey: "my-routing-key", rpc: true, timeout: 1000 }).then((consumerResponse: string) => "response");
instance.producer.produce("queue:name", { message: "content" }, { rpc: true, routingKey: "my-routing-key", timeout: 1000 });
// Config
var custom = bunnymq({
host: 'amqp://localhost',
//number of fetched messages at once on the channel
const instanceWithCustomOptions = bunnymq({
host: "amqp://localhost",
prefetch: 5,
//requeue put back message into the broker if consumer crashes/trigger exception
requeue: true,
//time between two reconnect (ms)
timeout: 1000,
consumerSuffix: '',
//generate a hostname so we can track this connection on the broker (rabbitmq management plugin)
consumerSuffix: "",
hostname: "",
//the transport to use to debug. if provided, bunnymq will show some logs
transport: new Object()
});
});
+90 -112
View File
@@ -1,120 +1,98 @@
// Type definitions for node-bunnymq 2.2.1
// Type definitions for node-bunnymq 2.3
// Project: https://github.com/dial-once/node-bunnymq
// Definitions by: Cyril Schumacher <https://github.com/cyrilschumacher>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
declare module "bunnymq" {
namespace bunnymq {
export type ConsumerCallback = (message: Object) => void;
declare function bunnymq(options?: bunnymq.Options): bunnymq.Instance;
declare namespace bunnymq {
type ConsumerCallback<T> = (...args: any[]) => T;
type LoggerOutput = (format: any, ...args: any[]) => void;
/**
* Consumer.
* @interface
*/
export interface Consumer {
/**
* Handle messages from a named queue.
* @param {string} queue A named queue.
* @param {ConsumerCallback} callback A callback.
*/
consume(queue: string, callback: ConsumerCallback): void;
}
/**
* bunnymq instance.
* @interface
*/
export interface Instance {
/**
* Consumer.
* @type {Consumer}
*/
consumer: Consumer;
/**
* Producer.
* @type {Producer}
*/
producer: Producer;
}
/**
* Options.
* @interface
*/
export interface Options {
/**
* Consumer suffix.
* @type {string}
*/
consumerSuffix?: string;
/**
* Host.
* @type {string}
*/
host?: string;
/**
* Hostname.
* @type {string}
*/
hostname?: string;
/**
* Number of fetched messages at once on the channel.
* @type {number}
*/
prefetch?: number;
/**
* Requeue put back message into the broker if consumer crashes/trigger exception.
* @type {boolean}
*/
requeue?: boolean;
/**
* Time between two reconnect (in milliseconds).
* @type {number}
*/
timeout?: number;
/**
* Transport.
* @type {any}
*/
transport?: any;
}
/**
* Producer.
* @inteface
*/
export interface Producer {
/**
* Send messages to a named queue.
* @param {string} queue A named queue.
* @param {Object} message A message.
* @return {Object} The consumer response.
*/
produce(queue: string, message: Object, options?: ProducerOptions): PromiseLike<Object>;
}
/**
* Options for producer.
* @interface
*/
export interface ProducerOptions {
routingKey?: string;
rpc?: boolean;
}
interface Connection {
[address: string]: any;
startedAt: string;
}
/**
* Constructor.
* @param {Options} [options] Options.
* @return {Instance} A instance of bunnymq.
*/
function bunnymq(options?: bunnymq.Options): bunnymq.Instance;
export = bunnymq;
interface Consumer {
/**
* Handle messages from a named queue.
*
* @param {string} queue A named queue.
* @param {ConsumerCallback} callback A callback.
*/
consume<T>(queue: string, callback: ConsumerCallback<T>): void;
}
interface Instance {
connection: Connection;
consumer: Consumer;
producer: Producer;
/**
* Subscriber to handle messages from a named queue.
*
* @param {string} queue A named queue.
* @param {ConsumerCallback} callback A callback.
*/
subscribe<T>(queueName: string, callback: ConsumerCallback<T>): void;
/**
* Publisher to send messages to a named queue.
*
* @type {Producer}
* @return {Promise} The consumer response.
*/
publish<T>(queueName: string, message: any, options?: ProducerOptions): Promise<T>;
}
interface Logger {
debug: LoggerOutput;
error: LoggerOutput;
info: LoggerOutput;
log: LoggerOutput;
warn: LoggerOutput;
}
interface Options {
consumerSuffix?: string;
host?: string;
hostname?: string;
/**
* Number of fetched messages at once on the channel.
*
* @type {number}
*/
prefetch?: number;
/**
* Requeue put back message into the broker if consumer crashes/trigger exception.
*
* @type {boolean}
*/
requeue?: boolean;
rpcTimeout?: number;
timeout?: number;
transport?: any;
}
interface Producer {
/**
* Send messages to a named queue.
*
* @param {string} queue A named queue.
* @param {any} message A message.
* @return {Promise} The consumer response.
*/
produce<T>(queue: string, message: any, options?: ProducerOptions): Promise<T>;
}
interface ProducerOptions {
routingKey?: string;
rpc?: boolean;
timeout?: number;
}
}
export = bunnymq;
export as namespace bunnymq;
+3
View File
@@ -0,0 +1,3 @@
{
"extends": "dtslint/dt.json"
}