Skip to content

Commit 4909feb

Browse files
author
guylabs
committed
Return resource objects with parameters instead of an array with the name of the resource.
1 parent 441a36b commit 4909feb

File tree

4 files changed

+131
-13
lines changed

4 files changed

+131
-13
lines changed

README.md

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ The `_resources` method takes the following four parameters:
145145

146146
The `_resources` method returns the *Angular* resource "class" object with methods for the default set of resource actions. Read more [here](https://docs.angularjs.org/api/ngResource/service/$resource).
147147

148-
If no parameter is given the `_resources` method will return all available resources on the given object. When for example the following JSON response object is given:
148+
If no parameter is given the `_resources` method will return all available resources objects of the given object. When for example the following JSON response object is given:
149149

150150
```javascript
151151
var response = {
@@ -165,13 +165,31 @@ var response = {
165165
}
166166
var processedResponse = new SpringDataRestAdapter(response);
167167
```
168-
Then the following call to the `_resources` method without any parameter will return an array of all the resources available.
168+
Then the following call to the `_resources` method without any parameter will return an array of all available resource objects.
169169

170170
```javascript
171171
var availableResources = processedResponse._resources();
172172
```
173173

174-
The above call will result in the following return value: `['self', 'parentCategory']`. This functionality is useful if you want to first check all available resources before using the `_resources` method to retrieve the specific resource.
174+
The above call will result in the following return value:
175+
176+
```json
177+
[
178+
{
179+
"name":"self",
180+
"parameters": {
181+
page: undefined,
182+
size: undefined,
183+
sort: undefined
184+
}
185+
}
186+
{
187+
"name":"parentCategory"
188+
}
189+
]
190+
```
191+
192+
This functionality is useful if you want to first check all available resources before using the `_resources` method to retrieve the specific resource.
175193

176194
##### Example
177195

src/angular-spring-data-rest.js

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,14 @@
117117
* @param {object} paramDefaults optional $resource method parameter defaults
118118
* @param {object} actions optional $resource method actions
119119
* @param {object} options additional $resource method options
120-
* @returns {object|array} the result of the $resource method or the available resources as an array
120+
* @returns {object} the result of the $resource method or the available resources as a resource object array
121121
*
122122
* @see https://docs.angularjs.org/api/ngResource/service/$resource
123123
*/
124124
var resources = function (resourceObject, paramDefaults, actions, options) {
125+
var resources = this[config.links.key];
126+
127+
// if a resource object is given process it
125128
if (angular.isObject(resourceObject)) {
126129
if (!resourceObject.name) {
127130
throw new Error("The provided resource object must contain a name property.");
@@ -146,18 +149,22 @@
146149

147150
return callBackend(extractUrl(this, resourceObject.name), parameters, actions, options);
148151

149-
} else if (resourceObject in this[config.links.key]) {
152+
} else if (resourceObject in resources) {
150153
// get the url out of the resource name and return the backend function
151154
return callBackend(extractUrl(this, resourceObject), paramDefaults, actions, options);
152155
}
153156

154-
// return the available resources if the resource object is not set
155-
var resources = [];
156-
angular.forEach(this[config.links.key], function (value, key) {
157-
resources.push(key);
157+
// return the available resources as resource object array if the resource object parameter is not set
158+
var availableResources = [];
159+
angular.forEach(resources, function (value, key) {
160+
if (value.templated) {
161+
var templateParameters = extractTemplateParameters(value[config.hrefKey]);
162+
availableResources.push({"name": key, "parameters": templateParameters});
163+
} else {
164+
availableResources.push({"name": key});
165+
}
158166
});
159-
return resources;
160-
167+
return availableResources;
161168
};
162169

163170
/**
@@ -185,6 +192,27 @@
185192
return url.replace(/{.*}/g, '');
186193
};
187194

195+
/**
196+
* Returns the template parameters of the given url as array. e.g. from this url
197+
* 'http://localhost:8080/categories{?page,size,sort}' it will return the following array:
198+
* ['page', 'size', 'sort']
199+
*
200+
* @param {string} url the url with the template parameters
201+
* @returns {object} the array containing the template parameters
202+
*/
203+
var extractTemplateParameters = function (url) {
204+
var templateParametersObject = {};
205+
206+
var regexp = /{\?(.*)}/g;
207+
var templateParametersArray = regexp.exec(url)[1].split(',');
208+
209+
angular.forEach(templateParametersArray, function (value) {
210+
templateParametersObject[value] = undefined;
211+
});
212+
213+
return templateParametersObject;
214+
};
215+
188216
/**
189217
* The actual adapter method which processes the given JSON data object and adds
190218
* the wrapped resource property to all embedded elements where resources are available.

test/angular-spring-data-rest._test-helper.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,3 +207,20 @@ var mockIndexData = function () {
207207
);
208208
};
209209

210+
var mockWithoutTemplateParametersData = function () {
211+
return angular.copy(
212+
{
213+
"_links": {
214+
"users": {
215+
"href": "http://localhost:8080/users",
216+
"templated": false
217+
},
218+
"categories": {
219+
"href": "http://localhost:8080/categories",
220+
"templated": false
221+
}
222+
}
223+
}
224+
);
225+
};
226+

test/angular-spring-data-rest.resource.test.js

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,36 @@ describe("the resources property", function () {
119119
expect(this.response[this.config.resourcesKey]).toBeDefined();
120120

121121
// expect that the resource method returns the available resources on the object
122-
expect(this.response[this.config.resourcesKey]()).toEqual(['self']);
122+
expect(this.response[this.config.resourcesKey]()).toEqual([
123+
{
124+
name: 'self',
125+
parameters: {
126+
page: undefined,
127+
size: undefined,
128+
sort: undefined
129+
}
130+
}
131+
]
132+
);
133+
});
134+
135+
it("must return the resources of the object with empty parameters if the templated property is false", function () {
136+
this.rawResponse = mockWithoutTemplateParametersData();
137+
this.response = new SpringDataRestAdapter(this.rawResponse);
138+
139+
// expect that the resources method is present
140+
expect(this.response[this.config.resourcesKey]).toBeDefined();
141+
142+
// expect that the resource method returns the available resources on the object
143+
expect(this.response[this.config.resourcesKey]()).toEqual([
144+
{
145+
name: 'users'
146+
},
147+
{
148+
name: 'categories'
149+
}
150+
]
151+
);
123152
});
124153

125154
it("must return the resources of the index response", function () {
@@ -130,7 +159,33 @@ describe("the resources property", function () {
130159
expect(this.response[this.config.resourcesKey]).toBeDefined();
131160

132161
// expect that the resource method returns the available resources on the index response
133-
expect(this.response[this.config.resourcesKey]()).toEqual(['users', 'categories', 'accounts']);
162+
expect(this.response[this.config.resourcesKey]()).toEqual([
163+
{
164+
name: 'users',
165+
parameters: {
166+
page: undefined,
167+
size: undefined,
168+
sort: undefined
169+
}
170+
},
171+
{
172+
name: 'categories',
173+
parameters: {
174+
page: undefined,
175+
size: undefined,
176+
sort: undefined
177+
}
178+
},
179+
{
180+
name: 'accounts',
181+
parameters: {
182+
page: undefined,
183+
size: undefined,
184+
sort: undefined
185+
}
186+
}
187+
]
188+
);
134189
});
135190

136191
});

0 commit comments

Comments
 (0)