UNPKG

5.25 kBTypeScriptView Raw
1/*
2 * Copyright 2013-2018 The NATS Authors
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16import events = require('events');
17import tls = require('tls');
18
19export const version: string;
20
21/**
22 * Error codes
23 */
24export const BAD_AUTHENTICATION: string;
25export const BAD_JSON: string;
26export const BAD_MSG: string;
27export const BAD_REPLY: string;
28export const BAD_SUBJECT: string;
29export const CLIENT_CERT_REQ: string;
30export const CONN_CLOSED: string;
31export const CONN_ERR: string;
32export const INVALID_ENCODING: string;
33export const NATS_PROTOCOL_ERR: string;
34export const NON_SECURE_CONN_REQ: string;
35export const PERMISSIONS_ERR: string;
36export const REQ_TIMEOUT: string;
37export const SECURE_CONN_REQ: string;
38export const STALE_CONNECTION_ERR: string;
39
40/**
41 * Create a properly formatted inbox subject.
42*/
43export function createInbox(): string;
44
45/**
46 * Connect to a nats-server and return the client.
47 * Argument can be a url, or an object with a 'url'
48 * property and additional options.
49 */
50export function connect(opts: ClientOpts): Client;
51
52/**
53 * Connect to a nats-server and return the client.
54 * Argument can be a url, or an object with a 'url'
55 * property and additional options.
56 */
57export function connect(port: number): Client;
58
59/**
60 * Connect to a nats-server and return the client.
61 * Argument can be a url, or an object with a 'url'
62 * property and additional options.
63 */
64export function connect(url: string): Client;
65
66export interface ClientOpts {
67 url?: string,
68 user?: string,
69 pass?: string,
70 verbose?: boolean,
71 pedantic?: boolean,
72 reconnect?: boolean,
73 maxReconnectAttempts?: number,
74 reconnectTimeWait?: number,
75 servers?: Array<string>,
76 noRandomize?: boolean,
77 encoding?: BufferEncoding,
78 tls?: boolean | tls.TlsOptions,
79 name?: string,
80 yieldTime?: number,
81 waitOnFirstConnect?: boolean,
82 json?: boolean,
83 preserveBuffers?: boolean,
84 token?: string,
85 pingInterval?: number,
86 maxPingOut?: number,
87 useOldRequestStyle?: boolean
88}
89
90export interface SubscribeOptions {
91 queue?: string,
92 max?: number
93}
94
95declare class Client extends events.EventEmitter {
96 /**
97 * Create a properly formatted inbox subject.
98 */
99 createInbox(): string;
100
101 /**
102 * Close the connection to the server.
103 */
104 close():void;
105
106 /**
107 * Flush outbound queue to server and call optional callback when server has processed
108 * all data.
109 */
110 flush(callback?: Function):void;
111
112 /**
113 * Publish a message to the given subject, with optional reply and callback.
114 */
115 publish(callback: Function):void;
116 publish(subject: string, callback: Function):void;
117 publish(subject: string, msg: string | Buffer, callback: Function):void;
118 publish(subject: string, msg?: string | Buffer, reply?: string, callback?: Function):void;
119
120 /**
121 * Subscribe to a given subject, with optional options and callback. opts can be
122 * ommitted, even with a callback. The Subscriber Id is returned.
123 */
124 subscribe(subject: string, callback: Function): number;
125 subscribe(subject: string, opts: SubscribeOptions, callback: Function): number;
126
127 /**
128 * Unsubscribe to a given Subscriber Id, with optional max parameter.
129 */
130 unsubscribe(sid: number, max?: number):void;
131
132 /**
133 * Set a timeout on a subscription.
134 */
135 timeout(sid: number, timeout: number, expected: number, callback: (sid: number) => void):void;
136
137 /**
138 * Publish a message with an implicit inbox listener as the reply. Message is optional.
139 * This should be treated as a subscription. You can optionally indicate how many
140 * messages you only want to receive using opt_options = {max:N}. Otherwise you
141 * will need to unsubscribe to stop the message stream.
142 * The Subscriber Id is returned.
143 */
144 request(subject: string, callback: Function): number;
145 request(subject: string, msg: string | Buffer, callback: Function): number;
146 request(subject: string, msg?: string, options?: SubscribeOptions, callback?: Function): number;
147
148 /**
149 * Publish a message with an implicit inbox listener as the reply. Message is optional.
150 * This should be treated as a subscription. Request one, will terminate the subscription
151 * after the first response is received or the timeout is reached.
152 * The callback can be called with either a message payload or a NatsError to indicate
153 * a timeout has been reached.
154 * The Subscriber Id is returned.
155 */
156 requestOne(subject: string, msg: string | Buffer, options?: SubscribeOptions, timeout?: number, callback?:Function) : number
157
158 /**
159 * Report number of outstanding subscriptions on this connection.
160 */
161 numSubscriptions(): number;
162}
163
164declare class NatsError implements Error {
165 public name: string;
166 public message: string;
167 public code: string;
168 public chainedError: Error;
169
170 constructor(message:string, code:string, chainedError?:Error);
171}