Skip to content

Recipes ​

Read and Convert Values ​

useRequestQuery(event) returns the whole query, useRequestQuery(event, key) one value. A value is a string, or an array when the key repeats, so a handler converts and bounds it itself.

ts
import { App, defineCoreHandler } from 'routup';
import { query, useRequestQuery } from '@routup/query';

export const app = new App();

app.use(query());

// GET /search?q=routup&limit=5&tag=http&tag=router
app.get('/search', defineCoreHandler((event) => {
    const all = useRequestQuery(event);

    // a value is never a number: convert and check it yourself
    const limit = Number.parseInt(useRequestQuery(event, 'limit') ?? '', 10);

    // one tag gives a string, several give an array
    const tags: string[] = [useRequestQuery(event, 'tag') ?? []].flat();

    return {
        all,
        q: useRequestQuery(event, 'q') ?? '',
        limit: Number.isSafeInteger(limit) && limit > 0 ? Math.min(limit, 100) : 20,
        tags,
    };
}));
bash
curl 'http://localhost:3000/search?q=routup&limit=5&tag=http&tag=router'
# {"all":{"q":"routup","limit":"5","tag":["http","router"]},"q":"routup","limit":5,"tags":["http","router"]}
curl 'http://localhost:3000/search?limit=abc&tag=http'
# {"all":{"limit":"abc","tag":"http"},"q":"","limit":20,"tags":["http"]}

Filters With Nested Keys ​

Bracket keys become objects, and [] appends to an array, as in JSON:API style filters.

ts
import { App, defineCoreHandler } from 'routup';
import { query, useRequestQuery } from '@routup/query';

export const app = new App();

app.use(query());

// GET /issues?filter[status]=open&filter[labels][]=bug&filter[labels][]=ui&fields[issue]=title
app.get('/issues', defineCoreHandler((event) => ({
    filter: useRequestQuery(event, 'filter'),
    fields: useRequestQuery(event, 'fields'),
})));
bash
curl -g 'http://localhost:3000/issues?filter[status]=open&filter[labels][]=bug&filter[labels][]=ui&fields[issue]=title'
# {"filter":{"status":"open","labels":["bug","ui"]},"fields":{"issue":"title"}}

curl -g sends the brackets as they are; browsers and HTTP clients usually percent-encode them, which qs decodes the same way.

Parser Options ​

parse is passed to qs. Here dots nest like brackets, commas split values, and the limits are tighter than the defaults.

ts
import { App, defineCoreHandler } from 'routup';
import { query, useRequestQuery } from '@routup/query';

export const app = new App();

app.use(query({
    parse: {
        // ?filter.status=open -> { filter: { status: 'open' } }
        allowDots: true,
        // ?ids=1,2,3 -> { ids: ['1', '2', '3'] }
        comma: true,
        depth: 2,
        parameterLimit: 50,
    },
}));

app.get('/issues', defineCoreHandler((event) => useRequestQuery(event)));
bash
curl 'http://localhost:3000/issues?filter.status=open&ids=1,2,3'
# {"filter":{"status":"open"},"ids":["1","2","3"]}
curl -g 'http://localhost:3000/issues?a[b][c][d]=x'
# {"a":{"b":{"c":{"[d]":"x"}}}}

Parameters beyond the fiftieth are dropped without an error.

Default a Missing Value ​

setRequestQuery() changes what later handlers read, here a page size for requests that sent none. The request URL stays as it was.

ts
import { App, defineCoreHandler } from 'routup';
import { query, setRequestQuery, useRequestQuery } from '@routup/query';

export const app = new App();

app.use(query());

// later handlers read a limit even when the client sent none
app.use(defineCoreHandler((event) => {
    if (typeof useRequestQuery(event, 'limit') === 'undefined') {
        setRequestQuery(event, 'limit', '20');
    }

    return event.next();
}));

app.get('/items', defineCoreHandler((event) => ({ limit: useRequestQuery(event, 'limit') })));
bash
curl http://localhost:3000/items
# {"limit":"20"}
curl 'http://localhost:3000/items?limit=5'
# {"limit":"5"}