computer-scienceangular-roadmapbackend-roadmapblockchain-roadmapdba-roadmapdeveloper-roadmapdevops-roadmapfrontend-roadmapgo-roadmaphactoberfestjava-roadmapjavascript-roadmapnodejs-roadmappython-roadmapqa-roadmapreact-roadmaproadmapstudy-planvue-roadmapweb3-roadmap
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
51 lines
1.3 KiB
51 lines
1.3 KiB
import guides from '../content/guides.json'; |
|
import formatDate from 'date-fns/format'; |
|
import { NextApiRequest } from 'next'; |
|
import { AuthorType, findAuthorByUsername } from './author'; |
|
|
|
export type GuideType = { |
|
title: string; |
|
description: string; |
|
url: string; |
|
fileName: string; |
|
isPro: boolean; |
|
isDraft: boolean; |
|
createdAt: string; |
|
updatedAt: string; |
|
formattedCreatedAt: string; |
|
formattedUpdatedAt: string; |
|
authorUsername: string; |
|
author?: AuthorType; |
|
}; |
|
|
|
export function getAllGuides(limit: number = 0): GuideType[] { |
|
return (guides as GuideType[]) |
|
.filter(guide => !guide.isDraft) |
|
.sort((a, b) => (new Date(b.updatedAt) as any) - (new Date(a.updatedAt) as any)) |
|
.map(guide => ({ |
|
...guide, |
|
formattedCreatedAt: formatDate(new Date(guide.createdAt), 'MMMM d, yyyy'), |
|
formattedUpdatedAt: formatDate(new Date(guide.updatedAt), 'MMMM d, yyyy') |
|
})) |
|
.slice(0, limit ? limit : guides.length); |
|
} |
|
|
|
|
|
export function getRequestedGuide(req: NextApiRequest): GuideType | undefined { |
|
const allGuides = getAllGuides(); |
|
const guide = allGuides.find(guide => guide.url === req.url); |
|
if (!guide) { |
|
return undefined; |
|
} |
|
|
|
try { |
|
return { |
|
...guide, |
|
author: findAuthorByUsername(guide.authorUsername) |
|
}; |
|
} catch (e) { |
|
console.log(e); |
|
} |
|
|
|
return undefined; |
|
}
|
|
|