feat: integrate @qlik/sdk into mashup template (#915)

This commit is contained in:
Ahmad Mirzaei
2022-09-13 11:48:26 +02:00
committed by GitHub
parent d213eb5a0f
commit d86932c2c9
2 changed files with 18 additions and 37 deletions

View File

@@ -13,6 +13,7 @@
"dependencies": {
"@nebula.js/stardust": "<%= nebulaVersion %>",
"@nebula.js/sn-bar-chart": "^0.8.24",
"@qlik/sdk": "^0.9.1",
"enigma.js": "^2.6.3",
"parcel": "^2.3.2"
}

View File

@@ -1,44 +1,24 @@
import enigma from 'enigma.js';
import schema from 'enigma.js/schemas/12.936.0.json';
async function getQCSHeaders({ webIntegrationId, url }) {
const response = await fetch(`${url}/api/v1/csrf-token`, {
credentials: 'include',
headers: { 'qlik-web-integration-id': webIntegrationId },
});
if (response.status === 401) {
const loginUrl = new URL(`${url}/login`);
loginUrl.searchParams.append('returnto', window.location.href);
loginUrl.searchParams.append('qlik-web-integration-id', webIntegrationId);
window.location.href = loginUrl;
return undefined;
}
const csrfToken = new Map(response.headers).get('qlik-csrf-token');
return {
'qlik-web-integration-id': webIntegrationId,
'qlik-csrf-token': csrfToken,
};
}
async function getEnigmaApp({ host, appId, headers }) {
const params = Object.keys(headers)
.map((key) => `${key}=${headers[key]}`)
.join('&');
const enigmaGlobal = await enigma
.create({
schema,
url: `wss://${host}/app/${appId}?${params}`,
})
.open();
return enigmaGlobal.openDoc(appId);
}
import { Auth, AuthType } from '@qlik/sdk';
async function connect({ url, webIntegrationId, appId }) {
const host = url.replace(/^https?:\/\//, '').replace(/\/?/, '');
const headers = await getQCSHeaders({ url, webIntegrationId });
return getEnigmaApp({ host, headers, appId });
const authInstance = new Auth({
webIntegrationId,
autoRedirect: true,
authType: AuthType.WebIntegration,
host: url.replace(/^https?:\/\//, '').replace(/\/?/, ''),
});
if (!authInstance.isAuthenticated()) {
authInstance.authenticate();
} else {
const wssUrl = await authInstance.generateWebsocketUrl(appId);
const enigmaGlobal = await enigma.create({ schema, url: wssUrl }).open();
return enigmaGlobal.openDoc(appId);
}
return null;
}
export default connect;