Skip to content
Snippets Groups Projects
Commit 126c430f authored by Bekerle, Patrizio's avatar Bekerle, Patrizio :fire: Committed by Reiter, Christoph
Browse files

Implement support for reloading JSONLD after vpu-auth-init event

parent 81a1d80f
No related branches found
No related tags found
No related merge requests found
...@@ -45,79 +45,84 @@ export default class JSONLD { ...@@ -45,79 +45,84 @@ export default class JSONLD {
initStarted[apiUrl] = true; initStarted[apiUrl] = true;
// window.VPUAuthToken will be set by on vpu-auth-init if (window.VPUAuthToken !== undefined) {
window.addEventListener("vpu-auth-init", function(e) JSONLD.doInitialization(apiUrl);
{ } else {
const xhr = new XMLHttpRequest(); // window.VPUAuthToken will be set by vpu-auth-init event
xhr.open("GET", apiUrl, true); window.addEventListener("vpu-auth-init", () => JSONLD.doInitialization(apiUrl));
xhr.setRequestHeader('Authorization', 'Bearer ' + window.VPUAuthToken); }
}
xhr.onreadystatechange = function () {
if (xhr.readyState === 4 && xhr.status === 200) { static doInitialization(apiUrl) {
const json = JSON.parse(xhr.responseText); const xhr = new XMLHttpRequest();
xhr.open("GET", apiUrl, true);
let entryPoints = {}; xhr.setRequestHeader('Authorization', 'Bearer ' + window.VPUAuthToken);
for (let property in json) {
// for some reason the properties start with a lower case character xhr.onreadystatechange = function () {
if (!property.startsWith("@")) entryPoints[property.toLowerCase()] = json[property]; if (xhr.readyState === 4 && xhr.status === 200) {
} const json = JSON.parse(xhr.responseText);
// read the link header of the api response let entryPoints = {};
// const utils = require("./utils"); for (let property in json) {
const links = utils.parseLinkHeader(this.getResponseHeader("link")); // for some reason the properties start with a lower case character
if (!property.startsWith("@")) entryPoints[property.toLowerCase()] = json[property];
// get the hydra apiDocumentation url }
const apiDocUrl = links["http://www.w3.org/ns/hydra/core#apiDocumentation"];
// read the link header of the api response
if (apiDocUrl !== undefined) { // const utils = require("./utils");
// load the hydra apiDocumentation const links = utils.parseLinkHeader(this.getResponseHeader("link"));
const docXhr = new XMLHttpRequest();
docXhr.open("GET", apiDocUrl, true); // get the hydra apiDocumentation url
docXhr.setRequestHeader("Content-Type", "application/json"); const apiDocUrl = links["http://www.w3.org/ns/hydra/core#apiDocumentation"];
docXhr.onreadystatechange = function () {
if (docXhr.readyState === 4 && docXhr.status === 200) { if (apiDocUrl !== undefined) {
const json = JSON.parse(docXhr.responseText); // load the hydra apiDocumentation
const supportedClasses = json["hydra:supportedClass"]; const docXhr = new XMLHttpRequest();
docXhr.open("GET", apiDocUrl, true);
let entities = {}; docXhr.setRequestHeader("Content-Type", "application/json");
const baseUrl = utils.parseBaseUrl(apiUrl); docXhr.onreadystatechange = function () {
if (docXhr.readyState === 4 && docXhr.status === 200) {
// gather the entities const json = JSON.parse(docXhr.responseText);
supportedClasses.forEach(function (classData) { const supportedClasses = json["hydra:supportedClass"];
// add entry point url
const entityName = classData["hydra:title"]; let entities = {};
let entryPoint = entryPoints[entityName.toLowerCase()]; const baseUrl = utils.parseBaseUrl(apiUrl);
if (entryPoint !== undefined && !entryPoint.startsWith("http")) entryPoint = baseUrl + entryPoint;
classData["@entryPoint"] = entryPoint; // gather the entities
supportedClasses.forEach(function (classData) {
entities[entityName] = classData; // add entry point url
}); const entityName = classData["hydra:title"];
let entryPoint = entryPoints[entityName.toLowerCase()];
const instance = new JSONLD(baseUrl, entities); if (entryPoint !== undefined && !entryPoint.startsWith("http")) entryPoint = baseUrl + entryPoint;
instances[apiUrl] = instance; classData["@entryPoint"] = entryPoint;
// return the initialized JSONLD object entities[entityName] = classData;
for (const fnc of successFunctions[apiUrl]) if (typeof fnc == 'function') fnc(instance); });
successFunctions[apiUrl] = [];
} else { const instance = new JSONLD(baseUrl, entities);
for (const fnc of failureFunctions[apiUrl]) if (typeof fnc == 'function') fnc(); instances[apiUrl] = instance;
failureFunctions[apiUrl] = [];
} // return the initialized JSONLD object
}; for (const fnc of successFunctions[apiUrl]) if (typeof fnc == 'function') fnc(instance);
successFunctions[apiUrl] = [];
docXhr.send(); } else {
} else { for (const fnc of failureFunctions[apiUrl]) if (typeof fnc == 'function') fnc();
for (const fnc of failureFunctions[apiUrl]) if (typeof fnc == 'function') fnc(); failureFunctions[apiUrl] = [];
failureFunctions[apiUrl] = []; }
} };
docXhr.send();
} else { } else {
for (const fnc of failureFunctions[apiUrl]) if (typeof fnc == 'function') fnc(); for (const fnc of failureFunctions[apiUrl]) if (typeof fnc == 'function') fnc();
failureFunctions[apiUrl] = []; failureFunctions[apiUrl] = [];
} }
}; } else {
for (const fnc of failureFunctions[apiUrl]) if (typeof fnc == 'function') fnc();
failureFunctions[apiUrl] = [];
}
};
xhr.send(); xhr.send();
});
} }
static getInstance(apiUrl) { static getInstance(apiUrl) {
......
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