all files / src/network/remote/ item.js

22.86% Statements 8/35
15% Branches 3/20
15.38% Functions 2/13
22.86% Lines 8/35
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98                                                                                                                                                                                   
'use strict';
 
Object.defineProperty(exports, "__esModule", {
  value: true
});
 
exports.default = function (_ref) {
  var client = _ref.client,
      filterQuery = _ref.filterQuery,
      mustContain = _ref.mustContain,
      busy = _ref.busy,
      encodeQueryAsString = _ref.encodeQueryAsString;
 
  return {
    downloadItem: function downloadItem(id, offset, endByte, contentDisposition) {
      var params = { offset: offset, endByte: endByte, contentDisposition: contentDisposition };
      Object.keys(params).forEach(function (key) {
        if (params[key] === null) {
          delete params[key];
        }
      });
      return busy(client._.get('/item/' + id + '/download' + encodeQueryAsString(params)));
    },
    updateItemMetadata: function updateItemMetadata(id) {
      var metadata = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
 
      return busy(client._.put('/item/' + id + '/metadata', metadata));
    },
 
 
    // query = { folderId, text, limit, name, offset, sort, sortdir }
    listItems: function listItems() {
      var query = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
 
      var allowed = ['folderId', 'text', 'name', 'limit', 'offset', 'sort', 'sortdir'];
      var params = filterQuery.apply(undefined, [query].concat(allowed));
 
      return busy(client._.get('/item' + encodeQueryAsString(params)));
    },
    createItem: function createItem(folderId, name) {
      var description = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '';
 
      var params = { folderId: folderId, name: name, description: description };
      return busy(client._.post('/item' + encodeQueryAsString(params)));
    },
 
 
    // query = { limit, offset, sort }
    listFiles: function listFiles(id, query) {
      var allowed = ['limit', 'offset', 'sort'];
      var params = filterQuery.apply(undefined, [query].concat(allowed));
 
      if (!id) {
        return new Promise(function (resolve, reject) {
          return reject('No argument id provided');
        });
      }
 
      return busy(client._.get('/item/' + id + '/files' + encodeQueryAsString(params)));
    },
    getItemRootPath: function getItemRootPath(id) {
      return busy(client._.get('/item/' + id + '/rootpath'));
    },
    getItem: function getItem(id) {
      return busy(client._.get('/item/' + id));
    },
    deleteItem: function deleteItem(id) {
      return busy(client._.delete('/item/' + id));
    },
 
 
    // item = { id, folderId, name, description }
    editItem: function editItem(item) {
      var expected = ['folderId', 'name', 'description'];
      var params = filterQuery.apply(undefined, [item].concat(expected));
 
      var _mustContain = mustContain(params, '_id'),
          missingKeys = _mustContain.missingKeys,
          promise = _mustContain.promise;
 
      return missingKeys ? promise : busy(client._.put('/item/' + item._id + encodeQueryAsString(params)));
    },
 
 
    // destinationItem = { folderId, name, description }
    copyItem: function copyItem(id, destinationItem) {
      var expected = ['folderId', 'name', 'description'];
      var params = filterQuery.apply(undefined, [destinationItem].concat(expected));
 
      var _mustContain2 = mustContain(params, 'folderId'),
          missingKeys = _mustContain2.missingKeys,
          promise = _mustContain2.promise;
 
      return missingKeys ? promise : busy(client._.post('/item/' + id + '/copy' + encodeQueryAsString(params)));
    }
  };
};