Skip to content

Commit

Permalink
feat: integrate @qlik/sdk into mashup template (#915)
Browse files Browse the repository at this point in the history
  • Loading branch information
a-m-dev committed Sep 13, 2022
1 parent d213eb5 commit d86932c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 35 deletions.
1 change: 1 addition & 0 deletions commands/create/templates/mashup/_package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down
50 changes: 15 additions & 35 deletions commands/create/templates/mashup/src/connect.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,24 @@
import enigma from 'enigma.js';
import schema from 'enigma.js/schemas/12.936.0.json';
import { Auth, AuthType } from '@qlik/sdk';

async function getQCSHeaders({ webIntegrationId, url }) {
const response = await fetch(`${url}/api/v1/csrf-token`, {
credentials: 'include',
headers: { 'qlik-web-integration-id': webIntegrationId },
async function connect({ url, webIntegrationId, appId }) {
const authInstance = new Auth({
webIntegrationId,
autoRedirect: true,
authType: AuthType.WebIntegration,
host: url.replace(/^https?:\/\//, '').replace(/\/?/, ''),
});
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);
}
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);
}

async function connect({ url, webIntegrationId, appId }) {
const host = url.replace(/^https?:\/\//, '').replace(/\/?/, '');
const headers = await getQCSHeaders({ url, webIntegrationId });
return getEnigmaApp({ host, headers, appId });
return null;
}

export default connect;

0 comments on commit d86932c

Please sign in to comment.