menu
Menu
Mobify DevCenter
search_icon_focus

Ssr Request Processing

progressive-web-sdk/dist/utils/ssr-request-processing
class

QueryParameters

A class that represents and can manipulate a parsed query string as a subclass of Array.

The values in the Array are objects.

The values in the Array are objects with ‘key’, ‘originalKey’ ‘value’ and ‘originalValue’. The ‘key’ is decoded (from %-encoding) and lowercased. The ‘originalKey’ is as it originally occurred in the query string. It’s not decoded or lowercased. The ‘originalValue’ is as it originally occurred in the query string. It’s not decoded. The ‘value’ is decoded (from %-encoding).

If the query parameter is ‘key=value’, all fields are set. If the query parameter is ‘key=’ then ‘value’ and ‘originalValue’ will be the empty string. If the query parameter is just ‘key’ then ‘value’ and ‘originalValue’ will be null. If the query parameter is empty (double-&) then ‘key’ and ‘originalKey’ will be '', ‘value’ and ‘originalValue’ will be null.

It’s important that we PRESERVE the order of the parameters because some projects rely on it. The ‘querystring’ module assigns values to the resulting object in the order that it encounters them in the input string. Since ES2015 there are specific rules defining the order of properties, and apart from one particular case the order is chronological, which is what we want. The order of properties in an object depends on the type of the included properties and their values. Unfortunately, integer indices always come first, and the rules state that “an integer index is a string that, if converted to a 53-bit non-negative integer and back is the same value”. This means that any parameter whose key is an integer index will appear out of order in the result. Thus we can’t use the ‘querystring’ module.

Import
import { QueryParameters } from 'progressive-web-sdk/dist/utils/ssr-request-processing

Methods

parameters

Access the array of parameters owned by this QueryParameters object.


keys

An Array of all the keys (as lowercased, unescaped strings)


deleteByKey(key)

Delete any query parameters with the given key. Key names are matched in a case-insensitive way.

Returns:

Boolean - true if a parameter was deleted, false if not.

ParameterType
keyString

appendParameter(key, value)

Add a query parameter to the array (at the end)

ParameterTypeDefaultDescription
keyStringParameter name (key)
valueStringParameter value (may be omitted or set to null to store only a key).

toString()

Return a querystring built from the parameters in this instance, using the originalKey and originalValue properties of each parameter.

Any strings that this class escapes will be correctly encoded (using %20 for space). There is an issue with input querystrings that are supposed to be encoded correctly, but which contain space characters (in either the key or the value). We don’t want to convert them on input because that might cause the querystring as represented by this class to differ from the actual input. Instead, we define the behaviour of this function such that it will rebuild the querystring exactly as input EXCEPT that any spaces will be +-encoded.

Returns:

string


from()

Construct a new QueryParameters from the output of any filtering or mapping done on another QueryParameters instance’s ‘parameters’ array.

Returns:

QueryParameters