Skip to content

Commit

Permalink
feat: catchup
Browse files Browse the repository at this point in the history
  • Loading branch information
nickfrosty committed Nov 30, 2023
1 parent c8ba8c5 commit c030e2c
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 2 deletions.
48 changes: 48 additions & 0 deletions src/pages/api/nav/[[...group]].ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* api route to generate the nav item listing for
* each supported content record `group`
*/

import type { NextApiRequest, NextApiResponse } from "next";
import { NavItem, SimpleRecordGroupName } from "@/types";
import { generateNavItemListing } from "@/utils/navItem";
import {
allDeveloperGuides,
// allDeveloperResources,
allSolanaDocs,
allDeveloperWorkshops,
allSolanaRPCDocs,
} from "contentlayer/generated";

export default function handler(
req: NextApiRequest,
res: NextApiResponse<SimpleNotFound | NavItem[]>,
) {
// get the content record group
const group = req.query?.group?.toString() as SimpleRecordGroupName;
if (!group) return res.status(404).json({ notFound: true });

// retrieve the correct group's records by its simple group name
const records = ((group: SimpleRecordGroupName) => {
switch (group) {
case "rpc":
case "docs,rpc":
return allSolanaRPCDocs;
case "docs":
return allSolanaDocs;
case "guides":
return allDeveloperGuides;
// case "resources":
// return allDeveloperResources;
case "workshops":
return allDeveloperWorkshops;
}
})(group);

if (!records) return res.status(404).json({ notFound: true });

const navItems = generateNavItemListing(records);

// finally, return the json formatted listing of NavItems
return res.status(200).json(navItems);
}
79 changes: 79 additions & 0 deletions src/pages/api/paths/[[...group]].ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/**
* api route to generate a path listing for
* each supported content record `group`
*/

import type { NextApiRequest, NextApiResponse } from "next";
import { NavItem, SimpleRecordGroupName } from "@/types";
import { computeNavItem, shouldIgnoreRecord } from "@/utils/navItem";
import {
allDeveloperGuides,
allDeveloperResources,
allSolanaDocs,
allDeveloperWorkshops,
allSolanaRPCDocs,
} from "contentlayer/generated";

export default function handler(
req: NextApiRequest,
res: NextApiResponse<SimpleNotFound | NavItem[]>,
) {
// get the content record group
const group = req.query?.group?.toString() as SimpleRecordGroupName;
if (!group) return res.status(404).json({ notFound: true });

// retrieve the correct group's records by its simple group name
const records = ((group: SimpleRecordGroupName) => {
switch (group) {
case "rpc":
case "docs,rpc":
return allSolanaRPCDocs;
case "docs":
return allSolanaDocs;
case "guides":
return allDeveloperGuides;
case "resources":
return allDeveloperResources;
case "workshops":
return allDeveloperWorkshops;
}
})(group);

if (!records) return res.status(404).json({ notFound: true });

// init the listing response
const listing: Array<NavItem> = [];

/**
* todo: assorted things
* - better support for external links
*/

// compute the path data to return
records.map(record => {
if (shouldIgnoreRecord({ fileName: record._raw.sourceFileName })) return;

// @ts-ignore
const navItem = computeNavItem(record);

if (!navItem.href || !!record.isExternal) return;

listing.push(navItem);

// handle adding each of the alternative routes into the path listing
if (!!record?.altRoutes?.length) {
record.altRoutes.forEach(route => {
if (!!route?.trim()) {
listing.push(
Object.assign(navItem, {
href: route.trim(),
}),
);
}
});
}
});

// finally, return the json formatted listing
return res.status(200).json(listing);
}
5 changes: 4 additions & 1 deletion src/pages/api/records/[group].ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
allDeveloperResources,
allSolanaDocs,
allDeveloperWorkshops,
allSolanaRPCDocs,
} from "contentlayer/generated";
import { simplifyRecords } from "@/utils/parsers";

Expand All @@ -23,6 +24,9 @@ export default function handler(
// retrieve the correct group's records by its simple group name
let records: SupportedDocTypes[] = ((group: SimpleRecordGroupName) => {
switch (group) {
case "rpc":
case "docs,rpc":
return allSolanaRPCDocs;
case "docs":
return allSolanaDocs;
case "guides":
Expand All @@ -40,7 +44,6 @@ export default function handler(
records = simplifyRecords(records);

// todo: add pagination support?
// todo: migrate to trpc and add filters support as well

// finally, return the json formatted listing
return res.status(200).json(records);
Expand Down
7 changes: 6 additions & 1 deletion src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ export type SupportedDocTypes = Exclude<DocumentTypes, IgnoredDoc>;
*/
export type SimpleRecordGroupName =
| "docs"
| "rpc"
| "docs,rpc" // note: this is to support stringify-ing the route via the url
| "guides"
| "resources"
| "workshops";
Expand All @@ -28,7 +30,10 @@ type NavItemBase = {
href?: String;
sidebarSortOrder?: number;
metaOnly?: boolean;
/** List of alternate routes that should redirect to this same document */
/**
*
*/
items?: Array<any>;
altRoutes?: string[] | undefined;
};

Expand Down

0 comments on commit c030e2c

Please sign in to comment.