Skip to content

Commit

Permalink
fix(api-reference): update Astro.cookies types
Browse files Browse the repository at this point in the history
* Fix inaccurate types for Astro.cookies `get` and `set` methods
* Add missing `merge` method for Astro.cookies (the `AstroCookies` return type
could miss some documentation)
* Fix inaccurate type for AstroCookie `value`

I did not updated the name of the options types, but all of them should
be prefixed with `Astro`. I don't know if this is deliberate or an oversight
during an update.

See: `packages/astro/src/core/cookies/cookies.ts`
  • Loading branch information
ArmandPhilippot committed Jul 29, 2024
1 parent 00b1afd commit b607be5
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/content/docs/en/reference/api-reference.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -216,23 +216,23 @@ Astro.response.headers.set('Set-Cookie', 'a=b; Path=/;');
##### `get`

<p>
**Type:** `(key: string, options?: CookieGetOptions) => AstroCookie`
**Type:** `(key: string, options?: CookieGetOptions) => AstroCookie | undefined`
</p>

Gets the cookie as an [`AstroCookie`](#astrocookie) object, which contains the `value` and utility functions for converting the cookie to non-string types.

##### `has`

<p>
**Type:** `(key: string) => boolean`
**Type:** `(key: string, options?: CookieGetOptions) => boolean`
</p>

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`

<p>
**Type:** `(key: string, value: string | number | boolean | object, options?: CookieSetOptions) => void`
**Type:** `(key: string, value: string | object, options?: CookieSetOptions) => void`
</p>

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`.
Expand All @@ -247,6 +247,14 @@ Invalidates a cookie by setting the expiration date in the past (0 in Unix time)

Once a cookie is "deleted" (expired), `Astro.cookies.has()` will return `false` and `Astro.cookies.get()` will return an [`AstroCookie`](#astrocookie) with a `value` of `undefined`. Options available when deleting a cookie are: `domain`, `path`, `httpOnly`, `sameSite`, and `secure`.

##### `merge`

<p>
**Type:** `(cookies: AstroCookies) => void`
</p>

Merges a new `AstroCookies` instance into the current instance. Any new cookies will be added to the current instance, overwriting any existing cookies with the same name.

##### `headers`

<p>
Expand All @@ -262,7 +270,7 @@ Getting a cookie via `Astro.cookies.get()` returns a `AstroCookie` type. It has
##### `value`

<p>
**Type:** `string | undefined`
**Type:** `string`
</p>

The raw string value of the cookie.
Expand Down

0 comments on commit b607be5

Please sign in to comment.