Skip to content

Commit

Permalink
Document Astro.cookie options (#6042)
Browse files Browse the repository at this point in the history
Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>
Co-authored-by: Yan Thomas <61414485+Yan-Thomas@users.noreply.github.com>
  • Loading branch information
3 people authored Jan 4, 2024
1 parent 06242cb commit 59cc602
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions src/content/docs/en/reference/api-reference.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,9 @@ Astro.response.headers.set('Set-Cookie', 'a=b; Path=/;');

| 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. |
| `get` | `(key: string, options?: CookieGetOptions) => 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`. |
| `set` | `(key: string, value: string \| number \| boolean \| object, options?: CookieSetOptions) => 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, options?: CookieDeleteOptions) => 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`. Options allow setting the `domain` and `path` of the cookie to delete. |
| `headers` | `() => Iterator<string>` | Gets the header values for `Set-Cookie` that will be sent out with the response. |

Expand All @@ -228,6 +228,33 @@ Getting a cookie via `Astro.cookies.get()` returns a `AstroCookie` type. It has
| `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. |

#### `CookieGetOptions`

<Since v="4.1.0"/>

Getting a cookie also allows specifying options via the `CookieGetOptions` interface:

| Name | Type | Description |
| :-------- | :-------------------------- | :------------------------------------------------------------------------------------------------------------- |
| `decode` | `(value: string) => string` | Allows customization of how a cookie is deserialized into a value. |

#### `CookieSetOptions`

<Since v="4.1.0"/>

Setting a cookie via `Astro.cookies.set()` allows passing in a `CookieSetOptions` to customize how the cookie is serialized.

| Name | Type | Description |
| :-------- | :-------------------------- | :------------------------------------------------------------------------------------------------------------- |
| `domain` | `string` | Specifies the domain. If no domain is set, most clients will interpret to apply to the current domain. |
| `expires` | `Date` | Species the date on which the cookie will expire. |
| `httpOnly` | `boolean` | If true, the cookie will not be accessible client-side. |
| `maxAge` | `number` | Specifies a number, in seconds, for which the cookie is valid. |
| `path` | `string` | Specifies a subpath of the domain in which the cookie is applied. |
| `sameSite` | `boolean \| 'lax' \| 'none' \| 'strict'` | Specifies the value of the [SameSite](https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-rfc6265bis-09#section-5.4.7) cookie header |
| `secure` | `boolean` | If true, the cookie is only set on https sites. |
| `encode` | `(value: string) => string` | Allows customizing how the cookie is serialized |

### `Astro.redirect()`
`Astro.redirect()` allows you to redirect to another page.
A page (and not a child component) must `return` the result of `Astro.redirect()` for the redirect to occur.
Expand Down

0 comments on commit 59cc602

Please sign in to comment.