Skip to content

Commit

Permalink
fix: data props with schema
Browse files Browse the repository at this point in the history
  • Loading branch information
JeanneGrenet committed Jul 17, 2024
1 parent 5f06e5a commit 5dc5e08
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 28 deletions.
4 changes: 2 additions & 2 deletions apps/web/src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ export default async function Page() {

<ul>
{posts.map((post) => (
<li key={post.title}>
<li key={post.data.title}>
<Link href={`/blog/${post.metadata.slug}`}>
{post.title} {post.author && <>by {post.author}</>}
{post.data.title} {post.data.author && <>by {post.data.author}</>}
</Link>
</li>
))}
Expand Down
51 changes: 25 additions & 26 deletions packages/typed-mdx/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,8 @@ async function stringifyMDX(mdxPath: string): Promise<string> {
}

const metadataSchema = z.object({
metadata: z.object({
slug: z.string(),
filePath: z.string(),
}),
});

const bodySchema = z.object({
body: z.string(),
slug: z.string(),
filePath: z.string(),
});

const MDX_ENTENSION = ".mdx";
Expand All @@ -99,7 +93,11 @@ async function getAll<Z extends z.Schema>({
folder: string;
schema: Z;
}): Promise<
(z.infer<Z> & z.infer<typeof metadataSchema> & z.infer<typeof bodySchema>)[]
{
data: z.infer<Z>;
body: string;
metadata: z.infer<typeof metadataSchema>;
}[]
> {
assertSchemaIsObject(schema);

Expand All @@ -116,7 +114,7 @@ async function getAll<Z extends z.Schema>({
const mdxFileNames = postFilePaths.filter(
(postFilePath) => path.extname(postFilePath).toLowerCase() === MDX_ENTENSION
);
const data = await Promise.all(
return await Promise.all(
mdxFileNames.map(async (mdxFileName) => {
const parsedFrontmatter = await parseMdxFile(
`${folderPath}/${mdxFileName}`,
Expand All @@ -133,12 +131,9 @@ async function getAll<Z extends z.Schema>({
};
const body = await stringifyMDX(`${folderPath}/${mdxFileName}`);

return { metadata, body, ...parsedFrontmatter };
return { metadata, body, data: parsedFrontmatter };
})
);
return z
.array(schema.merge(metadataSchema).merge(bodySchema))
.parse(data.filter(Boolean)) as any; // TODO FIX THIS ANY
}

async function getBySlug<Z extends z.Schema>({
Expand All @@ -149,9 +144,11 @@ async function getBySlug<Z extends z.Schema>({
folder: string;
schema: Z;
slug: string;
}): Promise<
z.infer<Z> & z.infer<typeof metadataSchema> & z.infer<typeof bodySchema>
> {
}): Promise<{
data: z.infer<Z>;
body: string;
metadata: z.infer<typeof metadataSchema>;
}> {
assertSchemaIsObject(schema);

const filePath = `${CONTENT_FOLDER}/${folder}/${slug}.mdx` as const;
Expand All @@ -178,16 +175,18 @@ export function defineCollection<Z extends z.Schema>(options: {
strict?: boolean;
}): {
getAll: () => Promise<
Prettify<
z.infer<Z> & z.infer<typeof metadataSchema> & z.infer<typeof bodySchema>
>[]
Prettify<{
data: z.infer<Z>;
body: string;
metadata: z.infer<typeof metadataSchema>;
}>[]
>;
getBySlug: (
slug: string
) => Promise<
Prettify<
z.infer<Z> & z.infer<typeof metadataSchema> & z.infer<typeof bodySchema>
>
getBySlug: (slug: string) => Promise<
Prettify<{
data: z.infer<Z>;
body: string;
metadata: z.infer<typeof metadataSchema>;
}>
>;
schema: Z;
} {
Expand Down

0 comments on commit 5dc5e08

Please sign in to comment.