/
exmg-swagger-client-config.js
147 lines (132 loc) · 3.67 KB
/
exmg-swagger-client-config.js
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
import {PolymerElement} from '@polymer/polymer/polymer-element.js';
/**
* @namespace Exmg
*/
window.Exmg = window.Exmg || {};
/**
* Element for initializing Swagger Client Api's
*
* ```html
* <exmg-swagger-client-config swagger-url="[[swaggerUrl]]"></exmg-swagger-client-config>
* ```
*
* @customElement
* @polymer
* @group Exmg Core Elements
* @element exmg-swagger-client-config
* @memberof Exmg
* @extends Polymer.Element
* @summary Custom element to initialize Swagger Client Api's
*/
export class DataConnectionConfigElement extends PolymerElement {
static get is() {
return 'exmg-swagger-client-config';
}
static get properties() {
return {
/**
* The name of your config.
*
* You can use this with the `configName` property of other client element
* in order to use multiple configurations on a page at once.
* In that case the name is used as a key to lookup the configuration.
*/
name: {
type: String,
value: ''
},
/**
* Url pointing to the swagger 2.0 json file
*/
swaggerUrl: {
type: String,
observer: '_swaggerUrlChanged',
},
/**
* By default the server url from json file will be used. This serverUrl property is optional
* and can be used as an override.
*/
serverUrl: {
type: String,
observer: '_serverUrlChanged',
},
/**
* hook into the response globaly
*/
responseInterceptor: Function,
/**
* hook into the requests globaly
*/
requestInterceptor: Function,
/**
* Gives the initialization state of the API
*/
apiInitialized: {
type: Boolean,
notify: true,
readOnly: true,
value: false,
},
};
}
_swaggerUrlChanged(url) {
this._reset();
this.initSwagger();
}
_serverUrlChanged(url) {
if (Exmg.swaggerClients.getApi(this.name) && this.serverUrl) {
Exmg.swaggerClients.getApi(this.name).url = this.serverUrl;
}
}
_reset() {
this._setApiInitialized(false);
Exmg.swaggerClients.setApi(this.name, null);
}
initSwagger() {
/* Load swagger api */
this.swaggerClient = new SwaggerClient(this.swaggerUrl, {
url: this.serverUrl,
responseInterceptor: this.responseInterceptor,
requestInterceptor: this.requestInterceptor
}).then((swaggerClient) => {
/* enable cors */
swaggerClient.http.withCredentials = true;
swaggerClient.name = this.name;
/* if server url is different then the one specified in api */
if (this.serverUrl && this.serverUrl !== swaggerClient.url) {
swaggerClient.url = this.serverUrl;
}
/* Add swagger api to Exmg scope */
window.Exmg.swaggerClients.setApi(this.name, swaggerClient);
this.dispatchEvent(new CustomEvent('swagger-api-initialized', {bubbles: true, composed: true, detail: {
name: this.name, swaggerClient: swaggerClient}
}));
setTimeout(_ => this._setApiInitialized(true), 0);
return swaggerClient;
});
return this.swaggerClient;
}
}
window.customElements.define(DataConnectionConfigElement.is, DataConnectionConfigElement);
Exmg.DataConnectionConfigElement = DataConnectionConfigElement;
if (!window.Exmg.swaggerClients) {
class ExmgSwaggerClient {
constructor() {
this.apis = {};
}
setApi(name, api) {
this.apis[name] = api;
}
getApi(name) {
return this.apis[name];
}
api(name) {
const api = this.apis[name];
if (!api) {
throw new Error(`Api ${api} not found`);
}
return api;
}
}
window.Exmg.swaggerClients = new ExmgSwaggerClient();
}