mirror of
https://github.com/qlik-oss/nebula.js.git
synced 2025-12-25 01:04:14 -05:00
feat: integrate @qlik/sdk into mashup template (#915)
This commit is contained in:
@@ -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"
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user