@microsoft/omnichannel-amsclient
Advanced tools
Comparing version 0.1.0-main.294e9eb to 0.1.0-main.3cc1da3
# Changelog | ||
All notable changes to this project will be documented in this file. | ||
## [Unreleased] | ||
## [Unreleased] | ||
### Added | ||
- Prevent multiple iframes being loaded | ||
- Add `dispose()` on `FramedClient` |
@@ -1,1 +0,1 @@ | ||
exports.baseUrl = 'https://comms-test.omnichannelengagementhub.com/ams'; exports.sdkVersion = '0.1.0-main.294e9eb'; | ||
exports.baseUrl = 'https://comms.omnichannelengagementhub.com/ams'; exports.sdkVersion = '0.1.0-main.3cc1da3'; |
export default interface FileMetadata { | ||
fileSharingProtocolType: number; | ||
fileSharingProtocolType?: number; | ||
id: string; | ||
name: string; | ||
size: number; | ||
name?: string; | ||
size?: number; | ||
type: string; | ||
url: string; | ||
url?: string; | ||
} |
@@ -10,2 +10,3 @@ import AMSFileInfo from "./AMSFileInfo"; | ||
declare class FramedClient { | ||
private iframeId; | ||
private origin; | ||
@@ -29,5 +30,6 @@ private targetWindow; | ||
postMessage(eventType: PostMessageEventType, eventName: PostMessageEventName, data?: PostMessageRequestData, resolve?: CallableFunction, reject?: CallableFunction): void; | ||
handleEvent(event: MessageEvent<any>): Promise<void>; | ||
private createIframe; | ||
handleEvent(event: MessageEvent): Promise<void>; | ||
dispose(): void; | ||
private loadIframe; | ||
} | ||
export default FramedClient; |
@@ -56,2 +56,3 @@ "use strict"; | ||
var version = config_1.sdkVersion; | ||
var iframePrefix = 'Microsoft_Omnichannel_AMSClient_Iframe_Window'; | ||
var FramedClient = /** @class */ (function () { | ||
@@ -66,2 +67,3 @@ function FramedClient(logger) { | ||
this.logger = logger; | ||
this.iframeId = iframePrefix; | ||
this.onMessageEvent(function (event) { return _this.handleEvent(event); }); // eslint-disable-line @typescript-eslint/no-explicit-any | ||
@@ -83,3 +85,3 @@ } | ||
} | ||
return [4 /*yield*/, this.createIframe()]; | ||
return [4 /*yield*/, this.loadIframe()]; | ||
case 1: | ||
@@ -105,10 +107,16 @@ _a.sent(); | ||
return __generator(this, function (_a) { | ||
/* istanbul ignore next */ | ||
this.debug && console.log("[FramedClient][skypeAuth]"); | ||
data = { | ||
chatToken: chatToken || this.chatToken | ||
}; | ||
return [2 /*return*/, new Promise(function (resolve, reject) { | ||
_this.postMessage(PostMessageEventType_1.default.Request, PostMessageEventName_1.default.SkypeTokenAuth, data, resolve, reject); | ||
})]; | ||
switch (_a.label) { | ||
case 0: | ||
/* istanbul ignore next */ | ||
this.debug && console.log("[FramedClient][skypeAuth]"); | ||
data = { | ||
chatToken: chatToken || this.chatToken | ||
}; | ||
return [4 /*yield*/, this.loadIframe()]; | ||
case 1: | ||
_a.sent(); | ||
return [2 /*return*/, new Promise(function (resolve, reject) { | ||
_this.postMessage(PostMessageEventType_1.default.Request, PostMessageEventName_1.default.SkypeTokenAuth, data, resolve, reject); | ||
})]; | ||
} | ||
}); | ||
@@ -123,12 +131,18 @@ }); | ||
return __generator(this, function (_a) { | ||
/* istanbul ignore next */ | ||
this.debug && console.log("[FramedClient][createObject]"); | ||
data = { | ||
id: id, | ||
file: file, | ||
chatToken: chatToken || this.chatToken | ||
}; | ||
return [2 /*return*/, new Promise(function (resolve, reject) { | ||
_this.postMessage(PostMessageEventType_1.default.Request, PostMessageEventName_1.default.CreateObject, data, resolve, reject); | ||
})]; | ||
switch (_a.label) { | ||
case 0: | ||
/* istanbul ignore next */ | ||
this.debug && console.log("[FramedClient][createObject]"); | ||
data = { | ||
id: id, | ||
file: file, | ||
chatToken: chatToken || this.chatToken | ||
}; | ||
return [4 /*yield*/, this.loadIframe()]; | ||
case 1: | ||
_a.sent(); | ||
return [2 /*return*/, new Promise(function (resolve, reject) { | ||
_this.postMessage(PostMessageEventType_1.default.Request, PostMessageEventName_1.default.CreateObject, data, resolve, reject); | ||
})]; | ||
} | ||
}); | ||
@@ -143,12 +157,18 @@ }); | ||
return __generator(this, function (_a) { | ||
/* istanbul ignore next */ | ||
this.debug && console.log("[FramedClient][uploadDocument]"); | ||
data = { | ||
documentId: documentId, | ||
file: file, | ||
chatToken: chatToken || this.chatToken | ||
}; | ||
return [2 /*return*/, new Promise(function (resolve, reject) { | ||
_this.postMessage(PostMessageEventType_1.default.Request, PostMessageEventName_1.default.UploadDocument, data, resolve, reject); | ||
})]; | ||
switch (_a.label) { | ||
case 0: | ||
/* istanbul ignore next */ | ||
this.debug && console.log("[FramedClient][uploadDocument]"); | ||
data = { | ||
documentId: documentId, | ||
file: file, | ||
chatToken: chatToken || this.chatToken | ||
}; | ||
return [4 /*yield*/, this.loadIframe()]; | ||
case 1: | ||
_a.sent(); | ||
return [2 /*return*/, new Promise(function (resolve, reject) { | ||
_this.postMessage(PostMessageEventType_1.default.Request, PostMessageEventName_1.default.UploadDocument, data, resolve, reject); | ||
})]; | ||
} | ||
}); | ||
@@ -163,9 +183,15 @@ }); | ||
return __generator(this, function (_a) { | ||
data = { | ||
fileMetadata: fileMetadata, | ||
chatToken: chatToken || this.chatToken | ||
}; | ||
return [2 /*return*/, new Promise(function (resolve, reject) { | ||
_this.postMessage(PostMessageEventType_1.default.Request, PostMessageEventName_1.default.GetViewStatus, data, resolve, reject); | ||
})]; | ||
switch (_a.label) { | ||
case 0: | ||
data = { | ||
fileMetadata: fileMetadata, | ||
chatToken: chatToken || this.chatToken | ||
}; | ||
return [4 /*yield*/, this.loadIframe()]; | ||
case 1: | ||
_a.sent(); | ||
return [2 /*return*/, new Promise(function (resolve, reject) { | ||
_this.postMessage(PostMessageEventType_1.default.Request, PostMessageEventName_1.default.GetViewStatus, data, resolve, reject); | ||
})]; | ||
} | ||
}); | ||
@@ -180,10 +206,16 @@ }); | ||
return __generator(this, function (_a) { | ||
data = { | ||
fileMetadata: fileMetadata, | ||
viewLocation: viewLocation, | ||
chatToken: chatToken || this.chatToken | ||
}; | ||
return [2 /*return*/, new Promise(function (resolve, reject) { | ||
_this.postMessage(PostMessageEventType_1.default.Request, PostMessageEventName_1.default.GetView, data, resolve, reject); | ||
})]; | ||
switch (_a.label) { | ||
case 0: | ||
data = { | ||
fileMetadata: fileMetadata, | ||
viewLocation: viewLocation, | ||
chatToken: chatToken || this.chatToken | ||
}; | ||
return [4 /*yield*/, this.loadIframe()]; | ||
case 1: | ||
_a.sent(); | ||
return [2 /*return*/, new Promise(function (resolve, reject) { | ||
_this.postMessage(PostMessageEventType_1.default.Request, PostMessageEventName_1.default.GetView, data, resolve, reject); | ||
})]; | ||
} | ||
}); | ||
@@ -227,2 +259,6 @@ }); | ||
}; | ||
if (!this.targetWindow) { | ||
console.error('Target window not found!'); | ||
return; | ||
} | ||
this.targetWindow.postMessage(__assign({ requestId: requestId, | ||
@@ -242,3 +278,6 @@ eventType: eventType, | ||
this.debug && console.log(event); | ||
this.targetWindow = event.source; // Finds target window to post message back | ||
// Finds target window to post message back | ||
if (event.source) { | ||
this.targetWindow = event.source; | ||
} | ||
if (event.data.eventType === PostMessageEventType_1.default.Response) { | ||
@@ -249,20 +288,30 @@ /* istanbul ignore next */ | ||
if (event.data.eventName === PostMessageEventName_1.default.SkypeTokenAuth) { | ||
this.requestCallbacks[data.requestId].resolve(); | ||
delete this.requestCallbacks[data.requestId]; | ||
if (data.requestId in this.requestCallbacks) { | ||
this.requestCallbacks[data.requestId].resolve(); | ||
delete this.requestCallbacks[data.requestId]; | ||
} | ||
} | ||
else if (event.data.eventName === PostMessageEventName_1.default.CreateObject) { | ||
this.requestCallbacks[data.requestId].resolve(data.response); | ||
delete this.requestCallbacks[data.requestId]; | ||
if (data.requestId in this.requestCallbacks) { | ||
this.requestCallbacks[data.requestId].resolve(data.response); | ||
delete this.requestCallbacks[data.requestId]; | ||
} | ||
} | ||
else if (event.data.eventName === PostMessageEventName_1.default.UploadDocument) { | ||
this.requestCallbacks[data.requestId].resolve(data.response); | ||
delete this.requestCallbacks[data.requestId]; | ||
if (data.requestId in this.requestCallbacks) { | ||
this.requestCallbacks[data.requestId].resolve(data.response); | ||
delete this.requestCallbacks[data.requestId]; | ||
} | ||
} | ||
else if (event.data.eventName === PostMessageEventName_1.default.GetViewStatus) { | ||
this.requestCallbacks[data.requestId].resolve(data.response); | ||
delete this.requestCallbacks[data.requestId]; | ||
if (data.requestId in this.requestCallbacks) { | ||
this.requestCallbacks[data.requestId].resolve(data.response); | ||
delete this.requestCallbacks[data.requestId]; | ||
} | ||
} | ||
else if (event.data.eventName === PostMessageEventName_1.default.GetView) { | ||
this.requestCallbacks[data.requestId].resolve(data.response); | ||
delete this.requestCallbacks[data.requestId]; | ||
if (data.requestId in this.requestCallbacks) { | ||
this.requestCallbacks[data.requestId].resolve(data.response); | ||
delete this.requestCallbacks[data.requestId]; | ||
} | ||
} | ||
@@ -277,11 +326,21 @@ else if (event.data.eventName === PostMessageEventName_1.default.SendTelemetry) { | ||
}; | ||
FramedClient.prototype.createIframe = function () { | ||
FramedClient.prototype.dispose = function () { | ||
var _a; | ||
(_a = document.getElementById(this.iframeId)) === null || _a === void 0 ? void 0 : _a.remove(); | ||
this.requestCallbacks = {}; | ||
this.iframeLoaded = false; | ||
}; | ||
FramedClient.prototype.loadIframe = function () { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var iframeId; | ||
var _this = this; | ||
return __generator(this, function (_a) { | ||
iframeId = 'Microsoft_Omnichannel_AMSClient_Iframe_Window'; | ||
return [2 /*return*/, new Promise(function (resolve, reject) { | ||
var iframeElements = Array.from(document.getElementsByTagName('iframe')); | ||
var foundIframeElement = iframeElements.filter(function (iframeElement) { return iframeElement.id == _this.iframeId; }); | ||
// Avoid duplicate load | ||
if (foundIframeElement.length) { | ||
return resolve(); | ||
} | ||
var iframeElement = document.createElement('iframe'); | ||
iframeElement.id = iframeId; | ||
iframeElement.id = _this.iframeId; | ||
iframeElement.src = config_1.baseUrl + "/" + version + "/iframe.html?debug=" + _this.debug + "&telemetry=true"; | ||
@@ -288,0 +347,0 @@ iframeElement.addEventListener('load', function () { |
declare enum PostMessageEventName { | ||
IframeLoaded = "IframeLoaded", | ||
SkypeTokenAuth = "SkypeTokenAuth", | ||
CreateObject = "CreateObject", | ||
UploadDocument = "UploadDocument", | ||
GetViewStatus = "GetViewStatus", | ||
GetView = "GetView", | ||
SendTelemetry = "SendTelemetry" | ||
IframeLoaded = "AMSIframeLoaded", | ||
SkypeTokenAuth = "AMSSkypeTokenAuth", | ||
CreateObject = "AMSCreateObject", | ||
UploadDocument = "AMSUploadDocument", | ||
GetViewStatus = "AMSGetViewStatus", | ||
GetView = "AMSGetView", | ||
SendTelemetry = "AMSSendTelemetry" | ||
} | ||
export default PostMessageEventName; |
@@ -5,11 +5,11 @@ "use strict"; | ||
(function (PostMessageEventName) { | ||
PostMessageEventName["IframeLoaded"] = "IframeLoaded"; | ||
PostMessageEventName["SkypeTokenAuth"] = "SkypeTokenAuth"; | ||
PostMessageEventName["CreateObject"] = "CreateObject"; | ||
PostMessageEventName["UploadDocument"] = "UploadDocument"; | ||
PostMessageEventName["GetViewStatus"] = "GetViewStatus"; | ||
PostMessageEventName["GetView"] = "GetView"; | ||
PostMessageEventName["SendTelemetry"] = "SendTelemetry"; | ||
PostMessageEventName["IframeLoaded"] = "AMSIframeLoaded"; | ||
PostMessageEventName["SkypeTokenAuth"] = "AMSSkypeTokenAuth"; | ||
PostMessageEventName["CreateObject"] = "AMSCreateObject"; | ||
PostMessageEventName["UploadDocument"] = "AMSUploadDocument"; | ||
PostMessageEventName["GetViewStatus"] = "AMSGetViewStatus"; | ||
PostMessageEventName["GetView"] = "AMSGetView"; | ||
PostMessageEventName["SendTelemetry"] = "AMSSendTelemetry"; | ||
})(PostMessageEventName || (PostMessageEventName = {})); | ||
exports.default = PostMessageEventName; | ||
//# sourceMappingURL=PostMessageEventName.js.map |
{ | ||
"name": "@microsoft/omnichannel-amsclient", | ||
"version": "0.1.0-main.294e9eb", | ||
"version": "0.1.0-main.3cc1da3", | ||
"description": "Microsoft Omnichannel AMSClient", | ||
@@ -5,0 +5,0 @@ "files": [ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
208807
1627