-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
48 lines (43 loc) · 1.19 KB
/
index.js
File metadata and controls
48 lines (43 loc) · 1.19 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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
module.exports = {
useDrupalJSONAPI: async (options) => {
let {
baseURL = "",
collection = "",
include = [],
sortBy = "",
apiBase = "jsonapi",
} = options;
if (include.length !== 0) {
if (include.length === 1) {
include = `?include=${include}`;
} else {
let tempString = "";
include.forEach((element, index) => {
if (index !== 0) {
tempString += `.${element}`;
}
});
include = `?include=${include[0] + tempString}`;
}
}
let filters = `${include}${sortBy !== "" ? `&sort=${sortBy}` : ""}`;
if (include.length === 0 && sortBy !== "") {
filters = `?sort=${sortBy}`;
}
let fullURL = `${baseURL}/${apiBase}${
collection === "" ? "" : `/node/${collection}${filters}`
}`;
let response = await fetch(fullURL);
if (response.status === 200) {
let data = await response.json();
return data;
}
if (response.status === 404) {
return { error: "Could not locate the requested information." };
} else {
return {
error: "Uh oh! Something went wrong, please double check your options.",
};
}
},
};