diff --git a/packages/ui/app/src/api-page/endpoints/EndpointUrl.tsx b/packages/ui/app/src/api-page/endpoints/EndpointUrl.tsx index 2f430e4bf1..910342b672 100644 --- a/packages/ui/app/src/api-page/endpoints/EndpointUrl.tsx +++ b/packages/ui/app/src/api-page/endpoints/EndpointUrl.tsx @@ -39,7 +39,7 @@ export const EndpointUrl = React.forwardRef { const elements: (ReactElement | null)[] = []; if (selectedEnvironment != null) { - const url = parse(selectedEnvironment.baseUrl); + const url = parse(selectedEnvironment?.baseUrl); if (showEnvironment) { if (allEnvironmentIds.length < 2) { elements.push( diff --git a/packages/ui/app/src/api-page/web-socket/WebSocket.tsx b/packages/ui/app/src/api-page/web-socket/WebSocket.tsx index 4009b68e39..291bfe53db 100644 --- a/packages/ui/app/src/api-page/web-socket/WebSocket.tsx +++ b/packages/ui/app/src/api-page/web-socket/WebSocket.tsx @@ -155,7 +155,7 @@ const WebhookContent: FC = ({ websocket, isLastInApi, types }) - `${resolveEnvironment(websocket, selectedEnvironmentId).baseUrl}${websocket.path.map((path) => (path.type === "literal" ? path.value : `:${path.key}`)).join("/")}` + `${resolveEnvironment(websocket, selectedEnvironmentId)?.baseUrl}${websocket.path.map((path) => (path.type === "literal" ? path.value : `:${path.key}`)).join("/")}` } /> @@ -320,7 +320,7 @@ const WebhookContent: FC = ({ websocket, isLastInApi, types }) URL - {`${resolveEnvironment(websocket, selectedEnvironmentId).baseUrl ?? ""}${example?.path ?? stringifyResolvedEndpointPathParts(websocket.path)}`} + {`${resolveEnvironment(websocket, selectedEnvironmentId)?.baseUrl ?? ""}${example?.path ?? stringifyResolvedEndpointPathParts(websocket.path)}`} diff --git a/packages/ui/app/src/api-playground/PlaygroundWebSocket.tsx b/packages/ui/app/src/api-playground/PlaygroundWebSocket.tsx index dfec947c5a..ec7512364f 100644 --- a/packages/ui/app/src/api-playground/PlaygroundWebSocket.tsx +++ b/packages/ui/app/src/api-playground/PlaygroundWebSocket.tsx @@ -46,7 +46,7 @@ export const PlaygroundWebSocket: FC = ({ websocket, t useEffect(() => () => socket.current?.close(), []); const selectedEnvironmentId = useSelectedEnvironmentId(); - const baseUrl = resolveEnvironment(websocket, selectedEnvironmentId).baseUrl; + const baseUrl = resolveEnvironment(websocket, selectedEnvironmentId)?.baseUrl; const startSession = useCallback(async () => { return new Promise((resolve) => { diff --git a/packages/ui/app/src/api-playground/utils.ts b/packages/ui/app/src/api-playground/utils.ts index 2dec186d61..ae417095de 100644 --- a/packages/ui/app/src/api-playground/utils.ts +++ b/packages/ui/app/src/api-playground/utils.ts @@ -75,7 +75,7 @@ export function buildEndpointUrl( formState: PlaygroundRequestFormState | undefined, ): string { return buildRequestUrl( - endpoint && resolveEnvironment(endpoint).baseUrl, + endpoint && resolveEnvironment(endpoint)?.baseUrl, endpoint?.path, formState?.pathParameters, formState?.queryParameters, @@ -450,7 +450,7 @@ export function stringifyCurl({ return stringifyHttpRequestExampleToCurl({ method: endpoint.method, // TODO: wire through the hook based environment - url: buildRequestUrl(resolveEnvironment(endpoint).baseUrl, endpoint?.path, formState?.pathParameters), + url: buildRequestUrl(resolveEnvironment(endpoint)?.baseUrl, endpoint?.path, formState?.pathParameters), urlQueries: formState.queryParameters, headers, body: diff --git a/packages/ui/app/src/resolver/resolveCodeSnippets.ts b/packages/ui/app/src/resolver/resolveCodeSnippets.ts index a718defcfc..6209db623d 100644 --- a/packages/ui/app/src/resolver/resolveCodeSnippets.ts +++ b/packages/ui/app/src/resolver/resolveCodeSnippets.ts @@ -185,7 +185,7 @@ function getHarRequest( cookies: [], bodySize: -1, }; - request.url = buildRequestUrl(resolveEnvironment(endpoint).baseUrl, endpoint.path, example.pathParameters); + request.url = buildRequestUrl(resolveEnvironment(endpoint)?.baseUrl, endpoint.path, example.pathParameters); request.method = endpoint.method; request.queryString = Object.entries(example.queryParameters).map(([name, value]) => ({ name, diff --git a/packages/ui/app/src/resolver/types.ts b/packages/ui/app/src/resolver/types.ts index c06d5c40b7..70ea15d457 100644 --- a/packages/ui/app/src/resolver/types.ts +++ b/packages/ui/app/src/resolver/types.ts @@ -749,7 +749,7 @@ export function getParameterDescription( export const resolveEnvironment = ( endpoint: ResolvedWebSocketChannel | ResolvedEndpointDefinition, selectedEnvironmentId?: string, -): APIV1Read.Environment => { +): APIV1Read.Environment | undefined => { if (!selectedEnvironmentId && typeof window !== "undefined") { // TODO: replace this, this is a workaround for now, for functions that need to resolve in the playground, // but do not have access to hooks @@ -768,8 +768,9 @@ export const resolveEnvironmentUrlInCodeSnippet = ( selectedEnvironmentId?: string, ): string => { const urlToReplace = endpoint.environments.find((env) => requestCodeSnippet.includes(env.baseUrl))?.baseUrl; - return urlToReplace - ? requestCodeSnippet.replace(urlToReplace, resolveEnvironment(endpoint, selectedEnvironmentId).baseUrl) + const resolvedEnvironment = resolveEnvironment(endpoint, selectedEnvironmentId); + return urlToReplace && resolvedEnvironment + ? requestCodeSnippet.replace(urlToReplace, resolvedEnvironment.baseUrl) : requestCodeSnippet; }; diff --git a/packages/ui/app/src/util/endpoint.ts b/packages/ui/app/src/util/endpoint.ts index 2dd20d0034..95780a8fef 100644 --- a/packages/ui/app/src/util/endpoint.ts +++ b/packages/ui/app/src/util/endpoint.ts @@ -49,7 +49,7 @@ export function divideEndpointPathToParts(path: ResolvedEndpointPathParts[]): En } export function getEndpointEnvironmentUrl(endpoint: ResolvedEndpointDefinition): string | undefined { - return resolveEnvironment(endpoint).baseUrl; + return resolveEnvironment(endpoint)?.baseUrl; } export function getEndpointTitleAsString(endpoint: APIV1Read.EndpointDefinition): string {