Skip to content

Commit

Permalink
User may set custom headers
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
fizzl committed Jul 21, 2017
1 parent c5c4c4e commit 58afd9e
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 16 deletions.
12 changes: 11 additions & 1 deletion dist/LoopbackClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down
15 changes: 8 additions & 7 deletions dist/LoopbackModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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);
Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down
11 changes: 10 additions & 1 deletion src/LoopbackClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ class LoopbackClient {
this.password = password;
this.token = null;
this.baseUrl = baseUrl;
this.headers = {};
}

setHeaders(headers) {
this.headers = headers;
}

getBaseUrl() {
Expand All @@ -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 {
Expand Down
11 changes: 6 additions & 5 deletions src/LoopbackModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down
9 changes: 7 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)=>{
Expand Down

0 comments on commit 58afd9e

Please sign in to comment.