Skip to content

Commit 9b5c3a3

Browse files
committed
Cleaned configuration
1 parent 71eab22 commit 9b5c3a3

File tree

5 files changed

+392
-370
lines changed

5 files changed

+392
-370
lines changed

src/configuration.js

Lines changed: 1 addition & 350 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,11 @@
11
/**
22
* Those are HTTP safe methods for which there is no need to pass any data with the request.
33
*/
4-
var safeMethods = ['get', 'head', 'options', 'trace', 'getlist'];
5-
function isSafeOperation(operation) {
6-
return _.contains(safeMethods, operation.toLowerCase());
7-
};
8-
9-
10-
var utils = {
11-
isSafe: isSafe
12-
};
13-
144
// Configuration
155
var Configurer = {};
166
Configurer.init = function (object, config) {
177
object.configuration = config;
188

19-
var absolutePattern = /^https?:\/\//i;
20-
config.isAbsoluteUrl = function (string) {
21-
return _.isUndefined(config.absoluteUrl) || _.isNull(config.absoluteUrl) ?
22-
string && absolutePattern.test(string) :
23-
config.absoluteUrl;
24-
};
25-
269
config.absoluteUrl = _.isUndefined(config.absoluteUrl) ? true : config.absoluteUrl;
2710
object.setSelfLinkAbsoluteUrl = function (value) {
2811
config.absoluteUrl = value;
@@ -121,13 +104,6 @@ Configurer.init = function (object, config) {
121104
config.jsonp = active;
122105
};
123106

124-
config.isOverridenMethod = function (method, values) {
125-
var search = values || config.methodOverriders;
126-
return !_.isUndefined(_.find(search, function (one) {
127-
return one.toLowerCase() === method.toLowerCase();
128-
}));
129-
};
130-
131107
/**
132108
* Sets the URL creator type. For now, only Path is created. In the future we'll have queryParams
133109
**/
@@ -173,66 +149,12 @@ Configurer.init = function (object, config) {
173149
return this;
174150
};
175151

176-
config.isRestangularized = function (obj) {
177-
return !!obj[config.restangularFields.restangularized];
178-
};
179-
180-
config.setFieldToElem = function (field, elem, value) {
181-
var properties = field.split('.');
182-
var idValue = elem;
183-
_.each(_.initial(properties), function (prop) {
184-
idValue[prop] = {};
185-
idValue = idValue[prop];
186-
});
187-
idValue[_.last(properties)] = value;
188-
return this;
189-
};
190-
191-
config.getFieldFromElem = function (field, elem) {
192-
var properties = field.split('.');
193-
var idValue = elem;
194-
_.each(properties, function (prop) {
195-
if (idValue) {
196-
idValue = idValue[prop];
197-
}
198-
});
199-
return angular.copy(idValue);
200-
};
201-
202-
config.setIdToElem = function (elem, id /*, route */) {
203-
config.setFieldToElem(config.restangularFields.id, elem, id);
204-
return this;
205-
};
206-
207-
config.getIdFromElem = function (elem) {
208-
return config.getFieldFromElem(config.restangularFields.id, elem);
209-
};
210-
211-
config.isValidId = function (elemId) {
212-
return '' !== elemId && !_.isUndefined(elemId) && !_.isNull(elemId);
213-
};
214-
215-
config.setUrlToElem = function (elem, url /*, route */) {
216-
config.setFieldToElem(config.restangularFields.selfLink, elem, url);
217-
return this;
218-
};
219-
220-
config.getUrlFromElem = function (elem) {
221-
return config.getFieldFromElem(config.restangularFields.selfLink, elem);
222-
};
223-
224152
config.useCannonicalId = _.isUndefined(config.useCannonicalId) ? false : config.useCannonicalId;
225153
object.setUseCannonicalId = function (value) {
226154
config.useCannonicalId = value;
227155
return this;
228156
};
229157

230-
config.getCannonicalIdFromElem = function (elem) {
231-
var cannonicalId = elem[config.restangularFields.cannonicalId];
232-
var actualId = config.isValidId(cannonicalId) ? cannonicalId : config.getIdFromElem(elem);
233-
return actualId;
234-
};
235-
236158
/**
237159
* Sets the Response parser. This is used in case your response isn't directly the data.
238160
* For example if you have a response like {meta: {'meta'}, data: {name: 'Gonto'}}
@@ -418,20 +340,6 @@ Configurer.init = function (object, config) {
418340
return object.addElementTransformer(route, false, fn);
419341
};
420342

421-
config.transformElem = function (elem, isCollection, route, Restangular, force) {
422-
if (!force && !config.transformLocalElements && !elem[config.restangularFields.fromServer]) {
423-
return elem;
424-
}
425-
var typeTransformers = config.transformers[route];
426-
var changedElem = elem;
427-
if (typeTransformers) {
428-
_.each(typeTransformers, function (transformer) {
429-
changedElem = transformer(isCollection, changedElem);
430-
});
431-
}
432-
return config.onElemRestangularized(changedElem, isCollection, route, Restangular);
433-
};
434-
435343
config.transformLocalElements = _.isUndefined(config.transformLocalElements) ?
436344
false :
437345
config.transformLocalElements;
@@ -450,263 +358,6 @@ Configurer.init = function (object, config) {
450358
//Internal values and functions
451359
config.urlCreatorFactory = {};
452360

453-
/**
454-
* Base URL Creator. Base prototype for everything related to it
455-
**/
456-
457-
var BaseCreator = function () {
458-
};
459-
460-
BaseCreator.prototype.setConfig = function (config) {
461-
this.config = config;
462-
return this;
463-
};
464-
465-
BaseCreator.prototype.parentsArray = function (current) {
466-
var parents = [];
467-
while (current) {
468-
parents.push(current);
469-
current = current[this.config.restangularFields.parentResource];
470-
}
471-
return parents.reverse();
472-
};
473-
474-
function RestangularResource(config, $http, url, configurer) {
475-
var resource = {};
476-
_.each(_.keys(configurer), function (key) {
477-
var value = configurer[key];
478-
479-
// Add default parameters
480-
value.params = _.extend({}, value.params, config.defaultRequestParams[value.method.toLowerCase()]);
481-
// We don't want the ? if no params are there
482-
if (_.isEmpty(value.params)) {
483-
delete value.params;
484-
}
485-
486-
if (utils.isSafeOperation(value.method)) {
487-
488-
resource[key] = function () {
489-
return $http(_.extend(value, {
490-
url: url
491-
}));
492-
};
493-
494-
} else {
495-
496-
resource[key] = function (data) {
497-
return $http(_.extend(value, {
498-
url: url,
499-
data: data
500-
}));
501-
};
502-
503-
}
504-
});
505-
506-
return resource;
507-
}
508-
509-
BaseCreator.prototype.resource = function (current, $http, localHttpConfig, callHeaders, callParams, what, etag, operation) {
510-
511-
var params = _.defaults(callParams || {}, this.config.defaultRequestParams.common);
512-
var headers = _.defaults(callHeaders || {}, this.config.defaultHeaders);
513-
514-
if (etag) {
515-
if (!utils.isSafeOperation(operation)) {
516-
headers['If-Match'] = etag;
517-
} else {
518-
headers['If-None-Match'] = etag;
519-
}
520-
}
521-
522-
var url = this.base(current);
523-
524-
if (what) {
525-
var add = '';
526-
if (!/\/$/.test(url)) {
527-
add += '/';
528-
}
529-
add += what;
530-
url += add;
531-
}
532-
533-
if (this.config.suffix &&
534-
url.indexOf(this.config.suffix, url.length - this.config.suffix.length) === -1 && !this.config.getUrlFromElem(current)) {
535-
url += this.config.suffix;
536-
}
537-
538-
current[this.config.restangularFields.httpConfig] = undefined;
539-
540-
return RestangularResource(this.config, $http, url, {
541-
getList: this.config.withHttpValues(localHttpConfig,
542-
{method: 'GET',
543-
params: params,
544-
headers: headers}),
545-
546-
get: this.config.withHttpValues(localHttpConfig,
547-
{method: 'GET',
548-
params: params,
549-
headers: headers}),
550-
551-
jsonp: this.config.withHttpValues(localHttpConfig,
552-
{method: 'jsonp',
553-
params: params,
554-
headers: headers}),
555-
556-
put: this.config.withHttpValues(localHttpConfig,
557-
{method: 'PUT',
558-
params: params,
559-
headers: headers}),
560-
561-
post: this.config.withHttpValues(localHttpConfig,
562-
{method: 'POST',
563-
params: params,
564-
headers: headers}),
565-
566-
remove: this.config.withHttpValues(localHttpConfig,
567-
{method: 'DELETE',
568-
params: params,
569-
headers: headers}),
570-
571-
head: this.config.withHttpValues(localHttpConfig,
572-
{method: 'HEAD',
573-
params: params,
574-
headers: headers}),
575-
576-
trace: this.config.withHttpValues(localHttpConfig,
577-
{method: 'TRACE',
578-
params: params,
579-
headers: headers}),
580-
581-
options: this.config.withHttpValues(localHttpConfig,
582-
{method: 'OPTIONS',
583-
params: params,
584-
headers: headers}),
585-
586-
patch: this.config.withHttpValues(localHttpConfig,
587-
{method: 'PATCH',
588-
params: params,
589-
headers: headers})
590-
});
591-
};
592-
593-
/**
594-
* This is the Path URL creator. It uses Path to show Hierarchy in the Rest API.
595-
* This means that if you have an Account that then has a set of Buildings, a URL to a building
596-
* would be /accounts/123/buildings/456
597-
**/
598-
var Path = function () {
599-
};
600-
601-
Path.prototype = new BaseCreator();
602-
603-
Path.prototype.base = function (current) {
604-
var __this = this;
605-
return _.reduce(this.parentsArray(current), function (acum, elem) {
606-
var elemUrl;
607-
var elemSelfLink = __this.config.getUrlFromElem(elem);
608-
if (elemSelfLink) {
609-
if (__this.config.isAbsoluteUrl(elemSelfLink)) {
610-
return elemSelfLink;
611-
} else {
612-
elemUrl = elemSelfLink;
613-
}
614-
} else {
615-
elemUrl = elem[__this.config.restangularFields.route];
616-
617-
if (elem[__this.config.restangularFields.restangularCollection]) {
618-
var ids = elem[__this.config.restangularFields.ids];
619-
if (ids) {
620-
elemUrl += '/' + ids.join(',');
621-
}
622-
} else {
623-
var elemId;
624-
if (__this.config.useCannonicalId) {
625-
elemId = __this.config.getCannonicalIdFromElem(elem);
626-
} else {
627-
elemId = __this.config.getIdFromElem(elem);
628-
}
629-
630-
if (config.isValidId(elemId) && !elem.singleOne) {
631-
elemUrl += '/' + (__this.config.encodeIds ? encodeURIComponent(elemId) : elemId);
632-
}
633-
}
634-
}
635-
636-
return acum.replace(/\/$/, '') + '/' + elemUrl;
637-
638-
}, this.config.baseUrl);
639-
};
640-
641-
642-
Path.prototype.fetchUrl = function (current, what) {
643-
var baseUrl = this.base(current);
644-
if (what) {
645-
baseUrl += '/' + what;
646-
}
647-
return baseUrl;
648-
};
649-
650-
Path.prototype.fetchRequestedUrl = function (current, what) {
651-
var url = this.fetchUrl(current, what);
652-
var params = current[config.restangularFields.reqParams];
653-
654-
// From here on and until the end of fetchRequestedUrl,
655-
// the code has been kindly borrowed from angular.js
656-
// The reason for such code bloating is coherence:
657-
// If the user were to use this for cache management, the
658-
// serialization of parameters would need to be identical
659-
// to the one done by angular for cache keys to match.
660-
function sortedKeys(obj) {
661-
var keys = [];
662-
for (var key in obj) {
663-
if (obj.hasOwnProperty(key)) {
664-
keys.push(key);
665-
}
666-
}
667-
return keys.sort();
668-
}
669-
670-
function forEachSorted(obj, iterator, context) {
671-
var keys = sortedKeys(obj);
672-
for (var i = 0; i < keys.length; i++) {
673-
iterator.call(context, obj[keys[i]], keys[i]);
674-
}
675-
return keys;
676-
}
677-
678-
function encodeUriQuery(val, pctEncodeSpaces) {
679-
return encodeURIComponent(val).
680-
replace(/%40/gi, '@').
681-
replace(/%3A/gi, ':').
682-
replace(/%24/g, '$').
683-
replace(/%2C/gi, ',').
684-
replace(/%20/g, (pctEncodeSpaces ? '%20' : '+'));
685-
}
686-
687-
if (!params) {
688-
return url;
689-
}
690-
691-
var parts = [];
692-
forEachSorted(params, function (value, key) {
693-
if (value === null || value === undefined) {
694-
return;
695-
}
696-
if (!angular.isArray(value)) {
697-
value = [value];
698-
}
699-
700-
angular.forEach(value, function (v) {
701-
if (angular.isObject(v)) {
702-
v = angular.toJson(v);
703-
}
704-
parts.push(encodeUriQuery(key) + '=' + encodeUriQuery(v));
705-
});
706-
});
707-
708-
return url + (this.config.suffix || '') + ((url.indexOf('?') === -1) ? '?' : '&') + parts.join('&');
709-
};
710-
361+
// Setting different url creators from urlHandler.js
711362
config.urlCreatorFactory.path = Path;
712363
};

0 commit comments

Comments
 (0)