diff --git a/src/ApiRequestEditorElement.js b/src/ApiRequestEditorElement.js index 79f39dc..01fe58e 100644 --- a/src/ApiRequestEditorElement.js +++ b/src/ApiRequestEditorElement.js @@ -257,6 +257,14 @@ export class ApiRequestEditorElement extends AmfHelperMixin( this._selectedChanged(); this._updateServers(); this.readUrlData(); + this.dispatchEvent( + new CustomEvent(`api-request-panel-selection-changed`, { + composed: true, + detail: { + value: this.selected, + }, + }) + ); } get httpMethod() { @@ -509,6 +517,10 @@ export class ApiRequestEditorElement extends AmfHelperMixin( */ __amfChanged(amf) { const { urlFactory } = this; + let operationChanged = undefined; + if (urlFactory.amf) { + operationChanged = urlFactory.operation + } if (urlFactory) { if (!this.persistCache) { urlFactory.clearCache(); @@ -516,7 +528,7 @@ export class ApiRequestEditorElement extends AmfHelperMixin( urlFactory.amf = amf; this.readUrlData(); } - this._selectedChanged(); + this._selectedChanged(operationChanged); this._updateServers(); } @@ -589,9 +601,49 @@ export class ApiRequestEditorElement extends AmfHelperMixin( }); } - _selectedChanged() { + _computeSelected(amf, operationChanged) { + if (operationChanged && amf && amf[0]) { + const opKey = this._getAmfKey(this.ns.aml.vocabularies.apiContract.supportedOperation); + const lexicalKey = this._getAmfKey(this.ns.aml.vocabularies.docSourceMaps.lexical); + const lexicalValueKey = this._getAmfKey(this.ns.aml.vocabularies.docSourceMaps.value); + const sourceKey = this._getAmfKey(this.ns.aml.vocabularies.docSourceMaps.sources); + + const sourcesOld = this._getValueArray(operationChanged, sourceKey); + const lexicalsOld = this._getValueArray(sourcesOld[0], lexicalKey) + const lexicalValueOld = this._getValue(lexicalsOld[0], lexicalValueKey) + + const encodes = this._computeEncodes(amf); + const endpoints = this._computeEndpoints(encodes); + + let newOperationSelected = null + endpoints.forEach((endpoint) => { + const supportedOperation = this._ensureArray(endpoint[opKey]); + if (!supportedOperation) { + newOperationSelected = undefined + } + newOperationSelected = supportedOperation.find((operation) => { + const sources = this._getValueArray(operation, sourceKey); + const lexicals = this._getValueArray(sources[0], lexicalKey) + const lexicalValue = this._getValue(lexicals[0], lexicalValueKey) + return lexicalValue === lexicalValueOld + }); + + }); + if (!newOperationSelected) { + return this.selected; + } + + return newOperationSelected['@id']; + } + } + + + _selectedChanged(operationChanged) { this.clearRequest() const { amf, selected } = this; + if (operationChanged) { + this.selected = this._computeSelected(amf, operationChanged); + } if (!amf || !selected) { return; } diff --git a/src/ApiRequestPanelElement.js b/src/ApiRequestPanelElement.js index a3accf9..4d6e295 100644 --- a/src/ApiRequestPanelElement.js +++ b/src/ApiRequestPanelElement.js @@ -293,11 +293,11 @@ export class ApiRequestPanelElement extends EventsTargetMixin(LitElement) { this.allowCustomBaseUri = false; this.persistCache = false; - /** + /** * @type {TransportRequest} */ this.request = undefined; - /** + /** * @type {ArcResponse|ErrorResponse} */ this.response = undefined; @@ -434,8 +434,8 @@ export class ApiRequestPanelElement extends EventsTargetMixin(LitElement) { /** * Propagate `api-response` detail object. - * - * Until API Console v 6.x it was using a different response view. The current version + * + * Until API Console v 6.x it was using a different response view. The current version * uses new response view based on ARC response view which uses different data structure. * This function transforms the response to the one of the corresponding data types used in ARC. * However, this keeps compatibility with previous versions of the transport library so it is