Skip to content
Snippets Groups Projects
Commit 3887126b authored by Marin Karamihalev's avatar Marin Karamihalev
Browse files

resolved query issue for get and getNested

parent 840a120a
No related branches found
No related tags found
1 merge request!5Rebase with master
{
"name": "usp-js",
"version": "0.3.5",
"version": "0.3.6",
"description": "Helper library for easy usp communication using mqtt over tcp or ws.",
"main": "node/index.js",
"browser": "web/index.js",
......
......@@ -186,10 +186,13 @@ export const parseID = (msg: any) => {
const grab = (item: any, key: string) =>
Array.isArray(item)
? item.find((val) => val !== null && val !== undefined)
? isDigit(key)
? item.find((val) => val !== null && val !== undefined)
: item
: key === "-1"
? item.find((v) => v !== null)
: item[key];
const skipKeys = (obj: any, keys: string[]) =>
keys.length === 1
? grab(obj, keys[0])
......@@ -213,6 +216,13 @@ const removeNulls = (obj: any) =>
const pathJoin = (a: string, b: string) =>
a.endsWith(".") ? a + b : a + "." + b;
const cutQueryElements = (path: string): string =>
path
.split(/\[.*\]./)
.join("")
.split(/\*\./)
.join("");
const addQueries = (obj: any, path: string) =>
typeof obj !== "object"
? obj
......@@ -225,7 +235,7 @@ const addQueries = (obj: any, path: string) =>
)
.filter((v) => v !== null)
: {
__query__: path,
__query__: cutQueryElements(path),
...Object.entries(obj).reduce(
(acc, [key, val]) => ({
...acc,
......@@ -238,13 +248,6 @@ const addQueries = (obj: any, path: string) =>
),
};
const locatePath = (result: PathResult): string =>
result.resolvedPathResults?.reduce((shortestRes, currentRes) =>
currentRes.resolvedPath.length < shortestRes.resolvedPath.length
? currentRes
: shortestRes
).resolvedPath || result.requestedPath;
export const processGetResult = (
reqPathResults: PathResult[],
decodeOptions?: DecodeOptions
......@@ -261,6 +264,7 @@ export const processGetResult = (
? result
: ({ __query__: resolvedResult.requestedPath, result } as UspProperty);
}
if (respType === ResponseType.PropertyList) {
const result = results.map(
(result) => Object.values(result.resultParams)[0]
......@@ -277,22 +281,21 @@ export const processGetResult = (
})),
} as UspPropertyList);
}
if (
respType === ResponseType.Object ||
respType === ResponseType.ObjectList
) {
const actualPath = containsQuery(resolvedResult.requestedPath)
? locatePath(resolvedResult)
: resolvedResult.requestedPath;
const keys = actualPath.split(".").slice(0, -1);
const path = resolvedResult.requestedPath;
const keys = path.split(".").slice(0, -1);
const nestedObject = convertToNestedObject(results);
const mainObject = skipKeys(nestedObject, keys);
const cleanedObject = removeNulls(mainObject);
return !retainPath
? cleanedObject
: {
__query__: resolvedResult.requestedPath,
result: addQueries(mainObject, actualPath),
__query__: path,
result: addQueries(mainObject, path),
};
}
......
{
"ws": {
"host": "iopsys.lan",
"host": "localhost",
"username": "admin",
"password": "admin",
"port": 9001,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment