-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.d.ts
129 lines (116 loc) · 3.74 KB
/
index.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import events from 'events'
import amqplib from 'amqplib'
export as namespace hato;
/**
* Types
*/
export type ExchangeType = 'fanout' | 'direct' | 'topic' | 'headers';
export interface Consumer extends Promise<amqplib.Replies.Consume>, events.EventEmitter {}
export interface ConsumeMessage extends amqplib.ConsumeMessage {
ack(allUpTo?: boolean): void
nack(allUpTo?: boolean, requeue?: boolean): void
cancel(): void
}
/**
* Plugins
*/
export namespace Plugins {
abstract class Base {
constructor(name: string)
abstract init(): void;
abstract destroy(): void;
}
interface Plugin<T> extends Plugins.Base {
new(options?: T): Plugin<T>
}
interface Defaults {
Base: Base
GracefulShutdown: Plugin<Options.GracefulShutdown>
ConnectionRetry: Plugin<Options.ConnectionRetry>
Duplex: Plugin<Options.Duplex>
DLX: Plugin<Options.DLX>
Encoding: Plugin<Options.Encoding>
RPC: Plugin<Options.RPC>
Confirm: Plugin<Options.Confirm>
Retry: Plugin<Options.Retry>
ServiceContext: Plugin<Options.ServiceContext>
DefaultOptions: Plugin<Options.DefaultOptions>
}
namespace Options {
type GracefulShutdown = null;
interface ConnectionRetry {
retries?: number
min?: number
max?: number
base?: number
}
type Duplex = null;
interface DLX {
name?: string,
type?: ExchangeType,
options?: amqplib.Options.AssertExchange
}
type Encoding = 'json';
interface RPC {
uid?: () => string
timeout?: number
}
interface Confirm {
uid?: () => string
}
interface Retry {
retries?: number
strategy?: 'constant' | 'linear' | 'exponential'
min?: number
max?: number
base?: number
}
interface ServiceContext {
name?: string,
version?: string
instanceId?: string
namespace?: string
queue?: { options?: amqplib.Options.AssertQueue }
}
interface DefaultOptions {
prefetch?: number
queue?: amqplib.Options.AssertQueue
exchange?: amqplib.Options.AssertExchange
consume?: amqplib.Options.Consume
publish?: amqplib.Options.Publish
}
}
}
export const plugins: Plugins.Defaults
/**
* Client
*/
interface Logger {
debug(message: string): void
info(message: string): void
warn(message: string): void
error(message: string): void
}
interface API {
assert(enable: boolean): this
prefetch(count: number): this
type(type: ExchangeType): this
exchange(exchange: string, type: ExchangeType, options?: amqplib.Options.AssertExchange): this
queue(name: string, options?: amqplib.Options.AssertQueue): this
subscribe(pattern: string, fn: (msg: ConsumeMessage) => void): Consumer
consume(pattern: string, fn: (msg: ConsumeMessage) => void): Consumer
cancel(consumerTag: string): Promise<amqplib.Replies.Empty>
publish(routingKey: string, content: Buffer, options?: amqplib.Options.Publish): Promise<void>;
}
interface Options {
logger?: Logger
plugins?: (Plugins.Plugin<any> | string)[]
}
export interface IClient extends events.EventEmitter, API {
new(url: Parameters<typeof amqplib.connect>[0], options?: Options): this
start(url: Parameters<typeof amqplib.connect>[0], options?: Options): Promise<this>
start(): Promise<this>
close(): Promise<void>
}
export const Client: IClient;
export function connect(url: Parameters<typeof amqplib.connect>[0], options?: Options): Promise<IClient>