forked from microsoft/PowerBI-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate.ts
54 lines (45 loc) · 1.61 KB
/
create.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
import * as service from './service';
import * as models from 'powerbi-models';
import * as embed from './embed';
export class Create extends embed.Embed {
constructor(service: service.Service, element: HTMLElement, config: embed.IEmbedConfiguration) {
super(service, element, config);
}
/**
* Gets the dataset ID from the first available location: createConfig or embed url.
*
* @returns {string}
*/
getId(): string {
const datasetId = (this.createConfig && this.createConfig.datasetId) ? this.createConfig.datasetId : Create.findIdFromEmbedUrl(this.config.embedUrl);
if (typeof datasetId !== 'string' || datasetId.length === 0) {
throw new Error('Dataset id is required, but it was not found. You must provide an id either as part of embed configuration.');
}
return datasetId;
}
/**
* Validate create report configuration.
*/
validate(config: models.IReportCreateConfiguration): models.IError[] {
return models.validateCreateReport(config);
}
/**
* Adds the ability to get datasetId from url.
* (e.g. http://embedded.powerbi.com/appTokenReportEmbed?datasetId=854846ed-2106-4dc2-bc58-eb77533bf2f1).
*
* By extracting the ID we can ensure that the ID is always explicitly provided as part of the create configuration.
*
* @static
* @param {string} url
* @returns {string}
*/
static findIdFromEmbedUrl(url: string): string {
const datasetIdRegEx = /datasetId="?([^&]+)"?/
const datasetIdMatch = url.match(datasetIdRegEx);
let datasetId;
if (datasetIdMatch) {
datasetId = datasetIdMatch[1];
}
return datasetId;
}
}