From 758eb6d052f8aa57318e39889fbd3e396cfe003e Mon Sep 17 00:00:00 2001 From: Starbeamrainbowlabs Date: Wed, 5 Jun 2024 12:44:59 +0100 Subject: [PATCH] RouterContext: add conveience getter querystring --- CHANGELOG.md | 4 ++++ Server/RouterContext.mjs | 14 ++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 54ea31c..9f73593 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,10 @@ npm install --save powahroot ----- +## v1.2.8 +- Add convenience getter `RequestContext.querystring` + + ## v1.2.7 - Update dependencies to fix security issues diff --git a/Server/RouterContext.mjs b/Server/RouterContext.mjs index 3d842bb..2a2891e 100644 --- a/Server/RouterContext.mjs +++ b/Server/RouterContext.mjs @@ -1,6 +1,7 @@ "use strict"; import url from 'url'; +import querystring from 'querystring'; import Sender from './Sender.mjs'; import RequestEnvironment from './RequestEnvironment.mjs'; @@ -9,6 +10,19 @@ import RequestEnvironment from './RequestEnvironment.mjs'; * Contains context information about a single request / response pair. */ class RouterContext { + /** + * Returns the parsed querystring from the request url, or an empty object if no query string was found. + * + * NOTE FROM THE NODE.JS DOCS: The object returned by the querystring.parse() method [used in this getter] does not prototypically inherit from the JavaScript Object. This means that typical Object methods such as obj.toString(), obj.hasOwnProperty(), and others are not defined and will not work. + * + * @return {Object} The parsed query string as an object, or an empty object. + */ + get querystring() { + const qs = this.url.querystring; + if(qs.query == null) return {}; + return querystring.parse(qs); + } + constructor(in_request, in_response) { /** * The Node.JS request object