text: import { createNodeMiddleware } from '../middleware'
  export const assignAPI = createNodeMiddleware(({ file }) => {
      file.api = new ClientApi(file)
  })
  
  class ClientApi {
      constructor(file) {
          this.__file = file
          Object.defineProperty(this, '__file', { enumerable: false })
          this.isMeta = !!file.isMeta
          this.path = file.path
          this.title = _prettyName(file)
          this.meta = file.meta
      }
  
      get parent() { return !this.__file.root && this.__file.parent.api }
      get children() {
          return (this.__file.children || this.__file.isLayout && this.__file.parent.children || [])
              .filter(c => !c.isNonIndexable)
              .sort((a, b) => {
                  if (a.isMeta && b.isMeta) return 0
                  a = (a.meta.index || a.meta.title || a.path).toString()
                  b = (b.meta.index || b.meta.title || b.path).toString()
                  return a.localeCompare((b), undefined, { numeric: true, sensitivity: 'base' })
              })
              .map(({ api }) => api)
      }
      get next() { return _navigate(this, +1) }
      get prev() { return _navigate(this, -1) }
      async preload() {
          const filePromises = [
              ...this.__file.layouts,
              this.__file,
              this.index && this.index.__file //if this is a layout, we want to include its index
          ]
              .filter(Boolean)
              .map(file => file.component())
          await Promise.all(filePromises)
      }
      get component() {
          return this.__file.component ? //is file?
              this.__file.component()
              : this.__file.index ? //is dir with index?
                  this.__file.index.component()
                  : false
      }
      get componentWithIndex() {
          return new Promise(resolve =>
              Promise.all([
                  this.component,
                  this.index && this.index.component
              ])
                  .then(res => resolve(res))
          )
      }
      get index() {
          const child = this.__file.children &&
              this.__file.children.find(child => child.isIndex)
          return child && child.api
      }
  }
  
  function _navigate(node, direction) {
      if (!node.__file.root) {
          const siblings = node.parent.children
          const index = siblings.indexOf(node)
          return node.parent.children[index + direction]
      }
  }
  
  
  function _prettyName(file) {
      if (typeof file.meta.title !== 'undefined') return file.meta.title
      else return (file.shortPath || file.path)
          .split('/')
          .pop()
          .replace(/-/g, ' ')
  }
  
  