odata-qs
v0.0.4
Published
An OData compliant querystring parser
Downloads
1,726
Readme
odata-qs
An OData compliant querystring parser and serializer
Usage
import { parse } from "odata-qs"
const query = parse(
`(name eq 'Jacob' or name eq 'John') and age gt 18 and age lt 65`,
"and"
)
{
age: {
gt: {
operator: "gt",
subject: "age",
values: [18],
},
lt: {
operator: "lt",
subject: "age",
values: [65],
},
},
name: {
eq: {
operator: "eq",
subject: "name",
values: ["Jacob", "John"],
},
},
}
If you want a type-safe result, you can pass a third argument as an array of allowed subjects. If the query contains a subject that isn't in the array, it will throw an error at runtime, and during development you'll get full intellisense support.
import { parse } from "odata-qs"
const filter = parse(
`(name eq 'Jacob' or name eq 'John') and age gt 18 and age lt 65`,
"and",
["name", "age"]
)
filter.name // ✅
filter.age // ✅
filter.foo // Property 'foo' does not exist on type Record<'name' | 'age', …>
Serializing a filter expects a structured object with subject, operator, and value properties.
import { serialize } from "odata-qs"
serialize({
subject: "name",
operator: "eq",
value: "Jacob",
}) // name eq 'Jacob'
serialize({
subject: {
subject: "name",
operator: "eq",
value: "Jacob",
},
operator: "or",
value: {
subject: "name",
operator: "eq",
value: "John",
},
}) // name eq 'Jacob' or name eq 'John'