import fetch from 'isomorphic-fetch'

const serialise = data => JSON.stringify(data,
  (key, value) => (typeof value === 'function') ? value.toString() : value
)

export default config => {

  return {
    loadLessons() {
      return fetch(
        `${config.couchUrl}/${config.dataDB}/_design/lesson/_view/lesson`, {
          method: 'GET',
        }).then(res => res.json())
    },
    load(_id) {
      return fetch(
        `${config.couchUrl}/${config.dataDB}/${_id}`, {
          method: 'GET',
        }).then(res => res.json())
    },
    save(doc) {
      return fetch(`${config.couchUrl}/${config.dataDB}/${doc._id}`, {
        method: 'PUT',
        body: serialise(doc),
      })
    },
    createDb() {
      return fetch(`${config.couchUrl}/${config.dataDB}`, { method: 'PUT' })
    },
    deleteDb() {
      return fetch(`${config.couchUrl}/${config.dataDB}`, { method: 'DELETE' })
    },
  }
}

@@ -29,16 +29,55 @@
 c-fetch'
+%0Aimport couchTools from 'couchdb-tools'
 %0A%0Aconst 
@@ -167,16 +167,20 @@
 ion') ? 
+'('+
 value.to
@@ -187,16 +187,20 @@
 String()
++')'
  : value
@@ -230,17 +230,16 @@
 ig =%3E %7B%0A
-%0A
   return

make designdoc functions into expressions