From 0fd5fb3ab190ad9e544e3777f2fe73dc97698c42 Mon Sep 17 00:00:00 2001 From: buzzcode2007 <73412182+buzz-lightsnack-2007@users.noreply.github.com> Date: Thu, 20 Mar 2025 02:01:26 +0000 Subject: [PATCH] Add request parser and formatter --- scripts/RequestParser.js | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 scripts/RequestParser.js diff --git a/scripts/RequestParser.js b/scripts/RequestParser.js new file mode 100644 index 0000000..6d95a61 --- /dev/null +++ b/scripts/RequestParser.js @@ -0,0 +1,28 @@ +class RequestParser { + /** + Parse a new request. + + @param {object} REQUEST - The request object + */ + constructor(REQUEST) { + if (REQUEST ? (Object.keys(REQUEST).length) : false) { + this[`request`] = REQUEST + } + else {throw new Error()}; + } + + /** + Get and format the headers from the request. + + @returns {object} - The headers + */ + getHeaders() { + this[`headers`] = this[`request`][`headers`] + this[`headers`][`ipaddress`] = this[`request`][`ip`]; + this[`headers`][`software`] = this[`headers`][`user-agent`]; + this[`headers`][`language`] = this[`headers`]["accept-language"]; + return this[`headers`]; + } +} + +module.exports = {RequestParser}; \ No newline at end of file