Skip to content

Commit

Permalink
Merge pull request #2 from fizzl/master
Browse files Browse the repository at this point in the history
Let user add custom headers to requests
  • Loading branch information
sguilly authored Jul 28, 2017
2 parents 4a27ed0 + 58afd9e commit 460a96e
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 219 deletions.
26 changes: 12 additions & 14 deletions dist/LoopbackClient.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
/**
* 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; }; }();

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) {
Expand All @@ -21,37 +15,43 @@ 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;
}
}, {
key: 'getToken',
value: function getToken() {

return this.token;
}
}, {
key: 'createToken',
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 {
Expand All @@ -61,8 +61,6 @@ var LoopbackClient = function () {
});
}
});

return promise;
}
}, {
key: 'getModel',
Expand Down
50 changes: 14 additions & 36 deletions dist/LoopbackModel.js
Original file line number Diff line number Diff line change
@@ -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; }; }();
Expand All @@ -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;
}

Expand All @@ -28,108 +21,93 @@ 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);
}
}
});
});
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);
}
}
});
});

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);
}
}
});
});

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);
}
}
});
});

return promise;
}
}, {
key: 'findById',
value: function findById(data) {

var url = this.baseUrl + '/' + this.model + '/' + data.id;
return this.get(url, { filter: data.filter });
}
Expand Down
7 changes: 1 addition & 6 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}
*/
module.exports = _LoopbackClient2.default;
75 changes: 30 additions & 45 deletions src/LoopbackClient.js
Original file line number Diff line number Diff line change
@@ -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;
Loading

0 comments on commit 460a96e

Please sign in to comment.