In which folder should I put all my loaders/actions logic? #8383
Unanswered
theocerutti
asked this question in
Q&A
Replies: 1 comment
-
I like to convert my routes to folders and create a queries file inside.
In Then in in Then I call the functions from the export async function loader({ request, params }: LoaderFunctionArgs) {
await authenticate(request);
let userId = params.userId;
assertIsUUID(userId);
try {
// call the query functions
let [user, followers] = await Promise.all([
queryUserById(userId),
queryFollowersOfUser(userId)
]);
return json({ user, followers }); // return a response
} catch (exception) {
if (exception instanceof Response) throw exception; // re-throw responses
// handle specific errors you know the queries will throw
if (exception instanceof Error && exception.name === "User not found") {
throw json({ error: "NOT_FOUND" }, 404);
}
throw exception; // always re-throw unhandled exceptions
}
}
export async function action({ request, params }: ActionFunctionArgs) {
let currentUser = await authenticate(request);
let userId = params.userId;
assertIsUUID(userId);
let formData = await request.formData();
// if there's no intent, throw an error
if (!formData.has("intent")) throw new Error("MISSING_INTENT")
if (formData.get("intent") === INTENTS.followUser) {
await followUser(currentUser.id, userId); // call the query function
throw redirect(`/users/${userId}`); // I always throw redirects on actions
}
// validate more intents here and keep calling query functions
// finally throw a response error for any invalid intent
throw json({ error: "INVALID_INTENT", intent: formData.get("intent") }, 400);
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
My loaders and actions are starting to become really big so I want to split them into multiples files.
However I have no idea where should I put them.
Is this example good ?
Beta Was this translation helpful? Give feedback.
All reactions