Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit fbdab51

Browse files
maxmartmhevery
authored andcommitted
feat($resource): support custom headers per action
Closes #736
1 parent f2b7fff commit fbdab51

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

src/ngResource/resource.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@
3636
* @param {Object.<Object>=} actions Hash with declaration of custom action that should extend the
3737
* default set of resource actions. The declaration should be created in the following format:
3838
*
39-
* {action1: {method:?, params:?, isArray:?},
40-
* action2: {method:?, params:?, isArray:?},
39+
* {action1: {method:?, params:?, isArray:?, headers:?},
40+
* action2: {method:?, params:?, isArray:?, headers:?},
4141
* ...}
4242
*
4343
* Where:
@@ -49,6 +49,7 @@
4949
* - `params` – {object=} – Optional set of pre-bound parameters for this action.
5050
* - isArray – {boolean=} – If true then the returned object for this action is an array, see
5151
* `returns` section.
52+
* - `headers` – {object=} – Optional HTTP headers to send
5253
*
5354
* @returns {Object} A resource "class" object with methods for the default set of resource actions
5455
* optionally extended with custom `actions`. The default set contains these actions:
@@ -130,7 +131,7 @@
130131
* The object returned from this function execution is a resource "class" which has "static" method
131132
* for each action in the definition.
132133
*
133-
* Calling these methods invoke `$http` on the `url` template with the given `method` and `params`.
134+
* Calling these methods invoke `$http` on the `url` template with the given `method`, `params` and `headers`.
134135
* When the data is returned from the server then the object is an instance of the resource type and
135136
* all of the non-GET methods are available with `$` prefix. This allows you to easily support CRUD
136137
* operations (create, read, update, delete) on server-side data.
@@ -362,7 +363,8 @@ angular.module('ngResource', ['ng']).
362363
$http({
363364
method: action.method,
364365
url: route.url(extend({}, extractParams(data), action.params || {}, params)),
365-
data: data
366+
data: data,
367+
headers: extend({}, action.headers || {})
366368
}).then(function(response) {
367369
var data = response.data;
368370

test/ngResource/resourceSpec.js

+16
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,14 @@ describe("resource", function() {
1414
},
1515
patch: {
1616
method: 'PATCH'
17+
},
18+
conditionalPut: {
19+
method: 'PUT',
20+
headers: {
21+
'If-None-Match': '*'
22+
}
1723
}
24+
1825
});
1926
callback = jasmine.createSpy();
2027
}));
@@ -146,6 +153,15 @@ describe("resource", function() {
146153
});
147154

148155

156+
it('should send correct headers', function() {
157+
$httpBackend.expectPUT('/CreditCard/123', undefined, function(headers) {
158+
return headers['If-None-Match'] == "*";
159+
}).respond({id:123});
160+
161+
CreditCard.conditionalPut({id: {key:123}});
162+
});
163+
164+
149165
it("should read partial resource", function() {
150166
$httpBackend.expect('GET', '/CreditCard').respond([{id:{key:123}}]);
151167
var ccs = CreditCard.query();

0 commit comments

Comments
 (0)