const querystring = require('querystring')

export default class JesqueAdminClient {

  get(target, id, query) {
    let path = this.buildPath(target, id, query);
    return fetch(path, {
      method: 'GET',
      credentials: "same-origin",
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
      },
      cache: false
    }).then((res)=> {
      return res.json()
    })
  }

  delete(target, id, query) {
    let path = this.buildPath(target, id, query)
    return fetch(path, {
      method: 'DELETE',
      credentials: "same-origin",
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
      },
      cache: false
    }).then((res)=> {
      return res.json()
    })
  }

  post(target, id, data) {
    let path = this.buildPath(target, id, {});
    return fetch(path, {
      method: 'POST',
      credentials: "same-origin",
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
      },
      cache: false,
      body: JSON.stringify(data)
    }).then((res)=> {
      return res.json()
    })
  }

  buildPath(target, id, query) {
    let path = window.AppConfig.links[target];
    if (id) {
      path = `${path}/${id}`
    }
    if(query) {
      path = `${path}?${querystring.stringify(query)}`
    }
    return path
  }

}

@@ -498,16 +498,17 @@
 , query)
+;
 %0A    ret
@@ -1230,16 +1230,45 @@
   if (id
+ !== undefined && id !== null
 ) %7B%0A    
@@ -1304,16 +1304,17 @@
 %7D%0A    if
+ 
 (query) 

fix buildPath to accept 0 as id