This commit is contained in:
Benjamin Toby
2024-12-15 12:27:16 +01:00
parent d9d32a4643
commit bc037e839f
314 changed files with 10191 additions and 408 deletions
@@ -1,60 +1,58 @@
// @ts-check
/**
* ==============================================================================
* Imports
* ==============================================================================
*/
const http = require("http");
/**
* Parse request cookies
* ==============================================================================
* ===================================================
*
* @description This function takes in a request object and returns the cookies as a JS object
* @description This function takes in a request object and
* returns the cookies as a JS object
*
* @async
*
* @param {object} params - main params object
* @param {http.IncomingMessage} params.request - HTTPS request object
* @param {http.IncomingMessage & Object<string, any>} [params.request] - HTTPS request object
* @param {string} [params.cookieString]
*
* @returns {any | null}
* @returns {Object<string, string>}
*/
module.exports = function ({ request }) {
/**
* Check inputs
*
* @description Check inputs
*/
module.exports = function parseCookies({ request, cookieString }) {
try {
/** @type {string | undefined} */
const cookieStr = request
? request.headers.cookie
: cookieString
? cookieString
: undefined;
/** @type {string | undefined} */
const cookieString = request.headers.cookie;
if (!cookieStr) return {};
if (!cookieString || typeof cookieString !== "string") {
return null;
}
/** @type {string[]} */
const cookieSplitArray = cookieString.split(";");
/** @type {*} */
let cookieObject = {};
cookieSplitArray.forEach((keyValueString) => {
const [key, value] = keyValueString.split("=");
if (key && typeof key == "string") {
cookieObject[key.replace(/^ +| +$/, "")] =
value && typeof value == "string"
? value.replace(/^ +| +$/, "")
: null;
if (!cookieStr || typeof cookieStr !== "string") {
return {};
}
});
/**
* Make https request
*
* @description make a request to datasquirel.com
*/
/** @type {string[]} */
const cookieSplitArray = cookieStr.split(";");
return cookieObject;
/** @type {Object<string, string>} */
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 (/** @type {any} */ error) {
console.log(`ERROR parsing cookies: ${error.message}`);
return {};
}
};