mirror of
https://github.com/Redocly/redoc.git
synced 2025-01-31 10:04:08 +03:00
chore: refactor expandServerVariables function (#978)
This commit is contained in:
parent
afc7e36cf8
commit
7db7d4fc91
|
@ -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>
|
||||||
)}
|
)}
|
||||||
|
|
|
@ -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 || '',
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue
Block a user