Skip to content

Commit

Permalink
refactor: getNavigationRoot and getAllUrlsFromDocsConfig should both …
Browse files Browse the repository at this point in the history
…have the same parameter order
  • Loading branch information
abvthecity committed Mar 25, 2024
1 parent 03d5f20 commit 0f76888
Show file tree
Hide file tree
Showing 12 changed files with 51 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export function testGetAllUrlsFromDocsConfig(fixtureName: string): void {
const urls = getAllUrlsFromDocsConfig(
fixture.baseUrl.domain,
fixture.baseUrl.basePath,
fixture.definition.config,
fixture.definition.config.navigation,
fixture.definition.apis,
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ export function testGetNavigationRoot(fixtureName: string, slug: string): void {
const urls = getNavigationRoot(
slug.split("/"),
fixture.baseUrl.basePath,
fixture.definition.apis,
fixture.definition.config.navigation,
fixture.definition.apis,
);

expect(stripForSnapshot(urls)).toMatchSnapshot();
Expand Down
5 changes: 3 additions & 2 deletions packages/commons/fdr-utils/src/getAllUrlsFromDocsConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import { visitSidebarNodeRaw } from "./visitSidebarNodeRaw";
export function getAllUrlsFromDocsConfig(
host: string,
basePath: string | undefined,
docsConfig: DocsV1Read.DocsConfig,
nav: DocsV1Read.NavigationConfig,
apis: Record<string, APIV1Read.ApiDefinition>,
): string[] {
const root = resolveSidebarNodesRoot(docsConfig.navigation, apis, basePath);
const basePathSlug = basePath != null ? basePath.split("/").filter((t) => t.length > 0) : [];
const root = resolveSidebarNodesRoot(nav, apis, basePathSlug);
const visitedSlugs: string[] = [];

visitSidebarNodeRaw(root, (node) => {
Expand Down
6 changes: 4 additions & 2 deletions packages/commons/fdr-utils/src/getNavigationRoot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,12 @@ const PRIORITY_LIST: Record<SidebarNodeRaw.VisitableNode["type"], number> = {
export function getNavigationRoot(
slugArray: string[],
basePath: string | undefined,
apis: Record<FdrAPI.ApiId, APIV1Read.ApiDefinition>,
nav: DocsV1Read.NavigationConfig,
apis: Record<FdrAPI.ApiId, APIV1Read.ApiDefinition>,
): Found | Redirect | undefined {
const root = resolveSidebarNodesRoot(nav, apis, basePath);
const basePathSlug = basePath != null ? basePath.split("/").filter((t) => t.length > 0) : [];

const root = resolveSidebarNodesRoot(nav, apis, basePathSlug);
const hits: { node: SidebarNodeRaw.VisitableNode; parents: SidebarNodeRaw.ParentNode[] }[] = [];

visitSidebarNodeRaw(root, (node, parents) => {
Expand Down
4 changes: 1 addition & 3 deletions packages/commons/fdr-utils/src/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,8 @@ function stringifyEndpointPathParts(path: APIV1Read.EndpointPathPart[]): string
export function resolveSidebarNodesRoot(
nav: DocsV1Read.NavigationConfig,
apis: Record<FdrAPI.ApiId, APIV1Read.ApiDefinition>,
basePath: string | undefined,
basePathSlug: string[],
): SidebarNodeRaw.Root {
const basePathSlug = basePath != null ? basePath.split("/").filter((t) => t.length > 0) : [];

return {
type: "root",
slug: basePathSlug,
Expand Down
1 change: 0 additions & 1 deletion packages/ui/app/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ export type { ProxyRequest, ProxyResponse } from "./api-playground/types";
export type { FeatureFlags } from "./contexts/FeatureFlagContext";
export * from "./next-app/DocsPage";
export { NextApp } from "./next-app/NextApp";
export { getNotFoundPageStaticProps, NotFoundPage } from "./next-app/NotFoundPage";
export { REGISTRY_SERVICE } from "./services/registry";
export * from "./sidebar/serializer";
export { convertNavigatableToResolvedPath } from "./util/convertNavigatableToResolvedPath";
Expand Down
38 changes: 0 additions & 38 deletions packages/ui/app/src/next-app/NotFoundPage.tsx

This file was deleted.

39 changes: 37 additions & 2 deletions packages/ui/docs-bundle/src/pages/404.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,40 @@
import { getNotFoundPageStaticProps, NotFoundPage } from "@fern-ui/ui";
import { buildUrl } from "@fern-ui/fdr-utils";
import { REGISTRY_SERVICE } from "@fern-ui/ui";
import { GetStaticProps } from "next";
import dynamic from "next/dynamic";
import { useRouter } from "next/router";

export const getStaticProps = getNotFoundPageStaticProps;
export declare namespace NotFoundPage {
export interface Props {
basePath: string | null;
}
}

const Core: React.FC<NotFoundPage.Props> = ({ basePath }) => {
const router = useRouter();
void router.push(basePath ?? "/");
return null;
};

const NotFoundPage = dynamic(() => Promise.resolve(Core), {
ssr: false,
});

export default NotFoundPage;

export const getStaticProps: GetStaticProps<NotFoundPage.Props> = async ({ params = {} }) => {
const host = params.host as string | undefined;
const slugArray = params.slug as string[] | undefined;
const pathname = slugArray != null ? slugArray.join("/") : "";
const docs = await REGISTRY_SERVICE.docs.v2.read.getDocsForUrl({
url: process.env.NEXT_PUBLIC_DOCS_DOMAIN ?? buildUrl({ host: host ?? "", pathname }),
});

if (!docs.ok) {
return { props: { basePath: null } };
}

const basePath = docs.body.baseUrl.basePath ?? null;

return { props: { basePath } };
};
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ const resolveApiHandler: NextApiHandler = async (req, res: NextApiResponse<Resol
const navigation = getNavigationRoot(
pathname.slice(1).split("/"),
basePath,
docsDefinition.apis,
docsDefinition.config.navigation,
docsDefinition.apis,
);

if (navigation == null || navigation.type === "redirect") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ const handler: NextApiHandler = async (
const urls = getAllUrlsFromDocsConfig(
xFernHost,
docs.baseUrl.basePath,
docs.definition.config,
docs.definition.config.navigation,
docs.definition.apis,
);

Expand Down
2 changes: 1 addition & 1 deletion packages/ui/docs-bundle/src/pages/api/fern-docs/sitemap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export default async function GET(req: NextRequest): Promise<NextResponse> {
const urls = getAllUrlsFromDocsConfig(
xFernHost,
docs.baseUrl.basePath,
docs.definition.config,
docs.definition.config.navigation,
docs.definition.apis,
);

Expand Down
2 changes: 1 addition & 1 deletion packages/ui/docs-bundle/src/utils/getDocsPageProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ async function convertDocsToDocsPageProps({
};
}

const navigation = getNavigationRoot(slug, basePath, docs.definition.apis, docsConfig.navigation);
const navigation = getNavigationRoot(slug, basePath, docsConfig.navigation, docs.definition.apis);

if (navigation == null) {
// eslint-disable-next-line no-console
Expand Down

0 comments on commit 0f76888

Please sign in to comment.