-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathangularjs-rails-resource.min.js
7 lines (7 loc) · 16.9 KB
/
angularjs-rails-resource.min.js
1
2
3
4
5
6
7
/**
* A resource factory inspired by $resource from AngularJS
* @version v2.3.1 - 2021-07-26
* @link https://github.com/FineLinePrototyping/angularjs-rails-resource.git
* @author
*/
!function(a){angular.module("rails",["ng"])}(),function(a){angular.module("rails").factory("RailsInflector",function(){function a(a){return angular.isString(a)?a.replace(/_[\w\d]/g,function(a,b,c){return 0===b?a:c.charAt(b+1).toUpperCase()}):a}function b(a){return angular.isString(a)?a.replace(/[A-Z]/g,function(a,b){return 0===b?a:"_"+a.toLowerCase()}):a}function c(a){return a+"s"}return{camelize:a,underscore:b,pluralize:c}})}(),function(a){angular.module("rails").factory("RailsResourceInjector",["$injector",function(b){function c(c){return c?angular.isString(c)?b.get(c):c:a}function d(d){return d?b.instantiate(c(d)):a}function e(b){return angular.isObject(b)?b:b?d(b):a}return{createService:d,getService:e,getDependency:c}}])}(),function(a){angular.module("rails").factory("railsUrlBuilder",["$interpolate",function(a){return function(b){var c,d=b.url,e=b.idAttribute;return angular.isFunction(d)||angular.isUndefined(d)?d:(b.singular||-1!==d.indexOf(a.startSymbol())||(d=d+"/"+a.startSymbol()+e+a.endSymbol()),c=a(d),function(a){return d=c(a),"/"===d.charAt(d.length-1)&&(d=d.substr(0,d.length-1)),d})}}])}(),function(a){angular.module("rails").provider("railsSerializer",function(){var b={underscore:a,camelize:a,pluralize:a,exclusionMatchers:[]};this.underscore=function(a){return b.underscore=a,this},this.camelize=function(a){return b.camelize=a,this},this.pluralize=function(a){return b.pluralize=a,this},this.exclusionMatchers=function(a){return b.exclusionMatchers=a,this},this.$get=["$injector","RailsInflector","RailsResourceInjector",function(c,d,e){function f(c,d){function f(){angular.isFunction(c)&&(d=c,c={}),this.exclusions={},this.inclusions={},this.serializeMappings={},this.deserializeMappings={},this.customSerializedAttributes={},this.preservedAttributes={},this.customSerializers={},this.nestedResources={},this.polymorphics={},this.options=angular.extend({excludeByDefault:!1},b,c||{}),d&&d.call(this,this)}return f.prototype.exclude=function(){var a=this.exclusions;return angular.forEach(arguments,function(b){a[b]=!1}),this},f.prototype.only=function(){var a=this.inclusions;return this.options.excludeByDefault=!0,angular.forEach(arguments,function(b){a[b]=!0}),this},f.prototype.nestedAttribute=function(){var a=this;return angular.forEach(arguments,function(b){a.rename(b,b+"_attributes")}),this},f.prototype.resource=function(a,b,c){return this.nestedResources[a]=b,c&&this.serializeWith(a,c),this},f.prototype.polymorphic=function(){var a=this.polymorphics;return angular.forEach(arguments,function(b){a[b]=!0}),this},f.prototype.rename=function(b,c,d){return this.serializeMappings[b]=c,(d||d===a)&&(this.deserializeMappings[c]=b),this},f.prototype.add=function(a,b){return this.customSerializedAttributes[a]=b,this},f.prototype.preserve=function(a){return this.preservedAttributes[a]=!0,this},f.prototype.serializeWith=function(a,b){return this.customSerializers[a]=b,this},f.prototype.isExcludedFromSerialization=function(b){if(this.options.excludeByDefault&&!this.inclusions.hasOwnProperty(b)||this.exclusions.hasOwnProperty(b))return!0;if(this.options.exclusionMatchers){var c=!1;return angular.forEach(this.options.exclusionMatchers,function(d){angular.isString(d)?c=c||0===b.indexOf(d):angular.isFunction(d)?c=c||d.call(a,b):d instanceof RegExp&&(c=c||d.test(b))}),c}return!1},f.prototype.getSerializedAttributeName=function(b){var c=this.serializeMappings[b]||b,d=this.isExcludedFromSerialization(c),e=this.isExcludedFromSerialization(b);if(this.options.excludeByDefault){if(d&&e)return a}else if(d||e)return a;return this.underscore(c)},f.prototype.isExcludedFromDeserialization=function(a){return!1},f.prototype.getDeserializedAttributeName=function(b){var c=this.camelize(b);return c=this.deserializeMappings[b]||this.deserializeMappings[c]||c,this.isExcludedFromDeserialization(b)||this.isExcludedFromDeserialization(c)?a:c},f.prototype.getNestedResource=function(a,b){var c;return c=this.polymorphics[a]?b[a+"_type"]:this.nestedResources[a],e.getDependency(c)},f.prototype.getAttributeSerializer=function(b,c){var d=this.getNestedResource(b,c),f=this.customSerializers[b];return f?e.createService(f):d?d.config.serializer:a},f.prototype.serializeData=function(a){var b=a,c=this;if(angular.isArray(a))b=[],angular.forEach(a,function(a){b.push(c.serializeData(a))});else if(angular.isObject(a)){if(angular.isDate(a))return a;b={},this.serializeObject(b,a)}return b},f.prototype.serializeObject=function(a,b){var c=this;return angular.forEach(b,function(d,e){angular.isFunction(d)||c.serializeAttribute(a,e,d,b)}),b},f.prototype.serializeAttribute=function(b,c,d,e){var f=this.getAttributeSerializer(c,e),g=this.getSerializedAttributeName(c);g!==a&&(b[g]=f?f.serialize(d):this.serializeData(d))},f.prototype.serialize=function(a){var b=angular.copy(a),c=this;return angular.isObject(b)&&angular.forEach(this.customSerializedAttributes,function(d,e){angular.isArray(b)?angular.forEach(b,function(b,f){var g=d;angular.isFunction(d)&&(g=g.call(b,b)),c.serializeAttribute(b,e,g,a)}):(angular.isFunction(d)&&(d=d.call(a,a)),c.serializeAttribute(b,e,d,a))}),b=this.serializeData(b)},f.prototype.deserializeData=function(a,b,c){var d=a,e=this;if(angular.isArray(a))d=[],angular.forEach(a,function(a){d.push(e.deserializeData(a,b,c))});else if(angular.isObject(a)){if(angular.isDate(a))return a;d={},b&&(d=new b.config.resourceConstructor),this.deserializeObject(d,a,c)}return d},f.prototype.deserializeObject=function(a,b,c){var d=this;return angular.forEach(b,function(c,e){d.deserializeAttribute(a,e,c,b)}),c&&a.constructor.runInterceptorPhase&&a.constructor.runInterceptorPhase("afterDeserialize",a),b},f.prototype.deserializeAttribute=function(b,c,d,e){var f,g,h=this.getDeserializedAttributeName(c);h!==a&&(f=this.getAttributeSerializer(h,e),g=this.getNestedResource(h,e),this.preservedAttributes[h]?b[h]=d:b[h]=f?f.deserialize(d,g,!0):this.deserializeData(d,g,!0))},f.prototype.deserialize=function(a,b,c){return this.deserializeData(a,b,c)},f.prototype.pluralize=function(a){return this.options.pluralize?this.options.pluralize(a):a},f.prototype.underscore=function(a){return this.options.underscore?this.options.underscore(a):a},f.prototype.camelize=function(a){return this.options.camelize?this.options.camelize(a):a},f}return b.underscore=b.underscore||d.underscore,b.camelize=b.camelize||d.camelize,b.pluralize=b.pluralize||d.pluralize,f.defaultOptions=b,f}]})}(),function(a){angular.module("rails").factory("railsRootWrapper",function(){return{wrap:function(a,b){var c={};return c[angular.isArray(a)?b.config.pluralName:b.config.name]=a,c},unwrap:function(a,b,c){return a.data&&a.data.hasOwnProperty(b.config.name)?a.data=a.data[b.config.name]:a.data&&a.data.hasOwnProperty(b.config.pluralName)&&!c&&(a.data=a.data[b.config.pluralName]),a}}}),angular.module("rails").provider("RailsResource",function(){var b={rootWrapping:!0,updateMethod:"put",httpConfig:{},defaultParams:a,underscoreParams:!0,fullResponse:!1,singular:!1,extensions:[]};this.rootWrapping=function(a){return b.rootWrapping=a,this},this.updateMethod=function(a){return b.updateMethod=a,this},this.httpConfig=function(a){return b.httpConfig=a,this},this.defaultParams=function(a){return b.defaultParams=a,this},this.underscoreParams=function(a){return b.underscoreParams=a,this},this.fullResponse=function(a){return b.fullResponse=a,this},this.extensions=function(){return b.extensions=[],angular.forEach(arguments,function(a){b.extensions=b.extensions.concat(a)}),this},this.$get=["$http","$q","$timeout","railsUrlBuilder","railsSerializer","railsRootWrapper","RailsResourceInjector",function(a,c,d,e,f,g,h){function i(a){if(a){var b=this.constructor.deserialize({data:a});this.constructor.config.rootWrapping&&(b=g.unwrap(b,this.constructor,!0)),angular.extend(this,b.data),this.constructor.runInterceptorPhase("afterDeserialize",this)}}function j(a,b){return b&&("/"!==b[0]&&(a+="/"),a+=b),a}function k(a,b){for(var c,d=0,e=a.length;e>d;d++)c=a[d],angular.isString(c)&&(c=a[d]=h.getDependency(c)),b(c)}function l(a,b,c,d){var e=["included","extended,","configure"];a.$mixins||(a.$mixins=[]),angular.isString(c)&&(c=h.getDependency(c)),c&&-1===a.$mixins.indexOf(c)&&(angular.forEach(c,function(a,c){-1===e.indexOf(c)&&(b[c]=a)}),a.$mixins.push(c),angular.isFunction(d)&&d(a,c))}function m(a){var b=[];return angular.forEach(a,function(a){a="RailsResource"+a.charAt(0).toUpperCase()+a.slice(1)+"Mixin",b.push(h.getDependency(a))}),b}function n(a,b){return angular.isUndefined(a)?b:a}function o(a,b,c){return function(d){return(a||angular.identity)(d,b,c)}}function p(a,b,d){return function(e){return a?a(e,b,d):c.reject(e)}}function q(a,b){var c=a.then;return a.then=function(a,d,e){var f=c.apply(this,arguments);return q(f,b)},angular.extend(a,b),a}return i.extendTo=function(a){function b(){this.constructor=a}return angular.forEach(this,function(b,c){a[c]=b}),angular.isArray(this.$modules)&&(a.$modules=this.$modules.slice(0)),b.prototype=this.prototype,a.prototype=new b,a.__super__=this.prototype,a},i.extend=function(){return angular.forEach(arguments,function(a){l(this,this,a,function(a,b){angular.isFunction(b.extended)&&b.extended(a)})},this),this},i.include=function(){return angular.forEach(arguments,function(a){l(this,this.prototype,a,function(a,b){angular.isFunction(b.included)&&b.included(a)})},this),this},i.configure=function(a){return a=a||{},this.config&&(a=angular.extend({},this.config,a)),this.config={},this.config.idAttribute=a.idAttribute||"id",this.config.url=a.url,this.config.rootWrapping=n(a.rootWrapping,b.rootWrapping),this.config.httpConfig=a.httpConfig||b.httpConfig,this.config.httpConfig.headers=angular.extend({Accept:"application/json","Content-Type":"application/json"},this.config.httpConfig.headers||{}),this.config.defaultParams=a.defaultParams||b.defaultParams,this.config.underscoreParams=n(a.underscoreParams,b.underscoreParams),this.config.updateMethod=(a.updateMethod||b.updateMethod).toLowerCase(),this.config.fullResponse=n(a.fullResponse,b.fullResponse),this.config.singular=a.singular||b.singular,this.config.requestTransformers=a.requestTransformers?a.requestTransformers.slice(0):[],this.config.responseInterceptors=a.responseInterceptors?a.responseInterceptors.slice(0):[],this.config.afterResponseInterceptors=a.afterResponseInterceptors?a.afterResponseInterceptors.slice(0):[],this.config.interceptors=a.interceptors?a.interceptors.slice(0):[],this.config.serializer=h.getService(a.serializer||f()),this.config.name=this.config.serializer.underscore(a.name),this.config.name&&(this.config.pluralName=this.config.serializer.underscore(a.pluralName||this.config.serializer.pluralize(this.config.name))),this.config.urlBuilder=e(this.config),this.config.resourceConstructor=this,this.extend.apply(this,m((a.extensions||[]).concat(b.extensions))),angular.forEach(this.$mixins,function(b){angular.isFunction(b.configure)&&b.configure(this.config,a)},this),this.config},i.setUrl=function(a){this.configure({url:a})},i.buildUrl=function(a){return this.config.urlBuilder(a)},i.addInterceptor=function(a){this.config.interceptors.push(a)},i.intercept=function(a,b){var c={};b=h.getDependency(b),c[a]=function(a,c,d){return b(a,c,d)||a},this.addInterceptor(c)},i.interceptBeforeRequest=function(a){this.intercept("beforeRequest",a)},i.interceptBeforeRequestWrapping=function(a){this.intercept("beforeRequestWrapping",a)},i.interceptRequest=function(a){this.intercept("request",a)},i.interceptBeforeResponse=function(a){this.intercept("beforeResponse",a)},i.interceptBeforeResponseDeserialize=function(a){this.intercept("beforeResponseDeserialize",a)},i.interceptResponse=function(a){this.intercept("response",a)},i.interceptAfterResponse=function(a){this.intercept("afterResponse",a)},i.interceptAfterDeserialize=function(a){this.intercept("afterDeserialize",a)},i.beforeResponse=function(a){a=h.getDependency(a),this.interceptResponse(function(b,c,d){return a(b.data,c.config.resourceConstructor,d),b})},i.afterResponse=function(a){a=h.getDependency(a),this.interceptAfterResponse(function(b,c,d){return a(b,c.config.resourceConstructor,d),b})},i.beforeRequest=function(a){a=h.getDependency(a),this.interceptBeforeRequestWrapping(function(b,c){return b.data=a(b.data,c.config.resourceConstructor)||b.data,b})},i.serialize=function(a){return a.data&&(a.data=this.config.serializer.serialize(a.data)),a},i.deserialize=function(a){return a.data=this.config.serializer.deserialize(a.data,this.config.resourceConstructor),a},i.callResponseInterceptors=function(a,b){var c=this.config;return k(c.responseInterceptors,function(d){a.resource=c.resourceConstructor,a.context=b,a=d(a)}),a},i.callAfterResponseInterceptors=function(a){var b=this.config;return k(b.afterResponseInterceptors,function(c){a.resource=b.resourceConstructor,a=c(a)}),a},i.runInterceptorPhase=function(a,b,d){d=d||c.resolve(b);var e=this.config,f=[];for(k(e.interceptors,function(b){(b[a]||b[a+"Error"])&&f.push(b[a],b[a+"Error"])});f.length;){var g=f.shift(),h=f.shift();d=d.then(o(g,e.resourceConstructor,b),p(h,e.resourceConstructor,b))}return d},i.$http=function(b,e,f){function h(){n.resolve()}var i,j,l=angular.extend(angular.copy(this.config),f||{}),m=l.resourceConstructor,n=c.defer();return b&&b.timeout&&(b.timeout>0?i=d(n.resolve,b.timeout):angular.isFunction(b.timeout.then)&&b.timeout.then(n.resolve)),b=angular.extend({},b,{timeout:n.promise}),j=c.when(b),l.skipRequestProcessing?j=a(b):(j=this.runInterceptorPhase("beforeRequest",e,j).then(function(a){return a=m.serialize(a),k(l.requestTransformers,function(b){a.data=b(a.data,l.resourceConstructor)}),a}),j=this.runInterceptorPhase("beforeRequestWrapping",e,j),l.rootWrapping&&(j=j.then(function(a){return a.data=g.wrap(a.data,l.resourceConstructor),a})),j=this.runInterceptorPhase("request",e,j).then(function(b){return a(b)})),i&&(j=j.then(function(a){return d.cancel(i),a},function(a){return d.cancel(i),c.reject(a)})),j=this.runInterceptorPhase("beforeResponse",e,j).then(function(a){return a.originalData=a.data,a}),l.rootWrapping&&(j=j.then(function(a){return g.unwrap(a,l.resourceConstructor,!1)})),j=this.runInterceptorPhase("beforeResponseDeserialize",e,j).then(function(a){return m.deserialize(a)}),j=this.callResponseInterceptors(j,e),j=this.runInterceptorPhase("response",e,j).then(function(a){return e&&a.hasOwnProperty("data")&&angular.isObject(a.data)&&angular.extend(e,a.data),l.fullResponse?a:e||a.data}),j=this.callAfterResponseInterceptors(j,e),j=this.runInterceptorPhase("afterResponse",e,j),j=this.runInterceptorPhase("afterDeserialize",e,j),q(j,{resource:l.resourceConstructor,context:e,abort:h})},i.processParameters=function(a){var b={};return angular.isObject(a)&&this.config.underscoreParams?(angular.forEach(a,function(a,c){b[this.config.serializer.underscore(c)]=a},this),b):a},i.getParameters=function(a){var b;return this.config.defaultParams&&(b=angular.copy(this.config.defaultParams)),angular.isObject(a)&&(b=angular.extend(b||{},a)),this.processParameters(b)},i.getHttpConfig=function(a){var b=this.getParameters(a);return b?angular.extend({params:b},this.config.httpConfig):angular.copy(this.config.httpConfig)},i.$url=i.resourceUrl=function(a,b){var c=a;return angular.isObject(a)||(c={},c[this.config.idAttribute]=a),j(this.buildUrl(c||{}),b)},i.$get=function(a,b){return this.$http(angular.extend({method:"get",url:a},this.getHttpConfig(b)))},i.query=function(a,b){return this.$get(this.resourceUrl(b),a)},i.get=function(a,b){return this.$get(this.resourceUrl(a),b)},i.prototype.$url=function(a){return j(this.constructor.resourceUrl(this),a)},i.prototype.$http=function(a,b){return this.constructor.$http(a,this,b)},angular.forEach(["post","put","patch"],function(a){i["$"+a]=function(b,c,d,e){return c=angular.copy(c),this.$http(angular.extend({method:a,url:b,data:c},this.getHttpConfig(e)),null,d)},i.prototype["$"+a]=function(b,c,d){var e=angular.copy(this,{});return this.constructor.$http(angular.extend({method:a,url:b,data:e},this.constructor.getHttpConfig(d)),this)}}),i.prototype.create=function(){return this.$post(this.$url(),this)},i.prototype.update=function(){return this["$"+this.constructor.config.updateMethod](this.$url(),this)},i.prototype.get=function(){return this.constructor.$http(angular.extend({method:"GET",url:this.$url()},this.constructor.getHttpConfig()),this)},i.prototype.isNew=function(){var a=this.constructor.config.idAttribute;return angular.isUndefined(this[a])||null===this[a]},i.prototype.save=function(){return this.isNew()?this.create():this.update()},i.$delete=function(a,b){return this.$http(angular.extend({method:"delete",url:a},this.getHttpConfig(b)))},i.prototype.$delete=function(a,b){return this.constructor.$http(angular.extend({method:"delete",url:a},this.constructor.getHttpConfig(b)),this)},i.prototype.remove=i.prototype["delete"]=function(){return this.$delete(this.$url())},i}]}),angular.module("rails").factory("railsResourceFactory",["RailsResource",function(a){return function(b){function c(){c.__super__.constructor.apply(this,arguments)}return a.extendTo(c),c.configure(b),c}}])}();