2
0
mirror of https://github.com/tenrok/axios.git synced 2026-06-20 20:00:40 +03:00

refactor: improve Cookie Handling (#7129)

* Improve Cookie Handling

* Update lib/helpers/cookies.js

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update lib/helpers/cookies.js

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

---------

Co-authored-by: Jay <jasonsaayman@gmail.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
Amit Verma
2025-10-11 14:13:34 +05:30
committed by GitHub
parent 34ed58167f
commit f1c17be4a9
+22 -11
View File
@@ -5,27 +5,38 @@ export default platform.hasStandardBrowserEnv ?
// Standard browser envs support document.cookie // Standard browser envs support document.cookie
{ {
write(name, value, expires, path, domain, secure) { write(name, value, expires, path, domain, secure, sameSite) {
const cookie = [name + '=' + encodeURIComponent(value)]; if (typeof document === 'undefined') return;
utils.isNumber(expires) && cookie.push('expires=' + new Date(expires).toGMTString()); const cookie = [`${name}=${encodeURIComponent(value)}`];
utils.isString(path) && cookie.push('path=' + path); if (utils.isNumber(expires)) {
cookie.push(`expires=${new Date(expires).toUTCString()}`);
utils.isString(domain) && cookie.push('domain=' + domain); }
if (utils.isString(path)) {
secure === true && cookie.push('secure'); cookie.push(`path=${path}`);
}
if (utils.isString(domain)) {
cookie.push(`domain=${domain}`);
}
if (secure === true) {
cookie.push('secure');
}
if (utils.isString(sameSite)) {
cookie.push(`SameSite=${sameSite}`);
}
document.cookie = cookie.join('; '); document.cookie = cookie.join('; ');
}, },
read(name) { read(name) {
const match = document.cookie.match(new RegExp('(^|;\\s*)(' + name + ')=([^;]*)')); if (typeof document === 'undefined') return null;
return (match ? decodeURIComponent(match[3]) : null); const match = document.cookie.match(new RegExp('(?:^|; )' + name + '=([^;]*)'));
return match ? decodeURIComponent(match[1]) : null;
}, },
remove(name) { remove(name) {
this.write(name, '', Date.now() - 86400000); this.write(name, '', Date.now() - 86400000, '/');
} }
} }