1
1
'use strict' ;
2
+ 'use strict' ;
2
3
3
4
/**
4
5
* Parse headers into key value object
@@ -360,6 +361,8 @@ function $HttpProvider() {
360
361
*
361
362
* - **method** – `{string}` – HTTP method (e.g. 'GET', 'POST', etc)
362
363
* - **url** – `{string}` – Absolute or relative URL of the resource that is being requested.
364
+ * - **params** – `{Object.<string|Object>}` – Map of strings or objects which will be turned to
365
+ * `?key1=value1&key2=value2` after the url. If the value is not a string, it will be JSONified.
363
366
* - **data** – `{string|Object}` – Data to be sent as the request message data.
364
367
* - **headers** – `{Object}` – Map of strings representing HTTP headers to send to the server.
365
368
* - **transformRequest** – `{function(data, headersGetter)|Array.<function(data, headersGetter)>}` –
@@ -638,7 +641,8 @@ function $HttpProvider() {
638
641
var deferred = $q . defer ( ) ,
639
642
promise = deferred . promise ,
640
643
cache ,
641
- cachedResp ;
644
+ cachedResp ,
645
+ url = buildUrl ( config . url , config . params ) ;
642
646
643
647
$http . pendingRequests . push ( config ) ;
644
648
promise . then ( removePendingReq , removePendingReq ) ;
@@ -649,7 +653,7 @@ function $HttpProvider() {
649
653
}
650
654
651
655
if ( cache ) {
652
- cachedResp = cache . get ( config . url ) ;
656
+ cachedResp = cache . get ( url ) ;
653
657
if ( cachedResp ) {
654
658
if ( cachedResp . then ) {
655
659
// cached request has already been sent, but there is no response yet
@@ -665,13 +669,13 @@ function $HttpProvider() {
665
669
}
666
670
} else {
667
671
// put the promise for the non-transformed response into cache as a placeholder
668
- cache . put ( config . url , promise ) ;
672
+ cache . put ( url , promise ) ;
669
673
}
670
674
}
671
675
672
676
// if we won't have the response in cache, send the request to the backend
673
677
if ( ! cachedResp ) {
674
- $httpBackend ( config . method , config . url , reqData , done , reqHeaders , config . timeout ) ;
678
+ $httpBackend ( config . method , url , reqData , done , reqHeaders , config . timeout ) ;
675
679
}
676
680
677
681
return promise ;
@@ -686,10 +690,10 @@ function $HttpProvider() {
686
690
function done ( status , response , headersString ) {
687
691
if ( cache ) {
688
692
if ( isSuccess ( status ) ) {
689
- cache . put ( config . url , [ status , response , parseHeaders ( headersString ) ] ) ;
693
+ cache . put ( url , [ status , response , parseHeaders ( headersString ) ] ) ;
690
694
} else {
691
695
// remove promise from the cache
692
- cache . remove ( config . url ) ;
696
+ cache . remove ( url ) ;
693
697
}
694
698
}
695
699
@@ -719,5 +723,21 @@ function $HttpProvider() {
719
723
if ( idx !== - 1 ) $http . pendingRequests . splice ( idx , 1 ) ;
720
724
}
721
725
}
726
+
727
+
728
+ function buildUrl ( url , params ) {
729
+ if ( ! params ) return url ;
730
+ var parts = [ ] ;
731
+ forEachSorted ( params , function ( value , key ) {
732
+ if ( value == null || value == undefined ) return ;
733
+ if ( isObject ( value ) ) {
734
+ value = toJson ( value ) ;
735
+ }
736
+ parts . push ( encodeURIComponent ( key ) + '=' + encodeURIComponent ( value ) ) ;
737
+ } ) ;
738
+ return url + ( ( url . indexOf ( '?' ) == - 1 ) ? '?' : '&' ) + parts . join ( '&' ) ;
739
+ }
740
+
741
+
722
742
} ] ;
723
743
}
0 commit comments