From c5c4c4ecf3892e51e11470828a4e2e5ef7191888 Mon Sep 17 00:00:00 2001 From: Lassi Marttala Date: Fri, 21 Jul 2017 09:30:34 +0300 Subject: [PATCH 1/2] Beautify - Consistent whitespace - Consistent bracket places - ES6 fix: Use constant where possible, let instead of var - Fix type coercion warnings --- dist/LoopbackClient.js | 16 +---- dist/LoopbackModel.js | 35 ++-------- dist/index.js | 7 +- src/LoopbackClient.js | 70 +++++++------------ src/LoopbackModel.js | 149 +++++++++++++---------------------------- src/index.js | 10 +-- 6 files changed, 79 insertions(+), 208 deletions(-) diff --git a/dist/LoopbackClient.js b/dist/LoopbackClient.js index 14cc972..87bc2c6 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) { @@ -31,7 +25,6 @@ var LoopbackClient = function () { }, { key: 'getToken', value: function getToken() { - return this.token; } }, { @@ -39,19 +32,16 @@ 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) { - if (result instanceof Error) { reject(result.message); } else { @@ -61,8 +51,6 @@ var LoopbackClient = function () { }); } }); - - return promise; } }, { key: 'getModel', diff --git a/dist/LoopbackModel.js b/dist/LoopbackModel.js index f7b25e4..0fbf16c 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,7 @@ var LoopbackModel = function () { _classCallCheck(this, LoopbackModel); this.baseUrl = tokenClient.getBaseUrl(); - this.authorization = { authorization: tokenClient.getToken() }; - this.model = model; } @@ -28,8 +20,7 @@ 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, query: query @@ -45,25 +36,20 @@ 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 }; - if (query) { options.query = query; } - rest.postJson(url, data, options).on('complete', function (result, response) { - if (result instanceof Error) { reject(result.message); } else { @@ -75,23 +61,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 }).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 +82,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 }).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 +103,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..a47bba5 100644 --- a/src/LoopbackClient.js +++ b/src/LoopbackClient.js @@ -1,75 +1,51 @@ -/** - * 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; } - getBaseUrl() - { - return this.baseUrl + getBaseUrl() { + return this.baseUrl; } - getToken() - { - - return this.token + getToken() { + return this.token; } - createToken() - { - var promise = new Promise((resolve, reject) => { - - 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) =>{ + }; + rest.postJson(this.baseUrl + '/users/login?include=user', + data).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..cb8d1af 100644 --- a/src/LoopbackModel.js +++ b/src/LoopbackModel.js @@ -1,199 +1,142 @@ -/** - * 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.authorization = {authorization: tokenClient.getToken()}; this.model = model } get(url, query) { - - var promise = new Promise((resolve, reject) => { - - rest.get(url, - { + return new Promise((resolve, reject) => { + rest.get(url, { headers: this.authorization, 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 = { + post(url, data, query) { + return new Promise((resolve, reject) => { + const options = { headers: this.authorization - } - - if(query) - { + }; + 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, - { + return new Promise((resolve, reject) => { + rest.putJson(url, data, { headers: this.authorization } - ).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, - { + return new Promise((resolve, reject) => { + rest.del(url, { headers: this.authorization } - ).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; From 58afd9ef0faba076f94d4a0ccd9239956af13b80 Mon Sep 17 00:00:00 2001 From: Lassi Marttala Date: Fri, 21 Jul 2017 09:53:59 +0300 Subject: [PATCH 2/2] User may set custom headers - Add function setHeaders to client - Get custom headers from client in model - Add auth header to headers in model - Add test for headers in login test --- dist/LoopbackClient.js | 12 +++++++++++- dist/LoopbackModel.js | 15 ++++++++------- src/LoopbackClient.js | 11 ++++++++++- src/LoopbackModel.js | 11 ++++++----- test/index.js | 9 +++++++-- 5 files changed, 42 insertions(+), 16 deletions(-) diff --git a/dist/LoopbackClient.js b/dist/LoopbackClient.js index 87bc2c6..29f877b 100644 --- a/dist/LoopbackClient.js +++ b/dist/LoopbackClient.js @@ -15,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; @@ -41,7 +47,11 @@ var LoopbackClient = function () { 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 { diff --git a/dist/LoopbackModel.js b/dist/LoopbackModel.js index 0fbf16c..8e793dc 100644 --- a/dist/LoopbackModel.js +++ b/dist/LoopbackModel.js @@ -11,7 +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; } @@ -22,13 +23,13 @@ var LoopbackModel = function () { 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); @@ -44,7 +45,7 @@ var LoopbackModel = function () { return new Promise(function (resolve, reject) { var options = { - headers: _this2.authorization + headers: _this2.headers }; if (query) { options.query = query; @@ -53,7 +54,7 @@ var LoopbackModel = function () { if (result instanceof Error) { reject(result.message); } else { - if (response.statusCode != 200) { + if (response.statusCode !== 200) { reject(result); } else { resolve(result); @@ -69,7 +70,7 @@ var LoopbackModel = function () { 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); @@ -90,7 +91,7 @@ var LoopbackModel = function () { 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); diff --git a/src/LoopbackClient.js b/src/LoopbackClient.js index a47bba5..01b2941 100644 --- a/src/LoopbackClient.js +++ b/src/LoopbackClient.js @@ -9,6 +9,11 @@ class LoopbackClient { this.password = password; this.token = null; this.baseUrl = baseUrl; + this.headers = {}; + } + + setHeaders(headers) { + this.headers = headers; } getBaseUrl() { @@ -30,8 +35,12 @@ class LoopbackClient { password: this.password }; + const options = { + headers: this.headers + }; + rest.postJson(this.baseUrl + '/users/login?include=user', - data).on('complete', (result) => { + data, options).on('complete', (result) => { if (result instanceof Error) { reject(result.message); } else { diff --git a/src/LoopbackModel.js b/src/LoopbackModel.js index cb8d1af..b3f5a5a 100644 --- a/src/LoopbackModel.js +++ b/src/LoopbackModel.js @@ -5,14 +5,15 @@ const rest = require('restler'); class LoopbackModel { constructor(model, tokenClient) { this.baseUrl = tokenClient.getBaseUrl(); - this.authorization = {authorization: tokenClient.getToken()}; + this.headers = tokenClient.headers; + this.headers.authorization = tokenClient.getToken(); this.model = model } get(url, query) { return new Promise((resolve, reject) => { rest.get(url, { - headers: this.authorization, + headers: this.headers, query: query } ).on('complete', function (result, response) { @@ -35,7 +36,7 @@ class LoopbackModel { post(url, data, query) { return new Promise((resolve, reject) => { const options = { - headers: this.authorization + headers: this.headers }; if (query) { options.query = query @@ -59,7 +60,7 @@ class LoopbackModel { put(url, data) { return new Promise((resolve, reject) => { rest.putJson(url, data, { - headers: this.authorization + headers: this.headers } ).on('complete', function (result, response) { if (result instanceof Error) { @@ -80,7 +81,7 @@ class LoopbackModel { del(url) { return new Promise((resolve, reject) => { rest.del(url, { - headers: this.authorization + headers: this.headers } ).on('complete', function (result, response) { if (result instanceof Error) { 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)=>{