Repository navigation
Expand file tree
/
Copy pathApiConsoleApp.js
More file actions
438 lines (406 loc) · 12.5 KB
/
Copy pathApiConsoleApp.js
File metadata and controls
438 lines (406 loc) · 12.5 KB
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
/* eslint-disable class-methods-use-this */
/* eslint-disable prefer-destructuring */
import { html } from 'lit-element';
import '@polymer/app-layout/app-drawer/app-drawer.js';
import '@polymer/app-layout/app-drawer-layout/app-drawer-layout.js';
import '@polymer/app-layout/app-header/app-header.js';
import '@polymer/app-layout/app-header-layout/app-header-layout.js';
import '@polymer/app-layout/app-toolbar/app-toolbar.js';
import '@api-components/api-request/xhr-simple-request.js';
import '@advanced-rest-client/oauth-authorization/oauth1-authorization.js';
import '@advanced-rest-client/oauth-authorization/oauth2-authorization.js';
import '@anypoint-web-components/anypoint-button/anypoint-icon-button.js';
import '@polymer/iron-media-query/iron-media-query.js';
import '@advanced-rest-client/arc-icons/arc-icon.js';
import { ApiConsole } from './ApiConsole.js';
import styles from './ApiConsoleAppStyles.js';
/** @typedef {import('lit-html').TemplateResult} TemplateResult */
/** @typedef {import('lit-element').CSSResult} CSSResult */
export class ApiConsoleApp extends ApiConsole {
/**
* @type {CSSResult[]}
*/
static get styles() {
return [
/** @type CSSResult */ (ApiConsole.styles),
styles,
];
}
static get properties() {
return {
/**
* Computed title of the API
*/
apiTitle: { type: String },
/**
* True when the main layout element renders in narrow view.
* This changes when media query breakpoint has been reached or
* when narrow property is set.
*/
layoutNarrow: { type: Boolean, reflect: true },
/**
* An alignment of the layout drawer.
* Possible values are:
* - start
* - end
*
* Default to "start".
*/
drawerAlign: { type: String },
/**
* A width when the navigation drawer is automatically toggled to narrow
* view.
* By default it is `640px`.
*
* To control width of the navigation drawer, set `--app-drawer-width`
* CSS variable to requested size.
*/
responsiveWidth: { type: String },
/**
* When true it places try it panel next to the documentation panel.
* It is set automatically via media queries
*/
wideLayout: { type: Boolean },
/**
* Forces the console to send headers defined in this string overriding
* any used defined header.
* It can be useful if the console has to send any headers string
* to a server without user knowing about it.
* The headers should be valid HTTP headers string.
*/
appendHeaders: { type: String },
/**
* If set every request made from the console will be proxied by the service provided in this
* value.
* It will prefix entered URL with the proxy value. so the call to
* `http://domain.com/path/?query=some+value` will become
* `https://proxy.com/path/http://domain.com/path/?query=some+value`
*
* If the proxy require a to pass the URL as a query parameter define value as follows:
* `https://proxy.com/path/?url=`. In this case be sure to set `proxy-encode-url`
* attribute.
*/
proxy: { type: String },
/**
* If `proxy` is set, it will URL encode the request URL before appending it to the proxy URL.
* `http://domain.com/path/?query=some+value` will become
* `https://proxy.com/?url=http%3A%2F%2Fdomain.com%2Fpath%2F%3Fquery%3Dsome%2Bvalue`
*/
proxyEncodeUrl: { type: Boolean },
_renderInlineTryit: { type: Boolean },
};
}
get wideLayout() {
return this._wideLayout;
}
set wideLayout(value) {
const old = this._wideLayout;
/* istanbul ignore if */
if (old === value) {
return;
}
this._wideLayout = value;
this._updateRenderInlineTyit();
}
// @ts-ignore
get inlineMethods() {
return this._inlineMethods;
}
set inlineMethods(value) {
const old = this._inlineMethods;
/* istanbul ignore if */
if (old === value) {
return;
}
this._inlineMethods = value;
this._updateRenderInlineTyit();
}
// @ts-ignore
get selectedShapeType() {
return this._selectedShapeType;
}
set selectedShapeType(value) {
const old = this._selectedShapeType;
/* istanbul ignore if */
if (old === value) {
return;
}
this._selectedShapeType = value;
this._isMethod = value === 'method';
this._updateRenderInlineTyit();
this.requestUpdate('selectedShapeType', old);
}
/**
* Overrides api console's `_noServerSelector`.
* @return {boolean} True when `noServerSelector` or when wide layout is set.
*/
// @ts-ignore
get _noServerSelector() {
return this.noServerSelector || this.wideLayout;
}
/**
* @return {Boolean} True when the request panel is being rendered
*/
// @ts-ignore
get _rendersRequestPanel() {
return this.page === 'request' || this.wideLayout;
}
constructor() {
super();
this.responsiveWidth = '900px';
this.allowHideOptional = true;
this.manualNavigation = false;
this.layoutNarrow = false;
this.appendHeaders = undefined;
this.proxy = undefined;
this.proxyEncodeUrl = undefined;
this.drawerAlign = 'left';
}
connectedCallback() {
super.connectedCallback();
window.addEventListener('popstate', this._onRoute.bind(this));
}
_updateRenderInlineTyit() {
const value = this._computeRenderInlineTryIt(this.wideLayout, this._isMethod, this.inlineMethods);
this._renderInlineTryit = value;
this._noTryItValue = this._computeNoTryItValue(this.noTryIt, value);
}
/**
* When the console is initialized when being hidden
* it may not layout properly. The app drawer layout component
* positions elements statically so if the console is hidden it cannot do
* this properly. In this case call `resetLayout()` function
* when getting the console back from the hidden state.
*/
resetLayout() {
this.shadowRoot.querySelector('app-drawer-layout').notifyResize();
}
/**
* Computes value for `_renderInlineTryit` property.
* @param {Boolean} wideLayout
* @param {Boolean} isMethod
* @param {Boolean} inlineMethods
* @return {Boolean} True if is wideLayout, it is a method, or when inlineMethods
* is not set.
*/
_computeRenderInlineTryIt(wideLayout, isMethod, inlineMethods) {
if (!wideLayout || !isMethod ||inlineMethods) {
return false;
}
if (!this._isWebAPI(this.amf) || this._isGrpcApi(this.amf)) {
return false;
}
return wideLayout;
}
_computeNoTryItValue(noTryIt, renderInlineTyit) {
if (renderInlineTyit) {
return true;
}
if (!this._isWebAPI(this.amf) || this._isGrpcApi(this.amf)) {
return true;
}
return noTryIt;
}
/**
* Called when route change has been detected.
* @param {History|PopStateEvent} e
*/
_onRoute(e) {
const { state } = e;
if (!state) {
return;
}
this.selectedShape = state.selected;
this.selectedShapeType = state.type;
}
/**
* Overrides ApiConsole's `resetSelection()` function to manage history state
*/
resetSelection() {
super.resetSelection();
window.history.pushState({
page: 'docs',
type: 'summary',
selected: 'summary'
}, '', '#');
}
/**
* Handler for the API navigation event. Manages history API state.
* Overrides ApiConsole function.
*
* @param {CustomEvent} e
*/
_apiNavigationOcurred(e) {
super._apiNavigationOcurred(e);
const { selected, type } = e.detail;
const url = `${window.location.pathname}#docs/${type}/${selected}`;
window.history.pushState({
page: 'docs',
type,
selected
}, '', url);
}
/**
* Called when AMF model has changed. Sets API title and updates routes.
* @return {Promise}
*/
async _processModelChange() {
super._processModelChange();
// Update try-it panel visibility based on API type (gRPC detection)
this._updateRenderInlineTyit();
this.apiTitle = this._computeApiTitle(this.webApi);
await this.updateComplete;
if (window.history.state) {
this._onRoute(window.history);
} else {
this._selectionFromHash(window.location.hash);
}
}
/**
* Sets route params from hash value of an URL.
* @param {String} hash The hash value to process
*/
_selectionFromHash(hash) {
if (!hash) {
return;
}
const matches = hash.substr(1).match(/(docs|request)\/([^/]*)\/(.*)/);
if (!matches) {
return;
}
this.page = matches[1];
this.selectedShape = matches[3];
this.selectedShapeType = matches[2];
}
/**
* Computes value of `apiTitle` property.
*
* @param {Object} shape Shape of AMF model.
* @return {String|undefined} Description if defined.
*/
_computeApiTitle(shape) {
return /** @type string */ (this._getValue(shape, this.ns.aml.vocabularies.core.name));
}
/**
* Sets `narrow` from detected media query change.
* @param {CustomEvent} e
*/
_narrowHandler(e) {
this.narrow = e.detail.value;
}
/**
* Sets `wideLayout` from detected media query change.
* @param {CustomEvent} e
*/
_wideLayoutHandler(e) {
this.wideLayout = e.detail.value;
}
/**
* @return {TemplateResult} Template for media queries elements
*/
_mediaQueriesTemplate() {
return html`
<iron-media-query
query="(max-width: 740px)"
@query-matches-changed="${this._narrowHandler}"></iron-media-query>
<iron-media-query
query="(min-width: 1500px)"
@query-matches-changed="${this._wideLayoutHandler}"></iron-media-query>`;
}
/**
* @return {TemplateResult} Template for the side drawer toolbar
*/
_drawerToolbarTemplate() {
return html`<app-toolbar>
<div>API console</div>
</app-toolbar>`;
}
/**
* @return {TemplateResult} Template for the main content toolbar
*/
_contentToolbarTemplate() {
const {
manualNavigation,
apiTitle,
compatibility
} = this;
return html`<app-header
fixed
slot="header">
<app-toolbar>
<anypoint-icon-button
drawer-toggle
?hidden="${manualNavigation}"
?compatibility="${compatibility}"
>
<arc-icon drawer-toggle icon="menu"></arc-icon>
</anypoint-icon-button>
<div main-title>${apiTitle}</div>
<slot name="toolbar"></slot>
</app-toolbar>
</app-header>`;
}
/**
* @return {TemplateResult} Template for the api documentation section
*/
_apiDocumentationTemplate() {
// Webhooks have no invokable URL: the inline panel would render broken
// (no server/URL to send a request to) even though there's no "Try it"
// button to gate it, so the webhook check has to happen here too.
const webhookSelected = this._isWebhookOperation(this.selectedShape, this.webApi);
return html`<section class="api-docs">
${super._apiDocumentationTemplate()}
${this._renderInlineTryit && !webhookSelected ? html`<div class="inline-request">
${this._bannerMessage()}
${this._requestPanelTemplate()}
</div>` : ''}
</section>`;
}
/**
* @return {TemplateResult} Template for the main page content
*/
_mainContentTemplate() {
const {
responsiveWidth,
layoutNarrow,
drawerAlign,
navigationOpened
} = this;
return html`
${this._mediaQueriesTemplate()}
<app-drawer-layout
.responsiveWidth="${responsiveWidth}"
?forceNarrow="${layoutNarrow}"
fullbleed>
<app-drawer slot="drawer" .align="${drawerAlign}" .opened="${navigationOpened}">
${this._drawerToolbarTemplate()}
${this._navigationTemplate()}
</app-drawer>
<app-header-layout>
${this._contentToolbarTemplate()}
<div class="main-content">
<slot name="content"></slot>
${this._getPageTemplate()}
</div>
</app-header-layout>
</app-drawer-layout>`;
}
/**
* The components below are optional dependencies. They will not be used until
* sources of the components are included into AC bundle.
* This would help build the console and only add a dependency at the build time
* without statically include the dependency.
*
* @return {TemplateResult}
*/
_helpersTemplate() {
return html`
${super._helpersTemplate()}
<xhr-simple-request
.appendHeaders="${this.appendHeaders}"
.proxy="${this.proxy}"
.proxyEncodeUrl="${this.proxyEncodeUrl}"></xhr-simple-request>
<oauth1-authorization></oauth1-authorization>
<oauth2-authorization></oauth2-authorization>`;
}
}