Skip to content

Commit

Permalink
feat: add support for hideServerInfo and hideSecurityInfo options (
Browse files Browse the repository at this point in the history
…#2601)

The component already has flags `hideServerInfo` and `hideSecurityInfo`
defined but they are not used to actual hide the server info or to hide
the security info.
  • Loading branch information
weyert authored Jul 19, 2024
1 parent a9de72f commit ed23359
Show file tree
Hide file tree
Showing 13 changed files with 126 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,11 @@ const HttpOperationComponent = React.memo<HttpOperationProps>(

<NodeVendorExtensions data={data} />

<Request onChange={setTextRequestBodyIndex} operation={data} />
<Request
onChange={setTextRequestBodyIndex}
operation={data}
hideSecurityInfo={layoutOptions?.hideSecurityInfo}
/>

{data.responses && (
<Responses
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { Parameters } from './Parameters';

interface IRequestProps {
operation: IHttpEndpointOperation;
hideSecurityInfo?: boolean;
onChange?: (requestBodyIndex: number) => void;
}

Expand All @@ -30,6 +31,7 @@ export const Request: React.FunctionComponent<IRequestProps> = ({
} = {},
security,
},
hideSecurityInfo,
onChange,
}) => {
if (!request || typeof request !== 'object') return null;
Expand All @@ -50,7 +52,7 @@ export const Request: React.FunctionComponent<IRequestProps> = ({
<VStack spacing={8}>
<SectionTitle title="Request" />

<SecuritySchemes schemes={securitySchemes} parentId={operation.id} />
{hideSecurityInfo ? null : <SecuritySchemes schemes={securitySchemes} parentId={operation.id} />}

{pathParams.length > 0 && (
<VStack spacing={5}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@ describe('HttpService', () => {
expect(wrapper.getByText(httpService.name).tagName.toLowerCase()).toBe('h1');
});

it('should not display servers when hideServerInfo is provided', () => {
render(
<Router>
<HttpService layoutOptions={{ hideServerInfo: true }} data={{ ...httpService, security: [] }} />
</Router>,
);

const serverUrl = screen.queryByLabelText('Production API');
expect(serverUrl).not.toBeInTheDocument();
});

it('displays all server urls', () => {
render(<ServerInfo servers={httpService.servers ?? []} />);

Expand Down Expand Up @@ -154,6 +165,17 @@ describe('HttpService', () => {
expect(security).not.toBeInTheDocument();
});

it('should not render if hideSecurityInfo is provided', () => {
render(
<Router>
<HttpService layoutOptions={{ hideSecurityInfo: true }} data={{ ...httpService, security: [] }} />
</Router>,
);

const security = screen.queryByRole('heading', { name: 'Security' });
expect(security).not.toBeInTheDocument();
});

it('should render default description', () => {
render(<SecuritySchemes secSchemes={[[apiKey]]} parentId="2@adfg4F" />);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,19 @@ const HttpServiceComponent = React.memo<HttpServiceProps>(
)}

<VStack spacing={6}>
<ServerInfo servers={data.servers ?? []} mockUrl={mocking.mockUrl} />
{layoutOptions?.hideServerInfo ? null : <ServerInfo servers={data.servers ?? []} mockUrl={mocking.mockUrl} />}

<Box data-test="security">
{data.security?.length ? (
<SecuritySchemes
secSchemes={data.security}
defaultScheme={query.get('security') || undefined}
parentId={data.id}
/>
) : null}
</Box>
{layoutOptions?.hideSecurityInfo ? null : (
<Box data-test="security">
{data.security?.length ? (
<SecuritySchemes
secSchemes={data.security}
defaultScheme={query.get('security') || undefined}
parentId={data.id}
/>
) : null}
</Box>
)}

<Box data-test="additional-info">
{(data.contact?.email || data.license || data.termsOfService) && (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,17 @@ describe(NodeContent.name, () => {

unmount();
});

it('can hide SecurityInfo', () => {
const { unmount } = render(
<MemoryRouter>
<NodeContent node={nodeContent} Link={DummyLink} hideSecurityInfo />
</MemoryRouter>,
);

expect(screen.getByRole('heading', { name: /create todo/i })).toBeInTheDocument();
expect(screen.queryByText(/API Key/i)).not.toBeInTheDocument();

unmount();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type DocsBaseProps = Pick<
>;
type DocsLayoutProps = Pick<
Required<DocsProps>['layoutOptions'],
'compact' | 'hideTryIt' | 'hideTryItPanel' | 'hideExport'
'compact' | 'hideTryIt' | 'hideTryItPanel' | 'hideExport' | 'hideSecurityInfo' | 'hideServerInfo'
>;

export type NodeContentProps = {
Expand Down Expand Up @@ -61,6 +61,8 @@ export const NodeContent = ({
compact,
hideTryIt,
hideTryItPanel,
hideSecurityInfo,
hideServerInfo,

// Exporting
hideExport,
Expand All @@ -87,6 +89,8 @@ export const NodeContent = ({
compact,
hideTryIt: hideTryIt,
hideTryItPanel: hideTryItPanel,
hideSecurityInfo: hideSecurityInfo,
hideServerInfo: hideServerInfo,
hideExport:
hideExport ||
(node.links.export_url ?? node.links.export_original_file_url ?? node.links.export_bundled_file_url) ===
Expand Down
16 changes: 16 additions & 0 deletions packages/elements-dev-portal/src/containers/StoplightProject.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,18 @@ export interface StoplightProjectProps extends RoutingProps {
*/
hideExport?: boolean;

/**
* Allows to the server information
* @default false
*/
hideServerInfo?: boolean;

/**
* Allows to hide the security schemes
* @default false
*/
hideSecurityInfo?: boolean;

/**
* If set to true, all table of contents panels will be collapsed.
* @default false
Expand All @@ -76,6 +88,8 @@ export interface StoplightProjectProps extends RoutingProps {
const StoplightProjectImpl: React.FC<StoplightProjectProps> = ({
projectId,
hideTryIt,
hideSecurityInfo,
hideServerInfo,
hideMocking,
hideExport,
collapseTableOfContents = false,
Expand Down Expand Up @@ -136,6 +150,8 @@ const StoplightProjectImpl: React.FC<StoplightProjectProps> = ({
hideTryIt={hideTryIt}
hideMocking={hideMocking}
hideExport={hideExport}
hideSecurityInfo={hideSecurityInfo}
hideServerInfo={hideServerInfo}
tryItCredentialsPolicy={tryItCredentialsPolicy}
tryItCorsProxy={tryItCorsProxy}
/>
Expand Down
2 changes: 2 additions & 0 deletions packages/elements-dev-portal/src/web-components/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { StoplightProject } from '../containers/StoplightProject';
export const StoplightProjectElement = createElementClass(StoplightProject, {
projectId: { type: 'string', defaultValue: '' },
hideTryIt: { type: 'boolean' },
hideServerInfo: { type: 'boolean' },
hideSecurityInfo: { type: 'boolean' },
hideMocking: { type: 'boolean' },
hideExport: { type: 'boolean' },
basePath: { type: 'string' },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ type SidebarLayoutProps = {
hideSchemas?: boolean;
hideInternal?: boolean;
hideExport?: boolean;
hideServerInfo?: boolean;
hideSecurityInfo?: boolean;
exportProps?: ExportButtonProps;
tryItCredentialsPolicy?: 'omit' | 'include' | 'same-origin';
tryItCorsProxy?: string;
Expand All @@ -34,6 +36,8 @@ export const APIWithResponsiveSidebarLayout: React.FC<SidebarLayoutProps> = ({
hideSchemas,
hideInternal,
hideExport,
hideServerInfo,
hideSecurityInfo,
exportProps,
tryItCredentialsPolicy,
tryItCorsProxy,
Expand All @@ -51,8 +55,14 @@ export const APIWithResponsiveSidebarLayout: React.FC<SidebarLayoutProps> = ({
const node = isRootPath ? serviceNode : serviceNode.children.find(child => child.uri === pathname);

const layoutOptions = React.useMemo(
() => ({ hideTryIt: hideTryIt, compact: compact, hideExport: hideExport || node?.type !== NodeType.HttpService }),
[hideTryIt, hideExport, node, compact],
() => ({
hideTryIt: hideTryIt,
hideSecurityInfo: hideSecurityInfo,
hideServerInfo: hideServerInfo,
compact: compact,
hideExport: hideExport || node?.type !== NodeType.HttpService,
}),
[hideTryIt, hideSecurityInfo, hideServerInfo, compact, hideExport, node?.type],
);

if (!node) {
Expand Down
13 changes: 11 additions & 2 deletions packages/elements/src/components/API/APIWithSidebarLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ type SidebarLayoutProps = {
hideTryIt?: boolean;
hideSchemas?: boolean;
hideInternal?: boolean;
hideServerInfo?: boolean;
hideSecurityInfo?: boolean;
hideExport?: boolean;
exportProps?: ExportButtonProps;
tryItCredentialsPolicy?: 'omit' | 'include' | 'same-origin';
Expand All @@ -35,6 +37,8 @@ export const APIWithSidebarLayout: React.FC<SidebarLayoutProps> = ({
logo,
hideTryIt,
hideSchemas,
hideSecurityInfo,
hideServerInfo,
hideInternal,
hideExport,
exportProps,
Expand All @@ -53,8 +57,13 @@ export const APIWithSidebarLayout: React.FC<SidebarLayoutProps> = ({
const node = isRootPath ? serviceNode : serviceNode.children.find(child => child.uri === pathname);

const layoutOptions = React.useMemo(
() => ({ hideTryIt: hideTryIt, hideExport: hideExport || node?.type !== NodeType.HttpService }),
[hideTryIt, hideExport, node],
() => ({
hideTryIt: hideTryIt,
hideServerInfo: hideServerInfo,
hideSecurityInfo: hideSecurityInfo,
hideExport: hideExport || node?.type !== NodeType.HttpService,
}),
[hideTryIt, hideServerInfo, hideSecurityInfo, hideExport, node?.type],
);

if (!node) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ type StackedLayoutProps = {
serviceNode: ServiceNode;
hideTryIt?: boolean;
hideExport?: boolean;
hideServerInfo?: boolean;
hideSecurityInfo?: boolean;
exportProps?: ExportButtonProps;
tryItCredentialsPolicy?: TryItCredentialsPolicy;
tryItCorsProxy?: string;
Expand Down Expand Up @@ -72,6 +74,8 @@ export const APIWithStackedLayout: React.FC<StackedLayoutProps> = ({
serviceNode,
hideTryIt,
hideExport,
hideSecurityInfo,
hideServerInfo,
exportProps,
tryItCredentialsPolicy,
tryItCorsProxy,
Expand All @@ -93,7 +97,7 @@ export const APIWithStackedLayout: React.FC<StackedLayoutProps> = ({
nodeTitle={serviceNode.name}
nodeType={NodeType.HttpService}
location={location}
layoutOptions={{ showPoweredByLink, hideExport }}
layoutOptions={{ showPoweredByLink, hideExport, hideSecurityInfo, hideServerInfo }}
exportProps={exportProps}
tryItCredentialsPolicy={tryItCredentialsPolicy}
renderExtensionAddon={renderExtensionAddon}
Expand Down
18 changes: 18 additions & 0 deletions packages/elements/src/containers/API.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@ export interface CommonAPIProps extends RoutingProps {
*/
hideTryIt?: boolean;

/**
* Allows hiding the Security info section
*/
hideSecurityInfo?: boolean;

/**
* Allows hiding the Server info section
*/
hideServerInfo?: boolean;

/**
* Hides schemas from being displayed in Table of Contents
*/
Expand Down Expand Up @@ -115,6 +125,8 @@ export const APIImpl: React.FC<APIProps> = props => {
apiDescriptionUrl = '',
logo,
hideTryIt,
hideSecurityInfo,
hideServerInfo,
hideSchemas,
hideInternal,
hideExport,
Expand Down Expand Up @@ -184,6 +196,8 @@ export const APIImpl: React.FC<APIProps> = props => {
<APIWithStackedLayout
serviceNode={serviceNode}
hideTryIt={hideTryIt}
hideSecurityInfo={hideSecurityInfo}
hideServerInfo={hideServerInfo}
hideExport={hideExport}
exportProps={exportProps}
tryItCredentialsPolicy={tryItCredentialsPolicy}
Expand All @@ -197,6 +211,8 @@ export const APIImpl: React.FC<APIProps> = props => {
logo={logo}
serviceNode={serviceNode}
hideTryIt={hideTryIt}
hideSecurityInfo={hideSecurityInfo}
hideServerInfo={hideServerInfo}
hideSchemas={hideSchemas}
hideInternal={hideInternal}
hideExport={hideExport}
Expand All @@ -211,6 +227,8 @@ export const APIImpl: React.FC<APIProps> = props => {
logo={logo}
serviceNode={serviceNode}
hideTryIt={hideTryIt}
hideSecurityInfo={hideSecurityInfo}
hideServerInfo={hideServerInfo}
hideSchemas={hideSchemas}
hideInternal={hideInternal}
hideExport={hideExport}
Expand Down
2 changes: 2 additions & 0 deletions packages/elements/src/web-components/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ export const ApiElement = createElementClass(API, {
router: { type: 'string' },
layout: { type: 'string' },
hideTryIt: { type: 'boolean' },
hideServerInfo: { type: 'boolean' },
hideSecurityInfo: { type: 'boolean' },
hideSchemas: { type: 'boolean' },
hideInternal: { type: 'boolean' },
hideExport: { type: 'boolean' },
Expand Down

0 comments on commit ed23359

Please sign in to comment.