diff --git a/types/bunnymq/bunnymq-tests.ts b/types/bunnymq/bunnymq-tests.ts index 4105363f25..960c0b6949 100644 --- a/types/bunnymq/bunnymq-tests.ts +++ b/types/bunnymq/bunnymq-tests.ts @@ -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() -}); \ No newline at end of file +}); diff --git a/types/bunnymq/index.d.ts b/types/bunnymq/index.d.ts index 1167db21ed..c4c0a770e1 100644 --- a/types/bunnymq/index.d.ts +++ b/types/bunnymq/index.d.ts @@ -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 // 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 = (...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; - } - - /** - * 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(queue: string, callback: ConsumerCallback): 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(queueName: string, callback: ConsumerCallback): void; + + /** + * Publisher to send messages to a named queue. + * + * @type {Producer} + * @return {Promise} The consumer response. + */ + publish(queueName: string, message: any, options?: ProducerOptions): Promise; + } + + 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(queue: string, message: any, options?: ProducerOptions): Promise; + } + + interface ProducerOptions { + routingKey?: string; + rpc?: boolean; + timeout?: number; + } } + +export = bunnymq; +export as namespace bunnymq; diff --git a/types/bunnymq/tslint.json b/types/bunnymq/tslint.json new file mode 100644 index 0000000000..f93cf8562a --- /dev/null +++ b/types/bunnymq/tslint.json @@ -0,0 +1,3 @@ +{ + "extends": "dtslint/dt.json" +}