forked from microsoft/PowerBI-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice.ts
742 lines (640 loc) · 24.2 KB
/
service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
/* eslint-disable @typescript-eslint/prefer-function-type */
/* eslint-disable @typescript-eslint/no-unused-vars */
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
import { WindowPostMessageProxy } from 'window-post-message-proxy';
import { HttpPostMessage } from 'http-post-message';
import { Router, IExtendedRequest, Response as IExtendedResponse } from 'powerbi-router';
import { IPage, IQuickCreateConfiguration, IReportCreateConfiguration } from 'powerbi-models';
import {
Embed,
IBootstrapEmbedConfiguration,
IDashboardEmbedConfiguration,
IEmbedConfiguration,
IEmbedConfigurationBase,
IQnaEmbedConfiguration,
IReportEmbedConfiguration,
ITileEmbedConfiguration,
IVisualEmbedConfiguration,
} from './embed';
import { Report } from './report';
import { Create } from './create';
import { Dashboard } from './dashboard';
import { Tile } from './tile';
import { Page } from './page';
import { Qna } from './qna';
import { Visual } from './visual';
import * as utils from './util';
import { QuickCreate } from './quickCreate';
import * as sdkConfig from './config';
import { invalidEmbedUrlErrorMessage } from './errors';
export interface IEvent<T> {
type: string;
id: string;
name: string;
value: T;
}
/**
* @hidden
*/
export interface ICustomEvent<T> extends CustomEvent {
detail: T;
}
/**
* @hidden
*/
export interface IEventHandler<T> {
(event: ICustomEvent<T>): any;
}
/**
* @hidden
*/
export interface IHpmFactory {
(wpmp: WindowPostMessageProxy, targetWindow?: Window, version?: string, type?: string, origin?: string): HttpPostMessage;
}
/**
* @hidden
*/
export interface IWpmpFactory {
(name?: string, logMessages?: boolean, eventSourceOverrideWindow?: Window): WindowPostMessageProxy;
}
/**
* @hidden
*/
export interface IRouterFactory {
(wpmp: WindowPostMessageProxy): Router;
}
export interface IPowerBiElement extends HTMLElement {
powerBiEmbed: Embed;
}
export interface IDebugOptions {
logMessages?: boolean;
wpmpName?: string;
}
export interface IServiceConfiguration extends IDebugOptions {
autoEmbedOnContentLoaded?: boolean;
onError?: (error: any) => any;
version?: string;
type?: string;
sdkWrapperVersion?: string;
}
export interface IService {
hpm: HttpPostMessage;
}
export type IComponentEmbedConfiguration = IReportEmbedConfiguration | IDashboardEmbedConfiguration | ITileEmbedConfiguration | IVisualEmbedConfiguration | IQnaEmbedConfiguration;
/**
* @hidden
*/
export type EmbedComponentFactory = (service: Service, element: HTMLElement, config: IEmbedConfigurationBase, phasedRender?: boolean, isBootstrap?: boolean) => Embed;
/**
* The Power BI Service embed component, which is the entry point to embed all other Power BI components into your application
*
* @export
* @class Service
* @implements {IService}
*/
export class Service implements IService {
/**
* A list of components that this service can embed
*/
private static components: (typeof Report | typeof Tile | typeof Dashboard | typeof Qna | typeof Visual)[] = [
Tile,
Report,
Dashboard,
Qna,
Visual
];
/**
* The default configuration for the service
*/
private static defaultConfig: IServiceConfiguration = {
autoEmbedOnContentLoaded: false,
onError: (...args) => console.log(args[0], args.slice(1))
};
/**
* Gets or sets the access token as the global fallback token to use when a local token is not provided for a report or tile.
*
* @type {string}
* @hidden
*/
accessToken: string;
/** The Configuration object for the service*/
private config: IServiceConfiguration;
/** A list of Power BI components that have been embedded using this service instance. */
private embeds: Embed[];
/** TODO: Look for way to make hpm private without sacrificing ease of maintenance. This should be private but in embed needs to call methods.
*
* @hidden
*/
hpm: HttpPostMessage;
/** TODO: Look for way to make wpmp private. This is only public to allow stopping the wpmp in tests
*
* @hidden
*/
wpmp: WindowPostMessageProxy;
router: Router;
private uniqueSessionId: string;
/**
* @hidden
*/
private registeredComponents: { [componentType: string]: EmbedComponentFactory } = {};
/**
* Creates an instance of a Power BI Service.
*
* @param {IHpmFactory} hpmFactory The http post message factory used in the postMessage communication layer
* @param {IWpmpFactory} wpmpFactory The window post message factory used in the postMessage communication layer
* @param {IRouterFactory} routerFactory The router factory used in the postMessage communication layer
* @param {IServiceConfiguration} [config={}]
* @hidden
*/
constructor(hpmFactory: IHpmFactory, wpmpFactory: IWpmpFactory, routerFactory: IRouterFactory, config: IServiceConfiguration = {}) {
this.wpmp = wpmpFactory(config.wpmpName, config.logMessages);
this.hpm = hpmFactory(this.wpmp, null, config.version, config.type, config.sdkWrapperVersion);
this.router = routerFactory(this.wpmp);
this.uniqueSessionId = utils.generateUUID();
/**
* Adds handler for report events.
*/
this.router.post(`/reports/:uniqueId/events/:eventName`, (req, _res) => {
const event: IEvent<any> = {
type: 'report',
id: req.params.uniqueId as string,
name: req.params.eventName as string,
value: req.body
};
this.handleEvent(event);
});
this.router.post(`/reports/:uniqueId/pages/:pageName/events/:eventName`, (req, _res) => {
const event: IEvent<any> = {
type: 'report',
id: req.params.uniqueId as string,
name: req.params.eventName as string,
value: req.body
};
this.handleEvent(event);
});
this.router.post(`/reports/:uniqueId/pages/:pageName/visuals/:visualName/events/:eventName`, (req, _res) => {
const event: IEvent<any> = {
type: 'report',
id: req.params.uniqueId as string,
name: req.params.eventName as string,
value: req.body
};
this.handleEvent(event);
});
this.router.post(`/dashboards/:uniqueId/events/:eventName`, (req, _res) => {
const event: IEvent<any> = {
type: 'dashboard',
id: req.params.uniqueId as string,
name: req.params.eventName as string,
value: req.body
};
this.handleEvent(event);
});
this.router.post(`/tile/:uniqueId/events/:eventName`, (req, _res) => {
const event: IEvent<any> = {
type: 'tile',
id: req.params.uniqueId as string,
name: req.params.eventName as string,
value: req.body
};
this.handleEvent(event);
});
/**
* Adds handler for Q&A events.
*/
this.router.post(`/qna/:uniqueId/events/:eventName`, (req, _res) => {
const event: IEvent<any> = {
type: 'qna',
id: req.params.uniqueId as string,
name: req.params.eventName as string,
value: req.body
};
this.handleEvent(event);
});
/**
* Adds handler for front load 'ready' message.
*/
this.router.post(`/ready/:uniqueId`, (req, _res) => {
const event: IEvent<any> = {
type: 'report',
id: req.params.uniqueId as string,
name: 'ready',
value: req.body
};
this.handleEvent(event);
});
this.embeds = [];
// TODO: Change when Object.assign is available.
this.config = utils.assign({}, Service.defaultConfig, config);
if (this.config.autoEmbedOnContentLoaded) {
this.enableAutoEmbed();
}
}
/**
* Creates new report
*
* @param {HTMLElement} element
* @param {IEmbedConfiguration} [config={}]
* @returns {Embed}
*/
createReport(element: HTMLElement, config: IEmbedConfiguration | IReportCreateConfiguration): Embed {
config.type = 'create';
const powerBiElement = element as IPowerBiElement;
const component = new Create(this, powerBiElement, config);
powerBiElement.powerBiEmbed = component;
this.addOrOverwriteEmbed(component, element);
return component;
}
/**
* Creates new dataset
*
* @param {HTMLElement} element
* @param {IEmbedConfiguration} [config={}]
* @returns {Embed}
*/
quickCreate(element: HTMLElement, config: IQuickCreateConfiguration): Embed {
config.type = 'quickCreate';
const powerBiElement = element as IPowerBiElement;
const component = new QuickCreate(this, powerBiElement, config);
powerBiElement.powerBiEmbed = component;
this.addOrOverwriteEmbed(component, element);
return component;
}
/**
* TODO: Add a description here
*
* @param {HTMLElement} [container]
* @param {IEmbedConfiguration} [config=undefined]
* @returns {Embed[]}
* @hidden
*/
init(container?: HTMLElement, config: IEmbedConfiguration = undefined): Embed[] {
container = (container && container instanceof HTMLElement) ? container : document.body;
const elements = Array.prototype.slice.call(container.querySelectorAll(`[${Embed.embedUrlAttribute}]`));
return elements.map((element) => this.embed(element, config));
}
/**
* Given a configuration based on an HTML element,
* if the component has already been created and attached to the element, reuses the component instance and existing iframe,
* otherwise creates a new component instance.
*
* @param {HTMLElement} element
* @param {IEmbedConfigurationBase} [config={}]
* @returns {Embed}
*/
embed(element: HTMLElement, config: IComponentEmbedConfiguration | IEmbedConfigurationBase = {}): Embed {
return this.embedInternal(element, config);
}
/**
* Given a configuration based on an HTML element,
* if the component has already been created and attached to the element, reuses the component instance and existing iframe,
* otherwise creates a new component instance.
* This is used for the phased embedding API, once element is loaded successfully, one can call 'render' on it.
*
* @param {HTMLElement} element
* @param {IEmbedConfigurationBase} [config={}]
* @returns {Embed}
*/
load(element: HTMLElement, config: IComponentEmbedConfiguration | IEmbedConfigurationBase = {}): Embed {
return this.embedInternal(element, config, /* phasedRender */ true, /* isBootstrap */ false);
}
/**
* Given an HTML element and entityType, creates a new component instance, and bootstrap the iframe for embedding.
*
* @param {HTMLElement} element
* @param {IBootstrapEmbedConfiguration} config: a bootstrap config which is an embed config without access token.
*/
bootstrap(element: HTMLElement, config: IComponentEmbedConfiguration | IBootstrapEmbedConfiguration): Embed {
return this.embedInternal(element, config, /* phasedRender */ false, /* isBootstrap */ true);
}
/** @hidden */
embedInternal(element: HTMLElement, config: IComponentEmbedConfiguration | IEmbedConfigurationBase = {}, phasedRender?: boolean, isBootstrap?: boolean): Embed {
let component: Embed;
const powerBiElement = element as IPowerBiElement;
if (powerBiElement.powerBiEmbed) {
if (isBootstrap) {
throw new Error(`Attempted to bootstrap element ${element.outerHTML}, but the element is already a powerbi element.`);
}
component = this.embedExisting(powerBiElement, config, phasedRender);
}
else {
component = this.embedNew(powerBiElement, config, phasedRender, isBootstrap);
}
return component;
}
/** @hidden */
getNumberOfComponents(): number {
if (!this.embeds) {
return 0;
}
return this.embeds.length;
}
/** @hidden */
getSdkSessionId(): string {
return this.uniqueSessionId;
}
/**
* Returns the Power BI Client SDK version
*
* @hidden
*/
getSDKVersion(): string {
return sdkConfig.default.version;
}
/**
* Given a configuration based on a Power BI element, saves the component instance that reference the element for later lookup.
*
* @private
* @param {IPowerBiElement} element
* @param {IEmbedConfigurationBase} config
* @param {boolean} phasedRender
* @param {boolean} isBootstrap
* @returns {Embed}
* @hidden
*/
private embedNew(element: IPowerBiElement, config: IComponentEmbedConfiguration | IEmbedConfigurationBase, phasedRender?: boolean, isBootstrap?: boolean): Embed {
const componentType = config.type || element.getAttribute(Embed.typeAttribute);
if (!componentType) {
const scrubbedConfig = { ...config, accessToken: "" };
throw new Error(`Attempted to embed using config ${JSON.stringify(scrubbedConfig)} on element ${element.outerHTML}, but could not determine what type of component to embed. You must specify a type in the configuration or as an attribute such as '${Embed.typeAttribute}="${Report.type.toLowerCase()}"'.`);
}
// Saves the type as part of the configuration so that it can be referenced later at a known location.
config.type = componentType;
const component = this.createEmbedComponent(componentType, element, config, phasedRender, isBootstrap);
element.powerBiEmbed = component;
this.addOrOverwriteEmbed(component, element);
return component;
}
/**
* Given component type, creates embed component instance
*
* @private
* @param {string} componentType
* @param {HTMLElement} element
* @param {IEmbedConfigurationBase} config
* @param {boolean} phasedRender
* @param {boolean} isBootstrap
* @returns {Embed}
* @hidden
*/
private createEmbedComponent(componentType: string, element: HTMLElement, config: IEmbedConfigurationBase, phasedRender?: boolean, isBootstrap?: boolean): Embed {
const Component = utils.find((embedComponent) => componentType === embedComponent.type.toLowerCase(), Service.components);
if (Component) {
return new Component(this, element, config, phasedRender, isBootstrap);
}
// If component type is not legacy, search in registered components
const registeredComponent = utils.find((registeredComponentType) => componentType.toLowerCase() === registeredComponentType.toLowerCase(), Object.keys(this.registeredComponents));
if (!registeredComponent) {
throw new Error(`Attempted to embed component of type: ${componentType} but did not find any matching component. Please verify the type you specified is intended.`);
}
return this.registeredComponents[registeredComponent](this, element, config, phasedRender, isBootstrap);
}
/**
* Given an element that already contains an embed component, load with a new configuration.
*
* @private
* @param {IPowerBiElement} element
* @param {IEmbedConfigurationBase} config
* @returns {Embed}
* @hidden
*/
private embedExisting(element: IPowerBiElement, config: IComponentEmbedConfiguration | IEmbedConfigurationBase, phasedRender?: boolean): Embed {
const component = utils.find((x) => x.element === element, this.embeds);
if (!component) {
const scrubbedConfig = { ...config, accessToken: "" };
throw new Error(`Attempted to embed using config ${JSON.stringify(scrubbedConfig)} on element ${element.outerHTML} which already has embedded component associated, but could not find the existing component in the list of active components. This could indicate the embeds list is out of sync with the DOM, or the component is referencing the incorrect HTML element.`);
}
// TODO: Multiple embedding to the same iframe is not supported in QnA
if (config.type && config.type.toLowerCase() === "qna") {
return this.embedNew(element, config);
}
/**
* TODO: Dynamic embed type switching could be supported but there is work needed to prepare the service state and DOM cleanup.
* remove all event handlers from the DOM, then reset the element to initial state which removes iframe, and removes from list of embeds
* then we can call the embedNew function which would allow setting the proper embedUrl and construction of object based on the new type.
*/
if (typeof config.type === "string" && config.type !== component.config.type) {
/**
* When loading report after create we want to use existing Iframe to optimize load period
*/
if (config.type === "report" && utils.isCreate(component.config.type)) {
const report = new Report(this, element, config, /* phasedRender */ false, /* isBootstrap */ false, element.powerBiEmbed.iframe);
component.populateConfig(config, /* isBootstrap */ false);
report.load();
element.powerBiEmbed = report;
this.addOrOverwriteEmbed(component, element);
return report;
}
const scrubbedConfig = { ...config, accessToken: "" };
throw new Error(`Embedding on an existing element with a different type than the previous embed object is not supported. Attempted to embed using config ${JSON.stringify(scrubbedConfig)} on element ${element.outerHTML}, but the existing element contains an embed of type: ${this.config.type} which does not match the new type: ${config.type}`);
}
component.populateConfig(config, /* isBootstrap */ false);
component.load(phasedRender);
return component;
}
/**
* Adds an event handler for DOMContentLoaded, which searches the DOM for elements that have the 'powerbi-embed-url' attribute,
* and automatically attempts to embed a powerbi component based on information from other powerbi-* attributes.
*
* Note: Only runs if `config.autoEmbedOnContentLoaded` is true when the service is created.
* This handler is typically useful only for applications that are rendered on the server so that all required data is available when the handler is called.
*
* @hidden
*/
enableAutoEmbed(): void {
window.addEventListener('DOMContentLoaded', (_event: Event) => this.init(document.body), false);
}
/**
* Returns an instance of the component associated with the element.
*
* @param {HTMLElement} element
* @returns {(Report | Tile)}
*/
get(element: HTMLElement): Embed {
const powerBiElement = element as IPowerBiElement;
if (!powerBiElement.powerBiEmbed) {
throw new Error(`You attempted to get an instance of powerbi component associated with element: ${element.outerHTML} but there was no associated instance.`);
}
return powerBiElement.powerBiEmbed;
}
/**
* Finds an embed instance by the name or unique ID that is provided.
*
* @param {string} uniqueId
* @returns {(Report | Tile)}
* @hidden
*/
find(uniqueId: string): Embed {
return utils.find((x) => x.config.uniqueId === uniqueId, this.embeds);
}
/**
* Removes embed components whose container element is same as the given element
*
* @param {Embed} component
* @param {HTMLElement} element
* @returns {void}
* @hidden
*/
addOrOverwriteEmbed(component: Embed, element: HTMLElement): void {
// remove embeds over the same div element.
this.embeds = this.embeds.filter(function (embed) {
return embed.element !== element;
});
this.embeds.push(component);
}
/**
* Given an HTML element that has a component embedded within it, removes the component from the list of embedded components, removes the association between the element and the component, and removes the iframe.
*
* @param {HTMLElement} element
* @returns {void}
*/
reset(element: HTMLElement): void {
const powerBiElement = element as IPowerBiElement;
if (!powerBiElement.powerBiEmbed) {
return;
}
/** Removes the element frontLoad listener if exists. */
const embedElement = powerBiElement.powerBiEmbed;
if (embedElement.frontLoadHandler) {
embedElement.element.removeEventListener('ready', embedElement.frontLoadHandler, false);
}
/** Removes all event handlers. */
embedElement.allowedEvents.forEach((eventName) => {
embedElement.off(eventName);
});
/** Removes the component from an internal list of components. */
utils.remove((x) => x === powerBiElement.powerBiEmbed, this.embeds);
/** Deletes a property from the HTML element. */
delete powerBiElement.powerBiEmbed;
/** Removes the iframe from the element. */
const iframe = element.querySelector('iframe');
if (iframe) {
if (iframe.remove !== undefined) {
iframe.remove();
}
else {
/** Workaround for IE: unhandled rejection TypeError: object doesn't support property or method 'remove' */
iframe.parentElement.removeChild(iframe);
}
}
}
/**
* handles tile events
*
* @param {IEvent<any>} event
* @hidden
*/
handleTileEvents(event: IEvent<any>): void {
if (event.type === 'tile') {
this.handleEvent(event);
}
}
async invokeSDKHook(hook: Function, req: IExtendedRequest, res: IExtendedResponse): Promise<void> {
if (!hook) {
res.send(404, null);
return;
}
try {
let result = await hook(req.body);
res.send(200, result);
} catch (error) {
res.send(400, null);
console.error(error);
}
}
/**
* Given an event object, finds the embed component with the matching type and ID, and invokes its handleEvent method with the event object.
*
* @private
* @param {IEvent<any>} event
* @hidden
*/
private handleEvent(event: IEvent<any>): void {
const embed = utils.find(embed => {
return (embed.config.uniqueId === event.id);
}, this.embeds);
if (embed) {
const value = event.value;
if (event.name === 'pageChanged') {
const pageKey = 'newPage';
const page: IPage = value[pageKey];
if (!page) {
throw new Error(`Page model not found at 'event.value.${pageKey}'.`);
}
value[pageKey] = new Page(embed, page.name, page.displayName, true /* isActive */);
}
utils.raiseCustomEvent(embed.element, event.name, value);
}
}
/**
* API for warm starting powerbi embedded endpoints.
* Use this API to preload Power BI Embedded in the background.
*
* @public
* @param {IEmbedConfigurationBase} [config={}]
* @param {HTMLElement} [element=undefined]
*/
preload(config: IComponentEmbedConfiguration | IEmbedConfigurationBase, element?: HTMLElement): HTMLIFrameElement {
if (!utils.validateEmbedUrl(config.embedUrl)) {
throw new Error(invalidEmbedUrlErrorMessage);
}
const iframeContent = document.createElement("iframe");
iframeContent.setAttribute("style", "display:none;");
iframeContent.setAttribute("src", config.embedUrl);
iframeContent.setAttribute("scrolling", "no");
iframeContent.setAttribute("allowfullscreen", "false");
let node = element;
if (!node) {
node = document.getElementsByTagName("body")[0];
}
node.appendChild(iframeContent);
iframeContent.onload = () => {
utils.raiseCustomEvent(iframeContent, "preloaded", {});
};
return iframeContent;
}
/**
* Use this API to set SDK info
*
* @hidden
* @param {string} type
* @returns {void}
*/
setSdkInfo(type: string, version: string): void {
this.hpm.defaultHeaders['x-sdk-type'] = type;
this.hpm.defaultHeaders['x-sdk-wrapper-version'] = version;
}
/**
* API for registering external components
*
* @hidden
* @param {string} componentType
* @param {EmbedComponentFactory} embedComponentFactory
* @param {string[]} routerEventUrls
*/
register(componentType: string, embedComponentFactory: EmbedComponentFactory, routerEventUrls: string[]): void {
if (utils.find((embedComponent) => componentType.toLowerCase() === embedComponent.type.toLowerCase(), Service.components)) {
throw new Error('The component name is reserved. Cannot register a component with this name.');
}
if (utils.find((registeredComponentType) => componentType.toLowerCase() === registeredComponentType.toLowerCase(), Object.keys(this.registeredComponents))) {
throw new Error('A component with this type is already registered.');
}
this.registeredComponents[componentType] = embedComponentFactory;
routerEventUrls.forEach(url => {
if (!url.includes(':uniqueId') || !url.includes(':eventName')) {
throw new Error('Invalid router event URL');
}
this.router.post(url, (req, _res) => {
const event: IEvent<any> = {
type: componentType,
id: req.params.uniqueId as string,
name: req.params.eventName as string,
value: req.body
};
this.handleEvent(event);
});
});
}
}