diff options
Diffstat (limited to 'client/client.js')
| -rw-r--r-- | client/client.js | 61 |
1 files changed, 39 insertions, 22 deletions
diff --git a/client/client.js b/client/client.js index a48b53e..a769367 100644 --- a/client/client.js +++ b/client/client.js @@ -1,10 +1,10 @@ -function get() { +function _get(data) { return { method: 'GET', headers: { 'Accept': 'application/json', - 'Content-Type': 'application/json' }, + query: data, } } function post(data) { @@ -20,16 +20,26 @@ function post(data) { function postBody(data) { return { method: 'POST', + body: data, headers: { 'Accept': 'application/json', }, - body: data, } } function put(data) { return { method: 'PUT', - body: data, + body: JSON.stringify(data), + headers: { + 'Accept': 'application/json', + 'Content-Type': 'application/json' + }, + } +} +function destroy(data) { + return { + method: 'DELETE', + body: JSON.stringify(data), headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' @@ -39,44 +49,51 @@ function put(data) { function error(err) { console.warn(err) } - -export default { - folder: { - index: () => { - return fetch('/folders/') +function crud(type_s) { + const type = '/' + type_s + 's/' + return { + index: (data) => { + return fetch(type, _get(data)) .then(req => req.json()) .catch(error) }, show: (id) => { - return fetch('/folders/' + id) + return fetch(type + id) .then(req => req.json()) .catch(error) }, create: (data) => { - return fetch('/folders/new', post(data)) + return fetch(type, post(data)) .then(req => req.json()) .catch(error) }, update: (data) => { - return fetch('/folders/' + data.id, put(data)) + return fetch(type + data.id, put(data)) .then(req => req.json()) .catch(error) }, - }, - - file: { - upload: (folder_id, files) => { - var data = new FormData() - for (var i = 0; i < files.length; i++) { - data.append('file', files[i]) - } - return fetch('/folders/' + folder_id, postBody(files)) + + destroy: (data) => { + return fetch(type + data.id, destroy(data)) .then(req => req.json()) .catch(error) }, + } +} + +export default { + folder: crud('folder'), + file: crud('file'), + upload: (folder_id, files) => { + var data = new FormData() + for (var i = 0; i < files.length; i++) { + data.append('file', files[i]) + } + return fetch('/folders/' + folder_id, postBody(files)) + .then(req => req.json()) + .catch(error) }, - }
\ No newline at end of file |
