syncano-server
Advanced tools
Comparing version 0.8.1-8 to 0.8.1-9
@@ -21,6 +21,14 @@ 'use strict'; | ||
var _lodash = require('lodash.merge'); | ||
var _lodash = require('lodash.set'); | ||
var _lodash2 = _interopRequireDefault(_lodash); | ||
var _lodash3 = require('lodash.get'); | ||
var _lodash4 = _interopRequireDefault(_lodash3); | ||
var _lodash5 = require('lodash.merge'); | ||
var _lodash6 = _interopRequireDefault(_lodash5); | ||
var _queryBuilder = require('./query-builder'); | ||
@@ -141,3 +149,5 @@ | ||
relationships = this.relationships, | ||
instance = this.instance; | ||
instance = this.instance, | ||
mappedFields = this.mappedFields, | ||
_mapFields = this._mapFields; | ||
@@ -151,3 +161,3 @@ var fetch = this.fetch.bind(this); | ||
function request(url) { | ||
fetch(url).then(saveToResult).then(loadNextPage).then(resolveRelatedModels).then(replaceCustomTypesWithValue).then(resolveIfFinished).catch(function (err) { | ||
fetch(url).then(saveToResult).then(loadNextPage).then(resolveRelatedModels).then(replaceCustomTypesWithValue).then(mapResultFields).then(resolveIfFinished).catch(function (err) { | ||
return reject(err); | ||
@@ -276,2 +286,12 @@ }); | ||
function mapResultFields(shouldResolve) { | ||
if (shouldResolve === false) { | ||
return; | ||
} | ||
result = _mapFields(result, mappedFields); | ||
return true; | ||
} | ||
function resolveIfFinished(shouldResolve) { | ||
@@ -289,2 +309,11 @@ if (shouldResolve) { | ||
}, { | ||
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)); | ||
}, {}); | ||
}); | ||
} | ||
}, { | ||
key: '_getRelatedObjects', | ||
@@ -363,3 +392,3 @@ value: function _getRelatedObjects(reference, items) { | ||
return this.where(query).firstOrFail().catch(function () { | ||
return _this4.create((0, _lodash2.default)(attributes, values)); | ||
return _this4.create((0, _lodash6.default)(attributes, values)); | ||
}); | ||
@@ -388,3 +417,3 @@ } | ||
}).catch(function () { | ||
return _this5.create((0, _lodash2.default)(attributes, values)); | ||
return _this5.create((0, _lodash6.default)(attributes, values)); | ||
}); | ||
@@ -535,3 +564,3 @@ } | ||
var query = (0, _lodash2.default)({}, currentQuery, nextQuery); | ||
var query = (0, _lodash6.default)({}, currentQuery, nextQuery); | ||
@@ -557,2 +586,36 @@ return this.withQuery({ query: JSON.stringify(query) }); | ||
/** | ||
* Whitelist returned keys. | ||
* | ||
* @returns {Promise} | ||
* | ||
* @example {@lang javascript} | ||
* const posts = await data.users.fields('name', 'email as username')->list() | ||
*/ | ||
}, { | ||
key: 'fields', | ||
value: function fields() { | ||
for (var _len = arguments.length, _fields = Array(_len), _key = 0; _key < _len; _key++) { | ||
_fields[_key] = arguments[_key]; | ||
} | ||
if (Array.isArray(_fields[0])) { | ||
_fields = _fields[0]; | ||
} | ||
var fieldsToMap = _fields.map(function (field) { | ||
var _field$match = field.match(/([\w_\-.]*)(\sas\s)?(.*)?/), | ||
_field$match2 = _slicedToArray(_field$match, 4), | ||
from = _field$match2[1], | ||
to = _field$match2[3]; | ||
return _defineProperty({}, from, to); | ||
}); | ||
this.withMappedFields(fieldsToMap); | ||
return this; | ||
} | ||
/** | ||
* Expand references and relationships. | ||
@@ -571,4 +634,4 @@ * | ||
value: function _with() { | ||
for (var _len = arguments.length, models = Array(_len), _key = 0; _key < _len; _key++) { | ||
models[_key] = arguments[_key]; | ||
for (var _len2 = arguments.length, models = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) { | ||
models[_key2] = arguments[_key2]; | ||
} | ||
@@ -575,0 +638,0 @@ |
@@ -21,2 +21,4 @@ 'use strict'; | ||
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } } | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
@@ -69,2 +71,9 @@ | ||
}, { | ||
key: 'withMappedFields', | ||
value: function withMappedFields(fields) { | ||
this._mappedFields = Object.assign.apply(Object, [{}, this.mappedFields].concat(_toConsumableArray(fields))); | ||
return this; | ||
} | ||
}, { | ||
key: 'query', | ||
@@ -79,2 +88,7 @@ get: function get() { | ||
} | ||
}, { | ||
key: 'mappedFields', | ||
get: function get() { | ||
return this._mappedFields || []; | ||
} | ||
}]); | ||
@@ -81,0 +95,0 @@ |
{ | ||
"name": "syncano-server", | ||
"version": "0.8.1-8", | ||
"version": "0.8.1-9", | ||
"description": "A library to intereact with the Syncano API on a server side", | ||
@@ -45,2 +45,4 @@ "main": "lib/index.js", | ||
"form-data": "^2.1.4", | ||
"lodash.get": "^4.4.2", | ||
"lodash.set": "^4.3.2", | ||
"lodash.merge": "^4.6.0", | ||
@@ -47,0 +49,0 @@ "node-fetch": "2.0.0-alpha.3" |
import querystring from 'querystring' | ||
import FormData from 'form-data' | ||
import set from 'lodash.set' | ||
import get from 'lodash.get' | ||
import merge from 'lodash.merge' | ||
@@ -77,3 +79,3 @@ import QueryBuilder from './query-builder' | ||
const self = this | ||
const {baseUrl, relationships, instance} = this | ||
const {baseUrl, relationships, instance, mappedFields, _mapFields} = this | ||
const fetch = this.fetch.bind(this) | ||
@@ -91,2 +93,3 @@ const pageSize = this.query.page_size || 0 | ||
.then(replaceCustomTypesWithValue) | ||
.then(mapResultFields) | ||
.then(resolveIfFinished) | ||
@@ -211,2 +214,12 @@ .catch(err => reject(err)) | ||
function mapResultFields(shouldResolve) { | ||
if (shouldResolve === false) { | ||
return | ||
} | ||
result = _mapFields(result, mappedFields) | ||
return true | ||
} | ||
function resolveIfFinished(shouldResolve) { | ||
@@ -224,2 +237,10 @@ if (shouldResolve) { | ||
_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)) | ||
, {}) | ||
) | ||
} | ||
_getRelatedObjects(reference, items) { | ||
@@ -438,2 +459,27 @@ if (!reference) { | ||
/** | ||
* Whitelist returned keys. | ||
* | ||
* @returns {Promise} | ||
* | ||
* @example {@lang javascript} | ||
* const posts = await data.users.fields('name', 'email as username')->list() | ||
*/ | ||
fields(...fields) { | ||
if (Array.isArray(fields[0])) { | ||
fields = fields[0] | ||
} | ||
const fieldsToMap = fields | ||
.map(field => { | ||
const [, from,, to] = field.match(/([\w_\-.]*)(\sas\s)?(.*)?/) | ||
return {[from]: to} | ||
}) | ||
this.withMappedFields(fieldsToMap) | ||
return this | ||
} | ||
/** | ||
* Expand references and relationships. | ||
@@ -440,0 +486,0 @@ * |
@@ -44,2 +44,6 @@ import nodeFetch from 'node-fetch' | ||
get mappedFields() { | ||
return this._mappedFields || [] | ||
} | ||
withQuery(query) { | ||
@@ -56,2 +60,8 @@ this._query = Object.assign({}, this.query, query) | ||
} | ||
withMappedFields(fields) { | ||
this._mappedFields = Object.assign({}, this.mappedFields, ...fields) | ||
return this | ||
} | ||
} |
@@ -356,2 +356,11 @@ import nock from 'nock' | ||
describe('#fields()', () => { | ||
it('should be a method of the model', () => { | ||
should(data.users).have.property('fields').which.is.Function() | ||
}) | ||
it.skip('should be able to whitelist fields') | ||
it.skip('should be able to map field names') | ||
}) | ||
describe('#pluck()', () => { | ||
@@ -358,0 +367,0 @@ it('should be a method of the model', () => { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
899934
3751
5
+ Addedlodash.get@^4.4.2
+ Addedlodash.set@^4.3.2
+ Addedlodash.get@4.4.2(transitive)
+ Addedlodash.set@4.3.2(transitive)