creates definitions for @Pager/jackrabbit (#41330)

* Fixes incorrect return types in diff-delta and blob. Should be functions, not direct accessor properties. Also, `rawcontent` returns a wrapper around a Buffer, not a Buffer directly, as `content` does.

* updated package description and version

* fixed missing argument

* Updates jackrabbit types to export namespace for interface and type access. Adds missing options and arguments to a few methods.

* fixed linting issue

* revert changes to jackrabbit

* Creates type definitions for the pagerinc fork of jackrabbit which is the more supported version. The original types for jackrabbit actually don't work, while this one is actually tested.
This commit is contained in:
Benjamin Schuster-Boeckler 2020-01-03 00:43:51 +01:00 committed by Ryan Cavanaugh
parent 007889e18b
commit c1bea6ce63
4 changed files with 160 additions and 0 deletions

76
types/pager__jackrabbit/index.d.ts vendored Normal file
View File

@ -0,0 +1,76 @@
// Type definitions for @pager/jackrabbit 4.8
// Project: https://github.com/pagerinc/jackrabbit
// Definitions by: Benjamin Schuster-Boeckler <https://github.com/dagams>
// 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<string>;
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;
}
}

View File

@ -0,0 +1,64 @@
/// <reference types="node" />
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' });
}

View File

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

View File

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