datasquirel/dist/package-shared/utils/backend/parseCookies.js
Benjamin Toby e9bb9fb07f Updates
2025-02-19 20:01:50 +01:00

44 lines
1.4 KiB
JavaScript

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = parseCookies;
/**
* Parse request cookies
* ===================================================
*
* @description This function takes in a request object and
* returns the cookies as a JS object
*/
function parseCookies({ request, cookieString, }) {
var _a;
try {
/** @type {string | undefined} */
const cookieStr = request
? request.headers.cookie
: cookieString
? cookieString
: undefined;
if (!cookieStr)
return {};
if (!cookieStr || typeof cookieStr !== "string") {
return {};
}
const cookieSplitArray = cookieStr.split(";");
let cookieObject = {};
cookieSplitArray.forEach((keyValueString) => {
const [key, value] = keyValueString.split("=");
if (key && typeof key == "string") {
const parsedKey = key.replace(/^ +| +$/, "");
cookieObject[parsedKey] =
value && typeof value == "string"
? value.replace(/^ +| +$/, "")
: "";
}
});
return cookieObject;
}
catch (error) {
(_a = global.ERROR_CALLBACK) === null || _a === void 0 ? void 0 : _a.call(global, `Parse Cookies Error`, error);
return {};
}
}