refactor: upgrade cookie-es to v3 · h3js/h3@9d244a7
1-import type { CookieSerializeOptions, SetCookie } from "cookie-es";
1+import type { CookieSerializeOptions } from "cookie-es";
22import type { H3Event, HTTPEvent } from "../event.ts";
33import { parse as parseCookie, serialize as serializeCookie, parseSetCookie } from "cookie-es";
44import { validateData } from "./internal/validate.ts";
@@ -19,7 +19,7 @@ const CHUNKS_MAX_LENGTH = 4000;
1919 * const cookies = parseCookies(event)
2020 * ```
2121 */
22-export function parseCookies(event: HTTPEvent): Record<string, string> {
22+export function parseCookies(event: HTTPEvent): Record<string, string | undefined> {
2323return parseCookie(event.req.headers.get("cookie") || "");
2424}
2525@@ -42,7 +42,7 @@ export function getValidatedCookies<Event extends HTTPEvent, S extends StandardS
4242export function getValidatedCookies<Event extends HTTPEvent, OutputT>(
4343event: Event,
4444validate: (
45-data: Record<string, string>,
45+data: Record<string, string | undefined>,
4646) => ValidateResult<OutputT> | Promise<ValidateResult<OutputT>>,
4747options?: { onError?: () => ErrorDetails },
4848): Promise<OutputT>;
@@ -95,10 +95,14 @@ export function setCookie(
9595}
96969797// Merge and deduplicate unique set-cookie headers
98-const newCookieKey = _getDistinctCookieKey(name, (options || {}) as SetCookie);
98+const newCookieKey = _getDistinctCookieKey(name, options || {});
9999event.res.headers.delete("set-cookie");
100100for (const cookie of currentCookies) {
101-const _key = _getDistinctCookieKey(cookie.split("=")?.[0], parseSetCookie(cookie));
101+const parsed = parseSetCookie(cookie);
102+if (!parsed) {
103+continue;
104+}
105+const _key = _getDistinctCookieKey(cookie.split("=")?.[0], parsed);
102106if (_key === newCookieKey) {
103107continue;
104108}
@@ -237,7 +241,7 @@ export function deleteChunkedCookie(
237241 *
238242 * @see https://httpwg.org/specs/rfc6265.html#rfc.section.4.1.2
239243 */
240-function _getDistinctCookieKey(name: string, options: Partial<SetCookie>) {
244+function _getDistinctCookieKey(name: string, options: { domain?: string; path?: string }) {
241245return [name, options.domain || "", options.path || "/"].join(";");
242246}
243247