syncano-server
Advanced tools
Comparing version 1.0.1-7 to 1.0.1-8
@@ -146,5 +146,3 @@ 'use strict'; | ||
relationships = this.relationships, | ||
instance = this.instance, | ||
mappedFields = this.mappedFields, | ||
_mapFields = this._mapFields; | ||
instance = this.instance; | ||
@@ -293,3 +291,3 @@ var fetch = this.fetch.bind(this); | ||
result = _mapFields(result, mappedFields); | ||
result = self._mapFields(result); | ||
@@ -312,10 +310,27 @@ return true; | ||
key: '_mapFields', | ||
value: function _mapFields(items, fields) { | ||
return fields.length === 0 ? items : items.map(function (item) { | ||
return Object.keys(fields).reduce(function (all, key) { | ||
return (0, _lodash2.default)(all, fields[key] || key, (0, _lodash4.default)(item, key)); | ||
}, {}); | ||
}); | ||
value: function _mapFields(items) { | ||
var _this3 = this; | ||
var fields = this.mappedFields; | ||
if (fields.length === 0) { | ||
return items; | ||
} | ||
if (Array.isArray(items)) { | ||
return items.map(function (item) { | ||
return _this3._mapFieldsForSingleItem(item, fields); | ||
}); | ||
} | ||
return this._mapFieldsForSingleItem(items, fields); | ||
} | ||
}, { | ||
key: '_mapFieldsForSingleItem', | ||
value: function _mapFieldsForSingleItem(item, fields) { | ||
return Object.keys(fields).reduce(function (all, key) { | ||
return (0, _lodash2.default)(all, fields[key] || key, (0, _lodash4.default)(item, key)); | ||
}, {}); | ||
} | ||
}, { | ||
key: '_getRelatedObjects', | ||
@@ -365,6 +380,6 @@ value: function _getRelatedObjects(reference, items) { | ||
value: function firstOrFail() { | ||
var _this3 = this; | ||
var _this4 = this; | ||
return new Promise(function (resolve, reject) { | ||
_this3.first().then(function (object) { | ||
_this4.first().then(function (object) { | ||
return object ? resolve(object) : reject(new _errors.NotFoundError()); | ||
@@ -388,3 +403,3 @@ }).catch(function () { | ||
value: function firstOrCreate(attributes) { | ||
var _this4 = this; | ||
var _this5 = this; | ||
@@ -396,3 +411,3 @@ var values = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
return this.where(query).firstOrFail().catch(function () { | ||
return _this4.create((0, _lodash6.default)(attributes, values)); | ||
return _this5.create((0, _lodash6.default)(attributes, values)); | ||
}); | ||
@@ -412,3 +427,3 @@ } | ||
value: function updateOrCreate(attributes) { | ||
var _this5 = this; | ||
var _this6 = this; | ||
@@ -420,5 +435,5 @@ var values = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
return this.where(query).firstOrFail().then(function (res) { | ||
return _this5.update(res.id, values); | ||
return _this6.update(res.id, values); | ||
}).catch(function () { | ||
return _this5.create((0, _lodash6.default)(attributes, values)); | ||
return _this6.create((0, _lodash6.default)(attributes, values)); | ||
}); | ||
@@ -472,6 +487,6 @@ } | ||
value: function findOrFail(ids) { | ||
var _this6 = this; | ||
var _this7 = this; | ||
return new Promise(function (resolve, reject) { | ||
_this6.find(ids).then(function (response) { | ||
_this7.find(ids).then(function (response) { | ||
var shouldThrow = Array.isArray(ids) ? response.length !== ids.length : response === null; | ||
@@ -543,3 +558,3 @@ | ||
value: function where(column, operator, value) { | ||
var _this7 = this; | ||
var _this8 = this; | ||
@@ -553,3 +568,3 @@ if (Array.isArray(column)) { | ||
return _this7.where(itemColumn, itemOperator, itemValue); | ||
return _this8.where(itemColumn, itemOperator, itemValue); | ||
}); | ||
@@ -686,9 +701,5 @@ | ||
value: function _chunk(items, size) { | ||
var chunks = []; | ||
while (items.length > 0) { | ||
chunks.push(items.splice(0, size)); | ||
} | ||
return chunks; | ||
return items.map(function (e, i) { | ||
return i % size === 0 ? items.slice(i, i + size) : null; | ||
}).filter(Boolean); | ||
} | ||
@@ -698,9 +709,10 @@ }, { | ||
value: function _batch(body, headers) { | ||
var _this8 = this; | ||
var _this9 = this; | ||
var type = Array.isArray(body[0]) ? 'PATCH' : isNaN(body[0]) === false ? 'DELETE' : 'POST'; | ||
var requests = this._chunk(body, MAX_BATCH_SIZE).map(function (chunk) { | ||
return function () { | ||
var fetchObject = _this8._batchFetchObject(chunk); | ||
var fetchObject = _this9._batchFetchObject(chunk); | ||
return _this8.fetch(fetchObject.url, fetchObject, headers); | ||
return _this9.fetch(fetchObject.url, fetchObject, headers); | ||
}; | ||
@@ -716,4 +728,8 @@ }); | ||
request().then(function (data) { | ||
resolves = resolves.concat(data); | ||
var items = data.map(function (item) { | ||
return item.content ? item.content : item; | ||
}); | ||
resolves = resolves.concat(items); | ||
next(); // eslint-disable-line promise/no-callback-in-promise | ||
@@ -724,3 +740,3 @@ }).catch(function (err) { | ||
} else { | ||
resolve(resolves); | ||
resolve(type === 'DELETE' ? body : resolves); | ||
} | ||
@@ -761,6 +777,6 @@ })(); | ||
} else if (Array.isArray(body)) { | ||
return this._batch(body, headers); | ||
return this._batch(body, headers).then(this._mapFields.bind(this)); | ||
} | ||
return this.fetch(fetchObject.url, fetchObject, headers); | ||
return this.fetch(fetchObject.url, fetchObject, headers).then(this._mapFields.bind(this)); | ||
} | ||
@@ -789,3 +805,3 @@ | ||
value: function update(id, body) { | ||
var _this9 = this; | ||
var _this10 = this; | ||
@@ -806,3 +822,3 @@ var headers = null; | ||
return _this9._batch(ids); | ||
return _this10._batch(ids).then(_this10._mapFields.bind(_this10)); | ||
}); | ||
@@ -815,6 +831,6 @@ } | ||
} else if (Array.isArray(id)) { | ||
return this._batch(id); | ||
return this._batch(id).then(this._mapFields.bind(this)); | ||
} | ||
return this.fetch(fetchObject.url, fetchObject, headers); | ||
return this.fetch(fetchObject.url, fetchObject, headers).then(this._mapFields.bind(this)); | ||
} | ||
@@ -837,3 +853,3 @@ | ||
value: function _delete(id) { | ||
var _this10 = this; | ||
var _this11 = this; | ||
@@ -852,3 +868,3 @@ var isQueryDelete = id === undefined; | ||
return _this10._batch(ids); | ||
return _this11._batch(ids); | ||
}); | ||
@@ -855,0 +871,0 @@ } |
{ | ||
"name": "syncano-server", | ||
"version": "1.0.1-7", | ||
"version": "1.0.1-8", | ||
"description": "A library to intereact with the Syncano API on a server side.", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -88,3 +88,3 @@ import querystring from 'querystring' | ||
const self = this | ||
const {baseUrl, relationships, instance, mappedFields, _mapFields} = this | ||
const {baseUrl, relationships, instance} = this | ||
const fetch = this.fetch.bind(this) | ||
@@ -239,3 +239,3 @@ const pageSize = this.query.page_size || 0 | ||
result = _mapFields(result, mappedFields) | ||
result = self._mapFields(result) | ||
@@ -257,13 +257,23 @@ return true | ||
_mapFields(items, fields) { | ||
return fields.length === 0 | ||
? items | ||
: items.map(item => | ||
Object.keys(fields).reduce( | ||
(all, key) => set(all, fields[key] || key, get(item, key)), | ||
{} | ||
) | ||
) | ||
_mapFields(items) { | ||
const fields = this.mappedFields | ||
if (fields.length === 0) { | ||
return items | ||
} | ||
if (Array.isArray(items)) { | ||
return items.map(item => this._mapFieldsForSingleItem(item, fields)) | ||
} | ||
return this._mapFieldsForSingleItem(items, fields) | ||
} | ||
_mapFieldsForSingleItem(item, fields) { | ||
return Object.keys(fields).reduce( | ||
(all, key) => set(all, fields[key] || key, get(item, key)), | ||
{} | ||
) | ||
} | ||
_getRelatedObjects(reference, items) { | ||
@@ -555,12 +565,11 @@ if (!reference) { | ||
_chunk(items, size) { | ||
const chunks = [] | ||
while (items.length > 0) { | ||
chunks.push(items.splice(0, size)) | ||
} | ||
return chunks | ||
return items | ||
.map((e, i) => (i % size === 0 ? items.slice(i, i + size) : null)) | ||
.filter(Boolean) | ||
} | ||
_batch(body, headers) { | ||
const type = Array.isArray(body[0]) | ||
? 'PATCH' | ||
: isNaN(body[0]) === false ? 'DELETE' : 'POST' | ||
const requests = this._chunk(body, MAX_BATCH_SIZE).map(chunk => () => { | ||
@@ -581,4 +590,8 @@ const fetchObject = this._batchFetchObject(chunk) | ||
.then(data => { | ||
resolves = resolves.concat(data) | ||
const items = data.map( | ||
item => (item.content ? item.content : item) | ||
) | ||
resolves = resolves.concat(items) | ||
next() // eslint-disable-line promise/no-callback-in-promise | ||
@@ -590,3 +603,3 @@ }) | ||
} else { | ||
resolve(resolves) | ||
resolve(type === 'DELETE' ? body : resolves) | ||
} | ||
@@ -624,6 +637,8 @@ })() | ||
} else if (Array.isArray(body)) { | ||
return this._batch(body, headers) | ||
return this._batch(body, headers).then(this._mapFields.bind(this)) | ||
} | ||
return this.fetch(fetchObject.url, fetchObject, headers) | ||
return this.fetch(fetchObject.url, fetchObject, headers).then( | ||
this._mapFields.bind(this) | ||
) | ||
} | ||
@@ -662,3 +677,3 @@ | ||
return this._batch(ids) | ||
return this._batch(ids).then(this._mapFields.bind(this)) | ||
}) | ||
@@ -671,6 +686,8 @@ } | ||
} else if (Array.isArray(id)) { | ||
return this._batch(id) | ||
return this._batch(id).then(this._mapFields.bind(this)) | ||
} | ||
return this.fetch(fetchObject.url, fetchObject, headers) | ||
return this.fetch(fetchObject.url, fetchObject, headers).then( | ||
this._mapFields.bind(this) | ||
) | ||
} | ||
@@ -677,0 +694,0 @@ |
111701
2873