angular2-resource-and-ajax-interceptor
Advanced tools
Comparing version 2.0.0 to 2.0.1
@@ -146,2 +146,4 @@ System.register(['@angular/core', '@angular/http', './ajax-interceptor'], function(exports_1, context_1) { | ||
function RequestCallbackHD(url, headers, method, body, http, ajaxInterceptor) { | ||
this.STRIP_COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg; | ||
this.ARGUMENT_NAMES = /([^\s,]+)/g; | ||
this.url = url; | ||
@@ -165,8 +167,3 @@ this.method = method; | ||
} | ||
if (response.text() === '' || !response.text()) { | ||
sucessCallback(response.text()); | ||
} | ||
else { | ||
sucessCallback(response.json()); | ||
} | ||
_this.invokeCallback(sucessCallback, response); | ||
}, function (error) { | ||
@@ -176,3 +173,3 @@ if (_this.ajaxInterceptor.afterResponseError) { | ||
} | ||
errCallback(error); | ||
_this.invokeCallback(errCallback, error); | ||
}); | ||
@@ -185,8 +182,3 @@ } | ||
} | ||
if (response.text() === '' || !response.text()) { | ||
sucessCallback(response.text()); | ||
} | ||
else { | ||
sucessCallback(response.json()); | ||
} | ||
_this.invokeCallback(sucessCallback, response); | ||
}, function (error) { | ||
@@ -196,3 +188,3 @@ if (_this.ajaxInterceptor.afterResponseError) { | ||
} | ||
errCallback(error); | ||
_this.invokeCallback(errCallback, error); | ||
}); | ||
@@ -205,8 +197,3 @@ } | ||
} | ||
if (response.text() === '' || !response.text()) { | ||
sucessCallback(response.text()); | ||
} | ||
else { | ||
sucessCallback(response.json()); | ||
} | ||
_this.invokeCallback(sucessCallback, response); | ||
}, function (error) { | ||
@@ -216,6 +203,40 @@ if (_this.ajaxInterceptor.afterResponseError) { | ||
} | ||
errCallback(error); | ||
_this.invokeCallback(errCallback, error); | ||
}); | ||
} | ||
}; | ||
RequestCallbackHD.prototype.invokeCallback = function (callback, response) { | ||
var params = this.getParamNames(callback); | ||
var jsonData; | ||
if (!params) { | ||
return; | ||
} | ||
if (response.text() === '' || !response.text()) { | ||
jsonData = response.text(); | ||
} | ||
else { | ||
try { | ||
jsonData = response.json(); | ||
} | ||
catch (e) { | ||
jsonData = response.text(); | ||
} | ||
} | ||
if (params.length === 1) { | ||
callback(response); | ||
return; | ||
} | ||
if (params.length > 1) { | ||
callback(jsonData, response); | ||
return; | ||
} | ||
}; | ||
; | ||
RequestCallbackHD.prototype.getParamNames = function (func) { | ||
var fnStr = func.toString().replace(this.STRIP_COMMENTS, ''); | ||
var result = fnStr.slice(fnStr.indexOf('(') + 1, fnStr.indexOf(')')).match(this.ARGUMENT_NAMES); | ||
if (result === null) | ||
result = []; | ||
return result; | ||
}; | ||
return RequestCallbackHD; | ||
@@ -222,0 +243,0 @@ }()); |
@@ -116,3 +116,5 @@ import {Injectable, Inject} from '@angular/core'; | ||
method: string; body: any; | ||
http: Http | ||
http: Http; | ||
STRIP_COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg; | ||
ARGUMENT_NAMES = /([^\s,]+)/g; | ||
constructor(url: string, headers: Headers, method: string, body: any, http: Http, ajaxInterceptor: AjaxInterceptor) { | ||
@@ -136,7 +138,3 @@ this.url = url; | ||
} | ||
if(response.text() === '' || !response.text()) { | ||
sucessCallback(response.text()); | ||
}else { | ||
sucessCallback(response.json()); | ||
} | ||
this.invokeCallback(sucessCallback, response); | ||
}, | ||
@@ -147,3 +145,3 @@ (error) => { | ||
} | ||
errCallback(error); | ||
this.invokeCallback(errCallback, error); | ||
}); | ||
@@ -155,7 +153,3 @@ } else if (this.method === 'post' || this.method === 'put') { | ||
} | ||
if(response.text() === '' || !response.text()) { | ||
sucessCallback(response.text()); | ||
}else { | ||
sucessCallback(response.json()); | ||
} | ||
this.invokeCallback(sucessCallback, response); | ||
}, | ||
@@ -166,3 +160,3 @@ (error) => { | ||
} | ||
errCallback(error); | ||
this.invokeCallback(errCallback, error); | ||
}); | ||
@@ -174,7 +168,3 @@ } else { | ||
} | ||
if(response.text() === '' || !response.text()) { | ||
sucessCallback(response.text()); | ||
}else { | ||
sucessCallback(response.json()); | ||
} | ||
this.invokeCallback(sucessCallback, response); | ||
}, | ||
@@ -185,8 +175,41 @@ (error) => { | ||
} | ||
errCallback(error); | ||
this.invokeCallback(errCallback, error); | ||
}); | ||
} | ||
} | ||
invokeCallback(callback, response) { | ||
let params = this.getParamNames(callback); | ||
let jsonData; | ||
if(!params) { | ||
return; | ||
} | ||
if (response.text() === '' || !response.text()) { | ||
jsonData = response.text(); | ||
} else { | ||
try{ | ||
jsonData = response.json(); | ||
} | ||
catch(e) { | ||
jsonData = response.text(); | ||
} | ||
} | ||
if(params.length === 1) { | ||
callback(response); | ||
return; | ||
} | ||
if(params.length >1) { | ||
callback(jsonData, response); | ||
return; | ||
} | ||
}; | ||
getParamNames(func) { | ||
var fnStr = func.toString().replace(this.STRIP_COMMENTS, ''); | ||
var result = fnStr.slice(fnStr.indexOf('(') + 1, fnStr.indexOf(')')).match(this.ARGUMENT_NAMES); | ||
if (result === null) | ||
result = []; | ||
return result; | ||
} | ||
} |
{ | ||
"name": "angular2-resource-and-ajax-interceptor", | ||
"version": "2.0.0", | ||
"version": "2.0.1", | ||
"description": "angular2-resource-and-ajax-interceptor is angular2 http service helper.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -8,8 +8,8 @@ /** | ||
var map = { | ||
'app': 'app', // 'dist', | ||
// 'httpresource': 'node_modules/httpresource', | ||
'app': 'example', // 'dist', | ||
'httpresource' : 'httpresource', | ||
'@angular': 'node_modules/@angular', | ||
'angular2-in-memory-web-api': 'node_modules/angular2-in-memory-web-api', | ||
'rxjs': 'node_modules/rxjs', | ||
'httpresource' : 'httpresource' | ||
'rxjs': 'node_modules/rxjs' | ||
}; | ||
@@ -32,3 +32,3 @@ // packages tells the System loader how to load when no filename and/or no extension | ||
'router', | ||
'upgrade', | ||
'upgrade' | ||
]; | ||
@@ -35,0 +35,0 @@ // Individual files (~300 requests): |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
65331
32
999
1