Skip to content
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

Open
wants to merge 7 commits into
base: trunk
Choose a base branch
from

Conversation

ockham
Copy link
Contributor

@ockham ockham commented Mar 2, 2020

Changes proposed in this Pull Request

wpcom-proxy-request: Migrate to TypeScript

Testing instructions

Verify that Calypso still builds and works.

@matticbot
Copy link
Contributor

@ockham ockham force-pushed the update/wpcom-proxy-request-typescript branch 2 times, most recently from 066940a to 541c89b Compare March 2, 2020 10:02
@matticbot
Copy link
Contributor

matticbot commented Mar 2, 2020

Here is how your PR affects size of JS and CSS bundles shipped to the user's browser:

App Entrypoints (~141 bytes added 📈 [gzipped])

name                   parsed_size           gzip_size
entry-main                  +100 B  (+0.0%)      +30 B  (+0.0%)
entry-login                 +100 B  (+0.0%)      +32 B  (+0.0%)
entry-gutenboarding         +100 B  (+0.0%)      +47 B  (+0.0%)
entry-domains-landing       +100 B  (+0.0%)      +32 B  (+0.0%)

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.
Gzip Size: Compressed size of the JS and CSS files. This much data needs to be downloaded over network.

Generated by performance advisor bot at iscalypsofastyet.com.

@sirreal sirreal added [Status] Needs Review The PR is ready for review. This also triggers e2e canary tests and wp-desktop tests automatically. API Framework and removed [Status] In Progress labels Mar 2, 2020
@sirreal sirreal marked this pull request as ready for review March 2, 2020 16:39
@sirreal sirreal requested a review from a team as a code owner March 2, 2020 16:39
@ockham ockham force-pushed the update/wpcom-proxy-request-typescript branch 2 times, most recently from 3375399 to 8e0032f Compare March 3, 2020 16:22
@ockham
Copy link
Contributor Author

ockham commented Mar 3, 2020

Rebased (and hopefully updated correctly, now that #39825 is in).

@ockham ockham force-pushed the update/wpcom-proxy-request-typescript branch from 8e0032f to b307836 Compare March 23, 2020 18:45
@sarayourfriend sarayourfriend changed the base branch from master to trunk November 20, 2020 16:13
@sirreal sirreal force-pushed the update/wpcom-proxy-request-typescript branch 3 times, most recently from 825ce17 to f2491ab Compare December 30, 2020 11:28
@sirreal sirreal force-pushed the update/wpcom-proxy-request-typescript branch from f2491ab to bea41bc Compare December 30, 2020 11:28
@sirreal sirreal requested a review from a team December 30, 2020 11:42
@sirreal sirreal requested a review from a team December 30, 2020 11:42
@sirreal sirreal self-assigned this Dec 30, 2020
@sirreal
Copy link
Member

sirreal commented Dec 30, 2020

I polished this up and it's ready for review.

@sirreal sirreal requested a review from a team December 30, 2020 11:43
* @param {Function} [fn] - callback response
* @returns {window.XMLHttpRequest} XMLHttpRequest instance
* @param originalParams - request parameters
* @param fn - callback response
Copy link
Contributor

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?

Copy link
Member

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.

Comment on lines +210 to +215
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 > {
Copy link
Contributor

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?

Copy link
Member

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.

Comment on lines -294 to -295
buffered = [];

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this change?

Copy link
Member

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.

@github-actions
Copy link

github-actions bot commented May 5, 2021

This PR has been marked as stale due to lack of activity within the last 30 days.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API Framework [Goal] Full Site Editing Packages [Status] Needs Review The PR is ready for review. This also triggers e2e canary tests and wp-desktop tests automatically. [Status] Stale [Type] Task
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants