Skip to content
This repository has been archived by the owner on Jun 11, 2024. It is now read-only.

Improvements to the Testclient #132

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions example/apidoc.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,8 @@
"withCompare": true,
"withGenerator": true
}
"jQueryAjaxSetup": {
"headers": {
"X-Your-CustomHeader": "some value",
}
}
5 changes: 5 additions & 0 deletions lib/package_info.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,11 @@ PackageInfo.prototype._extractApiDocJsonData = function(json)
};
app.debug("read footer file: " + filename);
}

// Custom jQueryAjaxSetup
if (json.jQueryAjaxSetup) {
packageInfo.jQueryAjaxSetup = json.jQueryAjaxSetup;
}

return packageInfo;
}; // _extractApiDocJsonData
9 changes: 7 additions & 2 deletions template/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ require([
/**
* Load google web fonts.
*/
loadGoogleFontCss($);

loadGoogleFontCss($);
var api = apiData.api;

/**
Expand All @@ -70,6 +70,11 @@ require([
if(apiProject.template.withCompare == null) apiProject.template.withCompare = true;
if(apiProject.template.withGenerator == null) apiProject.template.withGenerator = true;

/**
* Setup jQuery Ajax
*/
$.ajaxSetup(apiProject.jQueryAjaxSetup);

/**
* Data transform.
*/
Expand Down
28 changes: 24 additions & 4 deletions template/utils/send_sample_request.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ define([
{
var $root = $('article[data-group="' + group + '"][data-name="' + name + '"][data-version="' + version + '"]');

// Fade out the previous response to make clear that there is a new response somewhen...
$root.find(".sample-request-response").fadeTo(1, 0.1);

// create JSON dictionary of parameters
var dict = {};
$root.find(".sample-request-param").each(function(i, element) {
Expand All @@ -54,25 +57,42 @@ define([
}
} // while

var jsonData = JSON.stringify(dict);

// send AJAX request, catch success or error callback
$.ajax({
url: url,
dataType: "json",
data: dict,
contentType: "application/json",
data: jsonData,
type: type.toUpperCase(),
success: displaySuccess,
error: displayError
});

function displaySuccess(data) {
$root.find(".sample-request-response").show();
var message = JSON.stringify(data, null, 4);

$root.find(".sample-request-response-json").html(JSON.stringify(data, null, 4));
$root.find(".sample-request-response").fadeTo(250, 1);

refreshScrollSpy();
};

function displayError(jqXHR, textStatus, error) {
$root.find(".sample-request-response").show();
$root.find(".sample-request-response-json").html(jqXHR.status + " Error: " + error);
var message = "Error " + jqXHR.status + ": " + error;
var jsonResponse = JSON.parse(jqXHR.responseText);

if (jsonResponse) {
message += "<br /><br />" + JSON.stringify(jsonResponse, null, 4)
}
else {
message += "<br /><br />" + jqXHR.responseText;
}

$root.find(".sample-request-response-json").html(message);
$root.find(".sample-request-response").fadeTo(250, 1);

refreshScrollSpy();
};
}
Expand Down