1import useSWR from "swr"
2
3const fetcher = url => fetch(url).then(res => res.json())
4const baseUrl = "https://jsonplaceholder.typicode.com"
5
6export const useGetPosts = path => {
7 if (!path) {
8 throw new Error("Path is required")
9 }
10
11 const url = baseUrl + path
12
13 const { data: posts, error } = useSWR(url, fetcher)
14
15 return { posts, error }
16}