-
Notifications
You must be signed in to change notification settings - Fork 946
Add typings for WebChannel #2385
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
a864185
e9c7d01
82f6645
dc0b8a5
3f76270
2936fec
1f4767b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,23 +15,73 @@ | |
* limitations under the License. | ||
*/ | ||
|
||
// TODO(mikelehen): Flesh out proper types (or figure out how to generate via | ||
// clutz or something). | ||
export class XhrIo {} | ||
export const ErrorCode: any; | ||
export const EventType: any; | ||
// WARNING: This is not a complete set of types exported by WebChannelWrapper. | ||
// It is merely meant to support the usage patterns of the Firestore SDK. | ||
|
||
export var EventType: { | ||
COMPLETE: string; | ||
}; | ||
|
||
export namespace WebChannel { | ||
export type EventType = any; | ||
export const EventType: any; | ||
export var EventType: { | ||
OPEN: string; | ||
CLOSE: string; | ||
ERROR: string; | ||
MESSAGE: string; | ||
}; | ||
} | ||
|
||
export var ErrorCode: { | ||
NO_ERROR: number; | ||
HTTP_ERROR: number; | ||
TIMEOUT: number; | ||
}; | ||
|
||
export interface Headers { | ||
[name: string]: string | number; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Out of curiosity, why doesn't this cause problems like StringMap did? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I opted not to enable renaming for Object literal access. Since all headers are invalid JavaScript identifiers, they are only ever set using the literal access notation (such as |
||
} | ||
|
||
export interface WebChannelError { | ||
error?: { status: string; message: string }; | ||
} | ||
|
||
type StringMap = { [key: string]: string }; | ||
export class XhrIo { | ||
send( | ||
url: string, | ||
method?: string, | ||
body?: string, | ||
headers?: Headers, | ||
timeoutInterval?: number | ||
): void; | ||
|
||
getLastErrorCode(): number; | ||
|
||
getLastError(): string; | ||
|
||
getStatus(): number; | ||
|
||
getResponseText(): string; | ||
|
||
getResponseJson(): WebChannelError | object; | ||
|
||
listenOnce(type: string, cb: (param: unknown) => void): void; | ||
} | ||
|
||
export interface WebChannelOptions { | ||
messageHeaders?: StringMap; | ||
initMessageHeaders?: StringMap; | ||
messageHeaders?: { | ||
// To ensure compatibility with property minifcation tools, keys need to | ||
// be listed explicity. | ||
[k: string]: never; | ||
}; | ||
initMessageHeaders?: { | ||
// To ensure compatibility with property minifcation tools, keys need to | ||
// be listed explicity. | ||
[k: string]: never; | ||
}; | ||
messageContentType?: string; | ||
messageUrlParams?: StringMap; | ||
messageUrlParams?: { | ||
database?: string; | ||
}; | ||
clientProtocolHeaderRequired?: boolean; | ||
concurrentRequestLimit?: number; | ||
supportsCrossDomainXhr?: boolean; | ||
|
@@ -44,13 +94,22 @@ export interface WebChannelOptions { | |
fastHandshake?: boolean; | ||
disableRedac?: boolean; | ||
clientProfile?: string; | ||
internalChannelParams?: { [key: string]: boolean | number }; | ||
internalChannelParams?: { | ||
forwardChannelRequestTimeoutMs?: number; | ||
}; | ||
xmlHttpFactory?: unknown; | ||
requestRefreshThresholds?: { [key: string]: number }; | ||
} | ||
|
||
export interface WebChannel { | ||
open(): void; | ||
close(): void; | ||
listen(type: string, cb: (param: unknown) => void): void; | ||
send(msg: unknown): void; | ||
} | ||
|
||
export interface WebChannelTransport { | ||
createWebChannel(url: string, options: WebChannelOptions): any; | ||
createWebChannel(url: string, options: WebChannelOptions): WebChannel; | ||
} | ||
|
||
export function createWebChannelTransport(): WebChannelTransport; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -68,8 +68,6 @@ goog.net.XhrIo.prototype['getLastError'] = | |
goog.net.XhrIo.prototype['getLastErrorCode'] = | ||
goog.net.XhrIo.prototype.getLastErrorCode; | ||
goog.net.XhrIo.prototype['getStatus'] = goog.net.XhrIo.prototype.getStatus; | ||
goog.net.XhrIo.prototype['getStatusText'] = | ||
goog.net.XhrIo.prototype.getStatusText; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not used by our code. Removing it is technically a breaking change though for consumers of this package. We might be ok though: https://github.com/search?l=JavaScript&q=%22%40firebase%2Fwebchannel-wrapper%22&type=Code |
||
goog.net.XhrIo.prototype['getResponseJson'] = | ||
goog.net.XhrIo.prototype.getResponseJson; | ||
goog.net.XhrIo.prototype['getResponseText'] = | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was it not possible to make WebChannel.EventType work? It seems like we're losing a bit of help from the type system here by switching to just
string
. Not the end of the world though.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
WebChannel.EventType is now a variable declaration and no longer a string. I had tried both
WebChannel.EventType
andtypepof WebChannel.EventType
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bleh... I came up with several ways to get this to compile (let us use
EventType
as a type as well as hang property values off it). E.g.:or:
but they didn't provide type-safety, since TypeScript's structural typing would let you pass anything (e.g.
{x: 1}
) for a parameter typed asEventType
. 😕So the only thing I found that actually works for type-safety is using
export enum EventType { COMPLETE }
like you originally had. So if you feel like putting that back, I would be okay with that. Else if you're tired of messing with it, this is okay too.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm. My "I couldn't get it to work" response above is based on the current version that used the variable declaration w/ nested property syntax. I am inclined to leave that as is for now, as it matches the underlying library closer than the Enum values.