GeezerVison is showing github.com — feat($resource): support custom headers per action · angular/angular.js@fbdab51
This repository was archived by the owner on Apr 12, 2024. It is now read-only.
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: src/ngResource/resource.js +6-4Lines changed: 6 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 36 | 36 | * @param {Object.<Object>=} actions Hash with declaration of custom action that should extend the | 37 | 37 | * default set of resource actions. The declaration should be created in the following format: | 38 | 38 | * | 39 | | - * {action1: {method:?, params:?, isArray:?}, | 40 | | - * action2: {method:?, params:?, isArray:?}, | | 39 | + * {action1: {method:?, params:?, isArray:?, headers:?}, | | 40 | + * action2: {method:?, params:?, isArray:?, headers:?}, | 41 | 41 | * ...} | 42 | 42 | * | 43 | 43 | * Where: | | 49 | 49 | * - `params` – {object=} – Optional set of pre-bound parameters for this action. | 50 | 50 | * - isArray – {boolean=} – If true then the returned object for this action is an array, see | 51 | 51 | * `returns` section. | | 52 | + * - `headers` – {object=} – Optional HTTP headers to send | 52 | 53 | * | 53 | 54 | * @returns {Object} A resource "class" object with methods for the default set of resource actions | 54 | 55 | * optionally extended with custom `actions`. The default set contains these actions: | | 130 | 131 | * The object returned from this function execution is a resource "class" which has "static" method | 131 | 132 | * for each action in the definition. | 132 | 133 | * | 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`. | 134 | 135 | * When the data is returned from the server then the object is an instance of the resource type and | 135 | 136 | * all of the non-GET methods are available with `$` prefix. This allows you to easily support CRUD | 136 | 137 | * operations (create, read, update, delete) on server-side data. | @@ -362,7 +363,8 @@ angular.module('ngResource', ['ng']). | 362 | 363 | $http({ | 363 | 364 | method: action.method, | 364 | 365 | url: route.url(extend({}, extractParams(data), action.params || {}, params)), | 365 | | - data: data | | 366 | + data: data, | | 367 | + headers: extend({}, action.headers || {}) | 366 | 368 | }).then(function(response) { | 367 | 369 | var data = response.data; | 368 | 370 |
| |
Collapse file Copy file name to clipboardExpand all lines: test/ngResource/resourceSpec.js +16Lines changed: 16 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -14,7 +14,14 @@ describe("resource", function() { | 14 | 14 | }, | 15 | 15 | patch: { | 16 | 16 | method: 'PATCH' | | 17 | + }, | | 18 | + conditionalPut: { | | 19 | + method: 'PUT', | | 20 | + headers: { | | 21 | + 'If-None-Match': '*' | | 22 | + } | 17 | 23 | } | | 24 | + | 18 | 25 | }); | 19 | 26 | callback = jasmine.createSpy(); | 20 | 27 | })); | @@ -146,6 +153,15 @@ describe("resource", function() { | 146 | 153 | }); | 147 | 154 |
| 148 | 155 |
| | 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 | + | 149 | 165 | it("should read partial resource", function() { | 150 | 166 | $httpBackend.expect('GET', '/CreditCard').respond([{id:{key:123}}]); | 151 | 167 | var ccs = CreditCard.query(); | |
|