Skip to content

Commit

Permalink
Extract ng-http-client-helper
Browse files Browse the repository at this point in the history
  • Loading branch information
AlekseyMartynov committed Jan 24, 2019
1 parent 08b6f2c commit 37e4de7
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 68 deletions.
103 changes: 35 additions & 68 deletions experiments/ng-http-client-fixture.js
Original file line number Diff line number Diff line change
@@ -1,73 +1,40 @@
/* eslint-env browser */
(function($, http) {
"use strict";

var nonce = Date.now();

var httpClient = new http.HttpClient(
new http.HttpXhrBackend({
build: function() {
return new XMLHttpRequest();
}
})
);

function createXhrSurrogate(response) {
function getResponseHeader(name) {
return response.headers.get(name);
}

function makeResponseText() {
var error = response.error;
if(error === undefined)
return "N/A";

if(typeof error !== "string" || String(getResponseHeader("Content-Type")).indexOf("application/json") === 0)
return JSON.stringify(error);

return error;
}

return {
status: response.status,
statusText: response.statusText,
getResponseHeader: getResponseHeader,
responseText: makeResponseText()
};
}

$.ajax = function(options) {
var d = $.Deferred();

var method = (options.method || "get").toLowerCase();
var data = options.data;
var xhrFields = options.xhrFields;

if(options.cache === false && method === "get" && data)
data._ = nonce++;
(function() {
"use strict";

httpClient
.request(
method,
options.url,
{
params: data,
responseType: options.dataType,
headers: options.headers,
withCredentials: xhrFields && xhrFields.withCredentials,
observe: "response"
function createHttpClient(http) {
return new http.HttpClient(
new http.HttpXhrBackend({
build: function() {
return new XMLHttpRequest();
}
)
.subscribe(
function(response) {
d.resolve(response.body, "success", createXhrSurrogate(response));
},
function(response) {
d.reject(createXhrSurrogate(response), "error");
}
);

return d.promise();
};
})
);
}

})(window.jQuery, window.ng.common.http);
var ajax;
var createNgHttpClientSendRequestFunc;

if(typeof define === "function" && define.amd) {
define(function(require) {
ajax = require("devextreme/core/utils/ajax");
createNgHttpClientSendRequestFunc = require("./ng-http-client-helper");

ajax.inject({
sendRequest: createNgHttpClientSendRequestFunc(
createHttpClient(require("@angular/common/bundles/common-http.umd")),
require("devextreme/core/utils/deferred").Deferred
)
});
});
} else if (typeof module === "object" && module.exports) {
throw "TODO";
} else {
var $ = window.jQuery;
$.ajax = window.DevExpress.data.AspNet.createNgHttpClientSendRequestFunc(
createHttpClient(window.ng.common.http),
$.Deferred
);
}
})();
78 changes: 78 additions & 0 deletions experiments/ng-http-client-helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/* eslint-env browser */

(function(sendRequestFactory) {
"use strict";

if(typeof define === "function" && define.amd) {
define(function(require, exports, module) {
module.exports = sendRequestFactory;
});
} else if (typeof module === "object" && module.exports) {
throw "TODO";
} else {
window.DevExpress.data.AspNet.createNgHttpClientSendRequestFunc = sendRequestFactory;
}

})(function(httpClient, Deferred) {
"use strict";

var nonce = Date.now();

function createXhrSurrogate(response) {
function getResponseHeader(name) {
return response.headers.get(name);
}

function makeResponseText() {
var error = response.error;
if(error === undefined)
return "N/A";

if(typeof error !== "string" || String(getResponseHeader("Content-Type")).indexOf("application/json") === 0)
return JSON.stringify(error);

return error;
}

return {
status: response.status,
statusText: response.statusText,
getResponseHeader: getResponseHeader,
responseText: makeResponseText()
};
}

return function(options) {
var d = Deferred();

var method = (options.method || "get").toLowerCase();
var data = options.data;
var xhrFields = options.xhrFields;

if(options.cache === false && method === "get" && data)
data._ = nonce++;

httpClient
.request(
method,
options.url,
{
params: data,
responseType: options.dataType,
headers: options.headers,
withCredentials: xhrFields && xhrFields.withCredentials,
observe: "response"
}
)
.subscribe(
function(response) {
d.resolve(response.body, "success", createXhrSurrogate(response));
},
function(response) {
d.reject(createXhrSurrogate(response), "error");
}
);

return d.promise();
};
});
1 change: 1 addition & 0 deletions js-test/test.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"../node_modules/rxjs/bundles/rxjs.umd.js",
"../node_modules/@angular/core/bundles/core.umd.js",
"../node_modules/@angular/common/bundles/common-http.umd.js",
"../experiments/ng-http-helper-fixture.js",
"../experiments/ng-http-client-fixture.js"
);
}
Expand Down
4 changes: 4 additions & 0 deletions js-test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

if(typeof define === "function" && define.amd) {
define(function(require) {
require("../experiments/ng-http-client-fixture");

factory(
require("xhr-mock").default,
require("devextreme/core/version"),
Expand All @@ -14,6 +16,8 @@
);
});
} else if (typeof module === "object" && module.exports) {
//require("../experiments/ng-http-client-fixture");

module.exports = factory(
require("xhr-mock").default,
require("devextreme/core/version"),
Expand Down

0 comments on commit 37e4de7

Please sign in to comment.