diff --git a/types/pager__jackrabbit/index.d.ts b/types/pager__jackrabbit/index.d.ts new file mode 100644 index 0000000000..c4dd28017e --- /dev/null +++ b/types/pager__jackrabbit/index.d.ts @@ -0,0 +1,76 @@ +// Type definitions for @pager/jackrabbit 4.8 +// Project: https://github.com/pagerinc/jackrabbit +// Definitions by: Benjamin Schuster-Boeckler +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 3.5 + +import * as amqplib from "amqplib"; + +export = jackrabbit; +declare function jackrabbit(url: string): jackrabbit.JackRabbit; + +declare namespace jackrabbit { + type Message = amqplib.Message; + + type ExchangeOptions = amqplib.Options.AssertExchange & { + noReply?: boolean; + }; + + interface JackRabbit extends NodeJS.EventEmitter { + default(): Exchange; + direct(name?: string, options?: ExchangeOptions): Exchange; + fanout(name?: string, options?: ExchangeOptions): Exchange; + topic(name?: string, options?: ExchangeOptions): Exchange; + close(callback?: (e: Error) => any): void; + getInternals: () => { + amqp: any; + connection: amqplib.Connection; + }; + } + + enum exchangeType { + direct = "direct", + fanout = "fanout", + topic = "topic" + } + + interface Exchange extends NodeJS.EventEmitter { + name: string; + type: exchangeType; + options: amqplib.Options.AssertExchange; + queue(options: QueueOptions): Queue; + connect(con: amqplib.Connection): Exchange; + publish(message: any, options?: PublishOptions): Exchange; + } + + type PublishOptions = amqplib.Options.Publish & { + key: string; + reply?: AckCallback; + }; + + type QueueOptions = amqplib.Options.AssertQueue & { + name?: string; + key?: string; + keys?: ReadonlyArray; + prefetch?: number; + }; + + type AckCallback = (data?: any) => void; + + interface Queue extends NodeJS.EventEmitter { + name: string; + options: QueueOptions; + connect(con: amqplib.Connection): void; + consume: ( + callback: ( + data: any, + ack: AckCallback, + nack: () => void, + msg: Message + ) => void, + options?: amqplib.Options.Consume + ) => void; + cancel(done: any): void; + purge(done: any): void; + } +} diff --git a/types/pager__jackrabbit/pager__jackrabbit-tests.ts b/types/pager__jackrabbit/pager__jackrabbit-tests.ts new file mode 100644 index 0000000000..9e0d8b4899 --- /dev/null +++ b/types/pager__jackrabbit/pager__jackrabbit-tests.ts @@ -0,0 +1,64 @@ +/// +import jackrabbit = require('@pager/jackrabbit'); + +const RABBIT_URL = 'amqp://localhost'; + +// $ExpectError +jackrabbit(); +// $ExpectError +jackrabbit(1); + +const rabbit = jackrabbit(RABBIT_URL); + +// test default exchange based on '1-hello-world' example +let defaultExchange: jackrabbit.Exchange; +defaultExchange = rabbit.default(); +let hello: jackrabbit.Queue; +hello = defaultExchange.queue({ name: 'hello' }); + +defaultExchange.publish('Hello World!', { key: 'hello' }); + +let onMessage: jackrabbit.AckCallback; +onMessage = () => {}; +hello.consume(onMessage, { noAck: true }); + +// test fanout exchange based on '3-pubsub' example +const fanoutExchange = rabbit.fanout(); + +fanoutExchange.publish('this is a log'); + +const fanoutQueue = fanoutExchange.queue({ exclusive: true }); +fanoutQueue.consume(onMessage, { noAck: true }); + +// test direct exchange based on '4-routing' example +const directExchange = rabbit.direct('direct_logs'); + +directExchange.publish({ text: 'this is a harmless log' }, { key: 'info' }); +directExchange.publish({ text: 'this one is more important' }, { key: 'warning' }); +directExchange.publish({ text: 'pay attention to me!' }, { key: 'error' }); + +const errorsQueue = directExchange.queue({ exclusive: true, key: 'error' }); +const logsQueue = directExchange.queue({ exclusive: true, keys: ['info', 'warning'] }); + +errorsQueue.consume((data: any, ack: jackrabbit.AckCallback, nack: () => void, msg: jackrabbit.Message) => {}); +logsQueue.consume((data: any, ack: jackrabbit.AckCallback) => {}); + +// test reply based on '6-rpc' example +const exchange = rabbit.default(); +const rpc = exchange.queue({ name: 'rpc_queue', prefetch: 1, durable: false }); + +exchange.publish( + { n: 40 }, + { + key: 'rpc_queue', + reply: onReply, + } +); + +function onReply(data: any) {} + +rpc.consume(onRequest); + +function onRequest(data: any, reply: (data: any) => void) { + reply({ field: 'value' }); +} diff --git a/types/pager__jackrabbit/tsconfig.json b/types/pager__jackrabbit/tsconfig.json new file mode 100644 index 0000000000..fd9cc27031 --- /dev/null +++ b/types/pager__jackrabbit/tsconfig.json @@ -0,0 +1,19 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": ["es6"], + "noImplicitAny": true, + "noImplicitThis": true, + "strictFunctionTypes": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": ["../"], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true, + "paths": { + "@pager/jackrabbit": ["pager__jackrabbit"] + } + }, + "files": ["index.d.ts", "pager__jackrabbit-tests.ts"] +} diff --git a/types/pager__jackrabbit/tslint.json b/types/pager__jackrabbit/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/pager__jackrabbit/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }