restful-ng-mock
Version:
Quickly mock up common HTTP service API patterns.
1 lines • 5.54 kB
JavaScript
(function(){"use strict";angular.module("restfulNgMock",[]),angular.module("restfulNgMock").factory("basicMock",["$httpBackend",function(t){function o(t,o){if(this._baseUrl=t||"",!r.test(this._baseUrl))throw'Invalid baseUrl for resourceMock: "'+t+'".';this.options=angular.extend({},this.DEFAULT_OPTIONS),this.setOptions(o||{})}var r=/^(\/[\w\-]+|)(\/[\w\-]+|\/\?)*$/;o.prototype.constructor=o,o.prototype.HttpError=function(){function t(t,o){this.code=t,this.message=o}return t}(),o.prototype.HttpRequest=function(){function t(t,o,r,e,n,i){this.pathArgs=t,this.method=o,this.rawUrl=r,this.url=e,this.rawBody=n,this.body=/^application\/json($|;)/.test(i["Content-Type"])?JSON.parse(n):n,this.headers=i}return t}(),o.prototype.RouteOptions=function(){function t(){this.postProcs=[]}return t.prototype.addPostProc=function(t){return this.postProcs.push(t),this},t}(),o.prototype.DEFAULT_OPTIONS={debug:!1,httpResponseInfoLabel:!1},o.prototype._buildResponse=function(t,o){(angular.isUndefined(t)||null===t)&&(t=new this.HttpError(404,"Not Found"));var r={code:200,message:"OK"};t instanceof this.HttpError&&(r.code=t.code,r.message=t.message,t=this.options.httpResponseInfoLabel?{}:r),angular.isObject(t)&&!angular.isArray(t)&&this.options.httpResponseInfoLabel&&(t[this.options.httpResponseInfoLabel]=r);var e=JSON.stringify(t);if(this.options.debug){var n=this.options.debug;"function"!=typeof n&&(n=this._defaultDebug),n(o,r,JSON.parse(e))}return[r.code,e,{"Content-Type":"application/json"}]},o.prototype._defaultDebug=function(t,o,r){var e=function(t,o){return Array(Math.max(o-(t+"").length+1,0)).join(0)+t},n=new Date,i=[e(n.getHours(),2),e(n.getMinutes(),2),e(n.getSeconds(),2),e(n.getMilliseconds(),3)];console.log([i.join(":"),">>> "+t.method+" "+t.rawUrl,"<<< "+o.code,r])},o.prototype.route=function(e,n,i){if(!r.test(n))throw'Invalid url for route: "'+n+'".';var s=this._baseUrl+n,u=s.replace(/\//g,"\\/").replace(/\?/g,"([\\w\\-]+)"),a=RegExp("^"+u+"(?:\\?.*)?$"),p=new this.RouteOptions,c=this;return t.when(e,a).respond(function(t,r,e,n){var s=purl(r,!0),u=a.exec(s.attr("path")).slice(1),h=new c.HttpRequest(u,t,r,s,e,n),l=i.call(c,h);return angular.forEach(p.postProcs,function(t){!l||l instanceof o.prototype.HttpError||(l=t.call(c,l,h))}),c._buildResponse(l,h)}),p},o.prototype.setOptions=function(t){var o=this;return angular.forEach(t,function(t,r){if(o.DEFAULT_OPTIONS[r]===void 0)throw"Invalid option key "+r;o.options[r]=t}),o};var e=function(t,r){return new o(t,r)};return e.classFn=o,e}]),angular.module("restfulNgMock").factory("resourceMock",["basicMock",function(t){function o(t,r,e){o._super.constructor.call(this,t,e),this.dataSource=r||{},this.requiredParams=0;for(var n=0;t.length>n;++n)"?"===t.charAt(n)&&++this.requiredParams;this.indexRoute=this.route("GET","",function(t){return this.indexAction(t)}),this.showRoute=this.route("GET","/?",function(t){return this.showAction(t)}),this.createRoute=this.route("POST","",function(t){return this.createAction(t)}),this.updateRoute=this.route("PUT","/?",function(t){return this.updateAction(t)}),this.deleteRoute=this.route("DELETE","/?",function(t){return this.deleteAction(t)})}(function(){function r(){this.constructor=o}var e=t.classFn;r.prototype=e.prototype,o.prototype=new r,o._super=e.prototype})(),o.prototype.parent=o.prototype,o.prototype.subResourceMock=function(t,r,e){return new o(this._baseUrl+"/?"+t,r,e)},o.prototype.addIndexFilter=function(t,o){return o=o||function(o,r){return""+r[t]==""+o},this.indexRoute.addPostProc(function(r,e){if(e.url.param(t)){var n,i=[];for(n in r)o(e.url.param(t),r[n])&&i.push(r[n]);return i}}),this},o.prototype.addIndexArrayFilter=function(t,o,r){return o=o||",",r=r||function(o,r){return""+r[t]==""+o},this.indexRoute.addPostProc(function(e,n){if(n.url.param(t)){var i,s,u=[],a=n.url.param(t).split(o);for(i in e)for(s in a)if(r(a[s],e[i])){u.push(e[i]);break}return u}}),this},o.prototype.addIndexPagination=function(t,o){t=t||"skip",o=o||"limit",this.indexRoute.addPostProc(function(r,e){var n=parseInt(e.url.param(t),10);n&&(r=r.slice(n));var i=parseInt(e.url.param(o),10);return i&&(r=r.slice(0,i)),r})},o.prototype.addSingletonPostProcs=function(t){var o=[this.showRoute,this.createRoute,this.updateRoute,this.deleteRoute];angular.forEach(o,function(o){o.addPostProc(t)})},o.prototype.addLabeller=function(t,o){return t&&this.addSingletonPostProcs(function(o){var r={};return r[t]=o,r}),o&&this.indexRoute.addPostProc(function(t){var r={};return r[o]=t,r}),this},o.prototype.getStorage=function(t,o){t=t||[],o=o||!1;for(var r=this.dataSource,e=0;t.length>e;++e)if(r[t[e]])r=r[t[e]];else{if(!o)return null;r[t[e]]={},r=r[t[e]]}return r||null},o.prototype.indexAction=function(t){var o=this.getStorage(t.pathArgs);if(o){var r=[];angular.forEach(o,function(t,o){/^\d+$/.test(o)&&(o=parseInt(o,10)),r.push(o)}),r.sort();var e=[];return angular.forEach(r,function(t){e.push(o[t])}),e}},o.prototype.showAction=function(t){return this.getStorage(t.pathArgs)},o.prototype.createAction=function(t){var o=t.body;return o.id=Math.round(Math.random()*Math.pow(2,32)),this.getStorage(t.pathArgs,!0)[o.id]=o,o},o.prototype.updateAction=function(t){var o=t.body,r=this.getStorage(t.pathArgs.slice(0,-1)),e=t.pathArgs[t.pathArgs.length-1];return r&&r[e]?(o.id=r[e].id,r[e]=o,o):void 0},o.prototype.deleteAction=function(t){var o=this.getStorage(t.pathArgs.slice(0,-1)),r=t.pathArgs[t.pathArgs.length-1];if(o&&o[r]){var e=o[r];return delete o[r],e}};var r=function(t,r){return new o(t,r)};return r.classFn=o,r}])})(window);