-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Добавление поддержки Promises
- Loading branch information
Showing
17 changed files
with
107 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,8 +4,7 @@ | |
* Vitaliy V. Makeev ([email protected]) | ||
*/ | ||
|
||
var _ = require('lodash') | ||
, callbackAdapter = require('../../../tools/index').callbackAdapter; | ||
var _ = require('lodash'); | ||
|
||
var chain = function () { | ||
return _.chain(this.load.apply(this, arguments)); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,10 +4,10 @@ | |
* Vitaliy V. Makeev ([email protected]) | ||
*/ | ||
|
||
var _ = require('lodash') | ||
, callbackAdapter = require('../../../tools/index').callbackAdapter; | ||
var _ = require('lodash'), | ||
callbackAdapter = require('project/callbackAdapter'); | ||
|
||
//TODO Ограничение на кол-во сохраняемых объектов в коллекции (проверить) | ||
//TODO Ограничение на кол-во удаляемых объектов в коллекции (проверить) | ||
|
||
/** | ||
* Del. Удаляет сущность или список сущностей. | ||
|
@@ -19,16 +19,17 @@ var _ = require('lodash') | |
*/ | ||
var del = function () { | ||
//TODO Ensure | ||
var args = _.toArray(arguments) | ||
, callback = typeof args.slice(-1)[0] === 'function' ? args.slice(-1)[0] : null; | ||
var that = this, | ||
args = _.toArray(arguments), | ||
callback = typeof args.slice(-1)[0] === 'function' ? args.slice(-1)[0] : null; | ||
|
||
var restClient = this.getProvider('ms-xml'), | ||
obj = null; | ||
|
||
var putArgs = args.slice(0, args.length); | ||
|
||
putArgs.push(function (err, data) { | ||
obj = callbackAdapter(err, data.obj, callback); | ||
obj = callbackAdapter(err, data.obj, callback, that.options.flowControl); | ||
}); | ||
|
||
restClient.del.apply(restClient, putArgs); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,8 +4,8 @@ | |
* Vitaliy V. Makeev ([email protected]) | ||
*/ | ||
|
||
var _ = require('lodash') | ||
, callbackAdapter = require('../../../tools/index').callbackAdapter; | ||
var _ = require('lodash'), | ||
callbackAdapter = require('project/callbackAdapter'); | ||
|
||
/** | ||
* First. Возвращает первую сущность из списка сущностей согласно запросу. | ||
|
@@ -17,6 +17,7 @@ var _ = require('lodash') | |
*/ | ||
var first = function (type, query, callback) { | ||
//TODO Ensure | ||
var that = this; | ||
var _restClient = this.getProvider('ms-xml'), | ||
_obj = null, | ||
_queryParametersList; | ||
|
@@ -25,7 +26,6 @@ var first = function (type, query, callback) { | |
var _params = _queryParametersList[paramsIndex]; | ||
|
||
if (_params && ('count' in _params ? _params.count !== 0 : true)) { | ||
|
||
_restClient.get(type, _.extend({}, _params, { count: 1 }), function (err, data) { | ||
if (err) return callback(err); | ||
|
||
|
@@ -36,25 +36,25 @@ var first = function (type, query, callback) { | |
_firstFromParts(++paramsIndex, callback) | ||
} | ||
}); | ||
|
||
} else { | ||
return callback(null, null); | ||
} | ||
} | ||
|
||
// query | ||
if (typeof query == 'object' && 'getQueryParameters' in query) { | ||
if (typeof query === 'object' && 'getQueryParameters' in query) { | ||
_queryParametersList = query.getQueryParameters(this.options.filterLimit); | ||
} | ||
|
||
//TODO Ничего не мешеает использовать first без query | ||
// .. ошибка | ||
else { | ||
return callbackAdapter(new TypeError('Incorrect query parameter'), null, callback); | ||
return callbackAdapter(new TypeError('Incorrect query parameter'), | ||
null, callback, that.options.flowControl); | ||
} | ||
|
||
_firstFromParts(0, function (err, data) { | ||
_obj = callbackAdapter(err, data, callback); | ||
_obj = callbackAdapter(err, data, callback, that.options.flowControl); | ||
}); | ||
|
||
return _obj; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,21 +4,22 @@ | |
* Vitaliy V. Makeev ([email protected]) | ||
*/ | ||
|
||
var _ = require('lodash') | ||
, callbackAdapter = require('../../../tools/index').callbackAdapter; | ||
var _ = require('lodash'), | ||
callbackAdapter = require('project/callbackAdapter'); | ||
|
||
//TODO Вероятно нужно перенести этот модуль в rest-clients/json (для целостности пониманя работы модуля) | ||
|
||
var callService = function (serviceName) { | ||
var args = _.toArray(arguments) | ||
, callback = typeof args.slice(-1)[0] === 'function' ? args.slice(-1)[0] : null | ||
, _restClient = this.getProvider('json-services') | ||
, _obj = null; | ||
var that = this, | ||
args = _.toArray(arguments), | ||
callback = typeof args.slice(-1)[0] === 'function' ? args.slice(-1)[0] : null, | ||
_restClient = this.getProvider('json-services'), | ||
_obj = null; | ||
|
||
var serviceArgs = args.slice(1, args.length - (callback ? 1 : 0)); | ||
|
||
serviceArgs.push(function (err, data) { | ||
_obj = callbackAdapter(err, data.obj, callback); | ||
_obj = callbackAdapter(err, data.obj, callback, that.options.flowControl); | ||
}); | ||
|
||
_restClient[serviceName].apply(_restClient, serviceArgs); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,8 +4,8 @@ | |
* Vitaliy V. Makeev ([email protected]) | ||
*/ | ||
|
||
var callbackAdapter = require('../../../tools/index').callbackAdapter | ||
, _ = require('lodash'); | ||
var _ = require('lodash'), | ||
callbackAdapter = require('project/callbackAdapter'); | ||
|
||
//noinspection JSValidateJSDoc,JSCommentMatchesSignature | ||
/** | ||
|
@@ -19,12 +19,13 @@ var callbackAdapter = require('../../../tools/index').callbackAdapter | |
*/ | ||
var load = function (type, query) { | ||
//TODO Ensure | ||
var args = _.toArray(arguments) | ||
, callback = typeof args.slice(-1)[0] === 'function' ? args.slice(-1)[0] : null | ||
, options = typeof args[2] === 'object' ? args[2] : {} | ||
, _queryParametersList | ||
, _restClient = this.getProvider('ms-xml') | ||
, _obj = null; | ||
var that = this, | ||
args = _.toArray(arguments), | ||
callback = typeof args.slice(-1)[0] === 'function' ? args.slice(-1)[0] : null, | ||
options = typeof args[2] === 'object' ? args[2] : {}, | ||
_queryParametersList, | ||
_restClient = this.getProvider('ms-xml'), | ||
_obj = null; | ||
|
||
function loadPartial(paramsIndex, paging, cumulativeTotal, resultCollection, callback) { | ||
|
||
|
@@ -69,27 +70,28 @@ var load = function (type, query) { | |
if (options.fileContent) params.fileContent = true; | ||
|
||
_restClient.get(type, params, function (err, data) { | ||
_obj = callbackAdapter(err, data.obj, callback); | ||
_obj = callbackAdapter(err, data.obj, callback, that.options.flowControl); | ||
}); | ||
} | ||
|
||
// .. или query | ||
else if (typeof query == 'object' && 'getQueryParameters' in query) { | ||
//TODO Не забыть про options при написании документации | ||
_queryParametersList = query.getQueryParameters(this.options.filterLimit); | ||
_queryParametersList = query.getQueryParameters(that.options.filterLimit); | ||
|
||
var paging = {}; | ||
if (_queryParametersList[0].start) paging.start = _queryParametersList[0].start; | ||
if (_queryParametersList[0].count) paging.count = _queryParametersList[0].count; | ||
|
||
loadPartial(0, paging, 0, [], function (err, data) { | ||
_obj = callbackAdapter(err, data, callback); | ||
_obj = callbackAdapter(err, data, callback, that.options.flowControl); | ||
}); | ||
} | ||
|
||
// .. ошибка | ||
else { | ||
return callbackAdapter(new TypeError('Incorrect uuid or query parameter'), null, callback); | ||
return callbackAdapter(new TypeError('Incorrect uuid or query parameter'), | ||
null, callback, that.options.flowControl); | ||
} | ||
|
||
return _obj; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,8 +4,8 @@ | |
* Vitaliy V. Makeev ([email protected]) | ||
*/ | ||
|
||
var _ = require('lodash') | ||
, callbackAdapter = require('../../../tools/index').callbackAdapter; | ||
var _ = require('lodash'), | ||
callbackAdapter = require('project/callbackAdapter'); | ||
|
||
//TODO Ограничение на кол-во сохраняемых объектов в коллекции (проверить) | ||
|
||
|
@@ -19,16 +19,17 @@ var _ = require('lodash') | |
*/ | ||
var save = function () { | ||
//TODO Ensure | ||
var args = _.toArray(arguments) | ||
, callback = typeof args.slice(-1)[0] === 'function' ? args.slice(-1)[0] : null; | ||
var that = this, | ||
args = _.toArray(arguments), | ||
callback = typeof args.slice(-1)[0] === 'function' ? args.slice(-1)[0] : null; | ||
|
||
var restClient = this.getProvider('ms-xml'), | ||
var restClient = that.getProvider('ms-xml'), | ||
obj = null; | ||
|
||
var putArgs = args.slice(0, args.length); | ||
|
||
putArgs.push(function (err, data) { | ||
obj = callbackAdapter(err, data.obj, callback); | ||
obj = callbackAdapter(err, data.obj, callback, that.options.flowControl); | ||
}); | ||
|
||
restClient.put.apply(restClient, putArgs); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,8 +4,8 @@ | |
* Vitaliy V. Makeev ([email protected]) | ||
*/ | ||
|
||
var _ = require('lodash') | ||
, callbackAdapter = require('../../../tools/index').callbackAdapter; | ||
var _ = require('lodash'), | ||
callbackAdapter = require('project/callbackAdapter'); | ||
|
||
/** | ||
* | ||
|
@@ -16,7 +16,8 @@ var _ = require('lodash') | |
*/ | ||
var total = function (type, query, callback) { | ||
//TODO Ensure | ||
var _restClient = this.getProvider('ms-xml'), | ||
var that = this, | ||
_restClient = this.getProvider('ms-xml'), | ||
_total = null, | ||
_queryParametersList; | ||
|
||
|
@@ -43,13 +44,14 @@ var total = function (type, query, callback) { | |
_queryParametersList = query.getQueryParameters(); | ||
|
||
_totalFromParts(0, 0, function (err, data) { | ||
_total = callbackAdapter(err, data, callback); | ||
_total = callbackAdapter(err, data, callback, that.options.flowControl); | ||
}); | ||
} | ||
|
||
// .. error | ||
else { | ||
return callbackAdapter(new TypeError('Incorrect query parameter'), null, callback); | ||
return callbackAdapter(new TypeError('Incorrect query parameter'), | ||
null, callback, that.options.flowControl); | ||
} | ||
|
||
return _total; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,25 @@ | ||
/** | ||
* providerResponseHandler | ||
* Date: 23.03.14 | ||
* Vitaliy V. Makeev ([email protected]) | ||
*/ | ||
|
||
var _ = require('lodash') | ||
, callbackAdapter = require('../../../tools').callbackAdapter; | ||
var _ = require('lodash'); | ||
|
||
//TODO Часть кода providerResponseHandler'ов не оправданно дублируется .. > | ||
var providerResponseHandler = function (err, result, callback) { | ||
var _log = require('project/logger'); | ||
|
||
// .. этот кусок общий для всех | ||
if (!err) { | ||
|
||
switch (result.response.responseCode) { | ||
|
||
//TODO Прописать все ошибки stock сервисов | ||
//TODO Есть ли общие для всех ошибки (нужно ли выделять)? | ||
|
||
// ошибка пришла ввиде XML сериализуем и обработаем ниже | ||
case 500: | ||
return callbackAdapter( | ||
new Error('Server error 500'), result, callback); | ||
return callback( | ||
new Error('Server error 500'), result); | ||
|
||
// ошибка авторизации | ||
case 401: | ||
return callbackAdapter( | ||
new Error('Request requires HTTP authentication'), result, callback); | ||
return callback( | ||
new Error('Request requires HTTP authentication'), result); | ||
|
||
// корректный ответ сервера (работаем с ним дальше) | ||
case 200: | ||
|
@@ -36,17 +28,17 @@ var providerResponseHandler = function (err, result, callback) { | |
// любой другой код ответа - ошибка | ||
default: | ||
//TODO ??? Надо парсить Html ответа и выделять описание ошибки | ||
_log.log('Ответ сервера: \n' + result.response.contentText); | ||
return callbackAdapter( | ||
new Error('Server response error ' + result.response.responseCode), result, callback); | ||
_log.log('Server response: \n' + result.response.contentText); | ||
return callback( | ||
new Error('Server response error ' + result.response.responseCode), result); | ||
} | ||
|
||
if (result.response.contentText.length > 0) { | ||
result.obj = JSON.parse(result.response.contentText); | ||
} | ||
} | ||
|
||
return callbackAdapter(err, result, callback); | ||
return callback(err, result); | ||
}; | ||
|
||
module.exports = providerResponseHandler; |
Oops, something went wrong.