From 4f90dc29ed63ada4b0ab587a6b6a96b5ebbcb088 Mon Sep 17 00:00:00 2001 From: caele Date: Tue, 10 Sep 2024 15:56:28 +0200 Subject: [PATCH 1/2] feat: replace sdk in mashup template --- .../create/templates/mashup/_package.json | 5 +- .../templates/mashup/src/Authenticator.js | 86 ------------------- .../create/templates/mashup/src/configure.js | 18 ---- .../create/templates/mashup/src/connect.js | 26 ------ commands/create/templates/mashup/src/index.js | 43 ++++++---- 5 files changed, 30 insertions(+), 148 deletions(-) delete mode 100644 commands/create/templates/mashup/src/Authenticator.js delete mode 100644 commands/create/templates/mashup/src/configure.js delete mode 100644 commands/create/templates/mashup/src/connect.js diff --git a/commands/create/templates/mashup/_package.json b/commands/create/templates/mashup/_package.json index 64160b3e9..8e30e1598 100644 --- a/commands/create/templates/mashup/_package.json +++ b/commands/create/templates/mashup/_package.json @@ -13,8 +13,7 @@ "dependencies": { "@nebula.js/stardust": "<%= nebulaVersion %>", "@nebula.js/sn-bar-chart": "^1.x", - "@qlik/sdk": "^0.12.0", - "enigma.js": "^2.6.3", - "parcel": "2.8.0" + "@qlik/api": "^1.17.0", + "parcel": "2.12.0" } } diff --git a/commands/create/templates/mashup/src/Authenticator.js b/commands/create/templates/mashup/src/Authenticator.js deleted file mode 100644 index 160ee6e56..000000000 --- a/commands/create/templates/mashup/src/Authenticator.js +++ /dev/null @@ -1,86 +0,0 @@ -import enigma from 'enigma.js'; -import schema from 'enigma.js/schemas/12.2015.0.json'; -import { Auth, AuthType } from '@qlik/sdk'; - -export default class Authenticator { - /** - * - * @param {String} appId - application id from sense client - * @param {Strign} url - tenant url - */ - constructor({ appId, url }) { - this.authInstance = null; - this.appId = appId; - this.host = url.replace(/^https?:\/\//, '').replace(/\/?/, ''); - } - - /** - * gets you the promise of enigma instance for your app - * based on webIntegrationId - * @param {Strign} webIntegrationId - your web Integration Id from managment console - * @returns {Promise} enigma app promise - */ - async AuthenticateWithWebIntegrationId({ webIntegrationId }) { - this.authInstance = new Auth({ - authType: AuthType.WebIntegration, - autoRedirect: true, - host: this.host, - webIntegrationId, - }); - - if (!this.authInstance.isAuthenticated()) { - this.authInstance.authenticate(); - } else return this.getEnigmaApp(); - - return null; - } - - /** - * gets you the promise of enigma instance for your app - * based on clientId - * @param {Strign} clientId - your client Id from managment console - * @param {Strign} redirectUri - the redirect url while bringing you the code + state, default is `window.location.origin` - * @returns {Promise} enigma app promise - */ - async AuthenticateWithOAuth({ clientId, redirectUri }) { - this.authInstance = new Auth({ - authType: AuthType.OAuth2, - host: this.host, - redirectUri: redirectUri || window.location.origin, - clientId, - }); - - const urlParams = new URLSearchParams(window.location.search); - if (urlParams.get('code')) { - try { - await this.authInstance.authorize(window.location.href); - const url = new URL(window.location); - url.searchParams.delete('code'); - url.searchParams.delete('state'); - window.history.replaceState(null, null, url); - } catch (error) { - console.error({ error }); - } - - return this.getEnigmaApp(); - } - - if (!(await this.authInstance.isAuthorized())) { - const { url } = await this.authInstance.generateAuthorizationUrl(); - const protocol = url.includes('https://') ? 'https' : 'http'; - window.location = `${protocol}://${url}`; - } - - return null; - } - - /** - * returns a promise of enigma instance - * @returns {Promise} enigma app instance promise - */ - async getEnigmaApp() { - const url = await this.authInstance.generateWebsocketUrl(this.appId); - const enigmaGlobal = await enigma.create({ schema, url }).open(); - return enigmaGlobal.openDoc(this.appId); - } -} diff --git a/commands/create/templates/mashup/src/configure.js b/commands/create/templates/mashup/src/configure.js deleted file mode 100644 index 3785e6e81..000000000 --- a/commands/create/templates/mashup/src/configure.js +++ /dev/null @@ -1,18 +0,0 @@ -import { embed } from '@nebula.js/stardust'; - -import barchart from '@nebula.js/sn-bar-chart'; - -const n = embed.createConfiguration({ - context: { - theme: 'light', - language: 'en-US', - }, - types: [ - { - name: 'barchart', - load: () => Promise.resolve(barchart), - }, - ], -}); - -export default n; diff --git a/commands/create/templates/mashup/src/connect.js b/commands/create/templates/mashup/src/connect.js deleted file mode 100644 index ce4ccecfa..000000000 --- a/commands/create/templates/mashup/src/connect.js +++ /dev/null @@ -1,26 +0,0 @@ -import { AuthType } from '@qlik/sdk'; -import Authenticator from './Authenticator'; - -async function connect({ connectionType, url, appId, ...rest }) { - const AuthenticatorInstance = new Authenticator({ - url, - appId, - }); - - switch (connectionType) { - case AuthType.WebIntegration: { - return AuthenticatorInstance.AuthenticateWithWebIntegrationId({ - ...rest, - }); - } - - case AuthType.OAuth2: { - return AuthenticatorInstance.AuthenticateWithOAuth({ ...rest }); - } - - default: - throw new Error('Please Provide a `connectionType` to proceed!'); - } -} - -export default connect; diff --git a/commands/create/templates/mashup/src/index.js b/commands/create/templates/mashup/src/index.js index 5682e309a..b578fc8a4 100644 --- a/commands/create/templates/mashup/src/index.js +++ b/commands/create/templates/mashup/src/index.js @@ -1,25 +1,38 @@ /* eslint-disable */ -import { AuthType } from '@qlik/sdk'; -import embed from './configure'; -import connect from './connect'; +import qlikApi from '@qlik/api'; +import { embed } from '@nebula.js/stardust'; +import barchart from '@nebula.js/sn-bar-chart'; async function run() { - const app = await connect({ - connectionType: '', - url: '', - appId: '', + const appId = ''; + const hostConfig = { + authType: '', + // connection config based on authType + webIntegrationId: '', // cookie + clientId: '', // oauth2 + }; - // you should use only one of below keys - // based on your `connectionType` - clientId: '', - webIntegrationId: '', - }); + qlikApi.auth.setDefaultHostConfig(hostConfig); + const appSession = qlikApi.qix.openAppSession(appId); + const app = await appSession.getDoc(); - const n = embed(app); + const nebula = embed(app, { + context: { + theme: 'light', + language: 'en-US', + }, + types: [ + { + name: 'barchart', + load: () => Promise.resolve(barchart), + }, + ], + }); - (await n.selections()).mount(document.querySelector('.toolbar')); + (await nebula.selections()).mount(document.querySelector('.toolbar')); - // n.render({}); + // nebula.render({ element: document.querySelector('.object'), id: "" }); } run(); From 277d6379f99dd4bd104849ad03e36956cef098b4 Mon Sep 17 00:00:00 2001 From: caele Date: Wed, 11 Sep 2024 10:10:38 +0200 Subject: [PATCH 2/2] chore: tweak template --- commands/create/templates/mashup/src/index.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/commands/create/templates/mashup/src/index.js b/commands/create/templates/mashup/src/index.js index b578fc8a4..b3b3ece5d 100644 --- a/commands/create/templates/mashup/src/index.js +++ b/commands/create/templates/mashup/src/index.js @@ -5,12 +5,14 @@ import barchart from '@nebula.js/sn-bar-chart'; async function run() { const appId = ''; + // https://github.com/qlik-oss/qlik-api-ts/blob/main/docs/authentication.md const hostConfig = { authType: '', // connection config based on authType webIntegrationId: '', // cookie clientId: '', // oauth2 + accessTokenStorage: 'session', // for oauth2 }; qlikApi.auth.setDefaultHostConfig(hostConfig);