From 1f1aefb128fb69e8c4e08f04147fe16adc3e2e4e Mon Sep 17 00:00:00 2001 From: Jacob Gardner Date: Tue, 26 Feb 2019 13:09:22 -0600 Subject: [PATCH 1/4] Add server properties to connection object --- types/amqplib/callback_api.d.ts | 106 ++++++++++++++++++++++++++------ 1 file changed, 88 insertions(+), 18 deletions(-) diff --git a/types/amqplib/callback_api.d.ts b/types/amqplib/callback_api.d.ts index 9fca3972e4..5d92376606 100644 --- a/types/amqplib/callback_api.d.ts +++ b/types/amqplib/callback_api.d.ts @@ -2,36 +2,92 @@ import events = require('events'); import { Replies, Options, Message } from './properties'; export * from './properties'; +export interface ServerProperties { + host: string; + product: string; + version: string; + platform?: string; + copyright?: string; + information?: string; +} + export interface Connection extends events.EventEmitter { close(callback?: (err: any) => void): void; createChannel(callback: (err: any, channel: Channel) => void): void; createConfirmChannel(callback: (err: any, confirmChannel: ConfirmChannel) => void): void; + serverProperties: ServerProperties; } export interface Channel extends events.EventEmitter { close(callback: (err: any) => void): void; - assertQueue(queue?: string, options?: Options.AssertQueue, callback?: (err: any, ok: Replies.AssertQueue) => void): void; + assertQueue( + queue?: string, + options?: Options.AssertQueue, + callback?: (err: any, ok: Replies.AssertQueue) => void + ): void; checkQueue(queue: string, callback?: (err: any, ok: Replies.AssertQueue) => void): void; - deleteQueue(queue: string, options?: Options.DeleteQueue, callback?: (err: any, ok: Replies.DeleteQueue) => void): void; + deleteQueue( + queue: string, + options?: Options.DeleteQueue, + callback?: (err: any, ok: Replies.DeleteQueue) => void + ): void; purgeQueue(queue: string, callback?: (err: any, ok: Replies.PurgeQueue) => void): void; - bindQueue(queue: string, source: string, pattern: string, args?: any, callback?: (err: any, ok: Replies.Empty) => void): void; - unbindQueue(queue: string, source: string, pattern: string, args?: any, callback?: (err: any, ok: Replies.Empty) => void): void; + bindQueue( + queue: string, + source: string, + pattern: string, + args?: any, + callback?: (err: any, ok: Replies.Empty) => void + ): void; + unbindQueue( + queue: string, + source: string, + pattern: string, + args?: any, + callback?: (err: any, ok: Replies.Empty) => void + ): void; - assertExchange(exchange: string, type: string, options?: Options.AssertExchange, callback?: (err: any, ok: Replies.AssertExchange) => void): void; + assertExchange( + exchange: string, + type: string, + options?: Options.AssertExchange, + callback?: (err: any, ok: Replies.AssertExchange) => void + ): void; checkExchange(exchange: string, callback?: (err: any, ok: Replies.Empty) => void): void; - deleteExchange(exchange: string, options?: Options.DeleteExchange, callback?: (err: any, ok: Replies.Empty) => void): void; + deleteExchange( + exchange: string, + options?: Options.DeleteExchange, + callback?: (err: any, ok: Replies.Empty) => void + ): void; - bindExchange(destination: string, source: string, pattern: string, args?: any, callback?: (err: any, ok: Replies.Empty) => void): void; - unbindExchange(destination: string, source: string, pattern: string, args?: any, callback?: (err: any, ok: Replies.Empty) => void): void; + bindExchange( + destination: string, + source: string, + pattern: string, + args?: any, + callback?: (err: any, ok: Replies.Empty) => void + ): void; + unbindExchange( + destination: string, + source: string, + pattern: string, + args?: any, + callback?: (err: any, ok: Replies.Empty) => void + ): void; publish(exchange: string, routingKey: string, content: Buffer, options?: Options.Publish): boolean; sendToQueue(queue: string, content: Buffer, options?: Options.Publish): boolean; - consume(queue: string, onMessage: (msg: Message | null) => any, options?: Options.Consume, callback?: (err: any, ok: Replies.Consume) => void): void; + consume( + queue: string, + onMessage: (msg: Message | null) => any, + options?: Options.Consume, + callback?: (err: any, ok: Replies.Consume) => void + ): void; cancel(consumerTag: string, callback?: (err: any, ok: Replies.Empty) => void): void; get(queue: string, options?: Options.Get, callback?: (err: any, ok: Message | false) => void): void; @@ -48,22 +104,36 @@ export interface Channel extends events.EventEmitter { } export interface ConfirmChannel extends Channel { - publish(exchange: string, routingKey: string, content: Buffer, options?: Options.Publish, callback?: (err: any, ok: Replies.Empty) => void): boolean; - sendToQueue(queue: string, content: Buffer, options?: Options.Publish, callback?: (err: any, ok: Replies.Empty) => void): boolean; + publish( + exchange: string, + routingKey: string, + content: Buffer, + options?: Options.Publish, + callback?: (err: any, ok: Replies.Empty) => void + ): boolean; + sendToQueue( + queue: string, + content: Buffer, + options?: Options.Publish, + callback?: (err: any, ok: Replies.Empty) => void + ): boolean; waitForConfirms(callback?: (err: any) => void): void; } export const credentials: { external(): { - mechanism: string; - response(): Buffer; + mechanism: string; + response(): Buffer; }; - plain(username: string, password: string): { - mechanism: string; - response(): Buffer; - username: string; - password: string; + plain( + username: string, + password: string + ): { + mechanism: string; + response(): Buffer; + username: string; + password: string; }; }; From 219fd7573e818f3b54030c92ec13a1ec342a5c04 Mon Sep 17 00:00:00 2001 From: Jacob Gardner Date: Tue, 26 Feb 2019 13:27:55 -0600 Subject: [PATCH 2/4] Make sure both callback and promise Connection have serverProperties field. Add test. --- types/amqplib/amqplib-tests.ts | 14 ++++++++++++++ types/amqplib/callback_api.d.ts | 11 +---------- types/amqplib/index.d.ts | 3 ++- types/amqplib/properties.d.ts | 9 +++++++++ 4 files changed, 26 insertions(+), 11 deletions(-) diff --git a/types/amqplib/amqplib-tests.ts b/types/amqplib/amqplib-tests.ts index ef448d5b36..97844f3b50 100644 --- a/types/amqplib/amqplib-tests.ts +++ b/types/amqplib/amqplib-tests.ts @@ -14,6 +14,13 @@ amqp.connect('amqp://localhost') amqp.connect('amqp://localhost') .then(connection => { + connection.serverProperties.copyright; // $ExpectType string | undefined + connection.serverProperties.platform; // $ExpectType string | undefined + connection.serverProperties.information; // $ExpectType string | undefined + connection.serverProperties.host; // $ExpectType string + connection.serverProperties.product; // $ExpectType string + connection.serverProperties.version; // $ExpectType string + return connection.createChannel() .tap(channel => channel.checkQueue('myQueue')) .then(channel => { @@ -37,6 +44,13 @@ import amqpcb = require('amqplib/callback_api'); amqpcb.connect('amqp://localhost', (err, connection) => { if (!err) { + connection.serverProperties.copyright; // $ExpectType string | undefined + connection.serverProperties.platform; // $ExpectType string | undefined + connection.serverProperties.information; // $ExpectType string | undefined + connection.serverProperties.host; // $ExpectType string + connection.serverProperties.product; // $ExpectType string + connection.serverProperties.version; // $ExpectType string + connection.createChannel((err, channel) => { if (!err) { channel.assertQueue('myQueue', {}, (err, ok) => { diff --git a/types/amqplib/callback_api.d.ts b/types/amqplib/callback_api.d.ts index 5d92376606..4fcc0b5309 100644 --- a/types/amqplib/callback_api.d.ts +++ b/types/amqplib/callback_api.d.ts @@ -1,16 +1,7 @@ import events = require('events'); -import { Replies, Options, Message } from './properties'; +import { Replies, Options, Message, ServerProperties } from './properties'; export * from './properties'; -export interface ServerProperties { - host: string; - product: string; - version: string; - platform?: string; - copyright?: string; - information?: string; -} - export interface Connection extends events.EventEmitter { close(callback?: (err: any) => void): void; createChannel(callback: (err: any, channel: Channel) => void): void; diff --git a/types/amqplib/index.d.ts b/types/amqplib/index.d.ts index 7087d74edc..77e5d19ca9 100644 --- a/types/amqplib/index.d.ts +++ b/types/amqplib/index.d.ts @@ -8,13 +8,14 @@ import * as Promise from 'bluebird'; import * as events from 'events'; -import { Replies, Options, Message, GetMessage, ConsumeMessage } from './properties'; +import { Replies, Options, Message, GetMessage, ConsumeMessage, ServerProperties } from './properties'; export * from './properties'; export interface Connection extends events.EventEmitter { close(): Promise; createChannel(): Promise; createConfirmChannel(): Promise; + serverProperties: ServerProperties; } export interface Channel extends events.EventEmitter { diff --git a/types/amqplib/properties.d.ts b/types/amqplib/properties.d.ts index 97dc8ac393..7e9198ac7a 100644 --- a/types/amqplib/properties.d.ts +++ b/types/amqplib/properties.d.ts @@ -210,3 +210,12 @@ export interface XDeath { "original-expiration"?: any; "routing-keys": string[]; } + +export interface ServerProperties { + host: string; + product: string; + version: string; + platform?: string; + copyright?: string; + information?: string; +} From 6fde3e12581a1e0861c8d013e5c56df143841a23 Mon Sep 17 00:00:00 2001 From: Jacob Gardner Date: Tue, 26 Feb 2019 13:34:27 -0600 Subject: [PATCH 3/4] I read the original spec incorrectly. Updated it accordingly. --- types/amqplib/amqplib-tests.ts | 10 ++++++---- types/amqplib/properties.d.ts | 5 +++-- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/types/amqplib/amqplib-tests.ts b/types/amqplib/amqplib-tests.ts index 97844f3b50..a42d2f717d 100644 --- a/types/amqplib/amqplib-tests.ts +++ b/types/amqplib/amqplib-tests.ts @@ -15,11 +15,12 @@ amqp.connect('amqp://localhost') amqp.connect('amqp://localhost') .then(connection => { connection.serverProperties.copyright; // $ExpectType string | undefined - connection.serverProperties.platform; // $ExpectType string | undefined - connection.serverProperties.information; // $ExpectType string | undefined + connection.serverProperties.platform; // $ExpectType string + connection.serverProperties.information; // $ExpectType string connection.serverProperties.host; // $ExpectType string connection.serverProperties.product; // $ExpectType string connection.serverProperties.version; // $ExpectType string + connection.serverProperties.customField; // $ExpectType string | undefined return connection.createChannel() .tap(channel => channel.checkQueue('myQueue')) @@ -45,11 +46,12 @@ import amqpcb = require('amqplib/callback_api'); amqpcb.connect('amqp://localhost', (err, connection) => { if (!err) { connection.serverProperties.copyright; // $ExpectType string | undefined - connection.serverProperties.platform; // $ExpectType string | undefined - connection.serverProperties.information; // $ExpectType string | undefined + connection.serverProperties.platform; // $ExpectType string + connection.serverProperties.information; // $ExpectType string connection.serverProperties.host; // $ExpectType string connection.serverProperties.product; // $ExpectType string connection.serverProperties.version; // $ExpectType string + connection.serverProperties.customField; // $ExpectType string | undefined connection.createChannel((err, channel) => { if (!err) { diff --git a/types/amqplib/properties.d.ts b/types/amqplib/properties.d.ts index 7e9198ac7a..4c7a210c6f 100644 --- a/types/amqplib/properties.d.ts +++ b/types/amqplib/properties.d.ts @@ -215,7 +215,8 @@ export interface ServerProperties { host: string; product: string; version: string; - platform?: string; + platform: string; copyright?: string; - information?: string; + information: string; + [key: string]: string | undefined; } From 3649fccf5794e24b502df0e96731f8c2957f67db Mon Sep 17 00:00:00 2001 From: Jacob Gardner Date: Tue, 26 Feb 2019 13:37:13 -0600 Subject: [PATCH 4/4] Remove prettier formatting --- types/amqplib/callback_api.d.ts | 96 +++++++-------------------------- 1 file changed, 18 insertions(+), 78 deletions(-) diff --git a/types/amqplib/callback_api.d.ts b/types/amqplib/callback_api.d.ts index 4fcc0b5309..56842abc26 100644 --- a/types/amqplib/callback_api.d.ts +++ b/types/amqplib/callback_api.d.ts @@ -12,73 +12,27 @@ export interface Connection extends events.EventEmitter { export interface Channel extends events.EventEmitter { close(callback: (err: any) => void): void; - assertQueue( - queue?: string, - options?: Options.AssertQueue, - callback?: (err: any, ok: Replies.AssertQueue) => void - ): void; + assertQueue(queue?: string, options?: Options.AssertQueue, callback?: (err: any, ok: Replies.AssertQueue) => void): void; checkQueue(queue: string, callback?: (err: any, ok: Replies.AssertQueue) => void): void; - deleteQueue( - queue: string, - options?: Options.DeleteQueue, - callback?: (err: any, ok: Replies.DeleteQueue) => void - ): void; + deleteQueue(queue: string, options?: Options.DeleteQueue, callback?: (err: any, ok: Replies.DeleteQueue) => void): void; purgeQueue(queue: string, callback?: (err: any, ok: Replies.PurgeQueue) => void): void; - bindQueue( - queue: string, - source: string, - pattern: string, - args?: any, - callback?: (err: any, ok: Replies.Empty) => void - ): void; - unbindQueue( - queue: string, - source: string, - pattern: string, - args?: any, - callback?: (err: any, ok: Replies.Empty) => void - ): void; + bindQueue(queue: string, source: string, pattern: string, args?: any, callback?: (err: any, ok: Replies.Empty) => void): void; + unbindQueue(queue: string, source: string, pattern: string, args?: any, callback?: (err: any, ok: Replies.Empty) => void): void; - assertExchange( - exchange: string, - type: string, - options?: Options.AssertExchange, - callback?: (err: any, ok: Replies.AssertExchange) => void - ): void; + assertExchange(exchange: string, type: string, options?: Options.AssertExchange, callback?: (err: any, ok: Replies.AssertExchange) => void): void; checkExchange(exchange: string, callback?: (err: any, ok: Replies.Empty) => void): void; - deleteExchange( - exchange: string, - options?: Options.DeleteExchange, - callback?: (err: any, ok: Replies.Empty) => void - ): void; + deleteExchange(exchange: string, options?: Options.DeleteExchange, callback?: (err: any, ok: Replies.Empty) => void): void; - bindExchange( - destination: string, - source: string, - pattern: string, - args?: any, - callback?: (err: any, ok: Replies.Empty) => void - ): void; - unbindExchange( - destination: string, - source: string, - pattern: string, - args?: any, - callback?: (err: any, ok: Replies.Empty) => void - ): void; + bindExchange(destination: string, source: string, pattern: string, args?: any, callback?: (err: any, ok: Replies.Empty) => void): void; + unbindExchange(destination: string, source: string, pattern: string, args?: any, callback?: (err: any, ok: Replies.Empty) => void): void; publish(exchange: string, routingKey: string, content: Buffer, options?: Options.Publish): boolean; sendToQueue(queue: string, content: Buffer, options?: Options.Publish): boolean; - consume( - queue: string, - onMessage: (msg: Message | null) => any, - options?: Options.Consume, - callback?: (err: any, ok: Replies.Consume) => void - ): void; + consume(queue: string, onMessage: (msg: Message | null) => any, options?: Options.Consume, callback?: (err: any, ok: Replies.Consume) => void): void; cancel(consumerTag: string, callback?: (err: any, ok: Replies.Empty) => void): void; get(queue: string, options?: Options.Get, callback?: (err: any, ok: Message | false) => void): void; @@ -95,36 +49,22 @@ export interface Channel extends events.EventEmitter { } export interface ConfirmChannel extends Channel { - publish( - exchange: string, - routingKey: string, - content: Buffer, - options?: Options.Publish, - callback?: (err: any, ok: Replies.Empty) => void - ): boolean; - sendToQueue( - queue: string, - content: Buffer, - options?: Options.Publish, - callback?: (err: any, ok: Replies.Empty) => void - ): boolean; + publish(exchange: string, routingKey: string, content: Buffer, options?: Options.Publish, callback?: (err: any, ok: Replies.Empty) => void): boolean; + sendToQueue(queue: string, content: Buffer, options?: Options.Publish, callback?: (err: any, ok: Replies.Empty) => void): boolean; waitForConfirms(callback?: (err: any) => void): void; } export const credentials: { external(): { - mechanism: string; - response(): Buffer; + mechanism: string; + response(): Buffer; }; - plain( - username: string, - password: string - ): { - mechanism: string; - response(): Buffer; - username: string; - password: string; + plain(username: string, password: string): { + mechanism: string; + response(): Buffer; + username: string; + password: string; }; };