Skip to content
This repository has been archived by the owner on Apr 11, 2024. It is now read-only.

Need to get plain JSON from mounted REST resources rather than custom prototypes #711

Closed
Zelfapp opened this issue Feb 3, 2023 · 3 comments

Comments

@Zelfapp
Copy link

Zelfapp commented Feb 3, 2023

Is there a .toJSON() function on mounted resources, to get plain JSON back or do we simply use js to convert the custom prototypes returned from mounted resources by doing something like JSON.parse(JSON.stringify(transactions))? I'm not seeing functionality to get plain JSON in mounted resources doc.

Shopify shows examples of mounted resources for nodejs and return types examples are plain JSON, but custom prototypes are returned. My hope is that my devs will be able to refer to nodejs examples on Shopify API Rest examples, which are showing mounted resources being used and be able to rely on the Shopify example responses to craft functionality in our app.

image

The issue is that custom prototypes returned from mounted resources (for my use case), cannot be saved in Cloud Firestore. E.g. this mounted resource call to get all transactions returns a custom prototype "Transaction"...

const transactions = await shopify.rest.Transaction.all({
  session,
  order_id: 123456789,
})

... which generates the following firestore error:

Error: Value for argument "data" is not a valid Firestore document. Couldn't serialize object of type "Transaction" (found in field "transaction_data.`0`"). Firestore doesn't support JavaScript objects with custom prototypes (i.e. objects that were created via the "new" operator).
  • @shopify/shopify-api version: 6.1.0
  • Node version: v16.16.0
  • Operating system: Manjaro Linux

Expected behavior

Have the option to get plain JSON back as the examples responses on Shopify describe from mounted resources.

Actual behavior

Custom prototypes are returned rather than plain JSON.

Steps to reproduce the problem

  1. Use the Transaction mounted resource to get all transactions on an order and try to store in Cloud Firestore.
@Zelfapp Zelfapp changed the title Converting mounted REST resources from custom prototypes to standard objects Need to get plain JSON from mounted REST resources rather than custom prototypes Feb 3, 2023
@Zelfapp
Copy link
Author

Zelfapp commented Mar 2, 2023

@mkevinosullivan any thoughts on this?

@mkevinosullivan
Copy link
Contributor

👋🏻 @Zelfapp Sorry for the delay in responding (I thought I had already, my mistake).

As part of a fix for another issue, we added a toJSON() to the base class of the mounted resources in PR #690, and this was released as part of 6.2.0 on February 15 ... have you tried it with the latest release of the library?

@Zelfapp
Copy link
Author

Zelfapp commented Apr 24, 2023

@mkevinosullivan I did not see it. That's great to hear though. Thanks for the update.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants