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

adding cookies to api route response [simple result] #5060

Merged
merged 5 commits into from
Oct 18, 2022

Conversation

AirBorne04
Copy link
Contributor

Changes

  • attaching cookies to an endpoint response [when the result is a simple object]
  • eliminating inconsistency in cookie handling between the two result objects types (simple, Response)
  • adjusting the example for ssr to use the build in cookie handling vs lightcookie 3rd party

Testing

  • run the example project ssr from the repo
  • check both build -> preview with node and dev

Docs

  • this is removing an inconsistency which, to my knowledge, is not documented

@changeset-bot
Copy link

changeset-bot bot commented Oct 12, 2022

🦋 Changeset detected

Latest commit: 79bb937

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions github-actions bot added pkg: astro Related to the core `astro` package (scope) pkg: example Related to an example package (scope) labels Oct 12, 2022
@matthewp
Copy link
Contributor

Looks good overall! Just one thing about how the cookies are set in dev.

Love the example update to use Astro.cookies as well!

Comment on lines -426 to +388
res.writeHead(200, { 'Content-Type': `${contentType};charset=utf-8` });
res.end(result.body);
const response = new Response(result.body, {
status: 200,
headers: {
'Content-Type': `${contentType};charset=utf-8`,
},
});
attachToResponse(response, result.cookies);
await writeWebResponse(res, response);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I converted the direct writing to the response to a call to writeWebResponse in order to streamline the response handling. Also added a call to attachToResponse in order to connect the cookie to the response.

@matthewp
Copy link
Contributor

This looks great, thank you!

@matthewp matthewp merged commit 5923dd7 into withastro:main Oct 18, 2022
@astrobot-houston astrobot-houston mentioned this pull request Oct 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pkg: astro Related to the core `astro` package (scope) pkg: example Related to an example package (scope)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants