@microsoft/omnichannel-amsclient
Advanced tools
Comparing version 0.1.0-main.8285125 to 0.1.0-main.27aeb69
@@ -7,2 +7,5 @@ # Changelog | ||
### Added | ||
- Prevent multiple iframes being loaded | ||
- Prevent multiple iframes being loaded | ||
- Add `dispose()` on `FramedClient` | ||
- Use `amsEndpoint` property by default | ||
- Add `silentError` option |
@@ -50,2 +50,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var GlobalConfiguration_1 = require("./GlobalConfiguration"); | ||
var HeadersName; | ||
@@ -96,5 +97,5 @@ (function (HeadersName) { | ||
case 0: | ||
console.log("[API][skypeTokenAuth]"); | ||
GlobalConfiguration_1.default.debug && console.log("[API][skypeTokenAuth]"); | ||
patchChatToken(chatToken); | ||
url = ((_b = chatToken.regionGTMS) === null || _b === void 0 ? void 0 : _b.ams) + "/v1/skypetokenauth"; | ||
url = (chatToken.amsEndpoint || ((_b = chatToken.regionGTMS) === null || _b === void 0 ? void 0 : _b.ams)) + "/v1/skypetokenauth"; | ||
headers = (_a = {}, | ||
@@ -120,3 +121,3 @@ _a[HeadersName.Authorization] = "skype_token " + chatToken.token, | ||
error_1 = _c.sent(); | ||
console.log(error_1); | ||
!GlobalConfiguration_1.default.silentError && console.log(error_1); | ||
throw new Error('AMSAuth'); | ||
@@ -134,3 +135,3 @@ case 4: return [2 /*return*/]; | ||
case 0: | ||
console.log("[API][createObject]"); | ||
GlobalConfiguration_1.default.debug && console.log("[API][createObject]"); | ||
permissions = (_a = {}, | ||
@@ -145,3 +146,3 @@ _a[id] = ['read'], | ||
patchChatToken(chatToken); | ||
url = ((_c = chatToken === null || chatToken === void 0 ? void 0 : chatToken.regionGTMS) === null || _c === void 0 ? void 0 : _c.ams) + "/v1/objects"; | ||
url = (chatToken.amsEndpoint || ((_c = chatToken === null || chatToken === void 0 ? void 0 : chatToken.regionGTMS) === null || _c === void 0 ? void 0 : _c.ams)) + "/v1/objects"; | ||
headers = __assign(__assign({}, createDefaultHeaders(chatToken.token)), (_b = {}, _b[HeadersName.ContentType] = MIMEType.applicationJson, _b)); | ||
@@ -166,3 +167,3 @@ request = { | ||
error_2 = _d.sent(); | ||
console.log(error_2); | ||
!GlobalConfiguration_1.default.silentError && console.log(error_2); | ||
throw new Error('AMSCreateObjectFailed'); | ||
@@ -180,5 +181,5 @@ case 5: return [2 /*return*/]; | ||
case 0: | ||
console.log("[API][uploadDocument]"); | ||
GlobalConfiguration_1.default.debug && console.log("[API][uploadDocument]"); | ||
patchChatToken(chatToken); | ||
url = ((_b = chatToken === null || chatToken === void 0 ? void 0 : chatToken.regionGTMS) === null || _b === void 0 ? void 0 : _b.ams) + "/v1/objects/" + documentId + "/content/" + (file.type.includes('image') ? 'imgpsh' : 'original'); | ||
url = (chatToken.amsEndpoint || ((_b = chatToken === null || chatToken === void 0 ? void 0 : chatToken.regionGTMS) === null || _b === void 0 ? void 0 : _b.ams)) + "/v1/objects/" + documentId + "/content/" + (file.type.includes('image') ? 'imgpsh' : 'original'); | ||
headers = __assign(__assign({}, createDefaultHeaders(chatToken.token)), (_a = {}, _a[HeadersName.ContentType] = MIMEType.applicationFormUrlEncoded, _a)); | ||
@@ -207,3 +208,3 @@ request = { | ||
error_3 = _c.sent(); | ||
console.log(error_3); | ||
!GlobalConfiguration_1.default.silentError && console.log(error_3); | ||
throw new Error('AMSUploadDocumentFailed'); | ||
@@ -220,5 +221,5 @@ case 4: return [2 /*return*/]; | ||
case 0: | ||
console.log("[API][getViewStatus]"); | ||
GlobalConfiguration_1.default.debug && console.log("[API][getViewStatus]"); | ||
patchChatToken(chatToken); | ||
url = ((_a = chatToken === null || chatToken === void 0 ? void 0 : chatToken.regionGTMS) === null || _a === void 0 ? void 0 : _a.ams) + "/v1/objects/" + fileMetadata.id + "/views/" + (validImageTypes.includes(fileMetadata.type) ? 'imgpsh_fullsize_anim' : 'original') + "/status"; | ||
url = (chatToken.amsEndpoint || ((_a = chatToken === null || chatToken === void 0 ? void 0 : chatToken.regionGTMS) === null || _a === void 0 ? void 0 : _a.ams)) + "/v1/objects/" + fileMetadata.id + "/views/" + (validImageTypes.includes(fileMetadata.type) ? 'imgpsh_fullsize_anim' : 'original') + "/status"; | ||
headers = createDefaultHeaders(chatToken.token); | ||
@@ -243,3 +244,3 @@ request = { | ||
if (view_state && view_state !== AMSFileStatus.Ready.toString()) { | ||
console.error('view_state is not ready'); | ||
!GlobalConfiguration_1.default.silentError && console.error('view_state is not ready'); | ||
} | ||
@@ -252,3 +253,3 @@ if (content_state === AMSFileStatus.Expired.toString()) { | ||
error_4 = _b.sent(); | ||
console.log(error_4); | ||
!GlobalConfiguration_1.default.silentError && console.log(error_4); | ||
throw new Error('AMSGetViewStatusFailed'); | ||
@@ -264,3 +265,3 @@ case 5: return [2 /*return*/]; | ||
case 0: | ||
console.log("[API][getView]"); | ||
GlobalConfiguration_1.default.debug && console.log("[API][getView]"); | ||
patchChatToken(chatToken); | ||
@@ -289,3 +290,3 @@ url = viewLocation; | ||
error_5 = _a.sent(); | ||
console.log(error_5); | ||
!GlobalConfiguration_1.default.silentError && console.log(error_5); | ||
throw new Error('AMSGetViewFailed'); | ||
@@ -292,0 +293,0 @@ case 5: return [2 /*return*/]; |
@@ -1,1 +0,1 @@ | ||
exports.baseUrl = 'https://comms.omnichannelengagementhub.com/ams'; exports.sdkVersion = '0.1.0-main.8285125'; | ||
exports.baseUrl = 'https://comms.omnichannelengagementhub.com/ams'; exports.sdkVersion = '0.1.0-main.27aeb69'; |
@@ -8,4 +8,6 @@ import FramedClient from "./FramedClient"; | ||
logger?: PluggableLogger; | ||
silentError?: boolean; | ||
multiClient?: boolean; | ||
} | ||
declare const createAMSClient: (config: AMSConfig) => Promise<FramedClient | FramedlessClient>; | ||
export default createAMSClient; |
@@ -42,10 +42,22 @@ "use strict"; | ||
var FramedlessClient_1 = require("./FramedlessClient"); | ||
var GlobalConfiguration_1 = require("./GlobalConfiguration"); | ||
var createAMSClient = function (config) { return __awaiter(void 0, void 0, void 0, function () { | ||
var logger, client; | ||
var logger, framedClientConfig, client; | ||
return __generator(this, function (_a) { | ||
config.debug && console.log("[createAMSClient] " + (config.framedMode ? 'FramedClient' : 'FramedlessClient')); | ||
logger = new AMSLogger_1.default(config.logger); | ||
client = config.framedMode ? new FramedClient_1.default(logger) : new FramedlessClient_1.default(logger); | ||
config.debug && client.setDebug(config.debug || false); | ||
return [2 /*return*/, client]; | ||
switch (_a.label) { | ||
case 0: | ||
config.debug && console.log("[createAMSClient] " + (config.framedMode ? 'FramedClient' : 'FramedlessClient')); | ||
logger = new AMSLogger_1.default(config.logger); | ||
framedClientConfig = { | ||
multiClient: config.multiClient || false | ||
}; | ||
client = config.framedMode ? new FramedClient_1.default(logger, framedClientConfig) : new FramedlessClient_1.default(logger); | ||
return [4 /*yield*/, client.setup()]; | ||
case 1: | ||
_a.sent(); | ||
config.debug && client.setDebug(config.debug || false); | ||
GlobalConfiguration_1.default.debug = config.debug || false; | ||
GlobalConfiguration_1.default.silentError = config.silentError || true; | ||
return [2 /*return*/, client]; | ||
} | ||
}); | ||
@@ -52,0 +64,0 @@ }); }; |
import AMSFileInfo from "./AMSFileInfo"; | ||
import AMSLogger from "./AMSLogger"; | ||
import FileMetadata from "./FileMetadata"; | ||
import FramedClientConfig from "./FramedClientConfig"; | ||
import InitConfig from "./InitConfig"; | ||
@@ -10,2 +11,4 @@ import OmnichannelChatToken from "./OmnichannelChatToken"; | ||
declare class FramedClient { | ||
private clientId; | ||
private iframeId; | ||
private origin; | ||
@@ -18,4 +21,5 @@ private targetWindow; | ||
private logger?; | ||
constructor(logger?: AMSLogger | undefined); | ||
constructor(logger?: AMSLogger | undefined, framedClientConfig?: FramedClientConfig | undefined); | ||
setDebug(flag: boolean): void; | ||
setup(): Promise<void>; | ||
initialize(initConfig: InitConfig): Promise<void>; | ||
@@ -31,4 +35,5 @@ skypeTokenAuth(chatToken?: OmnichannelChatToken | null): Promise<void>; | ||
handleEvent(event: MessageEvent): Promise<void>; | ||
dispose(): void; | ||
private loadIframe; | ||
} | ||
export default FramedClient; |
@@ -51,2 +51,3 @@ "use strict"; | ||
var config_1 = require("./config"); | ||
var GlobalConfiguration_1 = require("./GlobalConfiguration"); | ||
var platform_1 = require("./utils/platform"); | ||
@@ -57,6 +58,8 @@ var PostMessageEventName_1 = require("./PostMessageEventName"); | ||
var version = config_1.sdkVersion; | ||
var iframePrefix = 'Microsoft_Omnichannel_AMSClient_Iframe_Window'; | ||
var FramedClient = /** @class */ (function () { | ||
function FramedClient(logger) { | ||
var _this = this; | ||
function FramedClient(logger, framedClientConfig) { | ||
if (logger === void 0) { logger = undefined; } | ||
if (framedClientConfig === void 0) { framedClientConfig = undefined; } | ||
this.clientId = uuid_1.uuidv4(); | ||
this.origin = window.location.origin; | ||
@@ -67,3 +70,6 @@ this.requestCallbacks = {}; | ||
this.logger = logger; | ||
this.onMessageEvent(function (event) { return _this.handleEvent(event); }); // eslint-disable-line @typescript-eslint/no-explicit-any | ||
this.iframeId = iframePrefix; | ||
if (framedClientConfig && framedClientConfig.multiClient) { | ||
this.iframeId = this.clientId; | ||
} | ||
} | ||
@@ -74,4 +80,5 @@ /* istanbul ignore next */ | ||
}; | ||
FramedClient.prototype.initialize = function (initConfig) { | ||
FramedClient.prototype.setup = function () { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var _this = this; | ||
return __generator(this, function (_a) { | ||
@@ -82,2 +89,3 @@ switch (_a.label) { | ||
this.debug && console.log("[FramedClient][initialize]"); | ||
this.onMessageEvent(function (event) { return _this.handleEvent(event); }); // eslint-disable-line @typescript-eslint/no-explicit-any | ||
if (!platform_1.default.isBrowser()) { | ||
@@ -90,7 +98,19 @@ throw new Error('FramedMode was used in non-Web platform'); | ||
if (!this.iframeLoaded) { | ||
console.error('iframe not loaded'); | ||
!GlobalConfiguration_1.default.silentError && console.error('iframe not loaded'); | ||
} | ||
return [2 /*return*/]; | ||
} | ||
}); | ||
}); | ||
}; | ||
FramedClient.prototype.initialize = function (initConfig) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
/* istanbul ignore next */ | ||
this.debug && console.log("[FramedClient][initialize]"); | ||
this.chatToken = initConfig.chatToken; | ||
return [4 /*yield*/, this.skypeTokenAuth()]; | ||
case 2: | ||
case 1: | ||
_a.sent(); | ||
@@ -236,3 +256,3 @@ return [2 /*return*/]; | ||
error_1 = _a.sent(); | ||
console.log(error_1); | ||
!GlobalConfiguration_1.default.silentError && console.log(error_1); | ||
throw new Error('fetchBlob'); | ||
@@ -257,6 +277,6 @@ case 4: return [2 /*return*/]; | ||
if (!this.targetWindow) { | ||
console.error('Target window not found!'); | ||
!GlobalConfiguration_1.default.silentError && console.error('Target window not found!'); | ||
return; | ||
} | ||
this.targetWindow.postMessage(__assign({ requestId: requestId, | ||
this.targetWindow.postMessage(__assign({ clientId: this.clientId, requestId: requestId, | ||
eventType: eventType, | ||
@@ -273,2 +293,5 @@ eventName: eventName }, data), '*'); | ||
} | ||
if (event.data.clientId !== this.clientId) { | ||
return [2 /*return*/]; | ||
} | ||
/* istanbul ignore next */ | ||
@@ -322,11 +345,15 @@ this.debug && console.log(event); | ||
}; | ||
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 == iframeId; }); | ||
var foundIframeElement = iframeElements.filter(function (iframeElement) { return iframeElement.id == _this.iframeId; }); | ||
// Avoid duplicate load | ||
@@ -337,4 +364,4 @@ if (foundIframeElement.length) { | ||
var iframeElement = document.createElement('iframe'); | ||
iframeElement.id = iframeId; | ||
iframeElement.src = config_1.baseUrl + "/" + version + "/iframe.html?debug=" + _this.debug + "&telemetry=true"; | ||
iframeElement.id = _this.iframeId; | ||
iframeElement.src = config_1.baseUrl + "/" + version + "/iframe.html?clientId=" + _this.clientId + "&debug=" + _this.debug + "&telemetry=true"; | ||
iframeElement.addEventListener('load', function () { | ||
@@ -341,0 +368,0 @@ /* istanbul ignore next */ |
@@ -14,2 +14,3 @@ import AMSCreateObjectResponse from "./AMSCreateObjectResponse"; | ||
setDebug(flag: boolean): void; | ||
setup(): Promise<void>; | ||
initialize(initConfig: InitConfig): Promise<void>; | ||
@@ -16,0 +17,0 @@ skypeTokenAuth(chatToken?: OmnichannelChatToken | null): Promise<Response>; |
@@ -57,2 +57,9 @@ "use strict"; | ||
}; | ||
FramedlessClient.prototype.setup = function () { | ||
return __awaiter(this, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
return [2 /*return*/, Promise.resolve()]; | ||
}); | ||
}); | ||
}; | ||
FramedlessClient.prototype.initialize = function (initConfig) { | ||
@@ -59,0 +66,0 @@ return __awaiter(this, void 0, void 0, function () { |
@@ -6,2 +6,3 @@ import PostMessageEventName from "./PostMessageEventName"; | ||
declare class IframeCommunicator { | ||
private clientId; | ||
private sourceWindow; | ||
@@ -11,3 +12,3 @@ private targetWindow; | ||
private telemetryEnabled; | ||
constructor(); | ||
constructor(clientId: string); | ||
setDebug(flag: boolean): void; | ||
@@ -14,0 +15,0 @@ enableTelemetry(): void; |
@@ -51,2 +51,3 @@ "use strict"; | ||
var API_1 = require("./API"); | ||
var fetchClientId_1 = require("./utils/fetchClientId"); | ||
var fetchDebugConfig_1 = require("./utils/fetchDebugConfig"); | ||
@@ -60,3 +61,4 @@ var fetchTelemetryConfig_1 = require("./utils/fetchTelemetryConfig"); | ||
var IframeCommunicator = /** @class */ (function () { | ||
function IframeCommunicator() { | ||
function IframeCommunicator(clientId) { | ||
this.clientId = clientId; | ||
this.sourceWindow = window; | ||
@@ -90,3 +92,3 @@ this.targetWindow = window.parent; | ||
if (data === void 0) { data = {}; } | ||
this.targetWindow.postMessage(__assign({ eventType: eventType, | ||
this.targetWindow.postMessage(__assign({ clientId: this.clientId, eventType: eventType, | ||
eventName: eventName, | ||
@@ -101,2 +103,5 @@ eventStatus: eventStatus }, data), '*'); | ||
case 0: | ||
if (event.data.clientId !== this.clientId) { | ||
return [2 /*return*/]; | ||
} | ||
if (!(event.data.eventType === PostMessageEventType_1.default.Request)) return [3 /*break*/, 24]; | ||
@@ -121,3 +126,3 @@ this.debug && console.log("[IframeCommunicator][Request]"); | ||
this.sendTelemetry(LogLevel_1.default.ERROR, { | ||
ChatId: data.chatToken.ChatId, | ||
ChatId: data.chatToken.chatId, | ||
AMSClientVersion: config_1.sdkVersion, | ||
@@ -136,3 +141,3 @@ Event: PostMessageEventName_1.default.SkypeTokenAuth, | ||
this.sendTelemetry(LogLevel_1.default.ERROR, { | ||
ChatId: data.chatToken.ChatId, | ||
ChatId: data.chatToken.chatId, | ||
AMSClientVersion: config_1.sdkVersion, | ||
@@ -165,3 +170,3 @@ Event: PostMessageEventName_1.default.SkypeTokenAuth, | ||
this.sendTelemetry(LogLevel_1.default.ERROR, { | ||
ChatId: data.chatToken.ChatId, | ||
ChatId: data.chatToken.chatId, | ||
AMSClientVersion: config_1.sdkVersion, | ||
@@ -195,3 +200,3 @@ Event: PostMessageEventName_1.default.CreateObject, | ||
this.sendTelemetry(LogLevel_1.default.ERROR, { | ||
ChatId: data.chatToken.ChatId, | ||
ChatId: data.chatToken.chatId, | ||
AMSClientVersion: config_1.sdkVersion, | ||
@@ -225,3 +230,3 @@ Event: PostMessageEventName_1.default.UploadDocument, | ||
this.sendTelemetry(LogLevel_1.default.ERROR, { | ||
ChatId: data.chatToken.ChatId, | ||
ChatId: data.chatToken.chatId, | ||
AMSClientVersion: config_1.sdkVersion, | ||
@@ -255,3 +260,3 @@ Event: PostMessageEventName_1.default.GetViewStatus, | ||
this.sendTelemetry(LogLevel_1.default.ERROR, { | ||
ChatId: data.chatToken.ChatId, | ||
ChatId: data.chatToken.chatId, | ||
AMSClientVersion: config_1.sdkVersion, | ||
@@ -280,6 +285,7 @@ Event: PostMessageEventName_1.default.GetView, | ||
(function () { | ||
var clientId = fetchClientId_1.default(); | ||
var telemetryConfig = fetchTelemetryConfig_1.default(); | ||
var debugConfig = fetchDebugConfig_1.default(); | ||
!debugConfig.disable && console.log("[IframeCommunicator][init]"); | ||
var iframeCommunicator = new IframeCommunicator(); | ||
var iframeCommunicator = new IframeCommunicator(clientId); | ||
iframeCommunicator.initialize(); | ||
@@ -286,0 +292,0 @@ iframeCommunicator.setDebug(!debugConfig.disable); |
@@ -9,3 +9,4 @@ interface OmnichannelChatToken { | ||
voiceVideoCallToken?: string; | ||
amsEndpoint?: string; | ||
} | ||
export default OmnichannelChatToken; |
{ | ||
"name": "@microsoft/omnichannel-amsclient", | ||
"version": "0.1.0-main.8285125", | ||
"version": "0.1.0-main.27aeb69", | ||
"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
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
216927
92
1716