-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfind-items.ts
More file actions
31 lines (29 loc) · 1.18 KB
/
find-items.ts
File metadata and controls
31 lines (29 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/**
* Find all corresponding relationship items
*
* @param {Object} response - JSON:API styled response from the server.
* @param {Object} temp - Relationship data field
* @throws {Error} A data relationship item does not contain type field.
* @throws {Error} A data relationship item does not contain id field.
* @return {Object} result - Included items grouped by their type
*/
export function findItems(response: any, temp: any) {
// Checking if temp has type field
if (!temp.hasOwnProperty('type')) {
throw '[Normalize] [JSON:API Syntax Error] A data relationship item does not contain type field!';
}
// Checking if temp has id field
if (!temp.hasOwnProperty('id')) {
throw '[Normalize] [JSON:API Syntax Error] A data relationship item does not contain id field!';
}
const result = [];
// If response has a 'included' property then look for results
if (response.hasOwnProperty('included')) {
// Adding all found results
result.push(
response.included.find((item: any) => (String(item.id) === String(temp.id)) &&
(String(item.type) === String(temp.type))),
);
}
return result;
}