auth0 / node-odata-parser

OData query string parser for node.js.
MIT License
105 stars 72 forks source link

Parse URL path according to the OData specification too #12

Open prantlf opened 9 years ago

prantlf commented 9 years ago

Breaking the path into resources and predicates would be helpful too when processing the OData URL, because hre can be expressions inside the path. For example, /Customers(123)/ContactName could be parsed into a JSON object with a schema similar to the query parsing output:

[
  {
    "name": "Customers",
    "predicates": [
      {
        "type": "literal",
        "value": 123
      }
    ]
  },
  {
    "name": "ContactName"
  }
]

Another example for /Orders(CustomerID=123,Pending=true) could be:

[
  {
    "name": "Orders",
    "predicates": [
      {
        "type": "property",
        "name": "CustomerID",
        "value": 123
      },
      {
        "type": "property",
        "name": "Pending",
        "value": true
      }
    ]
  }
]

There could be either a separate parsing function for the path, or the current odata-parser module could support both; the path with an optional query or the query alone.

I tried the former in the parse-path-separate branch with the extended interface in lib/index.js:

module.exports = {
  parse: require('./odata-parser').parse,
  parsePath: require('./odata-path-parser').parse
};

I could not find a real advantage of having handling the two URL parts separated. A disadvantage was duplicating a big part of the PEG grammar in the URL path handling file. Parsing can handle all three possibilities (path, path + query, query) with a simpler interface (just a single parse method) and the only challenge is how to return the path without breaking the current interface. I tried adding a new key - $path - to the output map in the parse-path-integrated branch; it is not a reserved OData field and the parse method has a common result schema:

{
  "$path": [
    // array of resources from the first and second samples above
  ],
  "$select": [
    "Name"
  ],
  ...
}

What do you think? Thanks!

jfromaniello commented 9 years ago

Yes, it seems a very good idea.

htammen commented 8 years ago

Hi, as far as I see this has not been implemented yet. Am I right?