UNPKG

1.56 kBJavaScriptView Raw
1import { combine, hOP, isUrlAbsolute } from "@pnp/core";
2import { extractWebUrl } from "./extract-web-url.js";
3export function odataUrlFrom(candidate) {
4 const parts = [];
5 const s = ["odata.type", "odata.editLink", "__metadata", "odata.metadata", "odata.id"];
6 if (hOP(candidate, s[0]) && candidate[s[0]] === "SP.Web") {
7 // webs return an absolute url in the id
8 if (hOP(candidate, s[4])) {
9 parts.push(candidate[s[4]]);
10 }
11 else if (hOP(candidate, s[2])) {
12 // we are dealing with verbose, which has an absolute uri
13 parts.push(candidate.__metadata.uri);
14 }
15 }
16 else {
17 if (hOP(candidate, s[3]) && hOP(candidate, s[1])) {
18 // we are dealign with minimal metadata (default)
19 // some entities return an abosolute url in the editlink while for others it is relative
20 // without the _api. This code is meant to handle both situations
21 const editLink = isUrlAbsolute(candidate[s[1]]) ? candidate[s[1]].split("_api")[1] : candidate[s[1]];
22 parts.push(extractWebUrl(candidate[s[3]]), "_api", editLink);
23 }
24 else if (hOP(candidate, s[1])) {
25 parts.push("_api", candidate[s[1]]);
26 }
27 else if (hOP(candidate, s[2])) {
28 // we are dealing with verbose, which has an absolute uri
29 parts.push(candidate.__metadata.uri);
30 }
31 }
32 if (parts.length < 1) {
33 return "";
34 }
35 return combine(...parts);
36}
37//# sourceMappingURL=odata-url-from.js.map
\No newline at end of file