From 703b70e455fe51dfda7681e8f809852aaa012bd1 Mon Sep 17 00:00:00 2001 From: Edmund Kump Date: Mon, 9 Mar 2015 14:41:02 -0400 Subject: [PATCH] adding upsert method for folders --- lib/index.js | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/lib/index.js b/lib/index.js index 14a076e..53a1488 100644 --- a/lib/index.js +++ b/lib/index.js @@ -230,7 +230,7 @@ addMethod(Folders.prototype, 'items', function (folder, limit, offset, fields, c } var uri = this.options.base_url+'/'+this.resource+'/'+folder+'/items'; - if (fields) { + if (fields) { uri += '?fields=' + fields +'&offset=' + offset + '&limit='+ limit; } else { uri += '?offset=' + offset + '&limit='+ limit; @@ -255,7 +255,7 @@ addMethod(Folders.prototype, 'items', function (folder, limit, offset, shareLink } var uri = this.options.base_url+'/'+this.resource+'/'+folder+'/items'; - if (fields) { + if (fields) { uri += '?fields=' + fields +'&offset=' + offset + '&limit='+ limit; } else { uri += '?offset=' + offset + '&limit='+ limit; @@ -291,6 +291,28 @@ Folders.prototype.create = function (name, parent, callback) { }); }; +// upsert a folder + Folders.prototype.upsert = function (name, parent, callback) { + request + .post(this.options.base_url+'/'+this.resource) + .set('Authorization', this.options.auth) + .send({ + name: name, + parent : { id: parent } + }) + .end(function (res) { + if (res.status == 409) { + var existingFolder = res.body.context_info.conflicts.filter( function(conflict) { return ((conflict.type == 'folder') && (conflict.name == name)) })[0]; + return callback(null, existingFolder); + } + else if (res.error) { + return callback('Error: '+res.error.message); + } + + callback(null, res.body); + }); + }; + // Deletes a folder. Recursive arguement must be included in order to delete folders that have items inside of them Folders.prototype.delete = function (folder, recursive, callback) { if (typeof recursive === 'function') {