forked from microsoft/PowerBI-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquickCreate.ts
119 lines (104 loc) · 2.77 KB
/
quickCreate.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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
import { IError, IQuickCreateConfiguration, validateQuickCreate } from 'powerbi-models';
import { Service } from './service';
import { Embed, IEmbedConfigurationBase, ISessionHeaders } from './embed';
/**
* A Power BI Quick Create component
*
* @export
* @class QuickCreate
* @extends {Embed}
*/
export class QuickCreate extends Embed {
/**
* Gets or sets the configuration settings for creating report.
*
* @type {IQuickCreateConfiguration}
* @hidden
*/
createConfig: IQuickCreateConfiguration;
/*
* @hidden
*/
constructor(service: Service, element: HTMLElement, config: IQuickCreateConfiguration, phasedRender?: boolean, isBootstrap?: boolean) {
super(service, element, config, /* iframe */ undefined, phasedRender, isBootstrap);
service.router.post(`/reports/${this.config.uniqueId}/eventHooks/:eventName`, async (req, _res) => {
switch (req.params.eventName) {
case "newAccessToken":
req.body = req.body || {};
req.body.report = this;
await service.invokeSDKHook(this.eventHooks?.accessTokenProvider, req, _res);
break;
default:
break;
}
});
}
/**
* Override the getId abstract function
* QuickCreate does not need any ID
*
* @returns {string}
*/
getId(): string {
return null;
}
/**
* Validate create report configuration.
*/
validate(config: IEmbedConfigurationBase): IError[] {
return validateQuickCreate(config);
}
/**
* Handle config changes.
*
* @hidden
* @returns {void}
*/
configChanged(isBootstrap: boolean): void {
if (isBootstrap) {
return;
}
this.createConfig = this.config as IQuickCreateConfiguration;
}
/**
* @hidden
* @returns {string}
*/
getDefaultEmbedUrlEndpoint(): string {
return "quickCreate";
}
/**
* Sends quickCreate configuration data.
*
* ```javascript
* quickCreate({
* accessToken: 'eyJ0eXA ... TaE2rTSbmg',
* datasetCreateConfig: {}})
* ```
*
* @hidden
* @param {IQuickCreateConfiguration} createConfig
* @returns {Promise<void>}
*/
async create(): Promise<void> {
const errors = validateQuickCreate(this.createConfig);
if (errors) {
throw errors;
}
try {
const headers: ISessionHeaders = {
uid: this.config.uniqueId,
sdkSessionId: this.service.getSdkSessionId()
};
if (!!this.eventHooks?.accessTokenProvider) {
headers.tokenProviderSupplied = true;
}
const response = await this.service.hpm.post<void>("/quickcreate", this.createConfig, headers, this.iframe.contentWindow);
return response.body;
} catch (response) {
throw response.body;
}
}
}