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

Commit af89daf

Browse files
committed
feat(ngResource): support all $http.config actions
This allows the transformation of the $http request in both directions, headers, caching, and timeout.
1 parent 224d7d6 commit af89daf

File tree

2 files changed

+60
-17
lines changed

2 files changed

+60
-17
lines changed

src/ngResource/resource.js

+38-17
Original file line numberDiff line numberDiff line change
@@ -37,24 +37,40 @@
3737
* the data object (useful for non-GET operations).
3838
*
3939
* @param {Object.<Object>=} actions Hash with declaration of custom action that should extend the
40-
* default set of resource actions. The declaration should be created in the following format:
40+
* default set of resource actions. The declaration should be created in the format of {@link
41+
* ng.$http#Parameters $http.config}:
4142
*
42-
* {action1: {method:?, params:?, isArray:?, headers:?},
43-
* action2: {method:?, params:?, isArray:?, headers:?},
43+
* {action1: {method:?, params:?, isArray:?, headers:?, ...},
44+
* action2: {method:?, params:?, isArray:?, headers:?, ...},
4445
* ...}
4546
*
4647
* Where:
4748
*
48-
* - `action` – {string} – The name of action. This name becomes the name of the method on your
49+
* - **`action`** – {string} – The name of action. This name becomes the name of the method on your
4950
* resource object.
50-
* - `method` – {string} – HTTP request method. Valid methods are: `GET`, `POST`, `PUT`, `DELETE`,
51-
* and `JSONP`
52-
* - `params` – {Object=} – Optional set of pre-bound parameters for this action. If any of the
53-
* parameter value is a function, it will be executed every time when a param value needs to be
54-
* obtained for a request (unless the param was overriden).
55-
* - isArray – {boolean=} – If true then the returned object for this action is an array, see
51+
* - **`method`** – {string} – HTTP request method. Valid methods are: `GET`, `POST`, `PUT`, `DELETE`,
52+
* and `JSONP`.
53+
* - **`params`** – {Object=} – Optional set of pre-bound parameters for this action. If any of the
54+
* parameter value is a function, it will be executed every time when a param value needs to be
55+
* obtained for a request (unless the param was overriden).
56+
* - **`isArray`** – {boolean=} – If true then the returned object for this action is an array, see
5657
* `returns` section.
57-
* - `headers` – {Object=} – Optional HTTP headers to send
58+
* - **`transformRequest`** – `{function(data, headersGetter)|Array.<function(data, headersGetter)>}` –
59+
* transform function or an array of such functions. The transform function takes the http
60+
* request body and headers and returns its transformed (typically serialized) version.
61+
* - **`transformResponse`** – `{function(data, headersGetter)|Array.<function(data, headersGetter)>}` –
62+
* transform function or an array of such functions. The transform function takes the http
63+
* response body and headers and returns its transformed (typically deserialized) version.
64+
* - **`cache`** – `{boolean|Cache}` – If true, a default $http cache will be used to cache the
65+
* GET request, otherwise if a cache instance built with
66+
* {@link ng.$cacheFactory $cacheFactory}, this cache will be used for
67+
* caching.
68+
* - **`timeout`** – `{number}` – timeout in milliseconds.
69+
* - **`withCredentials`** - `{boolean}` - whether to to set the `withCredentials` flag on the
70+
* XHR object. See {@link https://developer.mozilla.org/en/http_access_control#section_5
71+
* requests with credentials} for more information.
72+
* - **`responseType`** - `{string}` - see {@link
73+
* https://developer.mozilla.org/en-US/docs/DOM/XMLHttpRequest#responseType requestType}.
5874
*
5975
* @returns {Object} A resource "class" object with methods for the default set of resource actions
6076
* optionally extended with custom `actions`. The default set contains these actions:
@@ -374,12 +390,17 @@ angular.module('ngResource', ['ng']).
374390
}
375391

376392
var value = this instanceof Resource ? this : (action.isArray ? [] : new Resource(data));
377-
$http({
378-
method: action.method,
379-
url: route.url(extend({}, extractParams(data, action.params || {}), params)),
380-
data: data,
381-
headers: extend({}, action.headers || {})
382-
}).then(function(response) {
393+
var httpConfig = {};
394+
395+
forEach(action, function(value, key) {
396+
if (key != 'params' && key != 'isArray' ) {
397+
httpConfig[key] = copy(value);
398+
}
399+
});
400+
httpConfig.data = data;
401+
httpConfig.url = route.url(extend({}, extractParams(data, action.params || {}), params))
402+
403+
$http(httpConfig).then(function(response) {
383404
var data = response.data;
384405

385406
if (data) {

test/ngResource/resourceSpec.js

+22
Original file line numberDiff line numberDiff line change
@@ -427,4 +427,26 @@ describe("resource", function() {
427427
expect(callback).not.toHaveBeenCalled();
428428
});
429429
});
430+
431+
432+
it('should transform request/response', function() {
433+
var Person = $resource('/Person/:id', {}, {
434+
save: {
435+
method: 'POST',
436+
params: {id: '@id'},
437+
transformRequest: function(data) {
438+
return angular.toJson({ __id: data.id });
439+
},
440+
transformResponse: function(data) {
441+
return { id: data.__id };
442+
}
443+
}
444+
});
445+
446+
$httpBackend.expect('POST', '/Person/123', { __id: 123 }).respond({ __id: 456 });
447+
var person = new Person({id:123});
448+
person.$save();
449+
$httpBackend.flush();
450+
expect(person.id).toEqual(456);
451+
});
430452
});

0 commit comments

Comments
 (0)