-
Notifications
You must be signed in to change notification settings - Fork 54
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
[Feat]: Valid JSON formatted response #428
Comments
Currently all our client libraries, including the Node one, are configured to deserialize the JSON responses from the API into in-memory objects. Is there a particular use case you had in mind where accessing the raw JSON response rather than using the serialized object would be better? |
I certainly understand the thinking on this but I am referring primarily to SSR requests that are returned via a promise to a page. For every other third party api's we use (stripe, paypal, database requests, etc) they all return JSON compatible objects by default. The easypost object returned is non-standard / non-serializable format so at a minimum it would be great to fix that and make it the same as the documentation. Below is an example returned from a rate request (client.Shipment.create)
Obviously this format is not a Plain Old Javascript Object |
Hey @ahkelly, thanks for submitting this issue, I would like to try and better understand what you're running into. We don't currently have a way to get a raw JSON string instead of the object but I'm hoping by better understanding the issue you're running into that you won't need that. A raw JSON string will still force you to do the The raw JSON response from the API is converted to an object in the library. As far as I'm aware it is a valid javascript object and that is why you are able to stringify and parse it. What I'm not sure about is what our current response is preventing you from doing. This operation
should basically be a no-op and you should end up more or less with what you started with. It sounds like that is not the case and you are unable to do something with |
So I am using svelte/kit for the server side request Full svelte sample load request code is below, this will throw a 500 error as the response cannot be parsed as valid JSON thus the stringify dance I note above to correct that. FYI, if you save the exact example from the docs to an app.js and run
you will get the exact same response as my svelte code below receives.
Attempting to return this response as shown above will throw the following exception, essentially saying the svelte parser can't handle this non standard object
The response object I get (dumped directly to the console from the node request)
Further to your query regarding stringifying the response. As you can see above this is not a valid parseable object by JSON.parse so stringifying it fixes the weird format which opens with an orphan "Shipment" and adds quotes around all data names and turning above non-standard object into below.
|
So I think I understand the confusion around "non standard object". I was considering
would result in
I was considering them plain old javascript objects but there are differences between an instance of a class and a truly plain object that are impacting the sveltekit parser, which is a library called devalue. The error you shared
would return a complete javascript object that does not need any specific handling but the problem is in how sveltekit serializes and that it rejects javascript classes as "arbitrary non-POJOs" based on their prototype. The solution you have is actually the suggested work around from other sveltekit devs that have run into this with various libraries.
Or if you are on a recent version of node
Thanks for reporting this issue, we don't use sveltekit in house and hadn't run into it. We will evaluate potentially moving away from JS Unfortunately for now you'll need to do the conversion work around using one of the above methods in order to use the server |
Thanks for the explanation and understood. Did not occur to me you were returning this object as stripe, mailchimp, etc, etc all return standard JSON objects. I would offer it may be helpful to put this in the docs and update all your examples as they all show a standard JSON format as the response not a class object. |
Feature Request Is New
Description of the feature
Is there a way to request a valid JSON response from node requests, rather then having to convert the Object using:
Interestingly the docs show a valid JSON response but the api returns an object. Just wondering if there is or could be added a hidden flag to request JSON format.
The text was updated successfully, but these errors were encountered: