Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/henry781/querysearch
URL QueryParams parsing
https://github.com/henry781/querysearch
Last synced: 1 day ago
JSON representation
URL QueryParams parsing
- Host: GitHub
- URL: https://github.com/henry781/querysearch
- Owner: henry781
- Created: 2019-03-02T14:11:26.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2022-04-10T03:24:46.000Z (over 2 years ago)
- Last Synced: 2024-10-08T19:16:08.800Z (about 1 month ago)
- Language: TypeScript
- Homepage:
- Size: 79.1 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# QuerySearch
This library translates URL query params to a standard object including filter, sort, limit and offset.
let's consider this request url:
```
http://api/planets?
filter=star=sun&
filter=type=terrestrial&
filter=name[regex]=/^e/&
sort=name=ASC&
offset=0&
limit=10
```Express.js or some other frameworks will produce a queryParams object:
```
const queryparams = {
filter: [ 'star=sun', 'type=terrestrial', name[regex]=/^e/' ],
sort: ['name=ASC'],
offset: 0,
limit: 10
}
```QuerySearch can parse each queryParam and returns:
```
const querySearch = QuerySearch.fromQueryParams(queryParams);
console.log(querySearch);
{
filter: {
star: { $eq: 'sun' },
type: { $eq: 'terrestrial' },
name: { $regex: /^e/ }
},
sort: {
name: 1
},
offset: 0,
limit: 10
}
```It returns a clean search object you can use in your app!
Filter object is based on mongodb operators but there is no dependency with mongodb.
## Filter
### Simple filters
``
/planets?filter=star=sun&filter=type=terrestrial
``will produce:
```
{
star: { $eq: 'sun' },
type: { $eq: 'terrestrial' }
}
```### Operators
``
/planets?filter=name[regex]=/^ea/
``will produce:
```
{
name: { $regex: /^ea/ }
}
```## Sort
``
/planets?sort=name=ASC
``will produce:
```
{
name: 1
}
```## Limit & Offset
``
/planets?limit=10&offset=20
``will produce:
```
{
limit: 10,
offset: 20
}
```