Skip to main content

Query Handling

You can pass query parameters as either a raw query string or a query object.

Object

const queryByObjectEndpoint = API.endpoint()
.queryOf<{
foo: string;
bar: number;
}>()
.build({
id: "query-by-object",
method: "get",
path: "/query-by-object",
});

queryByObjectEndpoint.submit({
query: {
foo: "bar",
bar: 12,
},
});

String

const queryByStringEndpoint = API.endpoint()
.queryOf<string>()
.build({
id: "query-by-string",
method: "get",
path: "/query-by-string",
});

queryByStringEndpoint.submit({
query: "foo=bar&bar=12",
});

Custom Query Handling

You can pass a custom query stringifier/parser with the queryHandling option:

const API = new Api({
baseUrl: "https://example.com",
queryHandling: {
stringifier: (query) => {
// stringify query object to string
},
parse: (query) => {
// parse query string to object
},
},
});

Eg. to use the qs library:

import * as  qs from "qs";

const API = new Api({
baseUrl: "https://example.com",
queryHandling: {
stringifier: qs.stringify,
parse: qs.parse,
},
});

const queryByObjectEndpoint = API.endpoint()
.queryOf<{
ids: string[]
}>()
.build({
id: "query-by-string",
method: "get",
path: "/query-by-string",
});

queryByObjectEndpoint.submit({
query: {
ids: ["foo", "bar"],
},
});