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)=>{