Types for jackrabbit 4.3.0

This commit is contained in:
elvis
2018-04-01 22:08:47 +02:00
parent def3d5d177
commit 0dc7228f22
4 changed files with 145 additions and 0 deletions

66
types/jackrabbit/index.d.ts vendored Normal file
View File

@@ -0,0 +1,66 @@
// Type definitions for jackrabbit 4.3
// Project: https://github.com/hunterloftis/jackrabbit
// Definitions by: Elvis Adomnica <https://github.com/elvisvoer>
// 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<string>;
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;

View File

@@ -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' });
}

View File

@@ -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"]
}

View File

@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }