diff --git a/types/jackrabbit/index.d.ts b/types/jackrabbit/index.d.ts new file mode 100644 index 0000000000..ec0ff27fa9 --- /dev/null +++ b/types/jackrabbit/index.d.ts @@ -0,0 +1,66 @@ +// Type definitions for jackrabbit 4.3 +// Project: https://github.com/hunterloftis/jackrabbit +// Definitions by: Elvis Adomnica +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.4 + +import { Connection, Options, Message } from 'amqplib'; + +declare namespace jackrabbit { + function jackrabbit(url: string): JackRabbit; + + interface JackRabbit extends NodeJS.EventEmitter { + default(): Exchange; + direct(name?: string): Exchange; + fanout(name?: string): Exchange; + topic(name?: string): Exchange; + close(callback: (e: Error) => any): void; + getInternals: () => { + amqp: any; + connection: Connection; + }; + } + + enum exchangeType { + direct = 'direct', + fanout = 'fanout', + topic = 'topic', + } + + interface Exchange extends NodeJS.EventEmitter { + name: string; + type: exchangeType; + options: Options.AssertExchange; + queue(options: QueueOptions): Queue; + connect(con: Connection): Exchange; + publish(message: any, options?: PublishOptions): Exchange; + } + + type PublishOptions = Options.Publish & { + key: string; + reply?: AckCallback; + }; + + type QueueOptions = 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: Connection): void; + consume: ( + callback: (data: any, ack: AckCallback, nack: () => void, msg: Message) => void, + options?: Options.Consume + ) => void; + cancel(done: any): void; + purge(done: any): void; + } +} + +export default jackrabbit.jackrabbit; diff --git a/types/jackrabbit/jackrabbit-tests.ts b/types/jackrabbit/jackrabbit-tests.ts new file mode 100644 index 0000000000..57bd961259 --- /dev/null +++ b/types/jackrabbit/jackrabbit-tests.ts @@ -0,0 +1,62 @@ +import jackrabbit from 'jackrabbit'; + +const RABBIT_URL = 'amqp://localhost'; + +function onMessage(data: any) {} +function ack() {} + +// $ExpectError +jackrabbit(); +// $ExpectError +jackrabbit(1); + +const rabbit = jackrabbit(RABBIT_URL); + +// test default exchange based on '1-hello-world' example +const defaultExchange = rabbit.default(); +const hello = defaultExchange.queue({ name: 'hello' }); + +defaultExchange.publish('Hello World!', { key: 'hello' }); + +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((onMessage, ack) => {}); +logsQueue.consume((onMessage, ack) => {}); + +// 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/jackrabbit/tsconfig.json b/types/jackrabbit/tsconfig.json new file mode 100644 index 0000000000..3d053557b2 --- /dev/null +++ b/types/jackrabbit/tsconfig.json @@ -0,0 +1,16 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": ["es6"], + "noImplicitAny": true, + "noImplicitThis": true, + "strictFunctionTypes": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": ["../"], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": ["index.d.ts", "jackrabbit-tests.ts"] +} diff --git a/types/jackrabbit/tslint.json b/types/jackrabbit/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/jackrabbit/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }