forked from DefinitelyTyped/DefinitelyTyped
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pusher-js.d.ts
231 lines (203 loc) · 8.4 KB
/
pusher-js.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
// Type definitions for pusher-js 3.0.0
// Project: https://github.com/pusher/pusher-js
// Definitions by: Qubo <https://github.com/tkqubo>
// Definitions: https://github.com/borisyankov/DefinitelyTyped
declare module "pusher-js" {
namespace pusher {
interface PusherStatic {
new(apiKey: string, config?: Config): Pusher;
}
interface Pusher {
subscribe(name: string): Channel;
subscribeAll(): void;
unsubscribe(name: string): void;
channel(name: string): Channel;
allChannels(): Channel[];
bind(eventName: string, callback: Function): Pusher;
bind_all(callback: Function): Pusher;
disconnect(): void;
key: string;
config: Config; //TODO: add GlobalConfig typings
channels: any; //TODO: Type this
global_emitter: EventsDispatcher;
sessionId: number;
timeline: any; //TODO: Type this
connection: ConnectionManager;
}
interface Config {
/**
* Forces the connection to use encrypted transports.
*/
encrypted?: boolean;
/**
* Endpoint on your server that will return the authentication signature needed for private channels.
*/
authEndpoint?: string;
/**
* Defines how the authentication endpoint, defined using authEndpoint, will be called.
* There are two options available: ajax and jsonp.
*/
authTransport?: string;
/**
* Allows passing additional data to authorizers. Supports query string params and headers (AJAX only).
* For example, following will pass foo=bar via the query string and baz: boo via headers:
*/
auth?: AuthConfig;
/**
* Allows connecting to a different datacenter by setting up correct hostnames and ports for the connection.
*/
cluster?: string;
/**
* Disables stats collection, so that connection metrics are not submitted to Pusher’s servers.
*/
disableStats?: boolean;
/**
* Specifies which transports should be used by Pusher to establish a connection.
* Useful for applications running in controlled, well-behaving environments.
* Available transports: ws, wss, xhr_streaming, xhr_polling, sockjs.
* Additional transports may be added in the future and without adding them to this list, they will be disabled.
*/
enabledTransports?: string[];
/**
* Specified which transports must not be used by Pusher to establish a connection.
* This settings overwrites transports whitelisted via the enabledTransports options.
* Available transports: ws, wss, xhr_streaming, xhr_polling, sockjs.
* Additional transports may be added in the future and without adding them to this list, they will be enabled.
*/
disabledTransports?: string[];
/**
* Ignores null origin checks for HTTP fallbacks. Use with care, it should be disabled only if necessary (i.e. PhoneGap).
*/
ignoreNullOrigin?: boolean;
/**
* After this time (in miliseconds) without any messages received from the server,
* a ping message will be sent to check if the connection is still working.
* Default value is is supplied by the server, low values will result in unnecessary traffic.
*/
activityTimeout?: number;
/**
* Time before the connection is terminated after sending a ping message.
* Default is 30000 (30s). Low values will cause false disconnections, if latency is high.
*/
pongTimeout?: number;
wsHost?: string;
wsPort?: number;
wssPort?: number;
httpHost?: string;
httpPort?: number;
httpsPort?: number;
}
interface AuthConfig {
params?: { [key: string]: any };
headers?: { [key: string]: any };
}
interface GenericEventsDispatcher<Self extends EventsDispatcher> extends EventsDispatcher {
bind(eventName: string, callback: Function, context?: any): Self;
bind_all(callback: Function): Self;
unbind(eventName?: string, callback?: Function, context?: any): Self;
unbind_all(eventName?: string, callback?: Function): Self;
emit(eventName: string, data?: any): Self;
}
interface Channel extends GenericEventsDispatcher<Channel> {
/** Triggers an event */
trigger(eventName: string, data?: any): boolean;
pusher: Pusher;
name: string;
subscribed: boolean;
/**
* Authenticates the connection as a member of the channel.
* @param {String} socketId
* @param {Function} callback
*/
authorize(socketId: string, callback: (data: any) => void): void;
}
interface EventsDispatcher {
bind(eventName: string, callback: Function, context?: any): EventsDispatcher;
bind_all(callback: Function): EventsDispatcher;
unbind(eventName?: string, callback?: Function, context?: any): EventsDispatcher;
unbind_all(eventName?: string, callback?: Function): EventsDispatcher;
emit(eventName: string, data?: any): EventsDispatcher;
}
interface ConnectionManager extends GenericEventsDispatcher<ConnectionManager> {
key: string;
options: any; //TODO: Timeline.js
state: string;
connection: any; //TODO: Type this
encrypted: boolean;
timeline: any; //TODO: Type this
connectionCallbacks: {
message: (message: string) => void;
ping: () => void;
activity: () => void;
error: (error: any) => void;
closed: () => void;
};
errorCallbacks: {
ssl_only: () => void;
refused: () => void;
backoff: () => void;
retry: () => void;
};
handshakeCallbacks: {
ssl_only: () => void;
refused: () => void;
backoff: () => void;
retry: () => void;
connected: (handshake: any) => void; //TODO: Type this
};
/**
* Establishes a connection to Pusher.
*
* Does nothing when connection is already established. See top-level doc
* to find events emitted on connection attempts.
*/
connect(): void;
/**
* Sends raw data.
* @param {String} data
*/
send(data: string): boolean;
/** Sends an event.
*
* @param {String} name
* @param {String} data
* @param {String} [channel]
* @returns {Boolean} whether message was sent or not
*/
send_event(name: string, data: string, channel: string): boolean;
/** Closes the connection. */
disconnect(): void;
isEncrypted(): boolean;
}
interface PresenceChannel<T> extends Channel {
members: Members<T>;
}
interface Members<T> {
/**
* Returns member's info for given id.
*
* Resulting object containts two fields - id and info.
*
* @param {Number} id
* @return {Object} member's info or null
*/
get(id: number): T;
/**
* Calls back for each member in unspecified order.
*
* @param {Function} callback
*/
each(callback: (member: any) => void): void;
members: { [id: number]: UserInfo<T> };
count: number;
myID: number;
me: UserInfo<T>;
}
interface UserInfo<T> {
id: number;
info: T;
}
}
var pusher: pusher.PusherStatic;
export = pusher;
}