Skip to content

Commit 501349c

Browse files
author
Matt Mazzola
committed
Add unique id generation to base embed component and change 'report-id' header to 'uid'. Update tests with unique id.
1 parent 5184a96 commit 501349c

File tree

6 files changed

+254
-176
lines changed

6 files changed

+254
-176
lines changed

src/embed.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export interface IEmbedConfiguration {
3232
export type IGetGlobalAccessToken = () => string;
3333

3434
export interface IInternalEmbedConfiguration extends models.ILoadConfiguration {
35+
uniqueId: string;
3536
type: string;
3637
embedUrl: string;
3738
getGlobalAccessToken: IGetGlobalAccessToken;
@@ -78,6 +79,7 @@ export abstract class Embed {
7879
this.config = utils.assign({}, config);
7980
this.config.accessToken = this.getAccessToken(service.accessToken);
8081
this.config.embedUrl = this.getEmbedUrl();
82+
this.config.uniqueId = utils.createRandomString();
8183

8284
// TODO: The findIdFromEmbedUrl method is specific to Reports so it should be in the Report class, but it can't be called until
8385
// after we have fetched the embedUrl from the attributes
@@ -117,7 +119,7 @@ export abstract class Embed {
117119
throw errors;
118120
}
119121

120-
return this.hpm.post<void>('/report/load', config, { [`${this.config.type}-id`]: config.id })
122+
return this.hpm.post<void>('/report/load', config, { uid: this.config.uniqueId })
121123
.catch(response => {
122124
throw response.body;
123125
});

src/report.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export class Report extends embed.Embed {
2525
*/
2626
addFilter(filter: models.IFilter, target?: models.IPageTarget | models.IVisualTarget): Promise<void> {
2727
const targetUrl = this.getTargetUrl(target);
28-
return this.hpm.post<void>(`${targetUrl}/filters`, filter, { "report-id": this.config.id })
28+
return this.hpm.post<void>(`${targetUrl}/filters`, filter, { uid: this.config.uniqueId })
2929
.catch(response => {
3030
throw response.body;
3131
});
@@ -56,7 +56,7 @@ export class Report extends embed.Embed {
5656
*/
5757
getFilters(target?: models.IPageTarget | models.IVisualTarget): Promise<models.IFilter[]> {
5858
const targetUrl = this.getTargetUrl(target);
59-
return this.hpm.get<models.IFilter[]>(`${targetUrl}/filters`, { "report-id": this.config.id })
59+
return this.hpm.get<models.IFilter[]>(`${targetUrl}/filters`, { uid: this.config.uniqueId })
6060
.then(response => response.body,
6161
response => {
6262
throw response.body;
@@ -74,7 +74,7 @@ export class Report extends embed.Embed {
7474
* ```
7575
*/
7676
getPages(): Promise<models.IPage[]> {
77-
return this.hpm.get<models.IPage[]>('/report/pages', { "report-id": this.config.id })
77+
return this.hpm.get<models.IPage[]>('/report/pages', { uid: this.config.uniqueId })
7878
.then(response => response.body,
7979
response => {
8080
throw response.body;
@@ -98,7 +98,7 @@ export class Report extends embed.Embed {
9898
displayName: null
9999
};
100100

101-
return this.hpm.put<models.IError[]>('/report/pages/active', page, { "report-id": this.config.id })
101+
return this.hpm.put<models.IError[]>('/report/pages/active', page, { uid: this.config.uniqueId })
102102
.catch(response => {
103103
throw response.body;
104104
});
@@ -109,7 +109,7 @@ export class Report extends embed.Embed {
109109
*/
110110
removeFilter(filter: models.IFilter, target?: models.IPageTarget | models.IVisualTarget): Promise<void> {
111111
const targetUrl = this.getTargetUrl(target);
112-
return this.hpm.delete<models.IError[]>(`${targetUrl}/filters`, filter, { "report-id": this.config.id })
112+
return this.hpm.delete<models.IError[]>(`${targetUrl}/filters`, filter, { uid: this.config.uniqueId })
113113
.catch(response => {
114114
throw response.body;
115115
});
@@ -123,7 +123,7 @@ export class Report extends embed.Embed {
123123
* ```
124124
*/
125125
removeAllFilters(): Promise<void> {
126-
return this.hpm.delete<models.IError[]>('/report/allfilters', null, { "report-id": this.config.id })
126+
return this.hpm.delete<models.IError[]>('/report/allfilters', null, { uid: this.config.uniqueId })
127127
.catch(response => {
128128
throw response.body;
129129
});
@@ -136,7 +136,7 @@ export class Report extends embed.Embed {
136136
*/
137137
updateFilter(filter: models.IFilter, target?: models.IPageTarget | models.IVisualTarget): Promise<void> {
138138
const targetUrl = this.getTargetUrl(target);
139-
return this.hpm.put<models.IError[]>(`${targetUrl}/filters`, filter, { "report-id": this.config.id })
139+
return this.hpm.put<models.IError[]>(`${targetUrl}/filters`, filter, { uid: this.config.uniqueId })
140140
.catch(response => {
141141
throw response.body;
142142
});
@@ -146,7 +146,7 @@ export class Report extends embed.Embed {
146146
* Update settings of report (filter pane visibility, page navigation visibility)
147147
*/
148148
updateSettings(settings: models.ISettings): Promise<void> {
149-
return this.hpm.patch<models.IError[]>('/report/settings', settings, { "report-id": this.config.id })
149+
return this.hpm.patch<models.IError[]>('/report/settings', settings, { uid: this.config.uniqueId })
150150
.catch(response => {
151151
throw response.body;
152152
});

src/service.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,30 +88,30 @@ export class Service {
8888
/**
8989
* Add handler for report events
9090
*/
91-
this.router.post(`/reports/:reportId/events/:eventName`, (req, res) => {
91+
this.router.post(`/reports/:uniqueId/events/:eventName`, (req, res) => {
9292
const event: IEvent<any> = {
9393
type: 'report',
94-
id: req.params.reportId,
94+
id: req.params.uniqueId,
9595
name: req.params.eventName,
9696
value: req.body
9797
};
9898

9999
this.handleEvent(event);
100100
});
101-
this.router.post(`/reports/:reportId/pages/:pageName/events/:eventName`, (req, res) => {
101+
this.router.post(`/reports/:uniqueId/pages/:pageName/events/:eventName`, (req, res) => {
102102
const event: IEvent<any> = {
103103
type: 'report',
104-
id: req.params.reportId,
104+
id: req.params.uniqueId,
105105
name: req.params.eventName,
106106
value: req.body
107107
};
108108

109109
this.handleEvent(event);
110110
});
111-
this.router.post(`/reports/:reportId/visuals/:pageName/events/:eventName`, (req, res) => {
111+
this.router.post(`/reports/:uniqueId/visuals/:pageName/events/:eventName`, (req, res) => {
112112
const event: IEvent<any> = {
113113
type: 'report',
114-
id: req.params.reportId,
114+
id: req.params.uniqueId,
115115
name: req.params.eventName,
116116
value: req.body
117117
};
@@ -244,7 +244,7 @@ export class Service {
244244
const embed = utils.find(embed => {
245245
const config = embed.getConfig();
246246
return (config.type === event.type
247-
&& config.id === event.id);
247+
&& config.uniqueId === event.id);
248248
}, this.embeds);
249249

250250
if(embed) {

src/util.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,8 @@ export function assign(...args) {
7373
}
7474
}
7575
return output;
76-
};
76+
}
77+
78+
export function createRandomString(): string {
79+
return (Math.random() + 1).toString(36).substring(7);
80+
}

0 commit comments

Comments
 (0)