Parameters

The Parameters class represents a collection of key-value pairs used for representing URL parameters. It provides methods for working with URL parameters. Paramaters are automatically parsed to there expected types, this includes numbers and booleans. This class is very similar to the standard URLSearchParams class used in web APIs.

When parameters come from the Request class, from a URL they are automatically parsed (booleans and numbers) and seperated by commas. See request examples, for more information.


Constructor

constructor(init)

constructor() Creates a new instance of the Parameters class.


Methods

has(key)

has(key: string): boolean
Checks if a parameter with the specified key exists.

  • key - The key of the parameter to check.

get(key)

get(key: string): parameterValue | undefined
Returns the first value of the parameter with the specified key.

  • key (string): The key of the parameter.

getAll()

getAll(key: string): parameterValue[] | undefined
Returns all values of the parameter with the specified key.

  • key (string): The key of the parameter.

keys()

keys(): string[]
Returns an array of all the keys present in the parameters.


toJSON()

toJSON(): Record<string, parameterValue[]>
Returns a JSON representation of the parameters.


Examples

The following examples, illustrate how to construct a Paramaters class, append and retreive values from the object.

import { Paramaters } from "sherpa-core";
const _params = new Paramaters();

_params.append("topics", "AI");
_params.get("topics"); // returns "AI"
_params.getAll("topics"); // returns ["AI"]

_params.append("topics", "code");
_params.get("topics"); // returns "AI"
_params.getAll("topics"); // returns ["AI", "code"]

_params.append("page", "1");
_params.get("page"); // returns 1
_params.getAll("page"); // returns [1]

_params.append("isCool", "true");
_params.get("isCool"); // returns true
_params.getAll("isCool"); // returns [true]