diff --git a/src/index.ts b/src/index.ts index 51a1435..6f32264 100644 --- a/src/index.ts +++ b/src/index.ts @@ -293,24 +293,25 @@ export function stringifySetCookie( _val?: string | StringifyOptions, _opts?: SerializeOptions, ): string { - const cookie = - typeof _name === "object" - ? _name - : { ..._opts, name: _name, value: String(_val) }; + const cookie = typeof _name === "object" ? _name : _opts; + const name = typeof _name === "object" ? _name.name : _name; + const rawValue = typeof _name === "object" ? _name.value : String(_val); const options = typeof _val === "object" ? _val : _opts; const enc = options?.encode || encodeURIComponent; - if (!cookieNameRegExp.test(cookie.name)) { - throw new TypeError(`argument name is invalid: ${cookie.name}`); + if (!cookieNameRegExp.test(name)) { + throw new TypeError(`argument name is invalid: ${name}`); } - const value = cookie.value ? enc(cookie.value) : ""; + const value = rawValue ? enc(rawValue) : ""; if (!cookieValueRegExp.test(value)) { - throw new TypeError(`argument val is invalid: ${cookie.value}`); + throw new TypeError(`argument val is invalid: ${rawValue}`); } - let str = cookie.name + "=" + value; + let str = name + "=" + value; + + if (!cookie) return str; if (cookie.maxAge !== undefined) { if (!Number.isInteger(cookie.maxAge)) {