UNPKG

5.87 kBTypeScriptView Raw
1// Type definitions for phoenix 1.6
2// Project: https://github.com/phoenixframework/phoenix
3// Definitions by: Mirosław Ciastek <https://github.com/mciastek>
4// John Goff <https://github.com/John-Goff>
5// Po Chen <https://github.com/princemaple>
6// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
7// TypeScript Version: 2.4
8
9export type PushStatus = "ok" | "error" | "timeout";
10
11export class Push {
12 constructor(channel: Channel, event: string, payload: object, timeout: number);
13
14 send(): void;
15 resend(timeout: number): void;
16
17 receive(status: PushStatus, callback: (response?: any) => any): this;
18}
19
20export type ChannelState = "closed" | "errored" | "joined" | "joining" | "leaving";
21
22export class Channel {
23 constructor(topic: string, params?: object | (() => object), socket?: Socket);
24
25 state: ChannelState;
26 topic: string;
27
28 join(timeout?: number): Push;
29 leave(timeout?: number): Push;
30
31 onClose(callback: (payload: any, ref: any, joinRef: any) => void | Promise<void>): number;
32 onError(callback: (reason?: any) => void | Promise<void>): number;
33 onMessage(event: string, payload: any, ref: any): any;
34
35 on(event: string, callback: (response?: any) => void | Promise<void>): number;
36 off(event: string, ref?: number): void;
37
38 push(event: string, payload: object, timeout?: number): Push;
39}
40
41export type BinaryType = "arraybuffer" | "blob";
42export type ConnectionState = "connecting" | "open" | "closing" | "closed";
43
44export interface SocketConnectOption {
45 binaryType: BinaryType;
46 params: object | (() => object);
47 transport: new(endpoint: string) => object;
48 timeout: number;
49 heartbeatIntervalMs: number;
50 longpollerTimeout: number;
51 encode: (payload: object, callback: (encoded: any) => void | Promise<void>) => void;
52 decode: (payload: string, callback: (decoded: any) => void | Promise<void>) => void;
53 logger: (kind: string, message: string, data: any) => void;
54 reconnectAfterMs: (tries: number) => number;
55 rejoinAfterMs: (tries: number) => number;
56 vsn: string;
57}
58
59export type MessageRef = string;
60
61export class Socket {
62 constructor(endPoint: string, opts?: Partial<SocketConnectOption>);
63
64 protocol(): string;
65 endPointURL(): string;
66
67 connect(params?: any): void;
68 disconnect(callback?: () => void | Promise<void>, code?: number, reason?: string): void;
69 connectionState(): ConnectionState;
70 isConnected(): boolean;
71 replaceTransport(transport: new(endpoint: string) => object): void;
72
73 remove(channel: Channel): void;
74 channel(topic: string, chanParams?: object): Channel;
75 push(data: object): void;
76
77 log(kind: string, message: string, data: any): void;
78 hasLogger(): boolean;
79
80 onOpen(callback: () => void | Promise<void>): MessageRef;
81 onClose(callback: (event: CloseEvent) => void | Promise<void>): MessageRef;
82 onError(
83 callback: (
84 error: Event | string | number,
85 transport: new(endpoint: string) => object,
86 establishedConnections: number,
87 ) => void | Promise<void>,
88 ): MessageRef;
89 onMessage(callback: (message: object) => void | Promise<void>): MessageRef;
90
91 makeRef(): MessageRef;
92 off(refs: MessageRef[]): void;
93}
94
95export class LongPoll {
96 constructor(endPoint: string);
97
98 normalizeEndpoint(endPoint: string): string;
99 endpointURL(): string;
100
101 closeAndRetry(): void;
102 ontimeout(): void;
103
104 poll(): void;
105
106 send(body: any): void;
107 close(code?: any, reason?: any): void;
108}
109
110// tslint:disable:no-unnecessary-class
111export class Ajax {
112 static states: { [state: string]: number };
113
114 static request(
115 method: string,
116 endPoint: string,
117 accept: string,
118 body: any,
119 timeout?: number,
120 ontimeout?: any,
121 callback?: (response?: any) => void | Promise<void>,
122 ): void;
123
124 static xdomainRequest(
125 req: any,
126 method: string,
127 endPoint: string,
128 body: any,
129 timeout?: number,
130 ontimeout?: any,
131 callback?: (response?: any) => void | Promise<void>,
132 ): void;
133
134 static xhrRequest(
135 req: any,
136 method: string,
137 endPoint: string,
138 accept: string,
139 body: any,
140 timeout?: number,
141 ontimeout?: any,
142 callback?: (response?: any) => void | Promise<void>,
143 ): void;
144
145 static parseJSON(resp: string): JSON;
146 static serialize(obj: any, parentKey: string): string;
147 static appendParams(url: string, params: any): string;
148}
149
150export class Presence {
151 constructor(channel: Channel, opts?: PresenceOpts);
152
153 onJoin(callback: PresenceOnJoinCallback): void;
154 onLeave(callback: PresenceOnLeaveCallback): void;
155 onSync(callback: () => void | Promise<void>): void;
156 list<T = any>(chooser?: (key: string, presence: any) => T): T[];
157 inPendingSyncState(): boolean;
158
159 static syncState(
160 currentState: object,
161 newState: object,
162 onJoin?: PresenceOnJoinCallback,
163 onLeave?: PresenceOnLeaveCallback,
164 ): any;
165
166 static syncDiff(
167 currentState: object,
168 diff: { joins: object; leaves: object },
169 onJoin?: PresenceOnJoinCallback,
170 onLeave?: PresenceOnLeaveCallback,
171 ): any;
172
173 static list<T = any>(presences: object, chooser?: (key: string, presence: any) => T): T[];
174}
175
176export type PresenceOnJoinCallback = (key?: string, currentPresence?: any, newPresence?: any) => void;
177
178export type PresenceOnLeaveCallback = (key?: string, currentPresence?: any, newPresence?: any) => void;
179
180export interface PresenceOpts {
181 events?: { state: string; diff: string } | undefined;
182}
183
184export class Timer {
185 constructor(callback: () => void | Promise<void>, timerCalc: (tries: number) => number);
186
187 reset(): void;
188 scheduleTimeout(): void;
189}