Skip to content

Commit a1115dc

Browse files
committed
Add support for custom accept headers for real this time, added markdown-raw method, added support for non-JSON request results. Fixes octokit#239. Fixes octokit#235.
1 parent 9d0cccd commit a1115dc

20 files changed

+2836
-1611
lines changed

api/v3.0.0/authorization.js

Lines changed: 60 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@ var authorization = module.exports = {
3838

3939
var ret;
4040
try {
41-
ret = res.data && JSON.parse(res.data);
41+
ret = res.data;
42+
var contentType = res.headers["content-type"];
43+
if (contentType && contentType.indexOf("application/json") !== -1)
44+
ret = JSON.parse(ret);
4245
}
4346
catch (ex) {
4447
if (callback)
@@ -48,12 +51,14 @@ var authorization = module.exports = {
4851

4952
if (!ret)
5053
ret = {};
51-
if (!ret.meta)
52-
ret.meta = {};
53-
["x-ratelimit-limit", "x-ratelimit-remaining", "x-ratelimit-reset", "x-oauth-scopes", "link", "location", "last-modified", "etag", "status"].forEach(function(header) {
54-
if (res.headers[header])
55-
ret.meta[header] = res.headers[header];
56-
});
54+
if (typeof ret == "object") {
55+
if (!ret.meta)
56+
ret.meta = {};
57+
["x-ratelimit-limit", "x-ratelimit-remaining", "x-ratelimit-reset", "x-oauth-scopes", "link", "location", "last-modified", "etag", "status"].forEach(function(header) {
58+
if (res.headers[header])
59+
ret.meta[header] = res.headers[header];
60+
});
61+
}
5762

5863
if (callback)
5964
callback(null, ret);
@@ -78,7 +83,10 @@ var authorization = module.exports = {
7883

7984
var ret;
8085
try {
81-
ret = res.data && JSON.parse(res.data);
86+
ret = res.data;
87+
var contentType = res.headers["content-type"];
88+
if (contentType && contentType.indexOf("application/json") !== -1)
89+
ret = JSON.parse(ret);
8290
}
8391
catch (ex) {
8492
if (callback)
@@ -88,12 +96,14 @@ var authorization = module.exports = {
8896

8997
if (!ret)
9098
ret = {};
91-
if (!ret.meta)
92-
ret.meta = {};
93-
["x-ratelimit-limit", "x-ratelimit-remaining", "x-ratelimit-reset", "x-oauth-scopes", "link", "location", "last-modified", "etag", "status"].forEach(function(header) {
94-
if (res.headers[header])
95-
ret.meta[header] = res.headers[header];
96-
});
99+
if (typeof ret == "object") {
100+
if (!ret.meta)
101+
ret.meta = {};
102+
["x-ratelimit-limit", "x-ratelimit-remaining", "x-ratelimit-reset", "x-oauth-scopes", "link", "location", "last-modified", "etag", "status"].forEach(function(header) {
103+
if (res.headers[header])
104+
ret.meta[header] = res.headers[header];
105+
});
106+
}
97107

98108
if (callback)
99109
callback(null, ret);
@@ -120,7 +130,10 @@ var authorization = module.exports = {
120130

121131
var ret;
122132
try {
123-
ret = res.data && JSON.parse(res.data);
133+
ret = res.data;
134+
var contentType = res.headers["content-type"];
135+
if (contentType && contentType.indexOf("application/json") !== -1)
136+
ret = JSON.parse(ret);
124137
}
125138
catch (ex) {
126139
if (callback)
@@ -130,12 +143,14 @@ var authorization = module.exports = {
130143

131144
if (!ret)
132145
ret = {};
133-
if (!ret.meta)
134-
ret.meta = {};
135-
["x-ratelimit-limit", "x-ratelimit-remaining", "x-ratelimit-reset", "x-oauth-scopes", "link", "location", "last-modified", "etag", "status"].forEach(function(header) {
136-
if (res.headers[header])
137-
ret.meta[header] = res.headers[header];
138-
});
146+
if (typeof ret == "object") {
147+
if (!ret.meta)
148+
ret.meta = {};
149+
["x-ratelimit-limit", "x-ratelimit-remaining", "x-ratelimit-reset", "x-oauth-scopes", "link", "location", "last-modified", "etag", "status"].forEach(function(header) {
150+
if (res.headers[header])
151+
ret.meta[header] = res.headers[header];
152+
});
153+
}
139154

140155
if (callback)
141156
callback(null, ret);
@@ -165,7 +180,10 @@ var authorization = module.exports = {
165180

166181
var ret;
167182
try {
168-
ret = res.data && JSON.parse(res.data);
183+
ret = res.data;
184+
var contentType = res.headers["content-type"];
185+
if (contentType && contentType.indexOf("application/json") !== -1)
186+
ret = JSON.parse(ret);
169187
}
170188
catch (ex) {
171189
if (callback)
@@ -175,12 +193,14 @@ var authorization = module.exports = {
175193

176194
if (!ret)
177195
ret = {};
178-
if (!ret.meta)
179-
ret.meta = {};
180-
["x-ratelimit-limit", "x-ratelimit-remaining", "x-ratelimit-reset", "x-oauth-scopes", "link", "location", "last-modified", "etag", "status"].forEach(function(header) {
181-
if (res.headers[header])
182-
ret.meta[header] = res.headers[header];
183-
});
196+
if (typeof ret == "object") {
197+
if (!ret.meta)
198+
ret.meta = {};
199+
["x-ratelimit-limit", "x-ratelimit-remaining", "x-ratelimit-reset", "x-oauth-scopes", "link", "location", "last-modified", "etag", "status"].forEach(function(header) {
200+
if (res.headers[header])
201+
ret.meta[header] = res.headers[header];
202+
});
203+
}
184204

185205
if (callback)
186206
callback(null, ret);
@@ -205,7 +225,10 @@ var authorization = module.exports = {
205225

206226
var ret;
207227
try {
208-
ret = res.data && JSON.parse(res.data);
228+
ret = res.data;
229+
var contentType = res.headers["content-type"];
230+
if (contentType && contentType.indexOf("application/json") !== -1)
231+
ret = JSON.parse(ret);
209232
}
210233
catch (ex) {
211234
if (callback)
@@ -215,12 +238,14 @@ var authorization = module.exports = {
215238

216239
if (!ret)
217240
ret = {};
218-
if (!ret.meta)
219-
ret.meta = {};
220-
["x-ratelimit-limit", "x-ratelimit-remaining", "x-ratelimit-reset", "x-oauth-scopes", "link", "location", "last-modified", "etag", "status"].forEach(function(header) {
221-
if (res.headers[header])
222-
ret.meta[header] = res.headers[header];
223-
});
241+
if (typeof ret == "object") {
242+
if (!ret.meta)
243+
ret.meta = {};
244+
["x-ratelimit-limit", "x-ratelimit-remaining", "x-ratelimit-reset", "x-oauth-scopes", "link", "location", "last-modified", "etag", "status"].forEach(function(header) {
245+
if (res.headers[header])
246+
ret.meta[header] = res.headers[header];
247+
});
248+
}
224249

225250
if (callback)
226251
callback(null, ret);

0 commit comments

Comments
 (0)