Skip to content

Commit

Permalink
hide api playground on broken pages
Browse files Browse the repository at this point in the history
  • Loading branch information
abvthecity committed May 23, 2024
1 parent 5e612ad commit 4a30921
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 26 deletions.
22 changes: 9 additions & 13 deletions packages/ui/app/src/api-playground/PlaygroundContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,15 @@ export const PlaygroundContextProvider: FC<PropsWithChildren> = ({ children }) =
if (unfetchedApis.length === 0) {
return;
}

void Promise.all(
unfetchedApis.map(async (apiId) => {
const r = await fetch(
urljoin(basePath ?? "", "/api/fern-docs/resolve-api?path=" + (basePath ?? "/") + "&api=" + apiId),
);
const data: Record<string, ResolvedRootPackage> | null = await r.json();
if (data == null) {
return;
}
setApis((currentApis) => ({ ...currentApis, ...data }));
}),
);
const fetchApis = async () => {
const r = await fetch(urljoin(basePath ?? "", "/api/fern-docs/resolve-api"));
const data: Record<string, ResolvedRootPackage> | null = await r.json();
if (data == null) {
return;
}
setApis((currentApis) => ({ ...currentApis, ...data }));
};
void fetchApis();
}, [apiIds, apis, basePath, setApis]);

const flattenedApis = useMemo(() => mapValues(apis, flattenRootPackage), [apis]);
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/app/src/api-playground/PlaygroundDrawer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ export const PlaygroundDrawer: FC<PlaygroundDrawerProps> = ({ apis }) => {
group: undefined,
};

if (!hasPlayground) {
if (!hasPlayground || apiGroups.length === 0) {
return null;
}

Expand Down
4 changes: 2 additions & 2 deletions packages/ui/app/src/api-playground/VerticalSplitPane.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export function VerticalSplitPane({
}
}, []);

const handleVerticalResize = useVerticalSplitPane(setHeight);
const { handleVerticalResize } = useVerticalSplitPane(setHeight);

const [above, below] = Children.toArray(children);

Expand Down Expand Up @@ -74,7 +74,7 @@ export function HorizontalSplitPane({
children,
rizeBarHeight,
mode = "percent",
initialLeftWidth = mode === "percent" ? 0.6 : 300,
initialLeftWidth = mode === "percent" ? 0.5 : 300,
...props
}: PropsWithChildren<HorizontalSplitPaneProps>): ReactElement | null {
const [leftWidth, setLeftWidth] = useState(initialLeftWidth);
Expand Down
15 changes: 5 additions & 10 deletions packages/ui/docs-bundle/src/pages/api/fern-docs/resolve-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@ const resolveApiHandler: NextApiHandler = async (
return;
}
const hostWithoutTrailingSlash = xFernHost.endsWith("/") ? xFernHost.slice(0, -1) : xFernHost;
const maybePathName = req.query.path;
const api = req.query.api;
const fullUrl = req.url;

if (maybePathName == null || typeof maybePathName !== "string" || api == null || typeof api !== "string") {
return res.status(400).json(null);
if (fullUrl == null) {
res.status(400).json(null);
return;
}

const maybePathName = fullUrl.split("/api/fern-docs/resolve-api")[0] ?? "";
const pathname = maybePathName.startsWith("/") ? maybePathName : `/${maybePathName}`;
const url = `${hostWithoutTrailingSlash}${pathname}`;
// eslint-disable-next-line no-console
Expand All @@ -46,14 +47,8 @@ const resolveApiHandler: NextApiHandler = async (

const docs = docsResponse.body;
const docsDefinition = docs.definition;
const apiDefinition = docsDefinition.apis[api];
const docsConfig = docsDefinition.config;

if (apiDefinition == null) {
res.status(404).json(null);
return;
}

const basePathSlug =
docs.baseUrl.basePath != null ? docs.baseUrl.basePath.split("/").filter((t) => t.length > 0) : [];

Expand Down

0 comments on commit 4a30921

Please sign in to comment.