diff --git a/dist/LoopbackClient.js b/dist/LoopbackClient.js index 14cc972..29f877b 100644 --- a/dist/LoopbackClient.js +++ b/dist/LoopbackClient.js @@ -1,9 +1,3 @@ -/** - * Created by sguilly on 17/11/16. - */ -/** - * Created by sguilly on 03/11/16. - */ "use strict"; var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); @@ -11,7 +5,7 @@ var _createClass = function () { function defineProperties(target, props) { for function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } var rest = require('restler'); -var LoopbackModel = require('./LoopbackModel.js'); +var LoopbackModel = require(__dirname + '/LoopbackModel.js'); var LoopbackClient = function () { function LoopbackClient(baseUrl, user, password) { @@ -21,9 +15,15 @@ var LoopbackClient = function () { this.password = password; this.token = null; this.baseUrl = baseUrl; + this.headers = {}; } _createClass(LoopbackClient, [{ + key: 'setHeaders', + value: function setHeaders(headers) { + this.headers = headers; + } + }, { key: 'getBaseUrl', value: function getBaseUrl() { return this.baseUrl; @@ -31,7 +31,6 @@ var LoopbackClient = function () { }, { key: 'getToken', value: function getToken() { - return this.token; } }, { @@ -39,19 +38,20 @@ var LoopbackClient = function () { value: function createToken() { var _this = this; - var promise = new Promise(function (resolve, reject) { - + return new Promise(function (resolve, reject) { if (_this.token) { resolve(_this.token); } else { - var data = { email: _this.user, password: _this.password }; - rest.postJson(_this.baseUrl + '/users/login?include=user', data).on('complete', function (result) { + var options = { + headers: _this.headers + }; + rest.postJson(_this.baseUrl + '/users/login?include=user', data, options).on('complete', function (result) { if (result instanceof Error) { reject(result.message); } else { @@ -61,8 +61,6 @@ var LoopbackClient = function () { }); } }); - - return promise; } }, { key: 'getModel', diff --git a/dist/LoopbackModel.js b/dist/LoopbackModel.js index f7b25e4..8e793dc 100644 --- a/dist/LoopbackModel.js +++ b/dist/LoopbackModel.js @@ -1,9 +1,3 @@ -/** - * Created by sguilly on 17/11/16. - */ -/** - * Created by sguilly on 03/11/16. - */ "use strict"; var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); @@ -17,9 +11,8 @@ var LoopbackModel = function () { _classCallCheck(this, LoopbackModel); this.baseUrl = tokenClient.getBaseUrl(); - - this.authorization = { authorization: tokenClient.getToken() }; - + this.headers = tokenClient.headers; + this.headers.authorization = tokenClient.getToken(); this.model = model; } @@ -28,16 +21,15 @@ var LoopbackModel = function () { value: function get(url, query) { var _this = this; - var promise = new Promise(function (resolve, reject) { - + return new Promise(function (resolve, reject) { rest.get(url, { - headers: _this.authorization, + headers: _this.headers, query: query }).on('complete', function (result, response) { if (result instanceof Error) { reject(result.message); } else { - if (response.statusCode != 200) { + if (response.statusCode !== 200) { reject(result); } else { resolve(result); @@ -45,29 +37,24 @@ var LoopbackModel = function () { } }); }); - return promise; } }, { key: 'post', value: function post(url, data, query) { var _this2 = this; - var promise = new Promise(function (resolve, reject) { - + return new Promise(function (resolve, reject) { var options = { - headers: _this2.authorization + headers: _this2.headers }; - if (query) { options.query = query; } - rest.postJson(url, data, options).on('complete', function (result, response) { - if (result instanceof Error) { reject(result.message); } else { - if (response.statusCode != 200) { + if (response.statusCode !== 200) { reject(result); } else { resolve(result); @@ -75,23 +62,20 @@ var LoopbackModel = function () { } }); }); - - return promise; } }, { key: 'put', value: function put(url, data) { var _this3 = this; - var promise = new Promise(function (resolve, reject) { - + return new Promise(function (resolve, reject) { rest.putJson(url, data, { - headers: _this3.authorization + headers: _this3.headers }).on('complete', function (result, response) { if (result instanceof Error) { reject(result.message); } else { - if (response.statusCode != 200) { + if (response.statusCode !== 200) { reject(result); } else { resolve(result); @@ -99,23 +83,20 @@ var LoopbackModel = function () { } }); }); - - return promise; } }, { key: 'del', value: function del(url) { var _this4 = this; - var promise = new Promise(function (resolve, reject) { - + return new Promise(function (resolve, reject) { rest.del(url, { - headers: _this4.authorization + headers: _this4.headers }).on('complete', function (result, response) { if (result instanceof Error) { reject(result.message); } else { - if (response.statusCode != 200) { + if (response.statusCode !== 200) { reject(result); } else { resolve(result); @@ -123,13 +104,10 @@ var LoopbackModel = function () { } }); }); - - return promise; } }, { key: 'findById', value: function findById(data) { - var url = this.baseUrl + '/' + this.model + '/' + data.id; return this.get(url, { filter: data.filter }); } diff --git a/dist/index.js b/dist/index.js index d35d4e9..8fc979e 100644 --- a/dist/index.js +++ b/dist/index.js @@ -6,9 +6,4 @@ var _LoopbackClient2 = _interopRequireDefault(_LoopbackClient); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } -//export default LoopbackClient - -module.exports = _LoopbackClient2.default; /** - * @param {Type} - * @return {Type} - */ \ No newline at end of file +module.exports = _LoopbackClient2.default; \ No newline at end of file diff --git a/src/LoopbackClient.js b/src/LoopbackClient.js index 290edcc..01b2941 100644 --- a/src/LoopbackClient.js +++ b/src/LoopbackClient.js @@ -1,75 +1,60 @@ -/** - * Created by sguilly on 17/11/16. - */ -/** - * Created by sguilly on 03/11/16. - */ "use strict"; -var rest = require('restler'); -var LoopbackModel = require('./LoopbackModel.js'); +const rest = require('restler'); +const LoopbackModel = require(__dirname + '/LoopbackModel.js'); class LoopbackClient { - constructor(baseUrl,user,password) { - - this.user = user - this.password = password - this.token = null - this.baseUrl = baseUrl + constructor(baseUrl, user, password) { + this.user = user; + this.password = password; + this.token = null; + this.baseUrl = baseUrl; + this.headers = {}; } - getBaseUrl() - { - return this.baseUrl + setHeaders(headers) { + this.headers = headers; } - getToken() - { - - return this.token + getBaseUrl() { + return this.baseUrl; } - createToken() - { - var promise = new Promise((resolve, reject) => { + getToken() { + return this.token; + } - if(this.token) - { + createToken() { + return new Promise((resolve, reject) => { + if (this.token) { resolve(this.token); } else { - - - var data = { + const data = { email: this.user, password: this.password - } - - rest.postJson(this.baseUrl+'/users/login?include=user', data + }; - ).on('complete', (result) =>{ + const options = { + headers: this.headers + }; + rest.postJson(this.baseUrl + '/users/login?include=user', + data, options).on('complete', (result) => { if (result instanceof Error) { reject(result.message); - } else { - this.token = result.id + this.token = result.id; resolve(this.token); } }); - } - - }) - - return promise + }); } - getModel(name) - { - return new LoopbackModel(name,this) + getModel(name) { + return new LoopbackModel(name, this); } } - -module.exports = LoopbackClient +module.exports = LoopbackClient; diff --git a/src/LoopbackModel.js b/src/LoopbackModel.js index 1779694..b3f5a5a 100644 --- a/src/LoopbackModel.js +++ b/src/LoopbackModel.js @@ -1,199 +1,143 @@ -/** - * Created by sguilly on 17/11/16. - */ -/** - * Created by sguilly on 03/11/16. - */ "use strict"; -var rest = require('restler'); +const rest = require('restler'); class LoopbackModel { - constructor(model,tokenClient) { - - this.baseUrl = tokenClient.getBaseUrl() - - this.authorization = {authorization: tokenClient.getToken()} - + constructor(model, tokenClient) { + this.baseUrl = tokenClient.getBaseUrl(); + this.headers = tokenClient.headers; + this.headers.authorization = tokenClient.getToken(); this.model = model } get(url, query) { - - var promise = new Promise((resolve, reject) => { - - rest.get(url, - { - headers: this.authorization, + return new Promise((resolve, reject) => { + rest.get(url, { + headers: this.headers, query: query } - ).on('complete', function (result,response) { + ).on('complete', function (result, response) { if (result instanceof Error) { reject(result.message); } else { - if(response.statusCode != 200) - { + if (response.statusCode !== 200) { reject(result) } - else - { + else { resolve(result); } } }); - - }) - return promise + }); } - post(url, data,query) { - - - var promise = new Promise( (resolve, reject) => { - - var options = { - headers: this.authorization - } - - if(query) - { + post(url, data, query) { + return new Promise((resolve, reject) => { + const options = { + headers: this.headers + }; + if (query) { options.query = query } - rest.postJson(url, data, options - - ).on('complete', function (result,response) { - + ).on('complete', function (result, response) { if (result instanceof Error) { reject(result.message); - } else { - if(response.statusCode != 200) - { + if (response.statusCode !== 200) { reject(result) } - else - { + else { resolve(result); } - } }); - }); - - return promise - } put(url, data) { - - - var promise = new Promise( (resolve, reject) => { - - rest.putJson(url, data, - { - headers: this.authorization + return new Promise((resolve, reject) => { + rest.putJson(url, data, { + headers: this.headers } - ).on('complete', function (result,response) { + ).on('complete', function (result, response) { if (result instanceof Error) { reject(result.message); - } else { - if(response.statusCode != 200) - { + if(response.statusCode !== 200) { reject(result) } - else - { + else { resolve(result); } - } }); }); - - return promise - } del(url) { - - - var promise = new Promise( (resolve, reject) => { - - rest.del(url, - { - headers: this.authorization + return new Promise((resolve, reject) => { + rest.del(url, { + headers: this.headers } - ).on('complete', function (result,response) { + ).on('complete', function (result, response) { if (result instanceof Error) { reject(result.message); - } else { - if(response.statusCode != 200) - { + if (response.statusCode !== 200) { reject(result) } - else - { + else { resolve(result); } - } }); - }); - - return promise - } findById(data) { - - var url = `${this.baseUrl}/${this.model}/${data.id}` - return this.get(url, {filter: data.filter}) + const url = `${this.baseUrl}/${this.model}/${data.id}`; + return this.get(url, {filter: data.filter}); } create(data) { - var url = `${this.baseUrl}/${this.model}` - return this.post(url,data) + const url = `${this.baseUrl}/${this.model}`; + return this.post(url, data); } count(where) { - var url = `${this.baseUrl}/${this.model}/count` - return this.get(url,where) + const url = `${this.baseUrl}/${this.model}/count`; + return this.get(url, where); } updateAll(query, data) { - var url = `${this.baseUrl}/${this.model}/update` - return this.post(url,data,query) + const url = `${this.baseUrl}/${this.model}/update`; + return this.post(url, data, query); } - updateById(id,data) { - var url = `${this.baseUrl}/${this.model}/${id}` - return this.put(url,data) + updateById(id, data) { + const url = `${this.baseUrl}/${this.model}/${id}`; + return this.put(url, data); } find(filter) { - var url = `${this.baseUrl}/${this.model}` - return this.get(url,filter) + const url = `${this.baseUrl}/${this.model}`; + return this.get(url, filter); } findOne(query) { - var url = `${this.baseUrl}/${this.model}/findOne` - return this.get(url,query) + const url = `${this.baseUrl}/${this.model}/findOne`; + return this.get(url, query); } deleteById(id) { - var url = `${this.baseUrl}/${this.model}/${id}` - return this.del(url) + const url = `${this.baseUrl}/${this.model}/${id}`; + return this.del(url); } - } -module.exports = LoopbackModel +module.exports = LoopbackModel; diff --git a/src/index.js b/src/index.js index ae56186..cf7821f 100644 --- a/src/index.js +++ b/src/index.js @@ -1,10 +1,2 @@ -/** - * @param {Type} - * @return {Type} - */ - import LoopbackClient from './LoopbackClient' - -//export default LoopbackClient - -module.exports = LoopbackClient +module.exports = LoopbackClient; diff --git a/test/index.js b/test/index.js index 78cf4d8..f03953d 100644 --- a/test/index.js +++ b/test/index.js @@ -21,7 +21,12 @@ var idCustomer; test("token", (t) => { t.plan(1) - nock('http://localhost:42001', {"encodedQueryParams":true}) + nock('http://localhost:42001', { + "encodedQueryParams":true, + "reqheaders": { + 'x-testheader': 'yes' + } + }) .post('/api/users/login', {"email":user,"password":password}) .query({"include":"user"}) .reply(200, @@ -53,7 +58,7 @@ test("token", (t) => { 'close' ]); loopbackClient = new LoopbackClient('http://localhost:42001/api',user,password); - + loopbackClient.setHeaders({'x-testheader': 'yes'}) console.log('loopbackClient',loopbackClient) loopbackClient.createToken().then((token)=>{