Skip to content

Commit 3b512ec

Browse files
committed
feat: Add agent parameters section to API method documentation
- Introduced new properties for agent parameters and their visibility toggle. - Implemented logic to compute agent parameters based on the method. - Added rendering functionality for the agent parameters section in the documentation. - Created unit tests to validate the agent parameters behavior and rendering.
1 parent a1616e2 commit 3b512ec

File tree

2 files changed

+142
-0
lines changed

2 files changed

+142
-0
lines changed

src/ApiMethodDocumentation.js

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,14 @@ export class ApiMethodDocumentation extends AmfHelperMixin(LitElement) {
211211
* When set it hides bottom navigation links
212212
*/
213213
noNavigation: { type: Boolean },
214+
/**
215+
* When set it renders the agent parameters section
216+
*/
217+
_agentOpened: { type: Boolean },
218+
/**
219+
* Computed list of agent parameters.
220+
*/
221+
agentParameters: { type: Object },
214222
/**
215223
* When set the base URI won't be rendered for this method.
216224
*/
@@ -366,6 +374,8 @@ export class ApiMethodDocumentation extends AmfHelperMixin(LitElement) {
366374
this.renderCodeSnippets = false;
367375
this.deprecated = false;
368376
this.selectedMessage = null;
377+
this.agentParameters = undefined;
378+
this._agentOpened = false;
369379

370380
this.previous = undefined;
371381
this.next = undefined;
@@ -463,6 +473,7 @@ export class ApiMethodDocumentation extends AmfHelperMixin(LitElement) {
463473
this.operationId = this._getValue(method, this.ns.aml.vocabularies.apiContract.operationId);
464474
this.callbacks = this._computeCallbacks(method);
465475
this.deprecated = this._computeIsDeprecated(method);
476+
this.agentParameters = this._computeAgentParameters(method);
466477
}
467478

468479
_computeAsyncApiSecurity(){
@@ -683,6 +694,13 @@ export class ApiMethodDocumentation extends AmfHelperMixin(LitElement) {
683694
this.callbacksOpened = !this.callbacksOpened;
684695
}
685696

697+
/**
698+
* Toggles agent parameters section.
699+
*/
700+
_toggleAgent() {
701+
this._agentOpened = !this._agentOpened;
702+
}
703+
686704
/**
687705
* Computes example headers string for code snippets.
688706
* @param {any|any[]} headers Headers model from AMF
@@ -1095,6 +1113,7 @@ export class ApiMethodDocumentation extends AmfHelperMixin(LitElement) {
10951113
${this._getAsyncSecurityMethodTemplate()}
10961114
${this._getMessagesTemplate()}
10971115
${this._getCodeSnippetsTemplate()}
1116+
${this._getAgentTemplate()}
10981117
${this._getSecurityTemplate()}
10991118
${this._getParametersTemplate()}
11001119
${this._getHeadersTemplate()}
@@ -1103,6 +1122,42 @@ export class ApiMethodDocumentation extends AmfHelperMixin(LitElement) {
11031122
</section>`
11041123
}
11051124

1125+
_getAgentTemplate() {
1126+
const { agentParameters } = this;
1127+
if (!agentParameters || Object.keys(agentParameters).length === 0) {
1128+
return '';
1129+
}
1130+
const { _agentOpened, compatibility } = this;
1131+
const label = this._computeToggleActionLabel(_agentOpened);
1132+
const buttonState = this._computeToggleButtonState(_agentOpened);
1133+
const iconClass = this._computeToggleIconClass(_agentOpened);
1134+
return html`<section class="agent-parameters">
1135+
<div
1136+
class="section-title-area"
1137+
@click="${this._toggleAgent}"
1138+
title="Toggle agent parameters"
1139+
?opened="${_agentOpened}"
1140+
>
1141+
<div class="heading3 table-title" role="heading" aria-level="2">Agent parameters</div>
1142+
<div class="title-area-actions" aria-label="${buttonState}">
1143+
<anypoint-button class="toggle-button" ?compatibility="${compatibility}" data-toggle="agent-parameters">
1144+
${label}
1145+
<arc-icon class="icon ${iconClass}" icon="expandMore"></arc-icon>
1146+
</anypoint-button>
1147+
</div>
1148+
</div>
1149+
<anypoint-collapse .opened="${_agentOpened}">
1150+
<div class="parameters-container">
1151+
${Object.keys(agentParameters).map((key) => html`
1152+
<div class="property-item">
1153+
<div class="property-name">${key}: <strong>${String(agentParameters[key])}</strong></div>
1154+
</div>
1155+
`)}
1156+
</div>
1157+
</anypoint-collapse>
1158+
</section>`;
1159+
}
1160+
11061161
_getAsyncSecurityMethodTemplate() {
11071162
const { renderSecurity, methodSecurity } = this;
11081163
if (!renderSecurity || !methodSecurity || !methodSecurity.length || !this._isAsyncAPI(this.amf)) {
@@ -1332,6 +1387,49 @@ export class ApiMethodDocumentation extends AmfHelperMixin(LitElement) {
13321387
return undefined;
13331388
}
13341389

1390+
_computeAgentParameters(method) {
1391+
if (!method) {
1392+
return undefined;
1393+
}
1394+
1395+
const expect = this._computeExpects(method);
1396+
if (!expect) {
1397+
return undefined;
1398+
}
1399+
const payloads = this._computePayload(expect);
1400+
if (!payloads || payloads.length === 0) {
1401+
return undefined;
1402+
}
1403+
1404+
const ramlSchemaKey = this._getAmfKey(this.ns.aml.vocabularies.shapes.schema);
1405+
const schema = payloads[0][ramlSchemaKey];
1406+
if (!schema) {
1407+
return undefined;
1408+
}
1409+
1410+
const agentObject = this._computeAgents(schema[0]);
1411+
if (!agentObject) {
1412+
return undefined;
1413+
}
1414+
const actionKey = this._getAmfKey(this.ns.aml.vocabularies.data.action);
1415+
const action = agentObject[0][actionKey];
1416+
if (!action) {
1417+
return undefined;
1418+
}
1419+
const isUserInputKey = this._getAmfKey(this.ns.aml.vocabularies.data.isUserInput);
1420+
const isUserInput = action[0][isUserInputKey];
1421+
if (!isUserInput) {
1422+
return undefined;
1423+
}
1424+
const isInputEnabled = this._getValue(isUserInput[0], this.ns.aml.vocabularies.data.value);
1425+
1426+
const params = {
1427+
isInputEnabled,
1428+
};
1429+
1430+
return params;
1431+
}
1432+
13351433
/**
13361434
* Returns message value depending on operation node method
13371435
* Subscribe -> returns

test/agent-params.test.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { fixture, assert, html, aTimeout } from '@open-wc/testing';
2+
import { AmfLoader } from './amf-loader.js';
3+
import '../api-method-documentation.js';
4+
5+
describe('agent-api', function() {
6+
async function modelFixture(amf, endpoint, method) {
7+
return (await fixture(html`<api-method-documentation
8+
.amf="${amf}"
9+
.endpoint="${endpoint}"
10+
.method="${method}"></api-method-documentation>`));
11+
}
12+
13+
const apiFile = 'agents-api';
14+
15+
[
16+
['Compact model V1', true],
17+
['Regular model V1', false]
18+
].forEach(([label, compact]) => {
19+
describe(label, () => {
20+
let amf;
21+
let element;
22+
before(async () => {
23+
amf = await AmfLoader.load(apiFile, compact);
24+
});
25+
26+
it('isInputEnabled is true', async () => {
27+
const endpopint = AmfLoader.lookupEndpoint(amf, '/reservations/reservationlookup');
28+
const method = AmfLoader.lookupOperation(amf, '/reservations/reservationlookup', 'get');
29+
element = await modelFixture(amf, endpopint, method);
30+
await aTimeout();
31+
assert.equal(element.agentParameters.isInputEnabled, 'true');
32+
});
33+
34+
it('renders the summary', async () => {
35+
const endpopint = AmfLoader.lookupEndpoint(amf, '/reservations/reservationlookup');
36+
const method = AmfLoader.lookupOperation(amf, '/reservations/reservationlookup', 'get');
37+
element = await modelFixture(amf, endpopint, method);
38+
await aTimeout();
39+
const node = element.shadowRoot.querySelector('.summary');
40+
assert.ok(node);
41+
});
42+
});
43+
});
44+
});

0 commit comments

Comments
 (0)