Skip to content
Open
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
16 changes: 8 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,37 +253,37 @@ export function serialize(
const enc = options?.encode || encodeURIComponent;

if (!cookieNameRegExp.test(name)) {
throw new TypeError(`argument name is invalid: ${name}`);
throw new TypeError(`Argument "name" is invalid: ${name}. It does not match the cookie name regular expression ${cookieNameRegExp}.`);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's try to minimally change the error message, otherwise this may break someone string matching. Something like:

argument name is invalid: ${name}, should match ${cookieNameRegExp}

Ditto for all the other errors too, if we can make it compact and keep the formatting/start of the error the same happy to land this in a patch/minor.

}

const value = enc(val);

if (!cookieValueRegExp.test(value)) {
throw new TypeError(`argument val is invalid: ${val}`);
throw new TypeError(`Argument "val" is invalid: ${val}. It does not match the cookie value regular expression ${cookieValueRegExp}.`);
}

let str = name + "=" + value;
if (!options) return str;

if (options.maxAge !== undefined) {
if (!Number.isInteger(options.maxAge)) {
throw new TypeError(`option maxAge is invalid: ${options.maxAge}`);
throw new TypeError(`Option "maxAge" is invalid: ${options.maxAge}. Provided value is not an integer.`);
}

str += "; Max-Age=" + options.maxAge;
}

if (options.domain) {
if (!domainValueRegExp.test(options.domain)) {
throw new TypeError(`option domain is invalid: ${options.domain}`);
throw new TypeError(`Option "domain" is invalid: ${options.domain}. It does not match the domain value regular expression ${domainValueRegExp}.`);
}

str += "; Domain=" + options.domain;
}

if (options.path) {
if (!pathValueRegExp.test(options.path)) {
throw new TypeError(`option path is invalid: ${options.path}`);
throw new TypeError(`Option "path" is invalid: ${options.path}. It does not match the path value regular expression ${pathValueRegExp}.`);
}

str += "; Path=" + options.path;
Expand All @@ -294,7 +294,7 @@ export function serialize(
!isDate(options.expires) ||
!Number.isFinite(options.expires.valueOf())
) {
throw new TypeError(`option expires is invalid: ${options.expires}`);
throw new TypeError(`Option "expires" is invalid: ${options.expires}`);
}

str += "; Expires=" + options.expires.toUTCString();
Expand Down Expand Up @@ -328,7 +328,7 @@ export function serialize(
str += "; Priority=High";
break;
default:
throw new TypeError(`option priority is invalid: ${options.priority}`);
throw new TypeError(`Option "priority" is invalid: ${options.priority}. Allowed string values: low, medium, high.`);
}
}

Expand All @@ -349,7 +349,7 @@ export function serialize(
str += "; SameSite=None";
break;
default:
throw new TypeError(`option sameSite is invalid: ${options.sameSite}`);
throw new TypeError(`Option "sameSite" is invalid: ${options.sameSite}. Allowed string values: strict, lax, none.`);
}
}

Expand Down
Loading