Files
developer-roadmap/lib/guide.ts

46 lines
1.2 KiB
TypeScript
Raw Normal View History

2021-08-29 16:34:00 +02:00
import guides from '../content/guides.json';
import formatDate from 'date-fns/format';
2021-08-29 18:57:23 +02:00
import { NextApiRequest } from 'next';
import { AuthorType, findAuthorByUsername } from './author';
2021-08-29 16:34:00 +02:00
export type GuideType = {
2021-09-03 09:45:46 +02:00
id: string;
2021-08-29 16:34:00 +02:00
title: string;
description: string;
url: string;
fileName: string;
isPro: boolean;
isDraft: boolean;
createdAt: string;
updatedAt: string;
formattedCreatedAt: string;
formattedUpdatedAt: string;
2021-08-29 18:57:23 +02:00
authorUsername: string;
author?: AuthorType;
2021-08-29 16:34:00 +02:00
};
2021-09-03 09:45:46 +02:00
export function getGuideById(id: string): GuideType | undefined {
const allGuides = getAllGuides();
const foundGuide = allGuides.find(guide => guide.id === id);
if (!foundGuide) {
return undefined;
}
return {
...foundGuide,
author: findAuthorByUsername(foundGuide.authorUsername)
};
}
2021-08-29 16:36:51 +02:00
export function getAllGuides(limit: number = 0): GuideType[] {
2021-08-29 16:34:00 +02:00
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);
}