fix: operation url in static page (#2093)

This commit is contained in:
Alex Varchuk 2022-07-22 14:05:08 +03:00 committed by GitHub
parent 8869aff406
commit 98eec19647
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 10 deletions

View File

@ -68,18 +68,18 @@ export class Endpoint extends React.Component<EndpointProps, EndpointState> {
? expandDefaultServerVariables(server.url, server.variables)
: server.url;
const basePath = getBasePath(normalizedUrl);
const serverBaseUrl =
hideHostname || options.hideHostname
? basePath === '/'
? ''
: basePath
: normalizedUrl;
return (
<ServerItem key={normalizedUrl}>
<Markdown source={server.description || ''} compact={true} />
<SelectOnClick>
<ServerUrl>
<span>
{hideHostname || options.hideHostname
? basePath === '/'
? ''
: basePath
: normalizedUrl}
</span>
<span>{serverBaseUrl}</span>
{operation.path}
</ServerUrl>
</SelectOnClick>

View File

@ -175,6 +175,15 @@ export function getBasePath(serverUrl: string): string {
}
}
export function getBaseUrl(serverUrl: string): string {
try {
return parseURL(serverUrl).origin;
} catch (e) {
// when using with redoc-cli serverUrl can be empty resulting in crash
return serverUrl;
}
}
export function titleize(text: string) {
return text.charAt(0).toUpperCase() + text.slice(1);
}

View File

@ -16,7 +16,7 @@ import {
Referenced,
} from '../types';
import { IS_BROWSER } from './dom';
import { isNumeric, removeQueryString, resolveUrl, isArray, isBoolean } from './helpers';
import { isNumeric, resolveUrl, isArray, isBoolean, getBaseUrl } from './helpers';
function isWildcardStatusCode(statusCode: string | number): statusCode is string {
return typeof statusCode === 'string' && /\dxx/i.test(statusCode);
@ -606,8 +606,7 @@ export function normalizeServers(
return href.endsWith('.html') ? dirname(href) : href;
};
const baseUrl = specUrl === undefined ? removeQueryString(getHref()) : dirname(specUrl);
const baseUrl = specUrl === undefined ? getBaseUrl(getHref()) : dirname(specUrl);
if (servers.length === 0) {
// Behaviour defined in OpenAPI spec: https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#openapi-object
servers = [