Type-safe URL query string parsing and building
npm install @philiprehberger/query-string-tsType-safe URL query string parsing and building
npm install @philiprehberger/query-string-ts
import { parseQuery, buildQuery, appendQuery } from '@philiprehberger/query-string-ts';
const params = parseQuery('?page=2&tags=a&tags=b', {
page: 'number',
tags: 'string[]',
});
// { page: 2, tags: ['a', 'b'] } — typed
buildQuery({ page: 2, tags: ['a', 'b'] }); // "page=2&tags=a&tags=b"
appendQuery('https://example.com?a=1', { b: '2' }); // "https://example.com?a=1&b=2"
buildQuery({ tags: ['a', 'b'] }, { arrayFormat: 'comma' }); // "tags=a,b"
buildQuery({ tags: ['a', 'b'] }, { arrayFormat: 'brackets' }); // "tags[]=a&tags[]=b"
| Function | Description |
|---|---|
parseQuery(search, schema?) | Parse with optional type coercion |
buildQuery(params, options?) | Serialize object to query string |
appendQuery(url, params) | Append params to existing URL |
pickQuery(search, keys) | Extract subset of params |
omitQuery(search, keys) | Remove params by key |
npm install
npm run build
npm test
If you find this project useful: