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

Add a .text() accessor to FetchResponse #2922

Merged
merged 4 commits into from
Apr 15, 2016
Merged

Add a .text() accessor to FetchResponse #2922

merged 4 commits into from
Apr 15, 2016

Conversation

tdrl
Copy link

@tdrl tdrl commented Apr 15, 2016

Add .text() accessor to FetchResponse to allow retrieving text without converting it to JSON first. (I.e., extending FetchResponse to cover one additional method from the Response / Body API.)

…out converting it to JSON first. (I.e., extending FetchResponse to cover one additional method from the Response / Body API.)
@@ -387,6 +387,15 @@ class FetchResponse {
}

/**
* Drains the response and returns a promise that resolves with the response
* text.
* @returns {!Promise.<string>}
Copy link
Member

Choose a reason for hiding this comment

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

Remove . after Promise

Copy link
Member

Choose a reason for hiding this comment

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

qq, is it @returns or @return? https://developers.google.com/closure/compiler/docs/js-for-compiler#tags says it should be @return

Copy link
Author

Choose a reason for hiding this comment

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

Oops. My editor templates betrayed me. Fixed.

@cramforce
Copy link
Member

Please add a test.

*/
text() {
return this.drainText_();
}
Copy link
Member

@erwinmombay erwinmombay Apr 15, 2016

Choose a reason for hiding this comment

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

if we need drainText_ exposed, i think we should just make it public instead of this re-aliasing. but i don't mind this either way.

/cc @cramforce

Copy link
Member

@erwinmombay erwinmombay Apr 15, 2016

Choose a reason for hiding this comment

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

actually, looking through the full code again, this is probably better. (adding this new public text() and keeping the private drainText_ method)

Copy link
Member

Choose a reason for hiding this comment

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

Yes, text is part of the fetch API.

@tdrl
Copy link
Author

tdrl commented Apr 15, 2016

PTAL

const response = new FetchResponse(mockXhr);
return response.text().then(result => {
expect(result).to.equal('this is some test text');
expect(response.text, 'should throw').to.throw(Error);
Copy link
Contributor

Choose a reason for hiding this comment

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

This will throw for an unrelated reason, you'll need to #bind here.

Copy link
Author

Choose a reason for hiding this comment

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

Oh, good catch. I didn't realize that. Thanks! Fixed.

@cramforce cramforce merged commit 65c4b02 into ampproject:master Apr 15, 2016
@tdrl tdrl deleted the xhr-text-method branch April 18, 2016 13:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants