-
Notifications
You must be signed in to change notification settings - Fork 14
/
$postId.tsx
41 lines (37 loc) · 1.05 KB
/
$postId.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import { useCatch, useLoaderData } from "@remix-run/react";
import type { LoaderArgs } from "@remix-run/server-runtime";
import { zx } from "../../../../src/index";
async function getPost(postId: number) {
return Promise.resolve({
id: postId,
title: "A post",
body: "This is a post",
});
}
export async function loader({ params }: LoaderArgs) {
// try {
const { postId } = zx.parseParams(
params,
{ postId: zx.NumAsString },
// Set a custom message and status code for the response Zodix throws
// when parsing throws.
{ message: "Invalid postId parameter", status: 400 }
);
const post = await getPost(postId);
return { post };
}
export default function PostPage() {
const { post } = useLoaderData<typeof loader>();
return (
<div>
<h1>{post.title}</h1>
<p>Post ID: {post.id}</p>
<p>{post.body}</p>
</div>
);
}
// Catch the error response thrown by Zodix when parsing fails.
export function CatchBoundary() {
const caught = useCatch();
return <h1>Caught error: {caught.statusText}</h1>;
}