import axios from 'axios'

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

  // Let's supply all of the posts data to the /blog page
  addRoute({
    path: 'blog',
    data: {
      posts,
    },
  })

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

  config({})
}
