UNPKG

@corpecca/qp-web-resources

Version:
174 lines (142 loc) 6.41 kB
(function (qp, angular) { if (!angular) { return; } qp.ng = qp.ng || {}; qp.ng.http = { defaultError: { message: 'An error has occurred!', details: 'Error detail not sent by server.' }, defaultError401: { message: 'You are not authenticated!', details: 'You should be authenticated (sign in) in order to perform this operation.' }, defaultError403: { message: 'You are not authorized!', details: 'You are not allowed to perform this operation.' }, defaultError404: { message: 'Resource not found!', details: 'The resource requested could not be found on the server.' }, logError: function (error) { qp.log.error(error); }, showError: function (error) { if (error.details) { return qp.message.error(error.details, error.message || qp.ng.http.defaultError.message); } else { return qp.message.error(error.message || qp.ng.http.defaultError.message); } }, handleTargetUrl: function (targetUrl) { if (!targetUrl) { location.href = qp.appPath; } else { location.href = targetUrl; } }, handleNonQpErrorResponse: function (response, defer) { if (response.config.qpHandleError !== false) { switch (response.status) { case 401: qp.ng.http.handleUnAuthorizedRequest( qp.ng.http.showError(qp.ng.http.defaultError401), qp.appPath ); break; case 403: qp.ng.http.showError(qp.ajax.defaultError403); break; case 404: qp.ng.http.showError(qp.ajax.defaultError404); break; default: qp.ng.http.showError(qp.ng.http.defaultError); break; } } defer.reject(response); }, handleUnAuthorizedRequest: function (messagePromise, targetUrl) { if (messagePromise) { messagePromise.done(function () { qp.ng.http.handleTargetUrl(targetUrl || qp.appPath); }); } else { qp.ng.http.handleTargetUrl(targetUrl || qp.appPath); } }, handleResponse: function (response, defer) { var originalData = response.data; if (originalData.success === true) { response.data = originalData.result; defer.resolve(response); if (originalData.targetUrl) { qp.ng.http.handleTargetUrl(originalData.targetUrl); } } else if (originalData.success === false) { var messagePromise = null; if (originalData.error) { if (response.config.qpHandleError !== false) { messagePromise = qp.ng.http.showError(originalData.error); } } else { originalData.error = defaultError; } qp.ng.http.logError(originalData.error); response.data = originalData.error; defer.reject(response); if (response.status == 401 && response.config.qpHandleError !== false) { qp.ng.http.handleUnAuthorizedRequest(messagePromise, originalData.targetUrl); } } else { //not wrapped result defer.resolve(response); } } } var qpModule = angular.module('qp', []); qpModule.config([ '$httpProvider', function ($httpProvider) { $httpProvider.interceptors.push(['$q', function ($q) { return { 'request': function (config) { if (config.url.indexOf('.cshtml') !== -1) { config.url = qp.appPath + 'QpAppView/Load?viewUrl=' + config.url + '&_t=' + qp.pageLoadTime.getTime(); } return config; }, 'response': function (response) { if (!response.data || !response.data.__qp) { //Non ABP related return value return response; } var defer = $q.defer(); qp.ng.http.handleResponse(response, defer); return defer.promise; }, 'responseError': function (ngError) { var defer = $q.defer(); if (!ngError.data || !ngError.data.__qp) { qp.ng.http.handleNonQpErrorResponse(ngError, defer); } else { qp.ng.http.handleResponse(ngError, defer); } return defer.promise; } }; }]); } ]); qp.event.on('qp.dynamicScriptsInitialized', function () { qp.ng.http.defaultError.message = qp.localization.qpWeb('DefaultError'); qp.ng.http.defaultError.details = qp.localization.qpWeb('DefaultErrorDetail'); qp.ng.http.defaultError401.message = qp.localization.qpWeb('DefaultError401'); qp.ng.http.defaultError401.details = qp.localization.qpWeb('DefaultErrorDetail401'); qp.ng.http.defaultError403.message = qp.localization.qpWeb('DefaultError403'); qp.ng.http.defaultError403.details = qp.localization.qpWeb('DefaultErrorDetail403'); qp.ng.http.defaultError404.message = qp.localization.qpWeb('DefaultError404'); qp.ng.http.defaultError404.details = qp.localization.qpWeb('DefaultErrorDetail404'); }); })((qp || (qp = {})), (angular || undefined));