Skip to content

Commit 794c8b9

Browse files
committed
refactoring and standard eslint
1 parent 70e46b8 commit 794c8b9

6 files changed

Lines changed: 223 additions & 155 deletions

File tree

.eslintrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": ["standard"]
3+
}

.npmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
save-exact=true

index.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
/*!
22
* Copyright(c) 2014 Jan Blaha
33
*/
4-
5-
module.exports = require("./lib/client.js");
4+
module.exports = require('./lib/client.js')

lib/client.js

Lines changed: 59 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -5,80 +5,79 @@
55
* able to render reports or do crud using jaydata context.
66
*/
77

8-
var request = require("request"),
9-
url = require("url"),
10-
concat = require('concat-stream');
8+
var request = require('request')
9+
var url = require('url')
10+
var concat = require('concat-stream')
11+
var assign = require('object-assign')
1112

1213
module.exports = function (url, username, password) {
13-
return new Client(url, username, password);
14-
};
14+
return new Client(url, username, password)
15+
}
1516

1617
var Client = function (url, username, password) {
17-
this.url = url;
18-
this.username = username;
19-
this.password = password;
18+
this.url = url
19+
this.username = username
20+
this.password = password
2021
}
2122

2223
/**
2324
* Render report in temore server and return response
2425
* @returns object containing header property with response headers and body property with response body
2526
*/
2627
Client.prototype.render = function (req, options, cb) {
27-
28-
if (typeof cb == 'undefined') {
29-
cb = options;
30-
options = {}
28+
if (typeof cb === 'undefined') {
29+
cb = options
30+
options = {}
31+
}
32+
33+
options = assign({
34+
uri: url.resolve(this.url, 'api/report'),
35+
body: JSON.stringify(req),
36+
strictSSL: false,
37+
headers: {
38+
'Content-Type': 'application/json'
3139
}
40+
}, options)
3241

33-
34-
options.uri = options.uri || url.resolve(this.url, 'api/report');
35-
options.body = options.body || JSON.stringify(req);
36-
options.strictSSL = options.strictSSL || false;
37-
options.encoding = options.encoding || null;
38-
options.headers = options.headers || {
39-
'Content-Type': 'application/json'
40-
};
41-
42-
43-
if (this.username) {
44-
options.auth = {
45-
username: this.username,
46-
password: this.password
47-
}
42+
if (this.username) {
43+
options.auth = {
44+
username: this.username,
45+
password: this.password
4846
}
49-
50-
var responseStream = request.post(options);
51-
52-
responseStream.on("error", function(err) {
53-
cb(err);
54-
});
55-
56-
responseStream.on("response", function (response) {
57-
response.on("error", function (err) {
58-
cb(err)
59-
})
60-
61-
if (response.statusCode !== 200) {
62-
return responseToBuffer(response, function(data) {
63-
try {
64-
var errorMessage = JSON.parse(data.toString());
65-
var error = new Error(errorMessage.message);
66-
error.remoteStack = errorMessage.stack;
67-
cb(error)
68-
}
69-
catch(e) {
70-
cb(new Error(data));
71-
}
72-
});
47+
}
48+
49+
var responseStream = request.post(options)
50+
51+
responseStream.on('error', function (err) {
52+
cb(err)
53+
})
54+
55+
responseStream.on('response', function (response) {
56+
response.on('error', function (err) {
57+
cb(err)
58+
})
59+
60+
if (response.statusCode !== 200) {
61+
return responseToBuffer(response, function (data) {
62+
try {
63+
var errorMessage = JSON.parse(data.toString())
64+
var error = new Error(errorMessage.message)
65+
error.remoteStack = errorMessage.stack
66+
cb(error)
67+
} catch (e) {
68+
var err = new Error('Unknown error, status code ' + response.statusCode)
69+
err.response = response
70+
cb(err)
7371
}
72+
})
73+
}
7474

75-
response.body = function(cb) { responseToBuffer(response, cb); }
76-
cb(null, response);
77-
});
78-
};
79-
75+
response.body = function (cb) { responseToBuffer(response, cb) }
76+
cb(null, response)
77+
})
78+
}
8079

81-
function responseToBuffer(response, cb) {
82-
var writeStream = concat(cb);
83-
response.pipe(writeStream);
84-
}
80+
function responseToBuffer (response, cb) {
81+
var writeStream = concat(cb)
82+
response.pipe(writeStream)
83+
}

package.json

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,22 @@
1313
"description": "Remote node.js client to ",
1414
"main": "index.js",
1515
"scripts": {
16-
"test": "mocha --reporter spec test"
16+
"test": "mocha --reporter spec test && standard"
1717
},
1818
"licence": "MIT",
1919
"devDependencies": {
20-
"jsreport": "jsreport/jsreport",
21-
"mocha": "^1.21.4",
22-
"should": "^4.0.4"
20+
"eslint": "3.1.0",
21+
"jsreport-authentication": "1.0.3",
22+
"jsreport-core": "1.0.5",
23+
"jsreport-express": "1.0.2",
24+
"jsreport-jsrender": "1.0.1",
25+
"mocha": "2.5.3",
26+
"should": "9.0.2",
27+
"standard": "7.1.2"
2328
},
2429
"dependencies": {
25-
"concat-stream": "^1.4.6",
26-
"request": "^2.40.0"
30+
"concat-stream": "1.5.1",
31+
"lodash.assign": "4.0.9",
32+
"request": "2.73.0"
2733
}
2834
}

0 commit comments

Comments
 (0)