A Typescript library for query strings.
npm install aka-query-lib
This library simplifies working with query strings, dynamically typechecks them and enforces that they match a query interface.
See an example at example/example.ts
The user must instantiate a QuerySpec class from the following:
- A query interface, which lists all desired parameters to a query
- A type environment, which defines at least all types in the interface By default a type environment for basic plain-old-data types is provided for the user. Using the QuerySpec, the user can parse query strings and turn querys back into strings.
A query interface is a list of 3/4 tuples of the form:
- The name of the parameter (must be unique)
- The type of the parameter (must exist in the associated type environment)
- The default value of the parameter
- A function which constrains the possible values of the parameter (OPTIONAL)
A query type environment is a Map from type names (strings) to a tuple defining the type T with two functions:
- string -> T: where we convert a string to T and typecheck, this can fail so the return type is an Option type.
- T -> string: we convert the type T into a string so it can be embedded into a query string.
A query spec ensures the following properties:
- A query made from any query string is guaranteed to adhere to the spec
- Likewise a query string made from any query is guaranteed to adhere to the spec
Adherence to the spec is defined as:
- Returning zero or more parameters listed in the interface
- Excluding all non-conforming parameters
Parameters can be non-conforming if:
- They are missing from the query
- They fail their type check
- They fail their constraint check
In summary, the QuerySpec sanitizes all queries and forces adherence an interface and type environment