UNPKG

breeze-client

Version:

Breeze data management for JavaScript clients

191 lines (186 loc) 7.29 kB
import { config, core } from 'breeze-client'; /** Appends parameter to url, with appropriate delimiter */ function appendQueryStringParameter(url, parameter) { if (!parameter) return url; let separator = url.endsWith("?") ? "" : url.includes("?") ? "&" : "?"; return url + separator + parameter; } /** Encodes the object into query string parameters */ function encodeParams(obj) { let query = ''; let subValue, innerObj, fullSubName; for (let name in obj) { if (!obj.hasOwnProperty(name)) { continue; } let value = obj[name]; if (value instanceof Array) { for (let i = 0; i < value.length; ++i) { subValue = value[i]; fullSubName = name + '[' + i + ']'; innerObj = {}; innerObj[fullSubName] = subValue; query += encodeParams(innerObj) + '&'; } } else if (value && value.toISOString) { // a feature of Date-like things query += encodeURIComponent(name) + '=' + encodeURIComponent(value.toISOString()) + '&'; } else if (value instanceof Object) { for (let subName in value) { if (obj.hasOwnProperty(name)) { subValue = value[subName]; fullSubName = name + '[' + subName + ']'; innerObj = {}; innerObj[fullSubName] = subValue; query += encodeParams(innerObj) + '&'; } } } else if (value === null) { query += encodeURIComponent(name) + '=&'; } else if (value !== undefined) { query += encodeURIComponent(name) + '=' + encodeURIComponent(value) + '&'; } } return query.length ? query.substr(0, query.length - 1) : query; } class AjaxAngularjsAdapter { name; defaultSettings; requestInterceptor; $http; $rootScope; constructor() { this.name = "angularjs"; this.defaultSettings = {}; this.requestInterceptor = undefined; // Will set: // this.$http; // this.$rootScope; } static register(breezeConfig) { breezeConfig = breezeConfig || config; breezeConfig.registerAdapter("ajax", AjaxAngularjsAdapter); return breezeConfig.initializeAdapterInstance("ajax", "angularjs", true); } initialize() { let ng = core.requireLib("angular"); if (ng) { let $injector = ng.injector(['ng']); let http, rootScope; $injector.invoke(['$http', '$rootScope', function ($http, $rootScope) { http = $http; rootScope = $rootScope; }]); this.$http = http; this.$rootScope = rootScope; } } setHttp(http) { this.$http = http; this.$rootScope = null; // to suppress $rootScope.digest } ajax(config) { if (!this.$http) { throw new Error("Unable to locate angularjs for ajax adapter"); } let ngConfig = { method: config.type, url: config.url, dataType: config.dataType, contentType: config.contentType, crossDomain: config.crossDomain, headers: config.headers || {}, data: undefined }; if (config.params) { // Hack: because of the way that Angular handles writing parameters out to the url. // so this approach takes over the url param writing completely. // See: http://victorblog.com/2012/12/20/make-angularjs-http-service-behave-like-jquery-ajax/ ngConfig.url = appendQueryStringParameter(ngConfig.url, encodeParams(config.params)); } if (config.data) { ngConfig.data = config.data; } if (!core.isEmpty(this.defaultSettings)) { let compositeConfig = core.extend({}, this.defaultSettings); ngConfig = core.extend(compositeConfig, ngConfig); // extend is shallow; extend headers separately let headers = core.extend({}, this.defaultSettings.headers); // copy default headers 1st ngConfig.headers = core.extend(headers, ngConfig.headers); } let requestInfo = { adapter: this, config: ngConfig, dsaConfig: config, success: successFn, error: errorFn, responseSuccess: responseSuccessFn, responseError: responseErrorFn // adapter's error callback (ng 1.6+) }; if (core.isFunction(this.requestInterceptor)) { let ri = this.requestInterceptor; ri(requestInfo); if (ri.oneTime) { this.requestInterceptor = undefined; } } if (requestInfo.config) { // exists unless requestInterceptor killed it. let prom = this.$http(requestInfo.config); if (prom.success) { // response for ng < 1.6 prom.success(requestInfo.success).error(requestInfo.error); } else { // response for ng 1.6+ prom.then(requestInfo.responseSuccess).catch(requestInfo.responseError); } this.$rootScope && this.$rootScope.$digest(); } function responseSuccessFn(response) { return successFn(response.data, response.status, response.headers, response.config, response.statusText); } function successFn(data, status, headers, xconfig, statusText) { // HACK: because $http returns a server side null as a string containing "null" - this is WRONG. if (data === "null") data = null; let httpResponse = { config: config, data: data, getHeaders: headers, ngConfig: xconfig, status: status, statusText: statusText }; config.success(httpResponse); } function responseErrorFn(response) { return errorFn(response.data, response.status, response.headers, response.config, response.statusText); } function errorFn(data, status, headers, xconfig, statusText) { // Timeout appears as an error with status===0 and no data. // Make it better if (status === 0 && data == null) { data = 'timeout'; } let httpResponse = { config: config, data: data, getHeaders: headers, ngConfig: xconfig, status: status, statusText: statusText }; config.error(httpResponse); } } } config.registerAdapter("ajax", AjaxAngularjsAdapter); /** * Generated bundle index. Do not edit. */ export { AjaxAngularjsAdapter }; //# sourceMappingURL=breeze-client-adapter-ajax-angularjs.mjs.map