From 98eec19647b63f3598ec30fdeb428f614cf93ad4 Mon Sep 17 00:00:00 2001 From: Alex Varchuk Date: Fri, 22 Jul 2022 14:05:08 +0300 Subject: [PATCH] fix: operation url in static page (#2093) --- src/components/Endpoint/Endpoint.tsx | 14 +++++++------- src/utils/helpers.ts | 9 +++++++++ src/utils/openapi.ts | 5 ++--- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/components/Endpoint/Endpoint.tsx b/src/components/Endpoint/Endpoint.tsx index 9579eeca..67e8da8d 100644 --- a/src/components/Endpoint/Endpoint.tsx +++ b/src/components/Endpoint/Endpoint.tsx @@ -68,18 +68,18 @@ export class Endpoint extends React.Component { ? expandDefaultServerVariables(server.url, server.variables) : server.url; const basePath = getBasePath(normalizedUrl); + const serverBaseUrl = + hideHostname || options.hideHostname + ? basePath === '/' + ? '' + : basePath + : normalizedUrl; return ( - - {hideHostname || options.hideHostname - ? basePath === '/' - ? '' - : basePath - : normalizedUrl} - + {serverBaseUrl} {operation.path} diff --git a/src/utils/helpers.ts b/src/utils/helpers.ts index 16ed57c1..3e521601 100644 --- a/src/utils/helpers.ts +++ b/src/utils/helpers.ts @@ -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); } diff --git a/src/utils/openapi.ts b/src/utils/openapi.ts index 564fe7de..7101644f 100644 --- a/src/utils/openapi.ts +++ b/src/utils/openapi.ts @@ -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 = [