dsql-admin/dsql-app/package-shared/utils/serialize-cookies.js

49 lines
1.3 KiB
JavaScript
Raw Normal View History

2024-12-15 11:27:16 +00:00
// @ts-check
/**
*
* @param {object} params
* @param {import("../types").CookieObject[]} params.cookies
* @returns {string[]}
*/
function serializeCookies({ cookies }) {
/** @type {string[]} */
let cookiesStringsArray = [];
for (let i = 0; i < cookies.length; i++) {
const cookieObject = cookies[i];
let cookieString = `${cookieObject.name}=${cookieObject.value}`;
if (cookieObject.maxAge) {
cookieString += `;Max-Age=${cookieObject.maxAge}`;
}
if (cookieObject.path) {
cookieString += `;Path=${cookieObject.path}`;
}
if (cookieObject.domain) {
cookieString += `;Domain=${cookieObject.domain}`;
}
if (cookieObject.secure) {
cookieString += ";Secure";
}
if (cookieObject.httpOnly) {
cookieString += ";HttpOnly";
}
if (cookieObject.sameSite) {
cookieString += `;SameSite=${cookieObject.sameSite}`;
}
if (cookieObject.expires) {
cookieString += `;expires=${cookieObject.expires}`;
}
if (cookieObject.priority) {
cookieString += `;priority=${cookieObject.priority}`;
}
cookiesStringsArray.push(cookieString);
}
return cookiesStringsArray;
}
module.exports = serializeCookies;