-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheaders.js
72 lines (59 loc) · 2.11 KB
/
headers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/**
* angular-hu-headers v1.1.0
* https://github.com/angular-hu/angular-hu
* (c) 2015 Telefónica I+D - http://www.tid.es
* @license MIT
*/
(function(angular) {
'use strict';
angular.module('httpu.headers', [])
.factory('huFromCache', huFromCache)
.config(config);
function huFromCache() {
//Cache decorator to add the 'httpu-cached-at' when putting a {key, value}
return function(Cache) {
var oldPut = Cache.put;
Cache.put = function(key, value) {
if (angular.isArray(value) && angular.isNumber(value[0]) && angular.isObject(value[2])) {
value[2]['httpu-cached-at'] = String(Date.now());
}
return oldPut.call(Cache, key, value);
};
return Cache;
};
}
config.$inject = ['$provide'];
function config($provide) {
$provide.decorator('$httpBackend', decorator);
}
decorator.$inject = ['$delegate'];
function decorator($httpBackend) {
//ngMock and maybe other decorators can put properties in functions
//With ES6 proxies it will be easy than this hack
angular.forEach($httpBackend, function(prop, key) {
wrap[key] = prop;
});
return wrap;
//function(method, url, post, callback, headers, timeout, withCredentials, responseType)
function wrap() {
var args = Array.prototype.slice.call(arguments),
callback = args[3],
url = args[1], //url
startRequest = new Date().getTime();
args[3] = callbackDecorator; //callback
return $httpBackend.apply(this, args);
////////////////////////////
//$http.done(status, response, headersString, statusText)
function callbackDecorator() {
var callBackArgs = Array.prototype.slice.call(arguments),
endRequest = new Date().getTime(),
headersString = callBackArgs[2]; //headersString
headersString = headersString || '';
headersString += '\nhttpu-request-time: ' + (endRequest - startRequest);
headersString += '\nhttpu-request-url: ' + url;
callBackArgs[2] = headersString;
callback.apply(this, callBackArgs);
}
}
}
})(window.angular);