Skip to content

Commit

Permalink
Изменение внутренней логики создания экземпляра клиента
Browse files Browse the repository at this point in the history
Добавление возможности применять внешние расширения #9, #27
  • Loading branch information
wmakeev committed Apr 24, 2016
1 parent 0c75b97 commit c18489a
Showing 1 changed file with 58 additions and 46 deletions.
104 changes: 58 additions & 46 deletions src/moysklad-client/index.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,60 @@
/**
* MoyskladClient
* Date: 11.01.14
* Vitaliy V. Makeev ([email protected])
*/

var clientStamp = require('./client'),
query = require('./rest-clients/ms-xml/query'),
tools = require('project/tools'),
logger = require('project/logger'),
Auth = require('project/auth'),
pkg = require('../../package');

var xmlClientStamp = require('../rest-clients/ms-xml'),
jsonServicesClientStamp = require('../rest-clients/json');

logger.info('moysklad-client v' + pkg.version);
var _ = require('lodash')
var have = require('project/have')
var tools = require('project/tools')
var logger = require('project/logger')
var AuthStore = require('project/auth-store')
var pkg = require('../../package')

var resolverBehavior = require('project/behaviors/resolver')

var Query = require('./rest-clients/ms-xml/query')
var ClientBase = require('./client')
var MsXmlClient = require('../rest-clients/ms-xml')
var JsonClient = require('../rest-clients/json') // TODO Переименовать в json-services

var Client = ClientBase
.init(resolverBehavior)
.init(function (options, context) {
var authStore = new AuthStore()

this.addDependency({
'logger': logger,
'ms-xml': MsXmlClient().init(resolverBehavior).create(),
'json-service': JsonClient().init(resolverBehavior).create(),
'auth': authStore
})

if (options.login) {
authStore.setAuth(options.login, options.password)
}
})

logger.info('moysklad-client v' + pkg.version)

module.exports = {

createClient: function (login, password) {
var auth = new Auth(login, password);

var client = clientStamp();
client.setAuthStore(auth);

var xmlClient = xmlClientStamp();
xmlClient.setAuthStore(auth);
xmlClient.setProvider('logger', logger);
xmlClient.options = client.options;

var jsonServicesClient = jsonServicesClientStamp();
jsonServicesClient.setAuthStore(auth);
jsonServicesClient.setProvider('logger', logger);
jsonServicesClient.options = client.options;

client.setProvider('ms-xml', xmlClient);
client.setProvider('json-services', jsonServicesClient);
client.setProvider('logger', logger);

return client;
},

createQuery: query.createQuery,
tools: tools,
logger: logger,
version: pkg.version
};
createClient: function () {
var args = have(arguments, [
{},
{ login: 'string', password: 'string' },
{ options: 'object' }
])
var options = {}

if (args.options) {
options = _.assign(options, args.options)
}

return Client.init(function () {
if (args.login) {
this.resolve('auth').setAuth(args.login, args.password)
}
}).create(options)
},

Client: Client,

createQuery: Query.createQuery,
tools: tools,
logger: logger,
version: pkg.version
}

0 comments on commit c18489a

Please sign in to comment.