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 API reference for Astro.cookies #1637

Merged
merged 5 commits into from
Sep 29, 2022
Merged

Add API reference for Astro.cookies #1637

merged 5 commits into from
Sep 29, 2022

Conversation

matthewp
Copy link
Contributor

What kind of changes does this PR include?

  • New or updated content

Docs for Astro.cookies

Description

Docs for withastro/astro#4876

@netlify
Copy link

netlify bot commented Sep 28, 2022

Deploy Preview for astro-docs-2 ready!

Name Link
🔨 Latest commit 83a896e
🔍 Latest deploy log https://app.netlify.com/sites/astro-docs-2/deploys/63359a530602b20008e08b06
😎 Deploy Preview https://deploy-preview-1637--astro-docs-2.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@sarah11918 sarah11918 added add new content Document something that is not in docs. May require testing, confirmation, or affect other pages. merge-on-release Don't merge this before the feature is released! (MQ=approved but WAIT for feature release!) labels Sep 28, 2022
Copy link
Member

@sarah11918 sarah11918 left a comment

Choose a reason for hiding this comment

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

Yay, thanks for getting this together so promptly, @matthewp! Just some thoughts/suggestions below. This content isn't really my area, so will also wait for @Jutanium / @delucis to weigh in with more notable commentary.

src/pages/en/reference/api-reference.md Outdated Show resolved Hide resolved
src/pages/en/reference/api-reference.md Outdated Show resolved Hide resolved
src/pages/en/reference/api-reference.md Outdated Show resolved Hide resolved
| `get` | `(key: string) => AstroCookie` | Gets the cookie as an `AstroCookie` object, which contains the `value` and utility functions for converting the cookie to non-string types. |
| `has` | `(key: string) => boolean` | Whether this cookie exists. If the cookie has been set via `Astro.cookies.set()` this will return true, otherwise it will check cookies in the `Astro.request`. |
| `set` | `(key: string, value: string \| number \| boolean \| object, options?: CookieOptions) => void` | Sets the cookie `key` to the given value. This will attempt to convert the cookie value to a string. Options provide ways to set cookie features, such as the `maxAge` or `httpOnly`. |
| `delete` | `(key: string) => void` | Marks the cookie to be deleted. Deleting a cookie means sending out a header as part of the response. Once a cookie is deleted it will return `false` via `Astro.cookies.has()`. |
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
| `delete` | `(key: string) => void` | Marks the cookie to be deleted. Deleting a cookie means sending out a header as part of the response. Once a cookie is deleted it will return `false` via `Astro.cookies.has()`. |
Marks the cookie to be deleted. Deleting a cookie means sending out a header as part of the response. |

I'm a little unclear about the second sentence. delete marks the cookie for deletion. Is there a more direct way of saying how the cookie is deleted? What action deletes it, or after which event it is deleted?

OR, is the sentence necessary at all?

Marks the cookie to be deleted. Once a cookie is deleted it will return false via Astro.cookies.has().

Does that tell enough about what is happening? Would someone need to know/understand what happens in between marking the cookie for deletion, and then after it has been deleted? (Is there a special timing consideration for an in-between state that it's important to address?)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So technically what is happening is when you "delete" a cookie you are really just sending a Set-Cookie: key=value; expires=Thu, 01 Jan 1970 00:00:00 GMT. Note here that expires is in the past, this tells the browser that the cookie has expired so it deletes it.

So you're really just telling the browser that the cookie is no longer valid.

I'm not sure if we need to explain that part to the user here? I will defer to your judgement on that.

What I was trying to convey here is that, from the context of the Astro.cookies API, once you call delete(key) that cookie no longer exists, you cannot do get(key) or has(key) to get its value. It's "deleted" from memory, if you will.

Copy link
Member

@sarah11918 sarah11918 Sep 28, 2022

Choose a reason for hiding this comment

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

I might be tempted to say something like:

Marks the cookie as deleted. Once a cookie is deleted, Astro.cookies.has() will return false and Astro.cookies.get() will .... . (throw an error/return null.. whatever it does)

Would that describe it well enough? (Using "as" to imply that it "takes effect" immediately as far as the user is concerned? Could also say "as expired" or "as invalid") Then, describe what the other functions will return?

I'll leave the final wording, or whether or not to make any changes up to you, though. If that second sentence as it currently is would be clear in context to the general reader, then it's fine by me!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah I like this, it skips over the "how" which is not important from the API perspective and just focuses on how Astro.cookies will work after deletion. I'll update with something like this.

| `has` | `(key: string) => boolean` | Whether this cookie exists. If the cookie has been set via `Astro.cookies.set()` this will return true, otherwise it will check cookies in the `Astro.request`. |
| `set` | `(key: string, value: string \| number \| boolean \| object, options?: CookieOptions) => void` | Sets the cookie `key` to the given value. This will attempt to convert the cookie value to a string. Options provide ways to set cookie features, such as the `maxAge` or `httpOnly`. |
| `delete` | `(key: string) => void` | Marks the cookie to be deleted. Deleting a cookie means sending out a header as part of the response. Once a cookie is deleted it will return `false` via `Astro.cookies.has()`. |
| `headers` | `() => Iterator<string>` | Gets the header values for `Set-Cookie` that will be sent out with the response. |
Copy link
Member

Choose a reason for hiding this comment

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

no comment! Just can't delete this.

Copy link
Member

@delucis delucis left a comment

Choose a reason for hiding this comment

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

LGTM! Thanks, Matthew.

@sarah11918
Copy link
Member

Just reminding you @matthewp that this is your PR to merge when you're happy with it! (In Docs, we always leave it up to the author to merge, if they have maintainer authority.)

@matthewp matthewp merged commit 6e78552 into main Sep 29, 2022
@matthewp matthewp deleted the astro-cookies branch September 29, 2022 13:21
nokazn pushed a commit to nokazn/docs.astro.build that referenced this pull request Oct 2, 2022
* Add API reference for Astro.cookies

* Update src/pages/en/reference/api-reference.md

Co-authored-by: Sarah Rainsberger <[email protected]>

* Add link to cookie library

* Update src/pages/en/reference/api-reference.md

Co-authored-by: Chris Swithinbank <[email protected]>

* Clarify what delete does

Co-authored-by: Sarah Rainsberger <[email protected]>
Co-authored-by: Chris Swithinbank <[email protected]>
@sarah11918 sarah11918 added the minor-release For the next minor release; in the milestone, "merge queue" when approved by Sarah! label Jun 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
add new content Document something that is not in docs. May require testing, confirmation, or affect other pages. merge-on-release Don't merge this before the feature is released! (MQ=approved but WAIT for feature release!) minor-release For the next minor release; in the milestone, "merge queue" when approved by Sarah!
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants