Skip to content

Commit

Permalink
fix: operation url in static page (#2093)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexVarchuk committed Jul 22, 2022
1 parent 8869aff commit 98eec19
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
14 changes: 7 additions & 7 deletions src/components/Endpoint/Endpoint.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
9 changes: 9 additions & 0 deletions src/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
5 changes: 2 additions & 3 deletions src/utils/openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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 = [
Expand Down

0 comments on commit 98eec19

Please sign in to comment.