Introducing Socket Firewall: Free, Proactive Protection for Your Software Supply Chain.Learn More
Socket
Book a DemoInstallSign in
Socket

odata-qs

Package Overview
Dependencies
Maintainers
0
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

odata-qs

An OData compliant querystring parser

latest
Source
npmnpm
Version
0.1.0
Version published
Weekly downloads
551
138.53%
Maintainers
0
Weekly downloads
 
Created
Source

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'

Keywords

odata

FAQs

Package last updated on 03 Feb 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts