From 37e4de79067d8ebfff45c4049e5108c552426370 Mon Sep 17 00:00:00 2001 From: Aleksey Martynov Date: Thu, 24 Jan 2019 19:41:29 +0300 Subject: [PATCH] Extract ng-http-client-helper --- experiments/ng-http-client-fixture.js | 103 +++++++++----------------- experiments/ng-http-client-helper.js | 78 +++++++++++++++++++ js-test/test.html | 1 + js-test/test.js | 4 + 4 files changed, 118 insertions(+), 68 deletions(-) create mode 100644 experiments/ng-http-client-helper.js diff --git a/experiments/ng-http-client-fixture.js b/experiments/ng-http-client-fixture.js index bb53f8ef..f3b766e9 100644 --- a/experiments/ng-http-client-fixture.js +++ b/experiments/ng-http-client-fixture.js @@ -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 + ); + } +})(); diff --git a/experiments/ng-http-client-helper.js b/experiments/ng-http-client-helper.js new file mode 100644 index 00000000..fa262be2 --- /dev/null +++ b/experiments/ng-http-client-helper.js @@ -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(); + }; +}); diff --git a/js-test/test.html b/js-test/test.html index c2e7e6b3..799b42df 100644 --- a/js-test/test.html +++ b/js-test/test.html @@ -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" ); } diff --git a/js-test/test.js b/js-test/test.js index 5dda71b0..a7045d4c 100644 --- a/js-test/test.js +++ b/js-test/test.js @@ -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"), @@ -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"),