API
DirectoryFast is built on the latest NextJs features, including the App router.
Structure
All your "backend" stuff is located in the /api
folder.
Any route.ts
file will be an API endpoint, and the folder route will be the final API route. You can define dynamic content as well:
folder
url
/api/products
/products
/api/products/newest
/products/newest
/api/products/[slug]
/products/the-best-product
/api/category/[id]/products
/category/1337/products
Calls
API calls are handled by Tanstack-Query, a powerful and simple library that handle cache, background updates or stale data out of the box.
GET example:
const { isLoading: isUsersLoading, data: users } = useQuery({
queryKey: [`users`],
queryFn: async () => {
const res = await fetch(`/api/users`);
return res.json();
},
});
POST example:
const createMutation = useMutation({
mutationFn: async (values) => {
const res = await fetch("/api/categories", {
method: "POST",
body: JSON.stringify({
...values,
}),
});
return res.json();
},
});
Last updated