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 support for parsing json response #32

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

JakeDluhy
Copy link

Fixes #31

@JakeDluhy
Copy link
Author

I just rebased on top of master. Any chance I could get someone to look at this?

@mikemunsie
Copy link

Bump. Having same issue here, would love to see this PR merged 👍

try {
this.response = JSON.parse(this.responseText);
} catch (e) {
// Unable to parse JSON - no biggie
Copy link
Contributor

Choose a reason for hiding this comment

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

lets only catch errors related to parse failure. That we we don't swallow unexpected errors

try {
this.response = JSON.parse(this.responseText);
} catch (e) {
// Unable to parse JSON - no biggie
Copy link
Contributor

Choose a reason for hiding this comment

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

^

@@ -447,6 +447,12 @@
} catch (e) {
// Unable to parse XML - no biggie
}
} else if(this.responseText && /(application\/json)|(application\/vnd\.api\+json)/.test(type)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

space between if and (

@@ -441,6 +441,12 @@ var FakeXMLHttpRequestProto = {
} catch (e) {
// Unable to parse XML - no biggie
}
} else if(this.responseText && /(application\/json)|(application\/vnd\.api\+json)/.test(type)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

space between if and (

@stefanpenner
Copy link
Contributor

left some things that need to be addressed. I believe this is a good change, and correct me if I'm wrong it will require a major version bump, right?

@JakeDluhy
Copy link
Author

I made the requested changes. I don't think it will require a major version bump. It only adds the response key, it doesn't remove any other keys so it shouldn't break anything.

I also added another commit updating the README to have the extra key in the documentation.

@allthesignals
Copy link

I think this is related to my issue over at miragejs/ember-cli-mirage#1320. Any chance this can be merged? What else is left to do?

@samselikoff
Copy link

@stefanpenner I believe this is good – ok to merge?

@@ -447,6 +447,12 @@
} catch (e) {
// Unable to parse XML - no biggie
}
} else if (this.responseText && /(application\/json)|(application\/vnd\.api\+json)/.test(type)) {
try {
this.response = JSON.parse(this.responseText);

Choose a reason for hiding this comment

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

This isn't correct in terms of the spec. There is always a response no matter the responseType, but there is only a responseText when response type is text. response should be whatever the response type called for: string for text, xml for xml, object for json, etc.

https://xhr.spec.whatwg.org/#the-response-attribute

Copy link
Contributor

Choose a reason for hiding this comment

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

@nlfurniss thanks, yes we should be careful to not deviate from the spec.

Could you briefly describe the implications of this violation? I suspect you have a use-case in-mind, and rather then guessing what the use-case is, knowing it will help the rest of us think about this correctly.

I likely have some follow up questions.

Also, do you see a path forward for the PR, or does your concern invalidate it entirely?

Choose a reason for hiding this comment

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

My use-case is JSON, but it seemed like this semi-completeness might confuse people.

After looking at it again, I don't think it's a blocker but I'll create an issue after it lands setting out that response is not 100% supported

@samselikoff
Copy link

@stefanpenner what do you think of the proposed changes?

@nlfurniss
Copy link

See #32 (comment). These changes don’t follow the XHR spec

@stefanpenner
Copy link
Contributor

@samselikoff I think @nlfurniss's concerns likely need to be addressed. Spec violations can lead to some serious compat issues...

my related comment

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.

6 participants