A JavaScript library for building and encoding query strings.
npm install ts-query-string
import { QueryStringBuilder } from "ts-query-string";
const qs = QueryStringBuilder()
.add({ name: "param1", value: "value1" })
.add({ name: "param2", value: 2 })
.add({ name: "param3", value: "value3" })
.add({ name: "param4", value: "value4 with spaces" });
.build();
type QS = typeof qs; // QS = "param1=value1¶m2=2¶m3=value3¶m4=value4%20with%20spaces"
console.log(qs); // QS = "param1=value1¶m2=2¶m3=value3¶m4=value4%20with%20spaces"
QueryStringBuilder
A class for building a query string.
Add a name-value pair to the query string.
function add<Name extends string, Value extends ResolveableQueryValue>(input: {
name: Name;
value: Value;
}): QueryStringBuilder<[...T, typeof input]>;
Build the query string from the name-value pairs.
function build(): EncodeQueryString<T>;
A type that represents a query string encoded from the name-value pairs of type T.
type EncodeQueryString<T extends QueryValue[]> = ...
A type that represents the result of encoding a ResolveableQueryValue type.
type EncodeUri<T extends ResolveableQueryValue> = ...
A type that represents a value that can be resolved to a string.
type ResolveableQueryValue = string | number | boolean;
- The
add
method returns a new instance ofQueryStringBuilder
with the new name-value pair added to the type parameterT
. - If you insert a value that is not a
ResolveableQueryValue
type, the compiler will throw an error. - This package is written in TypeScript and the types are exported.
- If somehow you get an
Unecodeable<{character}>
in your query string type please open an issue or pull request.