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

chore(gatsby-cms-plugin): fetch content by ID using REST API from CMS for preview #1120

Merged
merged 1 commit into from
Jan 26, 2022

Conversation

mateuspontes
Copy link
Contributor

@mateuspontes mateuspontes commented Jan 20, 2022

What's the purpose of this pull request?

This PR enables gatsby-plugin-cms to receive webhookBody params, fetch the values on the CMS API and return it to preview on webOps.

How does it work?

REST API example: https://storecomponents.myvtex.com/_v/cms/api/faststore/home/72de3769-347e-4734-855a-e5a4f072f66b?versionId=e2f4f2c0-9001-4f6b-b318-35f50ae2e3fb

How to test it?

  1. clone this repo and branch
  2. link it on your favorite account (you can try with storecomponents.store) using wml
  3. run yarn develop on this folder to build the files and start the server on your store using ENABLE_GATSBY_REFRESH_ENDPOINT=true, you should see the home page from production.
  4. to reproduce the preview, call your refresh endpoint using this values:
curl -X POST -H "Content-Type: application/json" http://127.0.0.1:8000/__refresh -d '{"id": "847b580d-83cb-4129-ae5f-1ea69123ff66", "contentType": "home", "versionId": "61e0bd61-fc46-44d0-bebd-0897a35c6214"}'

References

This PR was totally inspired by https://github.com/vtex/cms/pull/8 from @tlgimenes

@mateuspontes mateuspontes requested a review from a team as a code owner January 20, 2022 18:38
@mateuspontes mateuspontes self-assigned this Jan 20, 2022
@netlify
Copy link

netlify bot commented Jan 20, 2022

✔️ Deploy Preview for faststoreui ready!

🔨 Explore the source changes: 40fa516

🔍 Inspect the deploy log: https://app.netlify.com/sites/faststoreui/deploys/61ef19c278ee8b000763bb3d

😎 Browse the preview: https://deploy-preview-1120--faststoreui.netlify.app

@netlify
Copy link

netlify bot commented Jan 20, 2022

✔️ Deploy Preview for faststore ready!

🔨 Explore the source changes: 40fa516

🔍 Inspect the deploy log: https://app.netlify.com/sites/faststore/deploys/61ef19c21e74d50008de552b

😎 Browse the preview: https://deploy-preview-1120--faststore.netlify.app

@codesandbox-ci
Copy link

codesandbox-ci bot commented Jan 20, 2022

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 40fa516:

Sandbox Source
Store UI Typescript Configuration

Comment on lines +16 to +19

console.error(await response.text())

throw new Error(`Error while fetching ${input}`)
Copy link
Contributor

Choose a reason for hiding this comment

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

I suggest putting this code inside an if branch and removing the response.json() from the branch. Response returning "ok" is the "happy path," so put it as most left as possible.

fetchAllRemoteNodes(gatsbyApi, options),
fetchAllLocalNodes(gatsbyApi),
]).then(([x, y]) => [...x, ...y].filter(Boolean))
const { webhookBody }: any = gatsbyApi
Copy link
Contributor

Choose a reason for hiding this comment

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

Can this object be typed with a better type, can't?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

we can type it, but we can't guarantee the user passing this params, by default it's an open object

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, you're right. But at least could type something like interface Interface { webhookBody?: any }

Copy link
Contributor

@igorbrasileiro igorbrasileiro left a comment

Choose a reason for hiding this comment

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

Great job, @mateuspontes. I don't have enough context about this plugin, but the code makes sense. I left some suggestions that aren't required. Did you test this code in some store repo? If yes, can you share?!

@mateuspontes mateuspontes force-pushed the chore/fetch-document-for-preview branch from 19d91b3 to 40fa516 Compare January 24, 2022 21:27
@mateuspontes mateuspontes changed the title Fetch content by ID using REST API from CMS for preview chore(gatsby-cms-plugin): fetch content by ID using REST API from CMS for preview Jan 24, 2022
@mateuspontes mateuspontes merged commit 9440ff1 into master Jan 26, 2022
@mateuspontes mateuspontes deleted the chore/fetch-document-for-preview branch January 26, 2022 17:56
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