@meanie/angular-api
Advanced tools
Comparing version 2.1.0 to 3.0.0
{ | ||
"name": "@meanie/angular-api", | ||
"description": "An Angular service for interaction with API's", | ||
"version": "2.1.0", | ||
"version": "3.0.0", | ||
"homepage": "https://github.com/meanie/angular-api", | ||
@@ -6,0 +6,0 @@ "author": { |
@@ -7,3 +7,3 @@ /** | ||
*/ | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; }; | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; | ||
@@ -116,2 +116,7 @@ (function (window, angular, undefined) { | ||
//No data? | ||
if (!data || data === null) { | ||
return null; | ||
} | ||
//Get model class and return model instance | ||
@@ -143,4 +148,9 @@ var Model = $injector.get(this.model); | ||
//Initialize if empty | ||
return response.data || (expectsArray ? [] : {}); | ||
//Empty array if no data sent | ||
if (expectsArray && !response.data) { | ||
return []; | ||
} | ||
//Sent data as is (also if null) | ||
return response.data; | ||
}; | ||
@@ -324,3 +334,3 @@ | ||
})(window, window.angular); | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; }; | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; | ||
@@ -369,13 +379,7 @@ (function (window, angular, undefined) { | ||
if (angular.isArray(obj[key])) { | ||
var _ret = function () { | ||
var arr = obj[key]; | ||
//eslint-disable-next-line no-unused-vars | ||
return { | ||
v: arr.map(function (value, key) { | ||
return copyProperty(arr, key); | ||
}) | ||
}; | ||
}(); | ||
if ((typeof _ret === 'undefined' ? 'undefined' : _typeof(_ret)) === "object") return _ret.v; | ||
var arr = obj[key]; | ||
//eslint-disable-next-line no-unused-vars | ||
return arr.map(function (value, key) { | ||
return copyProperty(arr, key); | ||
}); | ||
} | ||
@@ -868,2 +872,3 @@ if (obj[key] && angular.isFunction(obj[key].clone)) { | ||
//Loop the valid URL params now | ||
//eslint-disable-next-line no-unused-vars | ||
angular.forEach(urlParams, function (t, urlParam) { | ||
@@ -877,9 +882,8 @@ | ||
if (angular.isDefined(val) && val !== null) { | ||
(function () { | ||
var encodedVal = $url.encodeUriSegment(val); | ||
regex = new RegExp(':' + urlParam + '(\\W|$)', 'g'); | ||
url = url.replace(regex, function (match, tail) { | ||
return encodedVal + tail; | ||
}); | ||
})(); | ||
var encodedVal = $url.encodeUriSegment(val); | ||
regex = new RegExp(':' + urlParam + '(\\W|$)', 'g'); | ||
//eslint-disable-next-line no-unused-vars | ||
url = url.replace(regex, function (match, tail) { | ||
return encodedVal + tail; | ||
}); | ||
} | ||
@@ -889,3 +893,4 @@ | ||
else { | ||
regex = new RegExp('(\/?):' + urlParam + '(\\W|$)', 'g'); | ||
regex = new RegExp('(/?):' + urlParam + '(\\W|$)', 'g'); | ||
//eslint-disable-next-line no-unused-vars | ||
url = url.replace(regex, function (match, leadingSlashes, tail) { | ||
@@ -987,6 +992,3 @@ if (tail.charAt(0) === '/') { | ||
var request = createRequestConfig(action, params, data, config); | ||
var promise = $http(request).then(action.successInterceptor.bind(action), action.errorInterceptor.bind(action)); | ||
//Then handle the raw data | ||
return promise.then(function (raw) { | ||
return $http(request).then(action.successInterceptor.bind(action)).catch(action.errorInterceptor.bind(action)).then(function (raw) { | ||
if (action.expectsModel()) { | ||
@@ -993,0 +995,0 @@ return action.convertToModel(raw); |
@@ -1,2 +0,2 @@ | ||
var _typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol?"symbol":typeof t};!function(t,n,e){"use strict";n.module("Api.Action.Service",["Url.Service"]).factory("$apiAction",["$q","$injector","$log","$url",function(t,e,r,i){function o(t){return t?e.has(t)?t:("$baseModel"!==t&&r.warn("Unknown model class/service:",t),null):null}function s(t,e){if(n.extend(this,t),this.url=i.concat(e.url,this.url||""),this.model=this.model||e.model||!1,this.method=this.method||"GET",this.enforceDataFormat=e.enforceDataFormat||!1,e.config)for(var r in e.config)e.config.hasOwnProperty(r)&&(this[r]=e.config[r]);"undefined"==typeof this.params&&(this.params=e.params),this.params=this.params||{},this.model=o(this.model)}return s.prototype.hasBody=function(){return/^(POST|PUT|PATCH)$/i.test(this.method)},s.prototype.expectsArray=function(){return Boolean(this.isArray)},s.prototype.expectsModel=function(){return this.model&&Boolean(this.isModel)},s.prototype.convertToModel=function(t){var r=this;if(n.isArray(t))return t.map(function(t){return r.convertToModel(t)});var i=e.get(this.model);return new i(t)},s.prototype.successInterceptor=function(t){var e=this.expectsArray(),i=n.isArray(t.data);return i!==e&&(r.warn("Expected",e?"array":"object","as response, got",i?"array":_typeof(t.data)),this.enforceDataFormat&&(t.data=e?[]:{})),t.data||(e?[]:{})},s.prototype.errorInterceptor=function(n){return t.reject(n)},function(t,n){return new s(t,n)}}])}(window,window.angular),function(t,n,e){"use strict";n.module("Api.Service",["Api.Endpoint.Service"]).provider("$api",function(){this.defaults={verbose:!1,enforceDataFormat:!1,baseUrl:"/",actions:{query:{method:"GET",isArray:!0,isModel:!0},get:{method:"GET",isModel:!0},create:{method:"POST"},update:{method:"PUT"},"delete":{method:"DELETE"}},params:{id:"@id"},model:"",config:{},stripTrailingSlashes:!0},this.endpoints={},this.setVerbose=function(t){return this.defaults.verbose=!!t,this},this.setBaseUrl=function(t){return this.defaults.baseUrl=t.replace(/:([0-9]+)/,"\\:$1"),this},this.setEnforceDataFormat=function(t){return this.defaults.enforceDataFormat=!!t,this},this.setDefaultActions=function(t){return this.defaults.actions=t||{},this},this.setDefaultParams=function(t){return this.defaults.params=t||{},this},this.setDefaultModel=function(t){return this.defaults.model=t||!1,this},this.setConfig=function(t,n){return this.defaults.config[t]=n,this},this.stripTrailingSlashes=function(t){return this.defaults.stripTrailingSlashes=!!t,this},this.registerEndpoint=function(t,n){return t&&(this.endpoints[t]=n||{}),this},this.$get=["$log","$apiEndpoint",function(t,e){var r=this,i=function(t){return this[t]};return n.forEach(this.endpoints,function(o,s){i[s]&&t.warn("API endpoint",s,"is being overwritten."),o=n.extend({},r.defaults,o),o.verbose&&t.info("API endpoint",s+":",o),i[s]=e(s,o)}),i}]})}(window,window.angular);var _typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol?"symbol":typeof t};!function(t,n,e){"use strict";n.module("BaseModel.Service",[]).factory("$baseModel",["$window","$log","$injector",function(t,e,r){function i(t){var n=/(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}).*/;if(t.match(n)){var e=a(t,a.ISO_8601,!0);if(e.isValid())return e}return null}function o(t,e){if(n.isArray(t[e])){var r=function(){var n=t[e];return{v:n.map(function(t,e){return o(n,e)})}}();if("object"===("undefined"==typeof r?"undefined":_typeof(r)))return r.v}return t[e]&&n.isFunction(t[e].clone)?t[e].clone():n.copy(t[e])}function s(t,n){var e=n;Object.defineProperty(this,"$parent",{enumerable:!1,get:function(){return e},set:function(t){e=t}}),this.fromJSON(t)}var a=void 0;return r.has("moment")?a=r.get("moment"):"undefined"!=typeof t.moment&&(a=t.moment),s.prototype.convertToModel=function(t,i,o){var a=this;if("boolean"==typeof i&&(o=i,i=null),"undefined"==typeof this[t]&&(o?this[t]=[]:this[t]=null),i&&this[t]){if("string"==typeof i){if(!r.has(i))return e.warn("Unknown model",i,"specified for sub model conversion");i=r.get(i)}n.isArray(this[t])?this[t]=this[t].map(function(t){return new i(t,a)}):n.isString(this[t])&&s.isId(this[t])?this[t]=new i({id:this[t]},this):this[t]=new i(this[t],this)}},s.prototype.fromJSON=function(t){var e=this;return n.isObject(t)&&n.forEach(t,function(t,n){e[n]=s.valueFromJSON(t)},this),this},s.prototype.toJSON=function(t){var e={};return t&&n.isObject(t)&&n.forEach(t,function(t,n){e[n]=s.valueToJSON(t)}),n.forEach(this,function(t,n){e.hasOwnProperty(n)||(e[n]=s.valueToJSON(t))}),e},s.prototype.extract=function(t){var e=this;if("string"==typeof t)return o(this,t);var r={};return n.isArray(t)&&t.length?n.forEach(t,function(t){r[t]=o(e,t)}):n.forEach(this,function(t,n){"$$"!==n.substr(0,2)&&(r[n]=o(e,n))}),r},s.prototype.merge=function(t){var e=this;return t&&n.isObject(t)&&n.forEach(t,function(n,r){e[r]=o(t,r)}),this},s.prototype.clear=function(){for(var t in this)this.hasOwnProperty(t)&&delete this[t];return this},s.prototype.clone=function(t){var n=this.constructor,e=new n(this.extract(),this.$parent);return t&&delete e.id,e},s.prototype.isSame=function(t){return n.isObject(t)?this.id&&t.id&&this.id===t.id:this.id===t},s.prototype.copyProperty=function(t,e){this[e]&&n.isFunction(this[e].clone)?t[e]=this[e].clone():t[e]=n.copy(this[e])},s.prototype.setParent=function(t){this.$parent=t},s.valueFromJSON=function(t){if(n.isArray(t))return t.map(s.valueFromJSON);if(a&&n.isString(t)){var e=i(t);return e||t}if(t&&n.isObject(t)){if(t._isAMomentObject)return t.clone();var r={};for(var o in t)t.hasOwnProperty(o)&&(r[o]=s.valueFromJSON(t[o]));return r}return t},s.valueToJSON=function(t){if(n.isArray(t))return t.map(s.valueToJSON);if(t&&n.isObject(t)){if(n.isFunction(t.toJSON))return t.toJSON();var e={};for(var r in t)t.hasOwnProperty(r)&&"$$"!==r.substr(0,2)&&(e[r]=s.valueToJSON(t[r]));return e}return t},s.onlyId=function(t){return n.isArray(t)?t.map(s.onlyId):t&&"object"===("undefined"==typeof t?"undefined":_typeof(t))&&t.id?t.id:t},s.strip=function(t){for(var e=arguments.length,r=Array(e>1?e-1:0),i=1;i<e;i++)r[i-1]=arguments[i];return n.isArray(t)?t.map(function(t){return s.strip.apply(s,[t].concat(r))}):t&&"object"===("undefined"==typeof t?"undefined":_typeof(t))?(n.forEach(t,function(n,e){r.includes(e)||delete t[e]}),t):t},s.stripIds=function(t){return n.isArray(t)?t.map(function(t){return s.stripIds(t)}):t&&"object"===("undefined"==typeof t?"undefined":_typeof(t))?("undefined"!=typeof t.id&&delete t.id,n.forEach(t,function(t){t&&"object"===("undefined"==typeof t?"undefined":_typeof(t))&&s.stripIds(t)}),t):t},s.isId=function(t){return t.match(/^[a-f\d]{24}$/i)},s}])}(window,window.angular),function(t,n,e){"use strict";n.module("Api.Endpoint.Service",["Api.Action.Service","Api.Request.Service","Url.Service"]).factory("$apiEndpoint",["$url","$apiAction","$apiRequest",function(t,e,r){function i(i,o){var s=this;o.actions=o.actions||{},o.url=t.concat(o.baseUrl,o.url||t.concat(i,":id")),this.$config=o,this.$actions={},n.forEach(o.actions,function(t,i){s.$actions[i]=e(t||{},o),s[i]=n.bind(s,r,s.$actions[i])})}return function(t,n){return new i(t,n)}}])}(window,window.angular),function(t,n,e){"use strict";n.module("Api.Request.Service",["Url.Service"]).factory("$apiRequest",["$http","$url",function(t,r){function i(t){return t&&"hasOwnProperty"!==t&&/^(\.[a-zA-Z_$@][0-9a-zA-Z_$@]*)+$/.test("."+t)}function o(t,n){if(!i(n))throw new Error("Invalid dotted path:"+n);for(var r=n.split("."),o=0;o<r.length&&t!==e;o++){var s=r[o];t=null!==t?t[s]:e}return t}function s(t,e,r){var i={};return n.forEach(t||{},function(t,e){n.isFunction(t)&&(t=t()),n.isString(t)&&"@"===t.charAt(0)&&(t=o(r,t.substr(1))),i[e]=t}),n.extend(i,e||{})}function a(t){var e={};return n.forEach(t.split(/\W/),function(n){if("hasOwnProperty"===n)throw new Error("Invalid parameter name: hasOwnProperty");n&&!/^\\d+$/.test(n)&&new RegExp("(^|[^\\\\]):"+n+"(\\W|$)").test(t)&&(e[n]=!0)}),e}function u(t,n){return n&&(t=t.replace(/\/+$/,"")||"/"),t.replace(/\/\.(?=\w+($|\?))/,".").replace(/\/\\\./,"/.")}function c(t,e,i,o){return t=t.replace(/\\:/g,":"),n.forEach(i,function(i,o){var s=e.hasOwnProperty(o)?e[o]:null,a=void 0;n.isDefined(s)&&null!==s?!function(){var n=r.encodeUriSegment(s);a=new RegExp(":"+o+"(\\W|$)","g"),t=t.replace(a,function(t,e){return n+e})}():(a=new RegExp("(/?):"+o+"(\\W|$)","g"),t=t.replace(a,function(t,n,e){return"/"===e.charAt(0)?e:n+e}))}),u(t,o)}function f(t,e,r,i){var o={},u=["params","model","isArray","isModel","successInterceptor","errorInterceptor","stripTrailingSlashes"];if(n.forEach(t,function(t,e){u.indexOf(e)===-1&&(o[e]=n.copy(t))}),i&&n.isObject(i)&&n.forEach(i,function(t,e){o[e]=n.copy(t)}),t.hasBody()&&r&&n.isObject(r)&&(n.isFunction(r.toJSON)?o.data=r.toJSON():n.isArray(r)?o.data=r:o.data=n.extend({},r)),e=n.copy(e),e&&n.isObject(e))for(var f in e)e.hasOwnProperty(f)&&n.isObject(e[f])&&n.isFunction(e[f].toJSON)&&(e[f]=e[f].toJSON());e=s(t.params,e,r);var p=a(o.url);return o.url=c(t.url,e,p,t.stripTrailingSlashes),n.forEach(e,function(t,n){p[n]||(o.params=o.params||{},o.params[n]=t)}),o}return function(n,e,r,i){n.hasBody()&&e&&!r&&(r=e,e=null);var o=f(n,e,r,i),s=t(o).then(n.successInterceptor.bind(n),n.errorInterceptor.bind(n));return s.then(function(t){return n.expectsModel()?n.convertToModel(t):t})}}])}(window,window.angular); | ||
var _typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t};!function(t,n,r){"use strict";n.module("Api.Action.Service",["Url.Service"]).factory("$apiAction",["$q","$injector","$log","$url",function(t,r,e,i){function o(t){return t?r.has(t)?t:("$baseModel"!==t&&e.warn("Unknown model class/service:",t),null):null}function s(t,r){if(n.extend(this,t),this.url=i.concat(r.url,this.url||""),this.model=this.model||r.model||!1,this.method=this.method||"GET",this.enforceDataFormat=r.enforceDataFormat||!1,r.config)for(var e in r.config)r.config.hasOwnProperty(e)&&(this[e]=r.config[e]);void 0===this.params&&(this.params=r.params),this.params=this.params||{},this.model=o(this.model)}return s.prototype.hasBody=function(){return/^(POST|PUT|PATCH)$/i.test(this.method)},s.prototype.expectsArray=function(){return Boolean(this.isArray)},s.prototype.expectsModel=function(){return this.model&&Boolean(this.isModel)},s.prototype.convertToModel=function(t){var e=this;return n.isArray(t)?t.map(function(t){return e.convertToModel(t)}):t&&null!==t?new(r.get(this.model))(t):null},s.prototype.successInterceptor=function(t){var r=this.expectsArray(),i=n.isArray(t.data);return i!==r&&(e.warn("Expected",r?"array":"object","as response, got",i?"array":_typeof(t.data)),this.enforceDataFormat&&(t.data=r?[]:{})),r&&!t.data?[]:t.data},s.prototype.errorInterceptor=function(n){return t.reject(n)},function(t,n){return new s(t,n)}}])}(window,window.angular),function(t,n,r){"use strict";n.module("Api.Service",["Api.Endpoint.Service"]).provider("$api",function(){this.defaults={verbose:!1,enforceDataFormat:!1,baseUrl:"/",actions:{query:{method:"GET",isArray:!0,isModel:!0},get:{method:"GET",isModel:!0},create:{method:"POST"},update:{method:"PUT"},delete:{method:"DELETE"}},params:{id:"@id"},model:"",config:{},stripTrailingSlashes:!0},this.endpoints={},this.setVerbose=function(t){return this.defaults.verbose=!!t,this},this.setBaseUrl=function(t){return this.defaults.baseUrl=t.replace(/:([0-9]+)/,"\\:$1"),this},this.setEnforceDataFormat=function(t){return this.defaults.enforceDataFormat=!!t,this},this.setDefaultActions=function(t){return this.defaults.actions=t||{},this},this.setDefaultParams=function(t){return this.defaults.params=t||{},this},this.setDefaultModel=function(t){return this.defaults.model=t||!1,this},this.setConfig=function(t,n){return this.defaults.config[t]=n,this},this.stripTrailingSlashes=function(t){return this.defaults.stripTrailingSlashes=!!t,this},this.registerEndpoint=function(t,n){return t&&(this.endpoints[t]=n||{}),this},this.$get=["$log","$apiEndpoint",function(t,r){var e=this,i=function(t){return this[t]};return n.forEach(this.endpoints,function(o,s){i[s]&&t.warn("API endpoint",s,"is being overwritten."),(o=n.extend({},e.defaults,o)).verbose&&t.info("API endpoint",s+":",o),i[s]=r(s,o)}),i}]})}(window,window.angular);var _typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t};!function(t,n,r){"use strict";n.module("BaseModel.Service",[]).factory("$baseModel",["$window","$log","$injector",function(t,r,e){function i(t){var n=/(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}).*/;if(t.match(n)){var r=a(t,a.ISO_8601,!0);if(r.isValid())return r}return null}function o(t,r){if(n.isArray(t[r])){var e=t[r];return e.map(function(t,n){return o(e,n)})}return t[r]&&n.isFunction(t[r].clone)?t[r].clone():n.copy(t[r])}function s(t,n){var r=n;Object.defineProperty(this,"$parent",{enumerable:!1,get:function(){return r},set:function(t){r=t}}),this.fromJSON(t)}var a=void 0;return e.has("moment")?a=e.get("moment"):void 0!==t.moment&&(a=t.moment),s.prototype.convertToModel=function(t,i,o){var a=this;if("boolean"==typeof i&&(o=i,i=null),void 0===this[t]&&(this[t]=o?[]:null),i&&this[t]){if("string"==typeof i){if(!e.has(i))return r.warn("Unknown model",i,"specified for sub model conversion");i=e.get(i)}n.isArray(this[t])?this[t]=this[t].map(function(t){return new i(t,a)}):n.isString(this[t])&&s.isId(this[t])?this[t]=new i({id:this[t]},this):this[t]=new i(this[t],this)}},s.prototype.fromJSON=function(t){var r=this;return n.isObject(t)&&n.forEach(t,function(t,n){r[n]=s.valueFromJSON(t)},this),this},s.prototype.toJSON=function(t){var r={};return t&&n.isObject(t)&&n.forEach(t,function(t,n){r[n]=s.valueToJSON(t)}),n.forEach(this,function(t,n){r.hasOwnProperty(n)||(r[n]=s.valueToJSON(t))}),r},s.prototype.extract=function(t){var r=this;if("string"==typeof t)return o(this,t);var e={};return n.isArray(t)&&t.length?n.forEach(t,function(t){e[t]=o(r,t)}):n.forEach(this,function(t,n){"$$"!==n.substr(0,2)&&(e[n]=o(r,n))}),e},s.prototype.merge=function(t){var r=this;return t&&n.isObject(t)&&n.forEach(t,function(n,e){r[e]=o(t,e)}),this},s.prototype.clear=function(){for(var t in this)this.hasOwnProperty(t)&&delete this[t];return this},s.prototype.clone=function(t){var n=new(0,this.constructor)(this.extract(),this.$parent);return t&&delete n.id,n},s.prototype.isSame=function(t){return n.isObject(t)?this.id&&t.id&&this.id===t.id:this.id===t},s.prototype.copyProperty=function(t,r){this[r]&&n.isFunction(this[r].clone)?t[r]=this[r].clone():t[r]=n.copy(this[r])},s.prototype.setParent=function(t){this.$parent=t},s.valueFromJSON=function(t){if(n.isArray(t))return t.map(s.valueFromJSON);if(a&&n.isString(t))return i(t)||t;if(t&&n.isObject(t)){if(t._isAMomentObject)return t.clone();var r={};for(var e in t)t.hasOwnProperty(e)&&(r[e]=s.valueFromJSON(t[e]));return r}return t},s.valueToJSON=function(t){if(n.isArray(t))return t.map(s.valueToJSON);if(t&&n.isObject(t)){if(n.isFunction(t.toJSON))return t.toJSON();var r={};for(var e in t)t.hasOwnProperty(e)&&"$$"!==e.substr(0,2)&&(r[e]=s.valueToJSON(t[e]));return r}return t},s.onlyId=function(t){return n.isArray(t)?t.map(s.onlyId):t&&"object"===(void 0===t?"undefined":_typeof(t))&&t.id?t.id:t},s.strip=function(t){for(var r=arguments.length,e=Array(r>1?r-1:0),i=1;i<r;i++)e[i-1]=arguments[i];return n.isArray(t)?t.map(function(t){return s.strip.apply(s,[t].concat(e))}):t&&"object"===(void 0===t?"undefined":_typeof(t))?(n.forEach(t,function(n,r){e.includes(r)||delete t[r]}),t):t},s.stripIds=function(t){return n.isArray(t)?t.map(function(t){return s.stripIds(t)}):t&&"object"===(void 0===t?"undefined":_typeof(t))?(void 0!==t.id&&delete t.id,n.forEach(t,function(t){t&&"object"===(void 0===t?"undefined":_typeof(t))&&s.stripIds(t)}),t):t},s.isId=function(t){return t.match(/^[a-f\d]{24}$/i)},s}])}(window,window.angular),function(t,n,r){"use strict";n.module("Api.Endpoint.Service",["Api.Action.Service","Api.Request.Service","Url.Service"]).factory("$apiEndpoint",["$url","$apiAction","$apiRequest",function(t,r,e){function i(i,o){var s=this;o.actions=o.actions||{},o.url=t.concat(o.baseUrl,o.url||t.concat(i,":id")),this.$config=o,this.$actions={},n.forEach(o.actions,function(t,i){s.$actions[i]=r(t||{},o),s[i]=n.bind(s,e,s.$actions[i])})}return function(t,n){return new i(t,n)}}])}(window,window.angular),function(t,n,r){"use strict";n.module("Api.Request.Service",["Url.Service"]).factory("$apiRequest",["$http","$url",function(t,e){function i(t){return t&&"hasOwnProperty"!==t&&/^(\.[a-zA-Z_$@][0-9a-zA-Z_$@]*)+$/.test("."+t)}function o(t,n){if(!i(n))throw new Error("Invalid dotted path:"+n);for(var e=n.split("."),o=0;o<e.length&&t!==r;o++){var s=e[o];t=null!==t?t[s]:r}return t}function s(t,r,e){var i={};return n.forEach(t||{},function(t,r){n.isFunction(t)&&(t=t()),n.isString(t)&&"@"===t.charAt(0)&&(t=o(e,t.substr(1))),i[r]=t}),n.extend(i,r||{})}function a(t){var r={};return n.forEach(t.split(/\W/),function(n){if("hasOwnProperty"===n)throw new Error("Invalid parameter name: hasOwnProperty");n&&!/^\\d+$/.test(n)&&new RegExp("(^|[^\\\\]):"+n+"(\\W|$)").test(t)&&(r[n]=!0)}),r}function c(t,n){return n&&(t=t.replace(/\/+$/,"")||"/"),t.replace(/\/\.(?=\w+($|\?))/,".").replace(/\/\\\./,"/.")}function u(t,r,i,o){return t=t.replace(/\\:/g,":"),n.forEach(i,function(i,o){var s=r.hasOwnProperty(o)?r[o]:null,a=void 0;if(n.isDefined(s)&&null!==s){var c=e.encodeUriSegment(s);a=new RegExp(":"+o+"(\\W|$)","g"),t=t.replace(a,function(t,n){return c+n})}else a=new RegExp("(/?):"+o+"(\\W|$)","g"),t=t.replace(a,function(t,n,r){return"/"===r.charAt(0)?r:n+r})}),c(t,o)}function f(t,r,e,i){var o={},c=["params","model","isArray","isModel","successInterceptor","errorInterceptor","stripTrailingSlashes"];if(n.forEach(t,function(t,r){-1===c.indexOf(r)&&(o[r]=n.copy(t))}),i&&n.isObject(i)&&n.forEach(i,function(t,r){o[r]=n.copy(t)}),t.hasBody()&&e&&n.isObject(e)&&(n.isFunction(e.toJSON)?o.data=e.toJSON():n.isArray(e)?o.data=e:o.data=n.extend({},e)),(r=n.copy(r))&&n.isObject(r))for(var f in r)r.hasOwnProperty(f)&&n.isObject(r[f])&&n.isFunction(r[f].toJSON)&&(r[f]=r[f].toJSON());r=s(t.params,r,e);var l=a(o.url);return o.url=u(t.url,r,l,t.stripTrailingSlashes),n.forEach(r,function(t,n){l[n]||(o.params=o.params||{},o.params[n]=t)}),o}return function(n,r,e,i){n.hasBody()&&r&&!e&&(e=r,r=null);var o=f(n,r,e,i);return t(o).then(n.successInterceptor.bind(n)).catch(n.errorInterceptor.bind(n)).then(function(t){return n.expectsModel()?n.convertToModel(t):t})}}])}(window,window.angular); | ||
//# sourceMappingURL=angular-api.min.js.map |
@@ -102,2 +102,7 @@ | ||
//No data? | ||
if (!data || data === null) { | ||
return null; | ||
} | ||
//Get model class and return model instance | ||
@@ -132,4 +137,9 @@ let Model = $injector.get(this.model); | ||
//Initialize if empty | ||
return response.data || (expectsArray ? [] : {}); | ||
//Empty array if no data sent | ||
if (expectsArray && !response.data) { | ||
return []; | ||
} | ||
//Sent data as is (also if null) | ||
return response.data; | ||
}; | ||
@@ -136,0 +146,0 @@ |
@@ -121,2 +121,3 @@ | ||
//Loop the valid URL params now | ||
//eslint-disable-next-line no-unused-vars | ||
angular.forEach(urlParams, (t, urlParam) => { | ||
@@ -132,2 +133,3 @@ | ||
regex = new RegExp(':' + urlParam + '(\\W|$)', 'g'); | ||
//eslint-disable-next-line no-unused-vars | ||
url = url.replace(regex, (match, tail) => { | ||
@@ -140,3 +142,4 @@ return encodedVal + tail; | ||
else { | ||
regex = new RegExp('(\/?):' + urlParam + '(\\W|$)', 'g'); | ||
regex = new RegExp('(/?):' + urlParam + '(\\W|$)', 'g'); | ||
//eslint-disable-next-line no-unused-vars | ||
url = url.replace(regex, (match, leadingSlashes, tail) => { | ||
@@ -246,15 +249,12 @@ if (tail.charAt(0) === '/') { | ||
let request = createRequestConfig(action, params, data, config); | ||
let promise = $http(request).then( | ||
action.successInterceptor.bind(action), | ||
action.errorInterceptor.bind(action) | ||
); | ||
//Then handle the raw data | ||
return promise.then(raw => { | ||
if (action.expectsModel()) { | ||
return action.convertToModel(raw); | ||
} | ||
return raw; | ||
}); | ||
return $http(request) | ||
.then(action.successInterceptor.bind(action)) | ||
.catch(action.errorInterceptor.bind(action)) | ||
.then(raw => { | ||
if (action.expectsModel()) { | ||
return action.convertToModel(raw); | ||
} | ||
return raw; | ||
}); | ||
}; | ||
}); |
Sorry, the diff of this file is not supported yet
1846
200247
18