Skip to content

Commit 9f7b146

Browse files
Add Types for Webchannel
1 parent 542240d commit 9f7b146

File tree

2 files changed

+75
-13
lines changed

2 files changed

+75
-13
lines changed

packages/firestore/src/platform_browser/webchannel_connection.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,7 @@ export class WebChannelConnection implements Connection {
9898
const url = this.makeUrl(rpcName);
9999

100100
return new Promise((resolve: Resolver<Resp>, reject: Rejecter) => {
101-
// XhrIo doesn't have TS typings.
102-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
103-
const xhr: any = new XhrIo();
101+
const xhr = new XhrIo();
104102
xhr.listenOnce(EventType.COMPLETE, () => {
105103
try {
106104
switch (xhr.getLastErrorCode()) {
@@ -191,6 +189,7 @@ export class WebChannelConnection implements Connection {
191189

192190
this.modifyHeadersForRequest(headers, token);
193191

192+
// eslint-disable-next-line @typescript-eslint/no-floating-promises
194193
xhr.send(url, 'POST', requestString, headers, XHR_TIMEOUT_SECS);
195194
});
196195
}

packages/webchannel-wrapper/src/index.d.ts

Lines changed: 73 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,66 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17+
gi
18+
export enum EventType {
19+
OPEN = 'open',
20+
CLOSE = 'close',
21+
ERROR = 'error',
22+
MESSAGE = 'message',
23+
COMPLETE = 'complete'
24+
}
1725

18-
// TODO(mikelehen): Flesh out proper types (or figure out how to generate via
19-
// clutz or something).
20-
export class XhrIo {}
21-
export const ErrorCode: any;
22-
export const EventType: any;
2326
export namespace WebChannel {
24-
export type EventType = any;
25-
export const EventType: any;
27+
export enum EventType {
28+
OPEN = 'open',
29+
CLOSE = 'close',
30+
ERROR = 'error',
31+
MESSAGE = 'message',
32+
COMPLETE = 'complete'
33+
}
34+
}
35+
36+
export enum ErrorCode {
37+
NO_ERROR = 0,
38+
ACCESS_DENIED,
39+
FILE_NOT_FOUND,
40+
FF_SILENT_ERROR,
41+
CUSTOM_ERROR,
42+
EXCEPTION,
43+
HTTP_ERROR,
44+
ABORT,
45+
TIMEOUT,
46+
OFFLINE
47+
}
48+
49+
export interface Headers {
50+
[name: string]: string | number;
51+
}
52+
53+
export class XhrIo {
54+
send(
55+
url: string,
56+
method?: string,
57+
body?: ArrayBufferView | Blob | string | null,
58+
headers?: Headers,
59+
timeoutInterval?: number
60+
): Promise<XhrIo>;
61+
62+
getLastErrorCode(): ErrorCode;
63+
64+
getLastError(): string;
65+
66+
getStatus(): number;
67+
68+
getResponseText(): string;
69+
70+
getResponseJson(): Object | any;
71+
72+
abort(): void;
73+
74+
getResponseHeader(header: string): { [key: string]: string };
75+
76+
listenOnce<T>(type: EventType, cb: (param: T) => void): void;
2677
}
2778

2879
type StringMap = { [key: string]: string };
@@ -31,7 +82,9 @@ export interface WebChannelOptions {
3182
messageHeaders?: StringMap;
3283
initMessageHeaders?: StringMap;
3384
messageContentType?: string;
34-
messageUrlParams?: StringMap;
85+
messageUrlParams?: {
86+
database?: string;
87+
};
3588
clientProtocolHeaderRequired?: boolean;
3689
concurrentRequestLimit?: number;
3790
supportsCrossDomainXhr?: boolean;
@@ -44,13 +97,23 @@ export interface WebChannelOptions {
4497
fastHandshake?: boolean;
4598
disableRedac?: boolean;
4699
clientProfile?: string;
47-
internalChannelParams?: { [key: string]: boolean | number };
100+
internalChannelParams?: {
101+
forwardChannelRequestTimeoutMs?: number;
102+
};
48103
xmlHttpFactory?: unknown;
49104
requestRefreshThresholds?: { [key: string]: number };
50105
}
51106

107+
export interface WebChannel {
108+
open(): void;
109+
close(): void;
110+
halfClose(): void;
111+
listen<T>(type: EventType, cb: (param: T) => void): void;
112+
send<T>(msg: T): void;
113+
}
114+
52115
export interface WebChannelTransport {
53-
createWebChannel(url: string, options: WebChannelOptions): any;
116+
createWebChannel(url: string, options: WebChannelOptions): WebChannel;
54117
}
55118

56119
export function createWebChannelTransport(): WebChannelTransport;

0 commit comments

Comments
 (0)