Skip to content

Commit 518a23a

Browse files
committed
Bump labels, update to axios
1 parent ff3d533 commit 518a23a

File tree

3 files changed

+143
-13
lines changed

3 files changed

+143
-13
lines changed

index.js

+15-8
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ if (isNode())
3535
{
3636
var fs = _safeRequire("fs");
3737
var path = _safeRequire("path");
38-
var request = _safeRequire("request");
38+
//var request = _safeRequire("request");
39+
var request = require("./request");
3940

4041
var HttpsProxyAgent = _safeRequire("https-proxy-agent");
4142

@@ -65,9 +66,10 @@ if (isNode())
6566
headers["Content-Type"] = contentType;
6667
headers["Authorization"] = driver.getHttpHeaders()["Authorization"];
6768

68-
readStream.pipe(request({
69+
request({
6970
"method": "POST",
70-
"url": uploadUri,
71+
"url": uploadUri,
72+
"data": readStream,
7173
"headers": headers,
7274
"timeout": 120 * 1000 // 2 minutes
7375
}, function (err, httpResponse, body) {
@@ -77,15 +79,15 @@ if (isNode())
7779
return callback(err);
7880
}
7981

80-
if (httpResponse.statusCode >= 200 && httpResponse.statusCode <= 204)
82+
if (httpResponse.status >= 200 && httpResponse.status <= 204)
8183
{
8284
return callback();
8385
}
8486

8587
callback({
8688
"message": "Status: " + httpResponse.statusCode + ", Message: " + body
8789
});
88-
}));
90+
});
8991
};
9092

9193
Gitana.streamDownload = function(attachment, callback)
@@ -96,16 +98,21 @@ if (isNode())
9698
headers["Authorization"] = driver.getHttpHeaders()["Authorization"];
9799

98100
// download and pipe to stream
99-
var stream = request({
101+
request({
100102
"method": "GET",
101103
"url": attachment.getDownloadUri(),
102104
"headers": {
103105
"Authorization": attachment.getDriver().getHttpHeaders()["Authorization"]
104106
},
105107
"timeout": 120 * 1000 // 2 minutes
108+
}, function (err, response) {
109+
110+
if (err) {
111+
return callback(err);
112+
}
113+
114+
callback(null, response.data);
106115
});
107-
108-
callback(null, stream);
109116
};
110117

111118
Gitana.HTTP_XHR_FACTORY = function()

package.json

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
2-
"author": "Gitana Software, Inc. <info@cloudcms.com> (http://www.cloudcms.com)",
2+
"author": "Gitana Software, Inc. <info@gitana.io> (https://gitana.io)",
33
"name": "gitana",
4-
"description": "Cloud CMS Gitana Driver for Node JS",
4+
"description": "Gitana Cloud CMS Driver for Node JS",
55
"version": "1.0.323",
66
"repository": {
77
"type": "git",
@@ -15,10 +15,10 @@
1515
"grunt": "*"
1616
},
1717
"optionalDependencies": {
18-
"https-proxy-agent": "^2.2.1",
18+
"https-proxy-agent": "^7.0.1",
1919
"node-http-xhr": "^1.3.4",
20-
"request": "*",
21-
"xmlhttprequest": "*"
20+
"axios": "^1.5.0",
21+
"xmlhttprequest": "1.8.0"
2222
},
2323
"contributors": [
2424
{

request.js

+123
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
var axios = require("axios");
2+
3+
// var http = require("http");
4+
// var https = require("https");
5+
//
6+
// var FormData = require("form-data");
7+
8+
/**
9+
* Incoming config:
10+
*
11+
* {
12+
* "url": "",
13+
* "method": "",
14+
* "headers": {},
15+
* "qs": {},
16+
* "data": "" | {},
17+
* "json": {}
18+
* }
19+
*
20+
* Callback is (err, response).
21+
*
22+
* Where response is the Axios response object.
23+
*
24+
* @param config
25+
* @param callback
26+
*/
27+
module.exports = function(config, callback)
28+
{
29+
// request config - https://github.com/request/request#requestoptions-callback
30+
// axios config - https://www.npmjs.com/package/axios
31+
32+
if (!callback) {
33+
callback = function(err, response, data) {
34+
// nothing
35+
};
36+
}
37+
38+
var requestConfig = {};
39+
requestConfig.url = config.uri || config.url;
40+
requestConfig.method = config.method || "get";
41+
requestConfig.headers = {};
42+
43+
if (!config) {
44+
config = {};
45+
}
46+
if (!config.headers) {
47+
config.headers = {};
48+
}
49+
for (var k in config.headers)
50+
{
51+
var v = config.headers[k];
52+
if (v)
53+
{
54+
requestConfig.headers[k.trim().toLowerCase()] = v;
55+
}
56+
}
57+
// support for FormData headers
58+
// copy form data headers
59+
if (config.data && config.data.getHeaders)
60+
{
61+
var formDataHeaders = config.data.getHeaders();
62+
for (var k in formDataHeaders)
63+
{
64+
var v = formDataHeaders[k];
65+
requestConfig.headers[k] = v;
66+
}
67+
}
68+
69+
if (config.qs) {
70+
requestConfig.params = config.qs;
71+
}
72+
73+
if (config.json) {
74+
requestConfig.data = config.json;
75+
76+
if (!requestConfig.headers["content-type"]) {
77+
requestConfig.headers["content-type"] = "application/json";
78+
}
79+
}
80+
81+
if (config.data)
82+
{
83+
requestConfig.data = config.data;
84+
85+
if (!requestConfig.headers["content-type"])
86+
{
87+
if (!requestConfig.data)
88+
{
89+
if (requestConfig.data.getHeaders)
90+
{
91+
// assume this is a FormData and skip
92+
}
93+
else if (typeof(requestConfig.data) === "object")
94+
{
95+
// send as json
96+
requestConfig.headers["content-type"] = "application/json";
97+
}
98+
}
99+
}
100+
}
101+
102+
if (config.responseType) {
103+
requestConfig.responseType = config.responseType;
104+
}
105+
106+
107+
/*
108+
if (requestConfig.url.toLowerCase().indexOf("https:") > -1)
109+
{
110+
requestConfig.httpsAgent = https.globalAgent;
111+
}
112+
else
113+
{
114+
requestConfig.httpAgent = http.globalAgent;
115+
}
116+
*/
117+
118+
return axios.request(requestConfig).then(function(response) {
119+
callback(null, response, response.data);
120+
}, function(error) {
121+
callback(error);
122+
});
123+
};

0 commit comments

Comments
 (0)