From 1bff4e8493464e4387c2526832a65f27136926ef Mon Sep 17 00:00:00 2001
From: Markin Igor <markin.io210@gmail.com>
Date: Thu, 8 Nov 2018 17:15:03 +0300
Subject: [PATCH] Reworked API host initialization.

---
 javascript/src/iframe/viamapi-iframe.js | 10 ++++------
 javascript/src/viamapi-client.js        | 12 ++++++------
 2 files changed, 10 insertions(+), 12 deletions(-)

diff --git a/javascript/src/iframe/viamapi-iframe.js b/javascript/src/iframe/viamapi-iframe.js
index 442a825..10982b2 100644
--- a/javascript/src/iframe/viamapi-iframe.js
+++ b/javascript/src/iframe/viamapi-iframe.js
@@ -1484,6 +1484,9 @@ function getCertificateForPassport(passportUUID, internal) {
 const connection = Penpal.connectToParent({
   // Methods child is exposing to parent
   methods: {
+    initializeApiHost: (apiUrl) => {
+      window.API_HOST = apiUrl.charAt(apiUrl.length - 1) === "/" ? apiUrl : apiUrl + "/";
+    },
     createIdentity(pinCode) {
       return new Penpal.Promise(result => {
         createPassportCertificate(makeid()).then(function(keys){
@@ -1507,7 +1510,7 @@ const connection = Penpal.connectToParent({
             "code" : "200",
             "status" : "Identity created"
           })
-        });
+        }).catch(console.error);
       })
     },
     listIdentities() {
@@ -2158,11 +2161,6 @@ const connection = Penpal.connectToParent({
 connection.promise.then(async parent => {
   var identities = localStorage.getItem("identities")
 
-  let apiUrl = await parent.getApiUrl();
-  apiUrl = apiUrl.charAt(apiUrl.length - 1) === "/" ? apiUrl : apiUrl + "/";
-
-  window.API_HOST = apiUrl;
-
   console.log("Library loaded at: " + new Date().toISOString())
 
   if (identities === "" || identities === null) {
diff --git a/javascript/src/viamapi-client.js b/javascript/src/viamapi-client.js
index 69d1eb3..bb40ca7 100644
--- a/javascript/src/viamapi-client.js
+++ b/javascript/src/viamapi-client.js
@@ -8,7 +8,7 @@ const Penpal = require('penpal').default;
  * @param apiUrl -  API URL used to access API endpoints
  * @returns {*}
  */
-function setupViamAPI(divId, methods, iframeUrl, apiUrl) {
+async function setupViamAPI(divId, methods, iframeUrl, apiUrl) {
 	if (!apiUrl) {
 		return Promise.reject('API Host not specified.')
 	}
@@ -19,13 +19,13 @@ function setupViamAPI(divId, methods, iframeUrl, apiUrl) {
 	  // Container to which the iframe should be appended.
 	  appendTo: document.getElementById(divId),
 	  // Methods parent is exposing to child
-	  methods: {
-	  	...methods,
-	  	getApiUrl: () => apiUrl
-      }
+	  methods
 	});
 
-	return connection.promise
+	const child = await connection.promise;
+	await child.initializeApiHost(apiUrl);
+
+	return child;
 }
 
 window.setupViamAPI = setupViamAPI;
-- 
GitLab