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

View File

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