-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp.es5.js
116 lines (106 loc) · 4.16 KB
/
http.es5.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
'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 rq = require('request-promise');
var _ = require('lodash');
var config = require('./config');
// const util = require('../util');
var parseJson = function parseJson(str) {
try {
str = JSON.parse(str);
} finally {
return str;
}
};
var Http = function () {
function Http(baseUrl) {
_classCallCheck(this, Http);
this.baseUrl = baseUrl;
this.request = function (option) {
return rq(option).then(function (result) {
if (typeof result === 'string') {
result = parseJson(result);
}
return result;
});
};
this.setDefaultHeader = function (headers) {
headers['content-type'] = headers['content-type'] ? headers['content-type'] : 'application/json';
headers['referer'] = headers['referer'] ? headers['referer'] : config.http.referer;
headers['origin'] = headers['origin'] ? headers['origin'] : config.http.origin;
headers['user-agent'] = headers['user-agent'] ? headers['user-agent'] : config.http.userAgent;
headers['Accept'] = headers['Accept'] ? headers['Accept'] : config.http.accept;
headers['Accept-Language'] = headers['Accept-Language'] ? headers['Accept-Language'] : config.http.acceptLanguage;
return headers;
};
}
_createClass(Http, [{
key: 'get',
value: function get(url, headers) {
var option = {
uri: '' + this.baseUrl + url,
method: 'GET',
headers: {}
};
if (!_.isEmpty(headers)) {
option.headers = headers;
};
option.headers = this.setDefaultHeader(option.headers);
return this.request(option);
}
}, {
key: 'post',
value: function post(url, data, headers) {
var option = {
uri: '' + this.baseUrl + url,
method: 'POST',
headers: {}
};
if (!_.isEmpty(data)) {
option.body = JSON.stringify(data);
}
if (!_.isEmpty(headers)) {
option.headers = headers;
}
option.headers = this.setDefaultHeader(option.headers);
return this.request(option);
}
}, {
key: 'put',
value: function put(url, data, headers) {
var option = {
uri: '' + this.baseUrl + url,
method: 'PUT',
headers: {}
};
if (!_.isEmpty(data)) {
option.body = JSON.stringify(data);
}
if (!_.isEmpty(headers)) {
option.headers = headers;
};
option.headers = this.setDefaultHeader(option.headers);
return this.request(option);
}
}, {
key: 'del',
value: function del(url, data, headers) {
var option = {
uri: '' + this.baseUrl + url,
method: 'DELETE',
headers: {}
};
if (!_.isEmpty(data)) {
option.body = JSON.stringify(data);
}
if (!_.isEmpty(headers)) {
option.headers = headers;
}
option.headers = this.setDefaultHeader(option.headers);
return this.request(option);
}
}]);
return Http;
}();
;
module.exports = Http;