Skip to content

Commit 2f9ebe1

Browse files
committed
added obj parsing for json response
1 parent 65d5a3b commit 2f9ebe1

File tree

1 file changed

+32
-21
lines changed

1 file changed

+32
-21
lines changed

lib/swagger.js

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@ var __bind = function(fn, me){
44
};
55
};
66

7-
log = function() {
8-
if (window.console)
9-
return console.log(console, arguments);
7+
log = function(){
8+
log.history = log.history || [];
9+
log.history.push(arguments);
10+
if(this.console){
11+
console.log( Array.prototype.slice.call(arguments) );
12+
}
1013
};
1114

1215
var SwaggerApi = function(url, options) {
@@ -16,7 +19,6 @@ var SwaggerApi = function(url, options) {
1619
this.authorizations = null;
1720
this.authorizationScheme = null;
1821
this.info = null;
19-
this.useJQuery = true;
2022

2123
options = (options||{});
2224
if (url)
@@ -62,8 +64,7 @@ SwaggerApi.prototype.build = function() {
6264
}
6365
},
6466
response: function(resp) {
65-
var responseObj;
66-
responseObj = JSON.parse(resp.data);
67+
var responseObj = resp.obj;
6768
_this.swaggerVersion = responseObj.swaggerVersion;
6869
if (_this.swaggerVersion === "1.2") {
6970
return _this.buildFromSpec(responseObj);
@@ -73,7 +74,7 @@ SwaggerApi.prototype.build = function() {
7374
}
7475
}
7576
};
76-
var e = typeof window !== 'undefined' ? window : exports;
77+
var e = (typeof window !== 'undefined' ? window : exports);
7778
e.authorizations.apply(obj);
7879
new SwaggerHttp().execute(obj);
7980
return this;
@@ -284,8 +285,7 @@ var SwaggerResource = function(resourceObj, api) {
284285
return _this.api.fail("Unable to read api '" + _this.name + "' from path " + _this.url + " (server returned " + response.statusText + ")");
285286
},
286287
response: function(response) {
287-
var responseObj;
288-
responseObj = JSON.parse(response.data);
288+
var responseObj = response.obj;
289289
return _this.addApiDeclaration(responseObj);
290290
}
291291
}
@@ -406,6 +406,7 @@ SwaggerResource.prototype.addOperations = function(resource_path, ops, consumes,
406406
SwaggerResource.prototype.sanitize = function(nickname) {
407407
var op;
408408
op = nickname.replace(/[\s!@#$%^&*()_+=\[{\]};:<>|./?,\\'""-]/g, '_');
409+
//'
409410
op = op.replace(/((_){2,})/g, '_');
410411
op = op.replace(/^(_)*/g, '');
411412
op = op.replace(/([_])*$/g, '');
@@ -875,7 +876,7 @@ SwaggerOperation.prototype.help = function() {
875876
return msg;
876877
};
877878

878-
var SwaggerRequest = function(type, url, params, opts, successCallback, errorCallback, operation, execution) {
879+
var SwaggerRequest = function(type, url, params, opts, successCallback, errorCallback, operation, execution) {
879880
var _this = this;
880881
var errors = [];
881882
this.useJQuery = (typeof operation.useJQuery !== 'undefined' ? operation.useJQuery : null);
@@ -1031,7 +1032,7 @@ SwaggerRequest.prototype.asCurl = function() {
10311032
return "curl " + (results.join(" ")) + " " + this.url;
10321033
};
10331034

1034-
/*
1035+
/**
10351036
* SwaggerHttp is a wrapper for executing requests
10361037
*/
10371038
var SwaggerHttp = function() {};
@@ -1108,6 +1109,10 @@ JQueryHttpClient.prototype.execute = function(obj) {
11081109
headers: headers
11091110
};
11101111

1112+
if(response._headers["Content-Type"] && response._headers["Content-Type"].indexOf("application/json") == 0 && response.content.data !== "") {
1113+
out.obj = JSON.parse(response.responseText);
1114+
}
1115+
11111116
if(response.status >= 200 && response.status < 300)
11121117
cb.response(out);
11131118
else if(response.status === 0 || (response.status >= 400 && response.status < 599))
@@ -1171,35 +1176,41 @@ ShredHttpClient.prototype.execute = function(obj) {
11711176
var cb = obj.on, res;
11721177

11731178
var transform = function(response) {
1174-
return {
1179+
var out = {
11751180
headers: response._headers,
11761181
url: response.request.url,
11771182
method: response.request.method,
11781183
status: response.status,
11791184
data: response.content.data
11801185
};
1186+
1187+
if(response._headers["Content-Type"]) {
1188+
if(response._headers["Content-Type"].indexOf("application/json") == 0) {
1189+
if(response.content.data !== "")
1190+
out.obj = JSON.parse(response.content.data);
1191+
else
1192+
out.obj = {}
1193+
}
1194+
}
1195+
return out;
11811196
};
11821197

11831198
res = {
11841199
error: function(response) {
1185-
if (obj) {
1200+
if (obj)
11861201
return cb.error(transform(response));
1187-
}
11881202
},
11891203
redirect: function(response) {
1190-
if (obj) {
1204+
if (obj)
11911205
return cb.redirect(transform(response));
1192-
}
11931206
},
11941207
307: function(response) {
1195-
if (obj) {
1208+
if (obj)
11961209
return cb.redirect(transform(response));
1197-
}
11981210
},
11991211
response: function(response) {
1200-
if (obj) {
1212+
if (obj)
12011213
return cb.response(transform(response));
1202-
}
12031214
}
12041215
};
12051216
if (obj) {
@@ -1279,7 +1290,7 @@ PasswordAuthorization.prototype.apply = function(obj, authorizations) {
12791290
return true;
12801291
};
12811292

1282-
var e = typeof exports !== 'undefined' ? e : window;
1293+
var e = (typeof window !== 'undefined' ? window : exports);
12831294

12841295
e.SwaggerHttp = SwaggerHttp;
12851296
e.SwaggerRequest = SwaggerRequest;

0 commit comments

Comments
 (0)