import axios from 'axios'

export const config = {}

export default async ({ addRoute }) => {
  // We can fetch any data we want, however we want!
  const { data: posts } = await axios.get('https://jsonplaceholder.typicode.com/posts')

  // /blog is already a route, but we can still use addRoute
  // to supply all of the posts data to it
  addRoute({
    path: 'blog',
    data: {
      posts,
    },
  })

  // Now we'll take care of each individual post!
  // Since each of these is a generated route and not a page, we need to
  // specify a template to render it.
  posts.forEach(post =>
    addRoute({
      path: `blog/post/${post.id}`,
      component: 'Post',
      data: {
        post,
      },
    })
  )
}
