From 003c5388847e33ad42904141866b6c7fc3eb9d81 Mon Sep 17 00:00:00 2001 From: Andrew Jiang Date: Fri, 26 Jul 2024 13:18:28 -0400 Subject: [PATCH] fix: urljoin on basepath (#1207) --- packages/ui/app/src/api-playground/PlaygroundContext.tsx | 3 ++- packages/ui/app/src/services/useApiKeyInjectionConfig.ts | 3 ++- packages/ui/app/src/services/useSearchService.ts | 3 ++- packages/ui/docs-bundle/src/utils/getDocsPageProps.ts | 6 ++++-- 4 files changed, 10 insertions(+), 5 deletions(-) diff --git a/packages/ui/app/src/api-playground/PlaygroundContext.tsx b/packages/ui/app/src/api-playground/PlaygroundContext.tsx index 4a1d4bd7d6..a58e7260a6 100644 --- a/packages/ui/app/src/api-playground/PlaygroundContext.tsx +++ b/packages/ui/app/src/api-playground/PlaygroundContext.tsx @@ -18,7 +18,8 @@ const fetcher = async (url: string) => { export const PlaygroundContextProvider: FC = () => { const basePath = useBasePath(); - const key = urljoin(basePath ?? "", "/api/fern-docs/resolve-api"); + // note: if the first argument of urjoin is "", it will strip the leading slash. `|| "/"` ensures "" -> "/" + const key = urljoin(basePath || "/", "/api/fern-docs/resolve-api"); const { data } = useSWR | null>(key, fetcher, { revalidateOnFocus: false, diff --git a/packages/ui/app/src/services/useApiKeyInjectionConfig.ts b/packages/ui/app/src/services/useApiKeyInjectionConfig.ts index 84ce4086cb..689305ea6c 100644 --- a/packages/ui/app/src/services/useApiKeyInjectionConfig.ts +++ b/packages/ui/app/src/services/useApiKeyInjectionConfig.ts @@ -9,7 +9,8 @@ const DEFAULT = { enabled: false as const }; export function useApiKeyInjectionConfig(): APIKeyInjectionConfig { const basePath = useBasePath(); - const key = urljoin(basePath ?? "", API_KEY_INJECTION_ROUTE); + // note: if the first argument of urjoin is "", it will strip the leading slash. `|| "/"` ensures "" -> "/" + const key = urljoin(basePath || "/", API_KEY_INJECTION_ROUTE); const { data } = useSWR( key, diff --git a/packages/ui/app/src/services/useSearchService.ts b/packages/ui/app/src/services/useSearchService.ts index 0b9709a2b2..40807a211d 100644 --- a/packages/ui/app/src/services/useSearchService.ts +++ b/packages/ui/app/src/services/useSearchService.ts @@ -34,7 +34,8 @@ export function useSearchConfig(): [SearchConfig, refresh: () => void] { return [{ isAvailable: false }, noop]; } - const key = urljoin(basePath ?? "", "/api/fern-docs/search"); + // note: if the first argument of urjoin is "", it will strip the leading slash. `|| "/"` ensures "" -> "/" + const key = urljoin(basePath || "/", "/api/fern-docs/search"); const { data } = useSWR(key, (url: string) => fetch(url).then((res) => res.json()), { refreshInterval: 1000 * 60 * 60 * 2, // 2 hours diff --git a/packages/ui/docs-bundle/src/utils/getDocsPageProps.ts b/packages/ui/docs-bundle/src/utils/getDocsPageProps.ts index b752cd6d7f..29a38bee22 100644 --- a/packages/ui/docs-bundle/src/utils/getDocsPageProps.ts +++ b/packages/ui/docs-bundle/src/utils/getDocsPageProps.ts @@ -374,7 +374,8 @@ async function convertDocsToDocsPageProps({ theme: docs.baseUrl.domain.includes("cohere") ? "cohere" : "default", }; - props.fallback[urljoin(docs.baseUrl.basePath ?? "", "/api/fern-docs/search")] = await getSearchConfig( + // note: if the first argument of urjoin is "", it will strip the leading slash. `|| "/"` ensures "" -> "/" + props.fallback[urljoin(docs.baseUrl.basePath || "/", "/api/fern-docs/search")] = await getSearchConfig( xFernHost, docs.definition.search, ); @@ -392,7 +393,8 @@ async function convertDocsToDocsPageProps({ } const apiKeyInjectionConfig = await getAPIKeyInjectionConfigNode(xFernHost, cookies); - props.fallback[urljoin(docs.baseUrl.basePath ?? "", "/api/fern-docs/auth/api-key-injection")] = + // note: if the first argument of urjoin is "", it will strip the leading slash. `|| "/"` ensures "" -> "/" + props.fallback[urljoin(docs.baseUrl.basePath || "/", "/api/fern-docs/auth/api-key-injection")] = apiKeyInjectionConfig; return {