chore: refactor expandServerVariables function (#978)

This commit is contained in:
Anna Stasiuk 2019-07-29 13:08:17 +03:00 committed by Roman Hotsiy
parent afc7e36cf8
commit 7db7d4fc91
2 changed files with 22 additions and 20 deletions

View File

@ -5,7 +5,7 @@ import { Markdown } from '../Markdown/Markdown';
import { OptionsContext } from '../OptionsProvider';
import { SelectOnClick } from '../SelectOnClick/SelectOnClick';
import { getBasePath } from '../../utils';
import { expandDefaultServerVariables, getBasePath } from '../../utils';
import {
EndpointInfo,
HttpVerb,
@ -60,21 +60,24 @@ export class Endpoint extends React.Component<EndpointProps, EndpointState> {
/>
</EndpointInfo>
<ServersOverlay expanded={expanded}>
{operation.servers.map(server => (
<ServerItem key={server.url}>
{operation.servers.map(server => {
const normalizedUrl = expandDefaultServerVariables(server.url, server.variables);
return (
<ServerItem key={normalizedUrl}>
<Markdown source={server.description || ''} compact={true} />
<SelectOnClick>
<ServerUrl>
<span>
{hideHostname || options.hideHostname
? getBasePath(server.url)
: server.url}
? getBasePath(normalizedUrl)
: normalizedUrl}
</span>
{operation.path}
</ServerUrl>
</SelectOnClick>
</ServerItem>
))}
);
})}
</ServersOverlay>
</OperationEndpointWrap>
)}

View File

@ -453,7 +453,7 @@ export function mergeSimilarMediaTypes(types: Dict<OpenAPIMediaType>): Dict<Open
return mergedTypes;
}
function expandVariables(url: string, variables: object = {}) {
export function expandDefaultServerVariables(url: string, variables: object = {}) {
return url.replace(
/(?:{)(\w+)(?:})/g,
(match, name) => (variables[name] && variables[name].default) || match,
@ -482,15 +482,14 @@ export function normalizeServers(
];
}
function normalizeUrl(url: string, variables: object | undefined): string {
url = expandVariables(url, variables);
function normalizeUrl(url: string): string {
return resolveUrl(baseUrl, url);
}
return servers.map(server => {
return {
...server,
url: normalizeUrl(server.url, server.variables),
url: normalizeUrl(server.url),
description: server.description || '',
};
});