Skip to content

Commit 5d37125

Browse files
committed
fix: some better typing
1 parent 29c7eac commit 5d37125

File tree

4 files changed

+11
-13
lines changed

4 files changed

+11
-13
lines changed

src/lib/blog.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
// Install gray-matter and date-fns
22
import { join } from 'path';
33

4+
import { ContentTypes } from '../data/content-types';
45
import type { BlogPost } from '../data/content-types';
56
import { getAllContentFromDirectory } from './content-loaders/getAllContentFromDirectory';
67
import { getContentBySlug } from './content-loaders/getContentBySlug';
78

89
// Add markdown files in `src/content/blog`
910
const postsDirectory = join(process.cwd(), 'src', 'data', 'posts');
10-
const POST_CONTENT_TYPE = 'post';
1111

1212
export const getPostBySlug = async (slug: string) => {
13-
const post = await getContentBySlug(slug, postsDirectory, POST_CONTENT_TYPE);
13+
const post = await getContentBySlug(slug, postsDirectory, ContentTypes.Post);
1414

1515
return post as BlogPost;
1616
};
1717

1818
export const getAllPosts = async () => {
1919
const allPosts = (await getAllContentFromDirectory(
2020
postsDirectory,
21-
POST_CONTENT_TYPE
21+
ContentTypes.Post
2222
)) as BlogPost[];
2323

2424
return allPosts;

src/lib/content-loaders/getContentBySlug.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import fs from 'fs';
22
import { join } from 'path/posix';
33

4-
import type { MarkdownDocument } from '@data/content-types';
4+
import type { ContentType, MarkdownDocument } from '@data/content-types';
55
import { processMDXFileContent } from './processMDXFileContent';
66

77
/**
@@ -16,7 +16,7 @@ import { processMDXFileContent } from './processMDXFileContent';
1616
export const getContentBySlug = async (
1717
slug: string,
1818
directory: fs.PathLike,
19-
type: string
19+
type: ContentType
2020
): Promise<MarkdownDocument> => {
2121
try {
2222
const realSlug = slug.replace(/\.mdx$/, '');

src/lib/external-references.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { join } from 'path';
22

3+
import { ContentTypes } from '../data/content-types';
34
import type { Article } from '../data/content-types';
45
import { getAllContentFromDirectory } from './content-loaders/getAllContentFromDirectory';
56
import { getContentBySlug } from './content-loaders/getContentBySlug';
@@ -12,21 +13,19 @@ const externalReferencesDirectory = join(
1213
'external-references'
1314
);
1415

15-
const EXTERNAL_REFERENCES_CONTENT_TYPE = 'article';
16-
1716
export const getExternalReferenceBySlug = async (slug: string) => {
1817
const reference = await getContentBySlug(
1918
slug,
2019
externalReferencesDirectory,
21-
EXTERNAL_REFERENCES_CONTENT_TYPE
20+
ContentTypes.Article
2221
);
2322
return reference as Article;
2423
};
2524

2625
export const getAllExternalReferences = async () => {
2726
let articles = (await getAllContentFromDirectory(
2827
externalReferencesDirectory,
29-
EXTERNAL_REFERENCES_CONTENT_TYPE
28+
ContentTypes.Article
3029
)) as Article[];
3130

3231
// filter out articles that don't have a slug

src/lib/newsletters.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { join } from 'path';
22

3+
import { ContentTypes } from '../data/content-types';
34
import type { MarkdownDocument, Newsletter } from '../data/content-types';
45
import { getAllContentFromDirectory } from './content-loaders/getAllContentFromDirectory';
56
import { getContentBySlug } from './content-loaders/getContentBySlug';
@@ -12,8 +13,6 @@ export const newslettersDirectory = join(
1213
'newsletters'
1314
);
1415

15-
export const NEWSLETTERS_CONTENT_TYPE = 'newsletter';
16-
1716
// Helper function to safely process raw content into newsletters
1817
export const processNewslettersContent = (
1918
rawContent: MarkdownDocument[]
@@ -55,7 +54,7 @@ export const getNewsletterBySlug = async (slug: string) => {
5554
const reference = await getContentBySlug(
5655
slug,
5756
newslettersDirectory,
58-
NEWSLETTERS_CONTENT_TYPE
57+
ContentTypes.Newsletter
5958
);
6059
return reference as Newsletter;
6160
};
@@ -64,7 +63,7 @@ export const getAllNewsletters = async () => {
6463
try {
6564
const rawContent = await getAllContentFromDirectory(
6665
newslettersDirectory,
67-
NEWSLETTERS_CONTENT_TYPE
66+
ContentTypes.Newsletter
6867
);
6968
return processNewslettersContent(rawContent);
7069
} catch (error) {

0 commit comments

Comments
 (0)