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
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/pages/en/reference/api-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,33 @@ Astro.response.headers.set('Set-Cookie', 'a=b; Path=/;');
---
```

### `Astro.cookies`

matthewp marked this conversation as resolved.
Show resolved Hide resolved
<Since v="1.4.0" />

`Astro.cookies` contains utilities for reading and manipulating cookies.

| Name | Type | Description |
| :------------- | :------------------------------------------------ | :------------------------------------------------- |
| `get` | `(key: string) => AstroCookie` | Gets the cookie as an `(AstroCookie)[#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](https://www.npmjs.com/package/cookie#options-1), such as the `maxAge` or `httpOnly`. |
| `delete` | `(key: string) => void` | Marks the cookie as deleted. Once a cookie is deleted `Astro.cookies.has()` will return `false` and `Astro.cookies.get()` will return an `(AstroCookie)[#astrocookie]` with a `value` of `undefined`. |
| `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.



#### `AstroCookie`

Getting a cookie via `Astro.cookies.get()` returns a `AstroCookie` type. It has the following structure.

| Name | Type | Description |
| :------------- | :------------------------------------------------ | :------------------------------------------------- |
| `value` | `string` | The raw string value of the cookie. |
| `json` | `() => Record<string, any>` | Parses the cookie value via `JSON.parse()`, returning an object. Throws if the cookie value is not valid JSON. |
| `number` | `() => number` | Parses the cookie value as a Number. Returns NaN if not a valid number. |
| `boolean` | `() => boolean` | Converts the cookie value to a boolean. |


### `Astro.canonicalURL`

:::caution[Deprecated]
Expand Down