-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: integrate
@qlik/sdk
into mashup template (#915)
- Loading branch information
Showing
2 changed files
with
16 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |