Recursive flatten
@private
@param {RestRoutes} routes Routes object from `createRest()`
@param {string} prefix Add prefix to each route path
@param {RestRoutes} parent Parent routes
function flattenize(routes, prefix, parent) {
  const list = {}

  Object.keys(routes.local).forEach((method) => {
    const localName = prefix === '' ? '/' : `${prefix}/`

    if (!list[localName]) {
      list[localName] = {}
    }
    list[localName][method] = [].concat(
      parent.before,
      routes.before,
      routes.local[method],
      routes.after,
      parent.after
    )
  })

  Object.keys(routes.scoped).forEach((scope) => {
    const scoped = routes.scoped[scope]
    const listeners = flattenize(scoped, `${prefix}/${scope}`, {
      before: parent.before.concat(routes.before),
      after: routes.after.concat(parent.after),
    })

    Object.assign(list, listeners)
  })

  return list
}