-
Notifications
You must be signed in to change notification settings - Fork 0
jsonParameter parser works counterintuitive #1
Comments
@souljorje how would you specify this path: |
{
value: {
field: {
array: {
'[*]': {
field2: {
eq: 'foo',
// also I could specify at one place i.e.
ne: 'bar',
// etc
}
}
}
}
}
} |
Actually, would be cool to use proxy instead, and just write it like |
@Groxan my suggestion is kinda mongodb query syntax: nested & intuitive imho 🤷♂️ proxy for what? |
Proxy is used for creating dynamic object, so that you can "access" fields that are not defined. Nesting is good because you can merge multiple parameters (paths) into a single object, but it's less handy to write, especially in case of long and complex paths. Anyway, I don't object. Just my thoughts. |
Another issue that I see is constructing a query for a response object, that has value: {
someField: {
in: {
in: string[]
}
}
} No sure if it improves readability, not mentioning the complexity of creating a QS parser for such parameter. |
Another issue is creating a type for this parameter. The difficulty would be describing a Record of Records of unknown depth with the deepest level always being strongly typed to available |
@souljorje this can be done only knowing the particular contract storage type. We will definitely implement that in TzKT wrappers |
Here's a parser for suggested syntax. const isPlainObject = (v) => {
if (Object.prototype.toString.call(v) !== '[object Object]') return false;
const prototype = Object.getPrototypeOf(v);
return prototype === null || prototype === Object.prototype;
};
const objectToQueryObject = (obj, path) => {
return Object.entries(obj).reduce((acc, [key, value]) => {
const newPath = path ? `${path}.${key}` : key;
if (isPlainObject(value)) {
return {
...acc,
...objectToQueryObject(value, newPath)
};
}
acc[newPath] = value;
return acc;
}, {})
};
const queryObjectRaw = {
value: {
field: {
array: {
'[*]': {
field2: {
eq: 'foo',
ne: 'bar',
}
}
},
someOtherProp: {
in: [1, 2, 3],
ne: 'baz',
}
},
someOtherField: {
gt: 123
},
},
otherValue: {
eq: 2
},
};
const result = objectToQueryObject(queryObjectRaw);
console.log('result', result);
const queryString = new URLSearchParams(result).toString();
console.log('queryString', queryString); |
I'm submitting a ...
feature request
Summary
I wish it works like this:
I suggest to recursively create
that.kind.of.path
from object with last nested property as value. And also I suggest to usereduce
for declarativeness.The text was updated successfully, but these errors were encountered: