Skip to content

Commit

Permalink
feat: don't render groups with no items (#1830)
Browse files Browse the repository at this point in the history
  • Loading branch information
chohmann committed Nov 2, 2021
1 parent 1f1d82e commit 9a20992
Show file tree
Hide file tree
Showing 3 changed files with 410 additions and 452 deletions.
54 changes: 54 additions & 0 deletions packages/elements/src/components/API/__tests__/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -559,4 +559,58 @@ describe('computeAPITree', () => {
},
]);
});

it('excludes groups with no items', () => {
const apiDocument: OpenAPIObject = {
openapi: '3.0.0',
info: {
title: 'some api',
version: '1.0.0',
description: 'some description',
},
tags: [
{
name: 'a',
},
],
paths: {
'/something': {
post: {
'x-internal': true,
tags: ['a'],
},
},
'/something-else': {
post: {
tags: ['b'],
},
},
},
};

expect(computeAPITree(transformOasToServiceNode(apiDocument)!, { hideInternal: true })).toEqual([
{
id: '/',
meta: '',
slug: '/',
title: 'Overview',
type: 'overview',
},
{
title: 'Endpoints',
},
{
title: 'b',
items: [
{
id: '/paths/something-else/post',
meta: 'post',
slug: '/paths/something-else/post',
title: '/something-else',
type: 'http_operation',
},
],
},
]);
});
});
31 changes: 17 additions & 14 deletions packages/elements/src/components/API/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,21 +97,24 @@ export const computeAPITree = (serviceNode: ServiceNode, config: ComputeAPITreeC
});

groups.forEach(group => {
tree.push({
title: group.title,
items: group.items.flatMap(operationNode => {
if (mergedConfig.hideInternal && operationNode.data.internal) {
return [];
}
return {
id: operationNode.uri,
slug: operationNode.uri,
title: operationNode.name,
type: operationNode.type,
meta: operationNode.data.method,
};
}),
const items = group.items.flatMap(operationNode => {
if (mergedConfig.hideInternal && operationNode.data.internal) {
return [];
}
return {
id: operationNode.uri,
slug: operationNode.uri,
title: operationNode.name,
type: operationNode.type,
meta: operationNode.data.method,
};
});
if (items.length > 0) {
tree.push({
title: group.title,
items,
});
}
});
}

Expand Down
Loading

0 comments on commit 9a20992

Please sign in to comment.