-
Notifications
You must be signed in to change notification settings - Fork 2k
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
wpcom-proxy-request: Migrate to TypeScript #39801
base: trunk
Are you sure you want to change the base?
Conversation
066940a
to
541c89b
Compare
Here is how your PR affects size of JS and CSS bundles shipped to the user's browser: App Entrypoints (~141 bytes added 📈 [gzipped])
Common code that is always downloaded and parsed every time the app is loaded, no matter which route is used. Legend What is parsed and gzip size?Parsed Size: Uncompressed size of the JS and CSS files. This much code needs to be parsed and stored in memory. Generated by performance advisor bot at iscalypsofastyet.com. |
3375399
to
8e0032f
Compare
Rebased (and hopefully updated correctly, now that #39825 is in). |
8e0032f
to
b307836
Compare
825ce17
to
f2491ab
Compare
f2491ab
to
bea41bc
Compare
I polished this up and it's ready for review. |
* @param {Function} [fn] - callback response | ||
* @returns {window.XMLHttpRequest} XMLHttpRequest instance | ||
* @param originalParams - request parameters | ||
* @param fn - callback response |
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.
Is fn
optional or was that a mistake in the original JSDoc?
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.
It was, but apparently didn't need to be. This is an internal/private function that always receives a fn
callback.
export default function request( originalParams: WpcomRequestParams, fn: Callback ): XMLHttpRequest; | ||
export default function request< T >( originalParams: WpcomRequestParams ): Promise< T >; | ||
export default function request< T >( | ||
originalParams: WpcomRequestParams, | ||
fn?: Callback | ||
): XMLHttpRequest | Promise< T > { |
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.
Will these overloads not cause an issue when trying to call request(params).then()
? I'm curious how TS will negotiate the missing fn callback, which overload does it choose in that case?
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.
When request
is called with the fn
callback arg, it returns either XmlHttpRequest
or null
. The latter can be returned by wpcom-xhr-request
when used on Node.js, where there's no XHR. request
never returns a Promise
when the fn
callback is supplied.
Without the fn
callback, the return value is always a Promise
.
The returned XmlHttpRequest
object can be used to subscribe to progress event when uploading or downloading something. There's no other API for that in the wpcom-*-request
libs.
It would be nice to redesign this API in a major release. Mainly to get rid of exposing the XmlHttpRequest
object. It's not supported in Node.js, and in wpcom-proxy-request
the returned instance is somewhat synthetic, supporting event listeners but not much else.
And in the variant that returns a Promise
, progress event and also aborting requests is not supported at all. XmlHttpRequest
can do both.
Last note: the type of the request
function should be defined in the wpcom
library rather than in wpcom-proxy-request
. After all, wpcom-proxy-request
is just a "low level driver" for the wpcom
library, and wpcom-xhr-request
and other drivers are supposed to implement the same interface.
buffered = []; | ||
|
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.
Why this change?
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.
Buffered was a nullable array, now it's just an array. The uninstall
function handles clearing the array if necessary. It's no longer necessary to set it to an empty array for initialization.
This PR has been marked as stale due to lack of activity within the last 30 days. |
Changes proposed in this Pull Request
wpcom-proxy-request: Migrate to TypeScript
Testing instructions
Verify that Calypso still builds and works.