Comparing version 1.1.4 to 1.2.0
@@ -1,2 +0,2 @@ | ||
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):"object"==typeof exports?exports.FakeRest=e():t.FakeRest=e()}(this,function(){return function(t){function e(n){if(r[n])return r[n].exports;var o=r[n]={exports:{},id:n,loaded:!1};return t[n].call(o.exports,o,o.exports,e),o.loaded=!0,o.exports}var r={};return e.m=t,e.c=r,e.p="",e(0)}([function(t,e,r){"use strict";var n=r(3)["default"];Object.defineProperty(e,"__esModule",{value:!0});var o=r(1),i=n(o),u=r(2),c=n(u);e["default"]={Server:i["default"],Collection:c["default"]},t.exports=e["default"]},function(t,e,r){"use strict";function n(t){if(!t)return{};var e={},r=t.split("&");return r.map(function(t){if(-1===t.indexOf("="))e[t]=!0;else{var r=t.split("="),n=u(r,2),o=n[0],i=n[1];(0===i.indexOf("[")||0===i.indexOf("{"))&&(i=JSON.parse(i)),e[o.trim()]=i}}),e}var o=r(4)["default"],i=r(5)["default"],u=r(6)["default"],c=r(7)["default"],a=r(8)["default"],s=r(3)["default"];Object.defineProperty(e,"__esModule",{value:!0});var f=r(9),l=s(f),p=r(2),d=s(p),h=l["default"].getPolyfill(),y=function(){function t(){var e=arguments.length<=0||void 0===arguments[0]?"":arguments[0];i(this,t),this.baseUrl=e,this.loggingEnabled=!1,this.defaultQuery=function(){},this.batchUrl=null,this.collections={},this.requestInterceptors=[],this.responseInterceptors=[]}return o(t,[{key:"init",value:function(t){for(var e in t)this.addCollection(e,new d["default"](t[e],"id"))}},{key:"toggleLogging",value:function(){this.loggingEnabled=!this.loggingEnabled}},{key:"setDefaultQuery",value:function(t){this.defaultQuery=t}},{key:"setBatchUrl",value:function(t){this.batchUrl=t}},{key:"setBatch",value:function(t){console.warn("Server.setBatch() is deprecated, use Server.setBatchUrl() instead"),this.batchUrl=t}},{key:"addCollection",value:function(t,e){this.collections[t]=e,e.setServer(this),e.setName(t)}},{key:"getCollection",value:function(t){return this.collections[t]}},{key:"getCollectionNames",value:function(){return c(this.collections)}},{key:"addRequestInterceptor",value:function(t){this.requestInterceptors.push(t)}},{key:"addResponseInterceptor",value:function(t){this.responseInterceptors.push(t)}},{key:"getCount",value:function(t,e){return this.collections[t].getCount(e)}},{key:"getAll",value:function(t,e){return this.collections[t].getAll(e)}},{key:"getOne",value:function(t,e,r){return this.collections[t].getOne(e,r)}},{key:"addOne",value:function(t,e){return this.collections[t].addOne(e)}},{key:"updateOne",value:function(t,e,r){return this.collections[t].updateOne(e,r)}},{key:"removeOne",value:function(t,e){return this.collections[t].removeOne(e)}},{key:"decode",value:function(t){if(t.queryString=decodeURIComponent(t.url.slice(t.url.indexOf("?")+1)),t.params=n(t.queryString),t.requestBody)try{t.json=JSON.parse(t.requestBody)}catch(e){}return this.requestInterceptors.reduce(function(t,e){return e(t)},t)}},{key:"respond",value:function(t,e,r){var n=arguments.length<=3||void 0===arguments[3]?200:arguments[3];e||(e={}),e["Content-Type"]||(e["Content-Type"]="application/json");var o={status:n,headers:e,body:t};return o=this.responseInterceptors.reduce(function(t,e){return e(t,r)},o),this.log(r,o),r.respond(o.status,o.headers,JSON.stringify(o.body))}},{key:"log",value:function(t,e){this.loggingEnabled&&(console.group?(console.groupCollapsed(t.method,t.url,"(FakeRest)"),console.group("request"),console.log(t.method,t.url),console.log("headers",t.requestHeaders),console.log("body ",t.requestBody),console.groupEnd(),console.group("response",e.status),console.log("headers",e.headers),console.log("body ",e.body),console.groupEnd(),console.groupEnd()):(console.log("FakeRest request ",t.method,t.url,"headers",t.requestHeaders,"body",t.requestBody),console.log("FakeRest response",e.status,"headers",e.headers,"body",e.body)))}},{key:"batch",value:function(t){var e=t.json,r=this.handle.bind(this),n=c(e).reduce(function(t,n){var o,i={url:e[n],method:"GET",params:{},respond:function(t,e,r){o={code:t,headers:c(e||{}).map(function(t){return{name:t,value:e[t]}}),body:r||{}}}};return r(i),t[n]=o||{code:404,headers:[],body:{}},t},{});return this.respond(n,{},t,200)}},{key:"handle",value:function(t){if(t=this.decode(t),this.batchUrl&&this.batchUrl===t.url&&"POST"===t.method)return this.batch(t);var e=!0,r=!1,n=void 0;try{for(var o,i=a(this.getCollectionNames());!(e=(o=i.next()).done);e=!0){var u=o.value,c=t.url.match(new RegExp("^"+this.baseUrl+"\\/("+u+")(\\/(\\d+))?(\\?.*)?$"));if(c){var s=h({},this.defaultQuery(u),t.params);if(c[2]){var f=c[3];if("GET"==t.method)try{var l=this.getOne(u,f,s);return this.respond(l,null,t)}catch(p){return t.respond(404)}if("PUT"==t.method)try{var l=this.updateOne(u,f,t.json);return this.respond(l,null,t)}catch(p){return t.respond(404)}if("PATCH"==t.method)try{var l=this.updateOne(u,f,t.json);return this.respond(l,null,t)}catch(p){return t.respond(404)}if("DELETE"==t.method)try{var l=this.removeOne(u,f);return this.respond(l,null,t)}catch(p){return t.respond(404)}}else{if("GET"==t.method){var d=this.getCount(u,s.filter?{filter:s.filter}:{}),y=void 0,v=void 0,g=void 0;if(d>0){y=this.getAll(u,s);var b=s.range?s.range[0]:0,m=s.range?Math.min(y.length-1+b,s.range[1]):y.length-1;v="items "+b+"-"+m+"/"+d,g=y.length==d?200:206}else y=[],v="items */0",g=200;return this.respond(y,{"Content-Range":v},t,g)}if("POST"==t.method){var O=this.addOne(u,t.json),j=this.baseUrl+"/"+u+"/"+O[this.getCollection(u).identifierName];return this.respond(O,{Location:j},t,201)}}}}}catch(w){r=!0,n=w}finally{try{!e&&i["return"]&&i["return"]()}finally{if(r)throw n}}}},{key:"getHandler",value:function(){return this.handle.bind(this)}}]),t}();e["default"]=y,t.exports=e["default"]},function(t,e,r){"use strict";function n(t,e){if("function"==typeof e)return t.filter(e);if(e instanceof Object){var r=a(e).map(function(t){if("q"===t){var r=function(){var t=new RegExp(e.q,"i");return{v:function(e){for(var r in e)if(e[r]&&e[r].match&&null!==e[r].match(t))return!0;return!1}}}();if("object"==typeof r)return r.v}var n=e[t];if(-1!==t.indexOf("_lte")){var o=function(){var e=t.replace(/(_lte)$/,"");return{v:function(t){return t[e]<=n}}}();if("object"==typeof o)return o.v}if(-1!==t.indexOf("_gte")){var i=function(){var e=t.replace(/(_gte)$/,"");return{v:function(t){return t[e]>=n}}}();if("object"==typeof i)return i.v}if(-1!==t.indexOf("_lt")){var u=function(){var e=t.replace(/(_lt)$/,"");return{v:function(t){return t[e]<n}}}();if("object"==typeof u)return u.v}if(-1!==t.indexOf("_gt")){var c=function(){var e=t.replace(/(_gt)$/,"");return{v:function(t){return t[e]>n}}}();if("object"==typeof c)return c.v}return Array.isArray(n)?function(e){return n.filter(function(r){return r==e[t]}).length>0}:function(e){return Array.isArray(e[t])&&"string"==typeof n?-1!==e[t].indexOf(n):"boolean"==typeof e[t]&&"string"==typeof n?e[t]==("true"===n?!0:!1):e[t]==n}});return t.filter(function(t){return r.reduce(function(e,r){return e&&r(t)},!0)})}throw new Error("Unsupported filter type")}function o(t,e){if("function"==typeof e)return t.sort(e);if("string"==typeof e)return t.sort(function(t,r){return t[e]>r[e]?1:t[e]<r[e]?-1:0});if(Array.isArray(e)){var r=function(){var r=e[0],n="asc"==e[1].toLowerCase()?1:-1;return{v:t.sort(function(t,e){return t[r]>e[r]?n:t[r]<e[r]?-1*n:0})}}();if("object"==typeof r)return r.v}throw new Error("Unsupported sort type")}function i(t,e){if(Array.isArray(e))return t.slice(e[0],void 0!==e[1]?e[1]+1:void 0);throw new Error("Unsupported range type")}var u=r(4)["default"],c=r(5)["default"],a=r(7)["default"],s=r(3)["default"];Object.defineProperty(e,"__esModule",{value:!0});var f=r(9),l=s(f);r(10),r(11);var p=function(){function t(){var e=arguments.length<=0||void 0===arguments[0]?[]:arguments[0],r=arguments.length<=1||void 0===arguments[1]?"id":arguments[1];if(c(this,t),!Array.isArray(e))throw new Error("Can't initialize a Collection with anything else than an array of items");this.sequence=0,this.identifierName=r,this.items=[],this.server=null,this.name=null,e.map(this.addOne.bind(this))}return u(t,[{key:"setServer",value:function(t){this.server=t}},{key:"setName",value:function(t){this.name=t}},{key:"_oneToManyEmbedder",value:function(t){var e=this,r=this.name.slice(0,-1),n=r+"_id";return function(r){var o=e.server.collections[t];if(!o)throw new Error("Can't embed a non-existing collection "+t);return r[t]=o.getAll(Array.isArray(r[t])?{filter:function(e){return-1!==r[t].indexOf(e[o.identifierName])}}:{filter:function(t){return t[n]==r[e.identifierName]}}),r}}},{key:"_manyToOneEmbedder",value:function(t){var e=this,r=t+"s",n=t+"_id";return function(o){var i=e.server.collections[r];if(!i)throw new Error("Can't embed a non-existing collection "+t);try{o[t]=i.getOne(o[n])}catch(u){}return o}}},{key:"_itemEmbedder",value:function(t){var e=this,r=Array.isArray(t)?t:[t],n=r.map(function(t){return t.endsWith("s")?e._oneToManyEmbedder(t):e._manyToOneEmbedder(t)});return function(t){return n.reduce(function(t,e){return e(t)},t)}}},{key:"getCount",value:function(t){return this.getAll(t).length}},{key:"getAll",value:function(t){var e=this.items.slice(0);return t&&(t.filter&&(e=n(e,t.filter)),t.sort&&(e=o(e,t.sort)),t.range&&(e=i(e,t.range)),t.embed&&this.server&&(e=e.map(function(t){return l["default"]({},t)}).map(this._itemEmbedder(t.embed)))),e}},{key:"getIndex",value:function(t){var e=this;return this.items.findIndex(function(r){return r[e.identifierName]==t})}},{key:"getOne",value:function(t,e){var r=this.getIndex(t);if(-1===r)throw new Error("No item with identifier "+t);var n=this.items[r];return e&&e.embed&&this.server&&(n=l["default"]({},n),n=this._itemEmbedder(e.embed)(n)),n}},{key:"addOne",value:function(t){var e=t[this.identifierName];if(void 0!==e){if(-1!==this.getIndex(e))throw new Error("An item with the identifier "+e+" already exists");this.sequence=Math.max(this.sequence,e)+1}else t[this.identifierName]=this.sequence++;return this.items.push(t),t}},{key:"updateOne",value:function(t,e){var r=this.getIndex(t);if(-1===r)throw new Error("No item with identifier "+t);for(var n in e)this.items[r][n]=e[n];return this.items[r]}},{key:"removeOne",value:function(t){var e=this.getIndex(t);if(-1===e)throw new Error("No item with identifier "+t);var r=this.items[e];return this.items.splice(e,1),t==this.sequence-1&&this.sequence--,r}}]),t}();e["default"]=p,t.exports=e["default"]},function(t,e){"use strict";e["default"]=function(t){return t&&t.__esModule?t:{"default":t}},e.__esModule=!0},function(t,e,r){"use strict";var n=r(12)["default"];e["default"]=function(){function t(t,e){for(var r=0;r<e.length;r++){var o=e[r];o.enumerable=o.enumerable||!1,o.configurable=!0,"value"in o&&(o.writable=!0),n(t,o.key,o)}}return function(e,r,n){return r&&t(e.prototype,r),n&&t(e,n),e}}(),e.__esModule=!0},function(t,e){"use strict";e["default"]=function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")},e.__esModule=!0},function(t,e,r){"use strict";var n=r(8)["default"],o=r(13)["default"];e["default"]=function(){function t(t,e){var r=[],o=!0,i=!1,u=void 0;try{for(var c,a=n(t);!(o=(c=a.next()).done)&&(r.push(c.value),!e||r.length!==e);o=!0);}catch(s){i=!0,u=s}finally{try{!o&&a["return"]&&a["return"]()}finally{if(i)throw u}}return r}return function(e,r){if(Array.isArray(e))return e;if(o(Object(e)))return t(e,r);throw new TypeError("Invalid attempt to destructure non-iterable instance")}}(),e.__esModule=!0},function(t,e,r){t.exports={"default":r(18),__esModule:!0}},function(t,e,r){t.exports={"default":r(17),__esModule:!0}},function(t,e,r){"use strict";var n=r(21),o=r(14),i=r(15),u=r(16);n(o,{implementation:o,getPolyfill:i,shim:u}),t.exports=o},function(){!function(){if(!Array.prototype.findIndex){var t=function(t){var e=Object(this),r=Math.max(0,e.length)>>>0;if(0===r)return-1;if("function"!=typeof t||"[object Function]"!==Object.prototype.toString.call(t))throw new TypeError("Array#findIndex: predicate must be a function");for(var n=arguments.length>1?arguments[1]:void 0,o=0;r>o;o++)if(t.call(n,e[o],o,e))return o;return-1};if(Object.defineProperty)try{Object.defineProperty(Array.prototype,"findIndex",{value:t,configurable:!0,writable:!0})}catch(e){}Array.prototype.findIndex||(Array.prototype.findIndex=t)}}(this)},function(){/*! http://mths.be/endswith v0.2.0 by @mathias */ | ||
String.prototype.endsWith||!function(){"use strict";var t=function(){try{var t={},e=Object.defineProperty,r=e(t,t,t)&&e}catch(n){}return r}(),e={}.toString,r=function(t){if(null==this)throw TypeError();var r=String(this);if(t&&"[object RegExp]"==e.call(t))throw TypeError();var n=r.length,o=String(t),i=o.length,u=n;if(arguments.length>1){var c=arguments[1];void 0!==c&&(u=c?Number(c):0,u!=u&&(u=0))}var a=Math.min(Math.max(u,0),n),s=a-i;if(0>s)return!1;for(var f=-1;++f<i;)if(r.charCodeAt(s+f)!=o.charCodeAt(f))return!1;return!0};t?t(String.prototype,"endsWith",{value:r,configurable:!0,writable:!0}):String.prototype.endsWith=r}()},function(t,e,r){t.exports={"default":r(20),__esModule:!0}},function(t,e,r){t.exports={"default":r(19),__esModule:!0}},function(t,e,r){"use strict";var n=r(23),o=r(24),i=function(t){return"undefined"!=typeof t&&null!==t},u=r(22)(),c=Object,a=o.call(Function.call,Array.prototype.push),s=o.call(Function.call,Object.prototype.propertyIsEnumerable);t.exports=function(t){if(!i(t))throw new TypeError("target must be an object");var e,r,o,f,l,p=c(t);for(e=1;e<arguments.length;++e){if(r=c(arguments[e]),f=n(r),u&&Object.getOwnPropertySymbols)for(l=Object.getOwnPropertySymbols(r),o=0;o<l.length;++o)s(r,l[o])&&a(f,l[o]);for(o=0;o<f.length;++o)p[f[o]]=r[f[o]]}return p}},function(t,e,r){"use strict";var n=r(14),o=function(){if(!Object.assign||!Object.preventExtensions)return!1;var t=Object.preventExtensions({1:2});try{Object.assign(t,"xy")}catch(e){return"y"===t[1]}};t.exports=function(){return!Object.assign||o()?n:Object.assign}},function(t,e,r){"use strict";var n=r(21),o=r(15);t.exports=function(){var t=o();return Object.assign!==t&&n(Object,{assign:t}),t}},function(t,e,r){r(25),r(26),r(27),t.exports=r(28).core.getIterator},function(t,e,r){r(29),t.exports=r(28).core.Object.keys},function(t,e,r){r(25),r(26),r(27),t.exports=r(28).core.isIterable},function(t,e,r){var n=r(28);t.exports=function(t,e,r){return n.setDesc(t,e,r)}},function(t,e,r){"use strict";var n=r(23),o=r(30),i="function"==typeof Symbol&&"symbol"==typeof Symbol(),u=Object.prototype.toString,c=function(t){return"function"==typeof t&&"[object Function]"===u.call(t)},a=function(){var t={};try{Object.defineProperty(t,"x",{value:t,enumerable:!1});for(var e in t)return!1;return t.x===t}catch(r){return!1}},s=Object.defineProperty&&a(),f=function(t,e,r,n){(!(e in t)||c(n)&&n())&&(s?Object.defineProperty(t,e,{configurable:!0,enumerable:!1,writable:!0,value:r}):t[e]=r)},l=function(t,e){var r=arguments.length>2?arguments[2]:{},u=n(e);i&&(u=u.concat(Object.getOwnPropertySymbols(e))),o(u,function(n){f(t,n,e[n],r[n])})};l.supportsDescriptors=!!s,t.exports=l},function(t,e,r){"use strict";var n=r(23);t.exports=function(){if("function"!=typeof Symbol||"function"!=typeof Object.getOwnPropertySymbols)return!1;if("symbol"==typeof Symbol.iterator)return!0;var t={},e=Symbol("test");if("string"==typeof e)return!1;if(e instanceof Symbol)return!1;t[e]=42;for(e in t)return!1;if(0!==n(t).length)return!1;if("function"==typeof Object.keys&&0!==Object.keys(t).length)return!1;if("function"==typeof Object.getOwnPropertyNames&&0!==Object.getOwnPropertyNames(t).length)return!1;var r=Object.getOwnPropertySymbols(t);if(1!==r.length||r[0]!==e)return!1;if(!Object.prototype.propertyIsEnumerable.call(t,e))return!1;if("function"==typeof Object.getOwnPropertyDescriptor){var o=Object.getOwnPropertyDescriptor(t,e);if(42!==o.value||o.enumerable!==!0)return!1}return!0}},function(t,e,r){"use strict";var n=Object.prototype.hasOwnProperty,o=Object.prototype.toString,i=Array.prototype.slice,u=r(31),c=!{toString:null}.propertyIsEnumerable("toString"),a=function(){}.propertyIsEnumerable("prototype"),s=["toString","toLocaleString","valueOf","hasOwnProperty","isPrototypeOf","propertyIsEnumerable","constructor"],f=function(t){var e=t.constructor;return e&&e.prototype===t},l={$window:!0,$console:!0,$parent:!0,$self:!0,$frames:!0,$webkitIndexedDB:!0,$webkitStorageInfo:!0},p=function(){if("undefined"==typeof window)return!1;for(var t in window)if(!l["$"+t]&&n.call(window,t)&&null!==window[t]&&"object"==typeof window[t])try{f(window[t])}catch(e){return!0}return!1}(),d=function(t){if("undefined"==typeof window&&!p)return f(t);try{return f(t)}catch(e){return!1}},h=function(t){var e=null!==t&&"object"==typeof t,r="[object Function]"===o.call(t),i=u(t),f=e&&"[object String]"===o.call(t),l=[];if(!e&&!r&&!i)throw new TypeError("Object.keys called on a non-object");var p=a&&r;if(f&&t.length>0&&!n.call(t,0))for(var h=0;h<t.length;++h)l.push(String(h));if(i&&t.length>0)for(var y=0;y<t.length;++y)l.push(String(y));else for(var v in t)p&&"prototype"===v||!n.call(t,v)||l.push(String(v));if(c)for(var g=d(t),b=0;b<s.length;++b)g&&"constructor"===s[b]||!n.call(t,s[b])||l.push(s[b]);return l};h.shim=function(){if(Object.keys){var t=function(){return 2===(Object.keys(arguments)||"").length}(1,2);if(!t){var e=Object.keys;Object.keys=function(t){return e(u(t)?i.call(t):t)}}}else Object.keys=h;return Object.keys||h},t.exports=h},function(t){var e="Function.prototype.bind called on incompatible ",r=Array.prototype.slice,n=Object.prototype.toString,o="[object Function]";t.exports=function(t){var i=this;if("function"!=typeof i||n.call(i)!==o)throw new TypeError(e+i);for(var u=r.call(arguments,1),c=function(){if(this instanceof l){var e=i.apply(this,u.concat(r.call(arguments)));return Object(e)===e?e:this}return i.apply(t,u.concat(r.call(arguments)))},a=Math.max(0,i.length-u.length),s=[],f=0;a>f;f++)s.push("$"+f);var l=Function("binder","return function ("+s.join(",")+"){ return binder.apply(this,arguments); }")(c);if(i.prototype){var p=function(){};p.prototype=i.prototype,l.prototype=new p,p.prototype=null}return l}},function(t,e,r){r(32);var n=r(28),o=r(33).Iterators,i=r(34)("iterator"),u=o.Array,c=n.g.NodeList,a=n.g.HTMLCollection,s=c&&c.prototype,f=a&&a.prototype;n.FW&&(!c||i in s||n.hide(s,i,u),!a||i in f||n.hide(f,i,u)),o.NodeList=o.HTMLCollection=u},function(t,e,r){var n=r(28).set,o=r(35)(!0),i=r(36).safe("iter"),u=r(33),c=u.step;r(37)(String,"String",function(t){n(this,i,{o:String(t),i:0})},function(){var t,e=this[i],r=e.o,n=e.i;return n>=r.length?c(1):(t=o(r,n),e.i+=t.length,c(0,t))})},function(t,e,r){var n=r(28).core,o=r(33);n.isIterable=o.is,n.getIterator=o.get},function(t,e,r){"use strict";function n(t){return isNaN(t=+t)?0:(t>0?y:h)(t)}function o(t,e){return{enumerable:!(1&t),configurable:!(2&t),writable:!(4&t),value:e}}function i(t,e,r){return t[e]=r,t}function u(t){return b?function(e,r,n){return O.setDesc(e,r,o(t,n))}:i}function c(t){return null!==t&&("object"==typeof t||"function"==typeof t)}function a(t){return"function"==typeof t}function s(t){if(void 0==t)throw TypeError("Can't call method on "+t);return t}var f="undefined"!=typeof self?self:Function("return this")(),l={},p=Object.defineProperty,d={}.hasOwnProperty,h=Math.ceil,y=Math.floor,v=Math.max,g=Math.min,b=!!function(){try{return 2==p({},"a",{get:function(){return 2}}).a}catch(t){}}(),m=u(1),O=t.exports=r(38)({g:f,core:l,html:f.document&&document.documentElement,isObject:c,isFunction:a,that:function(){return this},toInteger:n,toLength:function(t){return t>0?g(n(t),9007199254740991):0},toIndex:function(t,e){return t=n(t),0>t?v(t+e,0):g(t,e)},has:function(t,e){return d.call(t,e)},create:Object.create,getProto:Object.getPrototypeOf,DESC:b,desc:o,getDesc:Object.getOwnPropertyDescriptor,setDesc:p,setDescs:Object.defineProperties,getKeys:Object.keys,getNames:Object.getOwnPropertyNames,getSymbols:Object.getOwnPropertySymbols,assertDefined:s,ES5Object:Object,toObject:function(t){return O.ES5Object(s(t))},hide:m,def:u(0),set:f.Symbol?i:m,each:[].forEach});"undefined"!=typeof __e&&(__e=l),"undefined"!=typeof __g&&(__g=f)},function(t,e,r){var n=r(28),o=r(39),i=n.isObject,u=n.toObject;n.each.call("freeze,seal,preventExtensions,isFrozen,isSealed,isExtensible,getOwnPropertyDescriptor,getPrototypeOf,keys,getOwnPropertyNames".split(","),function(t,e){var c=(n.core.Object||{})[t]||Object[t],a=0,s={};s[t]=0==e?function(t){return i(t)?c(t):t}:1==e?function(t){return i(t)?c(t):t}:2==e?function(t){return i(t)?c(t):t}:3==e?function(t){return i(t)?c(t):!0}:4==e?function(t){return i(t)?c(t):!0}:5==e?function(t){return i(t)?c(t):!1}:6==e?function(t,e){return c(u(t),e)}:7==e?function(t){return c(Object(n.assertDefined(t)))}:8==e?function(t){return c(u(t))}:r(40).get;try{c("z")}catch(f){a=1}o(o.S+o.F*a,"Object",s)})},function(t){var e=Object.prototype.hasOwnProperty,r=Object.prototype.toString;t.exports=function(t,n,o){if("[object Function]"!==r.call(n))throw new TypeError("iterator must be a function");var i=t.length;if(i===+i)for(var u=0;i>u;u++)n.call(o,t[u],u,t);else for(var c in t)e.call(t,c)&&n.call(o,t[c],c,t)}},function(t){"use strict";var e=Object.prototype.toString;t.exports=function(t){var r=e.call(t),n="[object Arguments]"===r;return n||(n="[object Array]"!==r&&null!==t&&"object"==typeof t&&"number"==typeof t.length&&t.length>=0&&"[object Function]"===e.call(t.callee)),n}},function(t,e,r){var n=r(28),o=r(41),i=r(36).safe("iter"),u=r(33),c=u.step,a=u.Iterators;r(37)(Array,"Array",function(t,e){n.set(this,i,{o:n.toObject(t),i:0,k:e})},function(){var t=this[i],e=t.o,r=t.k,n=t.i++;return!e||n>=e.length?(t.o=void 0,c(1)):"keys"==r?c(0,n):"values"==r?c(0,e[n]):c(0,[n,e[n]])},"values"),a.Arguments=a.Array,o("keys"),o("values"),o("entries")},function(t,e,r){"use strict";function n(t,e){o.hide(t,s,e),f in[]&&o.hide(t,f,e)}var o=r(28),i=r(42),u=i.classof,c=r(43),a=c.obj,s=r(34)("iterator"),f="@@iterator",l=r(44)("iterators"),p={};n(p,o.that),t.exports={BUGGY:"keys"in[]&&!("next"in[].keys()),Iterators:l,step:function(t,e){return{value:e,done:!!t}},is:function(t){var e=Object(t),r=o.g.Symbol;return(r&&r.iterator||f)in e||s in e||o.has(l,u(e))},get:function(t){var e,r=o.g.Symbol;return void 0!=t&&(e=t[r&&r.iterator||f]||t[s]||l[u(t)]),c(o.isFunction(e),t," is not iterable!"),a(e.call(t))},set:n,create:function(t,e,r,n){t.prototype=o.create(n||p,{next:o.desc(1,r)}),i.set(t,e+" Iterator")}}},function(t,e,r){var n=r(28).g,o=r(44)("wks");t.exports=function(t){return o[t]||(o[t]=n.Symbol&&n.Symbol[t]||r(36).safe("Symbol."+t))}},function(t,e,r){var n=r(28);t.exports=function(t){return function(e,r){var o,i,u=String(n.assertDefined(e)),c=n.toInteger(r),a=u.length;return 0>c||c>=a?t?"":void 0:(o=u.charCodeAt(c),55296>o||o>56319||c+1===a||(i=u.charCodeAt(c+1))<56320||i>57343?t?u.charAt(c):o:t?u.slice(c,c+2):(o-55296<<10)+(i-56320)+65536)}}},function(t,e,r){function n(t){return"Symbol(".concat(void 0===t?"":t,")_",(++o+Math.random()).toString(36))}var o=0;n.safe=r(28).g.Symbol||n,t.exports=n},function(t,e,r){var n=r(39),o=r(45),i=r(28),u=r(42),c=r(33),a=r(34)("iterator"),s="@@iterator",f="keys",l="values",p=c.Iterators;t.exports=function(t,e,r,d,h,y,v){function g(t){function e(e){return new r(e,t)}switch(t){case f:return function(){return e(this)};case l:return function(){return e(this)}}return function(){return e(this)}}c.create(r,e,d);var b,m,O=e+" Iterator",j=t.prototype,w=j[a]||j[s]||h&&j[h],x=w||g(h);if(w){var S=i.getProto(x.call(new t));u.set(S,O,!0),i.FW&&i.has(j,s)&&c.set(S,i.that)}if((i.FW||v)&&c.set(j,x),p[e]=x,p[O]=i.that,h)if(b={keys:y?x:g(f),values:h==l?x:g(l),entries:h!=l?x:g("entries")},v)for(m in b)m in j||o(j,m,b[m]);else n(n.P+n.F*c.BUGGY,e,b)}},function(t){t.exports=function(t){return t.FW=!1,t.path=t.core,t}},function(t,e,r){function n(t,e){return function(){return t.apply(e,arguments)}}function o(t,e,r){var i,s,f,l,p=t&o.G,d=t&o.P,h=p?u:t&o.S?u[e]:(u[e]||{}).prototype,y=p?c:c[e]||(c[e]={});p&&(r=e);for(i in r)s=!(t&o.F)&&h&&i in h,s&&i in y||(f=s?h[i]:r[i],p&&!a(h[i])?l=r[i]:t&o.B&&s?l=n(f,u):t&o.W&&h[i]==f?!function(t){l=function(e){return this instanceof t?new t(e):t(e)},l.prototype=t.prototype}(f):l=d&&a(f)?n(Function.call,f):f,y[i]=l,d&&((y.prototype||(y.prototype={}))[i]=f))}var i=r(28),u=i.g,c=i.core,a=i.isFunction;o.F=1,o.G=2,o.S=4,o.P=8,o.B=16,o.W=32,t.exports=o},function(t,e,r){function n(t){try{return u(t)}catch(e){return c.slice()}}var o=r(28),i={}.toString,u=o.getNames,c="object"==typeof window&&Object.getOwnPropertyNames?Object.getOwnPropertyNames(window):[];t.exports.get=function(t){return c&&"[object Window]"==i.call(t)?n(t):u(o.toObject(t))}},function(t){t.exports=function(){}},function(t,e,r){function n(t){return u.call(t).slice(8,-1)}var o=r(28),i=r(34)("toStringTag"),u={}.toString;n.classof=function(t){var e,r;return void 0==t?void 0===t?"Undefined":"Null":"string"==typeof(r=(e=Object(t))[i])?r:n(e)},n.set=function(t,e,r){t&&!o.has(t=r?t:t.prototype,i)&&o.hide(t,i,e)},t.exports=n},function(t,e,r){function n(t,e,r){if(!t)throw TypeError(r?e+r:e)}var o=r(28);n.def=o.assertDefined,n.fn=function(t){if(!o.isFunction(t))throw TypeError(t+" is not a function!");return t},n.obj=function(t){if(!o.isObject(t))throw TypeError(t+" is not an object!");return t},n.inst=function(t,e,r){if(!(t instanceof e))throw TypeError(r+": use the 'new' operator!");return t},t.exports=n},function(t,e,r){var n=r(28),o="__core-js_shared__",i=n.g[o]||(n.g[o]={});t.exports=function(t){return i[t]||(i[t]={})}},function(t,e,r){t.exports=r(28).hide}])}); | ||
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):"object"==typeof exports?exports.FakeRest=t():e.FakeRest=t()}(this,function(){return function(e){function t(n){if(r[n])return r[n].exports;var o=r[n]={exports:{},id:n,loaded:!1};return e[n].call(o.exports,o,o.exports,t),o.loaded=!0,o.exports}var r={};return t.m=e,t.c=r,t.p="",t(0)}([function(e,t,r){"use strict";var n=r(5)["default"];Object.defineProperty(t,"__esModule",{value:!0});var o=r(1),i=n(o),u=r(2),a=n(u),s=r(3),c=n(s),l=r(4),f=n(l);t["default"]={Server:i["default"],FetchServer:a["default"],Collection:c["default"],Single:f["default"]},e.exports=t["default"]},function(e,t,r){"use strict";var n=r(6)["default"],o=r(7)["default"],i=r(8)["default"],u=r(9)["default"],a=r(5)["default"];Object.defineProperty(t,"__esModule",{value:!0});var s=r(13),c=a(s),l=r(3),f=a(l),d=r(4),p=a(d),h=r(10),y=a(h),v=c["default"].getPolyfill(),g=function(){function e(){var t=arguments.length<=0||void 0===arguments[0]?"":arguments[0];o(this,e),this.baseUrl=t,this.loggingEnabled=!1,this.defaultQuery=function(){},this.batchUrl=null,this.collections={},this.singles={},this.requestInterceptors=[],this.responseInterceptors=[]}return n(e,[{key:"init",value:function(e){for(var t in e)Array.isArray(e[t])?this.addCollection(t,new f["default"](e[t],"id")):this.addSingle(t,new p["default"](e[t]))}},{key:"toggleLogging",value:function(){this.loggingEnabled=!this.loggingEnabled}},{key:"setDefaultQuery",value:function(e){this.defaultQuery=e}},{key:"setBatchUrl",value:function(e){this.batchUrl=e}},{key:"setBatch",value:function(e){console.warn("Server.setBatch() is deprecated, use Server.setBatchUrl() instead"),this.batchUrl=e}},{key:"addCollection",value:function(e,t){this.collections[e]=t,t.setServer(this),t.setName(e)}},{key:"getCollection",value:function(e){return this.collections[e]}},{key:"getCollectionNames",value:function(){return i(this.collections)}},{key:"addSingle",value:function(e,t){this.singles[e]=t,t.setServer(this),t.setName(e)}},{key:"getSingle",value:function(e){return this.singles[e]}},{key:"getSingleNames",value:function(){return i(this.singles)}},{key:"addRequestInterceptor",value:function(e){this.requestInterceptors.push(e)}},{key:"addResponseInterceptor",value:function(e){this.responseInterceptors.push(e)}},{key:"getCount",value:function(e,t){return this.collections[e].getCount(t)}},{key:"getAll",value:function(e,t){return this.collections[e].getAll(t)}},{key:"getOne",value:function(e,t,r){return this.collections[e].getOne(t,r)}},{key:"addOne",value:function(e,t){return this.collections[e].addOne(t)}},{key:"updateOne",value:function(e,t,r){return this.collections[e].updateOne(t,r)}},{key:"removeOne",value:function(e,t){return this.collections[e].removeOne(t)}},{key:"getOnly",value:function(e){return this.singles[e].getOnly()}},{key:"updateOnly",value:function(e,t){return this.singles[e].updateOnly(t)}},{key:"decode",value:function(e){if(e.queryString=decodeURIComponent(e.url.slice(e.url.indexOf("?")+1)),e.params=y["default"](e.queryString),e.requestBody)try{e.json=JSON.parse(e.requestBody)}catch(t){}return this.requestInterceptors.reduce(function(e,t){return t(e)},e)}},{key:"respond",value:function(e,t,r){var n=arguments.length<=3||void 0===arguments[3]?200:arguments[3];t||(t={}),t["Content-Type"]||(t["Content-Type"]="application/json");var o={status:n,headers:t,body:e};return o=this.responseInterceptors.reduce(function(e,t){return t(e,r)},o),this.log(r,o),r.respond(o.status,o.headers,JSON.stringify(o.body))}},{key:"log",value:function(e,t){this.loggingEnabled&&(console.group?(console.groupCollapsed(e.method,e.url,"(FakeRest)"),console.group("request"),console.log(e.method,e.url),console.log("headers",e.requestHeaders),console.log("body ",e.requestBody),console.groupEnd(),console.group("response",t.status),console.log("headers",t.headers),console.log("body ",t.body),console.groupEnd(),console.groupEnd()):(console.log("FakeRest request ",e.method,e.url,"headers",e.requestHeaders,"body",e.requestBody),console.log("FakeRest response",t.status,"headers",t.headers,"body",t.body)))}},{key:"batch",value:function(e){var t=e.json,r=this.handle.bind(this),n=i(t).reduce(function(e,n){var o,u={url:t[n],method:"GET",params:{},respond:function(e,t,r){o={code:e,headers:i(t||{}).map(function(e){return{name:e,value:t[e]}}),body:r||{}}}};return r(u),e[n]=o||{code:404,headers:[],body:{}},e},{});return this.respond(n,{},e,200)}},{key:"handle",value:function(e){if(e=this.decode(e),this.batchUrl&&this.batchUrl===e.url&&"POST"===e.method)return this.batch(e);var t=!0,r=!1,n=void 0;try{for(var o,i=u(this.getSingleNames());!(t=(o=i.next()).done);t=!0){var a=o.value,s=e.url.match(new RegExp("^"+this.baseUrl+"\\/("+a+")(\\/?.*)?$"));if(s){if("GET"==e.method)try{var c=this.getOnly(a);return this.respond(c,null,e)}catch(l){return e.respond(404)}if("PUT"==e.method)try{var c=this.updateOnly(a,e.json);return this.respond(c,null,e)}catch(l){return e.respond(404)}if("PATCH"==e.method)try{var c=this.updateOnly(a,e.json);return this.respond(c,null,e)}catch(l){return e.respond(404)}}}}catch(f){r=!0,n=f}finally{try{!t&&i["return"]&&i["return"]()}finally{if(r)throw n}}var d=!0,p=!1,h=void 0;try{for(var y,g=u(this.getCollectionNames());!(d=(y=g.next()).done);d=!0){var b=y.value,s=e.url.match(new RegExp("^"+this.baseUrl+"\\/("+b+")(\\/(\\d+))?(\\?.*)?$"));if(s){var m=v({},this.defaultQuery(b),e.params);if(s[2]){var O=s[3];if("GET"==e.method)try{var c=this.getOne(b,O,m);return this.respond(c,null,e)}catch(l){return e.respond(404)}if("PUT"==e.method)try{var c=this.updateOne(b,O,e.json);return this.respond(c,null,e)}catch(l){return e.respond(404)}if("PATCH"==e.method)try{var c=this.updateOne(b,O,e.json);return this.respond(c,null,e)}catch(l){return e.respond(404)}if("DELETE"==e.method)try{var c=this.removeOne(b,O);return this.respond(c,null,e)}catch(l){return e.respond(404)}}else{if("GET"==e.method){var j=this.getCount(b,m.filter?{filter:m.filter}:{}),w=void 0,x=void 0,E=void 0;if(j>0){w=this.getAll(b,m);var S=m.range?m.range[0]:0,k=m.range?Math.min(w.length-1+S,m.range[1]):w.length-1;x="items "+S+"-"+k+"/"+j,E=w.length==j?200:206}else w=[],x="items */0",E=200;return this.respond(w,{"Content-Range":x},e,E)}if("POST"==e.method){var _=this.addOne(b,e.json),P=this.baseUrl+"/"+b+"/"+_[this.getCollection(b).identifierName];return this.respond(_,{Location:P},e,201)}}}}}catch(f){p=!0,h=f}finally{try{!d&&g["return"]&&g["return"]()}finally{if(p)throw h}}}},{key:"getHandler",value:function(){return this.handle.bind(this)}}]),e}();t["default"]=g,e.exports=t["default"]},function(e,t,r){"use strict";var n=r(11)["default"],o=r(12)["default"],i=r(6)["default"],u=r(7)["default"],a=r(9)["default"],s=r(5)["default"];Object.defineProperty(t,"__esModule",{value:!0});var c=r(13),l=s(c),f=r(1),d=s(f),p=r(3),h=(s(p),r(4)),y=(s(h),r(10)),v=s(y),g=l["default"].getPolyfill(),b=function(e){function t(){u(this,t),n(Object.getPrototypeOf(t.prototype),"constructor",this).apply(this,arguments)}return o(t,e),i(t,[{key:"decode",value:function(e,t){var r=this,n="string"==typeof e?new Request(e,t):e;return n.queryString=decodeURIComponent(n.url.slice(n.url.indexOf("?")+1)),n.params=v["default"](n.queryString),n.text().then(function(e){n.requestBody=e;try{n.requestJson=JSON.parse(e)}catch(t){}}).then(function(){return r.requestInterceptors.reduce(function(e,t){return t(e)},n)})}},{key:"respond",value:function(e,t){return e=this.responseInterceptors.reduce(function(e,r){return r(e,t)},e),this.log(t,e),e}},{key:"log",value:function(e,t){this.loggingEnabled&&(console.group?(console.groupCollapsed(e.method,e.url,"(FakeRest)"),console.group("request"),console.log(e.method,e.url),console.log("headers",e.requestHeaders),console.log("body ",e.requestBody),console.groupEnd(),console.group("response",t.status),console.log("headers",t.headers),console.log("body ",t.body),console.groupEnd(),console.groupEnd()):(console.log("FakeRest request ",e.method,e.url,"headers",e.requestHeaders,"body",e.requestBody),console.log("FakeRest response",t.status,"headers",t.headers,"body",t.body)))}},{key:"batch",value:function(){throw new Error("not implemented")}},{key:"handle",value:function(e,t){var r=this;return this.decode(e,t).then(function(e){var t={headers:{"Content-Type":"application/json"},status:200};if(r.batchUrl&&r.batchUrl===e.url&&"POST"===e.method)return r.batch(e);var n=!0,o=!1,i=void 0;try{for(var u,s=a(r.getSingleNames());!(n=(u=s.next()).done);n=!0){var c=u.value,l=e.url.match(new RegExp("^"+r.baseUrl+"\\/("+c+")(\\/?.*)?$"));if(l){if("GET"==e.method){try{t.body=r.getOnly(c)}catch(f){reponse.status=404}return r.respond(t,e)}if("PUT"==e.method){try{t.body=r.updateOnly(c,e.requestJson)}catch(f){reponse.status=404}return r.respond(t,e)}if("PATCH"==e.method){try{t.body=r.updateOnly(c,e.requestJson)}catch(f){reponse.status=404}return r.respond(t,e)}}}}catch(d){o=!0,i=d}finally{try{!n&&s["return"]&&s["return"]()}finally{if(o)throw i}}var p=!0,h=!1,y=void 0;try{for(var v,b=a(r.getCollectionNames());!(p=(v=b.next()).done);p=!0){var m=v.value,l=e.url.match(new RegExp("^"+r.baseUrl+"\\/("+m+")(\\/(\\d+))?(\\?.*)?$"));if(l){var O=g({},r.defaultQuery(m),e.params);if(l[2]){var j=l[3];if("GET"==e.method){try{t.body=r.getOne(m,j,O)}catch(f){t.status=404}return r.respond(t,e)}if("PUT"==e.method){try{t.body=r.updateOne(m,j,e.requestJson)}catch(f){t.status=404}return r.respond(t,e)}if("PATCH"==e.method){try{t.body=r.updateOne(m,j,e.requestJson)}catch(f){t.status=404}return r.respond(t,e)}if("DELETE"==e.method){try{t.body=r.removeOne(m,j)}catch(f){t.status=404}return r.respond(t,e)}}else{if("GET"==e.method){var w=r.getCount(m,O.filter?{filter:O.filter}:{});if(w>0){var x=r.getAll(m,O),E=O.range?O.range[0]:0,S=O.range?Math.min(x.length-1+E,O.range[1]):x.length-1;t.body=x,t.headers["Content-Range"]="items "+E+"-"+S+"/"+w,t.status=x.length==w?200:206}else t.body=[],t.headers["Content-Range"]="items */0";return r.respond(t,e)}if("POST"==e.method){var k=r.addOne(m,e.requestJson),_=r.baseUrl+"/"+m+"/"+k[r.getCollection(m).identifierName];return t.body=k,t.headers.Location=_,t.status=201,r.respond(t,e)}}}}}catch(d){h=!0,y=d}finally{try{!p&&b["return"]&&b["return"]()}finally{if(h)throw y}}return r.respond(t,e)})}}]),t}(d["default"]);t["default"]=b,e.exports=t["default"]},function(e,t,r){"use strict";function n(e,t){if("function"==typeof t)return e.filter(t);if(t instanceof Object){var r=s(t).map(function(e){if("q"===e){var r=function(){var e=new RegExp(t.q,"i");return{v:function(t){for(var r in t)if(t[r]&&t[r].match&&null!==t[r].match(e))return!0;return!1}}}();if("object"==typeof r)return r.v}var n=t[e];if(-1!==e.indexOf("_lte")){var o=function(){var t=e.replace(/(_lte)$/,"");return{v:function(e){return e[t]<=n}}}();if("object"==typeof o)return o.v}if(-1!==e.indexOf("_gte")){var i=function(){var t=e.replace(/(_gte)$/,"");return{v:function(e){return e[t]>=n}}}();if("object"==typeof i)return i.v}if(-1!==e.indexOf("_lt")){var u=function(){var t=e.replace(/(_lt)$/,"");return{v:function(e){return e[t]<n}}}();if("object"==typeof u)return u.v}if(-1!==e.indexOf("_gt")){var a=function(){var t=e.replace(/(_gt)$/,"");return{v:function(e){return e[t]>n}}}();if("object"==typeof a)return a.v}return Array.isArray(n)?function(t){return n.filter(function(r){return r==t[e]}).length>0}:function(t){return Array.isArray(t[e])&&"string"==typeof n?-1!==t[e].indexOf(n):"boolean"==typeof t[e]&&"string"==typeof n?t[e]==("true"===n?!0:!1):t[e]==n}});return e.filter(function(e){return r.reduce(function(t,r){return t&&r(e)},!0)})}throw new Error("Unsupported filter type")}function o(e,t){if("function"==typeof t)return e.sort(t);if("string"==typeof t)return e.sort(function(e,r){return e[t]>r[t]?1:e[t]<r[t]?-1:0});if(Array.isArray(t)){var r=function(){var r=t[0],n="asc"==t[1].toLowerCase()?1:-1;return{v:e.sort(function(e,t){return e[r]>t[r]?n:e[r]<t[r]?-1*n:0})}}();if("object"==typeof r)return r.v}throw new Error("Unsupported sort type")}function i(e,t){if(Array.isArray(t))return e.slice(t[0],void 0!==t[1]?t[1]+1:void 0);throw new Error("Unsupported range type")}var u=r(6)["default"],a=r(7)["default"],s=r(8)["default"],c=r(5)["default"];Object.defineProperty(t,"__esModule",{value:!0});var l=r(13),f=c(l);r(14),r(19);var d=function(){function e(){var t=arguments.length<=0||void 0===arguments[0]?[]:arguments[0],r=arguments.length<=1||void 0===arguments[1]?"id":arguments[1];if(a(this,e),!Array.isArray(t))throw new Error("Can't initialize a Collection with anything else than an array of items");this.sequence=0,this.identifierName=r,this.items=[],this.server=null,this.name=null,t.map(this.addOne.bind(this))}return u(e,[{key:"setServer",value:function(e){this.server=e}},{key:"setName",value:function(e){this.name=e}},{key:"_oneToManyEmbedder",value:function(e){var t=this,r=this.name.slice(0,-1),n=r+"_id";return function(r){var o=t.server.collections[e];if(!o)throw new Error("Can't embed a non-existing collection "+e);return r[e]=o.getAll(Array.isArray(r[e])?{filter:function(t){return-1!==r[e].indexOf(t[o.identifierName])}}:{filter:function(e){return e[n]==r[t.identifierName]}}),r}}},{key:"_manyToOneEmbedder",value:function(e){var t=this,r=e+"s",n=e+"_id";return function(o){var i=t.server.collections[r];if(!i)throw new Error("Can't embed a non-existing collection "+e);try{o[e]=i.getOne(o[n])}catch(u){}return o}}},{key:"_itemEmbedder",value:function(e){var t=this,r=Array.isArray(e)?e:[e],n=r.map(function(e){return e.endsWith("s")?t._oneToManyEmbedder(e):t._manyToOneEmbedder(e)});return function(e){return n.reduce(function(e,t){return t(e)},e)}}},{key:"getCount",value:function(e){return this.getAll(e).length}},{key:"getAll",value:function(e){var t=this.items.slice(0);return e&&(e.filter&&(t=n(t,e.filter)),e.sort&&(t=o(t,e.sort)),e.range&&(t=i(t,e.range)),e.embed&&this.server&&(t=t.map(function(e){return f["default"]({},e)}).map(this._itemEmbedder(e.embed)))),t}},{key:"getIndex",value:function(e){var t=this;return this.items.findIndex(function(r){return r[t.identifierName]==e})}},{key:"getOne",value:function(e,t){var r=this.getIndex(e);if(-1===r)throw new Error("No item with identifier "+e);var n=this.items[r];return t&&t.embed&&this.server&&(n=f["default"]({},n),n=this._itemEmbedder(t.embed)(n)),n}},{key:"addOne",value:function(e){var t=e[this.identifierName];if(void 0!==t){if(-1!==this.getIndex(t))throw new Error("An item with the identifier "+t+" already exists");this.sequence=Math.max(this.sequence,t)+1}else e[this.identifierName]=this.sequence++;return this.items.push(e),e}},{key:"updateOne",value:function(e,t){var r=this.getIndex(e);if(-1===r)throw new Error("No item with identifier "+e);for(var n in t)this.items[r][n]=t[n];return this.items[r]}},{key:"removeOne",value:function(e){var t=this.getIndex(e);if(-1===t)throw new Error("No item with identifier "+e);var r=this.items[t];return this.items.splice(t,1),e==this.sequence-1&&this.sequence--,r}}]),e}();t["default"]=d,e.exports=t["default"]},function(e,t,r){"use strict";var n=r(6)["default"],o=r(7)["default"],i=r(5)["default"];Object.defineProperty(t,"__esModule",{value:!0});var u=r(13),a=i(u);r(19);var s=function(){function e(t){if(o(this,e),!(t instanceof Object))throw new Error("Can't initialize a Single with anything except an object");this.obj=t,this.server=null,this.name=null}return n(e,[{key:"setServer",value:function(e){this.server=e}},{key:"setName",value:function(e){this.name=e}},{key:"_oneToManyEmbedder",value:function(e){var t=this;return function(r){var n=t.server.collections[e];if(!n)throw new Error("Can't embed a non-existing collection "+e);return r[e]=n.getAll({filter:function(t){return-1!==r[e].indexOf(t[n.identifierName])}}),r}}},{key:"_manyToOneEmbedder",value:function(e){var t=this,r=e+"s",n=e+"_id";return function(o){var i=t.server.collections[r];if(!i)throw new Error("Can't embed a non-existing collection "+e);try{o[e]=i.getOne(o[n])}catch(u){}return o}}},{key:"_itemEmbedder",value:function(e){var t=this,r=Array.isArray(e)?e:[e],n=r.map(function(e){return e.endsWith("s")?t._oneToManyEmbedder(e):t._manyToOneEmbedder(e)});return function(e){return n.reduce(function(e,t){return t(e)},e)}}},{key:"getOnly",value:function(e){var t=this.obj;return e&&e.embed&&this.server&&(t=a["default"]({},t),t=this._itemEmbedder(e.embed)(t)),t}},{key:"updateOnly",value:function(e){for(var t in e)this.obj[t]=e[t];return this.obj}}]),e}();t["default"]=s,e.exports=t["default"]},function(e,t){"use strict";t["default"]=function(e){return e&&e.__esModule?e:{"default":e}},t.__esModule=!0},function(e,t,r){"use strict";var n=r(15)["default"];t["default"]=function(){function e(e,t){for(var r=0;r<t.length;r++){var o=t[r];o.enumerable=o.enumerable||!1,o.configurable=!0,"value"in o&&(o.writable=!0),n(e,o.key,o)}}return function(t,r,n){return r&&e(t.prototype,r),n&&e(t,n),t}}(),t.__esModule=!0},function(e,t){"use strict";t["default"]=function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")},t.__esModule=!0},function(e,t,r){e.exports={"default":r(25),__esModule:!0}},function(e,t,r){e.exports={"default":r(24),__esModule:!0}},function(e,t,r){"use strict";function n(e){if(!e)return{};var t={},r=e.split("&");return r.map(function(e){if(-1===e.indexOf("="))t[e]=!0;else{var r=e.split("="),n=o(r,2),i=n[0],u=n[1];(0===u.indexOf("[")||0===u.indexOf("{"))&&(u=JSON.parse(u)),t[i.trim()]=u}}),t}var o=r(16)["default"];Object.defineProperty(t,"__esModule",{value:!0}),t["default"]=n,e.exports=t["default"]},function(e,t,r){"use strict";var n=r(17)["default"];t["default"]=function(e,t,r){for(var o=!0;o;){var i=e,u=t,a=r;s=l=c=void 0,o=!1,null===i&&(i=Function.prototype);var s=n(i,u);if(void 0!==s){if("value"in s)return s.value;var c=s.get;return void 0===c?void 0:c.call(a)}var l=Object.getPrototypeOf(i);if(null===l)return void 0;e=l,t=u,r=a,o=!0}},t.__esModule=!0},function(e,t,r){"use strict";var n=r(18)["default"];t["default"]=function(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function, not "+typeof t);e.prototype=n(t&&t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}}),t&&(e.__proto__=t)},t.__esModule=!0},function(e,t,r){"use strict";var n=r(29),o=r(20),i=r(21),u=r(22);n(o,{implementation:o,getPolyfill:i,shim:u}),e.exports=o},function(){!function(){if(!Array.prototype.findIndex){var e=function(e){var t=Object(this),r=Math.max(0,t.length)>>>0;if(0===r)return-1;if("function"!=typeof e||"[object Function]"!==Object.prototype.toString.call(e))throw new TypeError("Array#findIndex: predicate must be a function");for(var n=arguments.length>1?arguments[1]:void 0,o=0;r>o;o++)if(e.call(n,t[o],o,t))return o;return-1};if(Object.defineProperty)try{Object.defineProperty(Array.prototype,"findIndex",{value:e,configurable:!0,writable:!0})}catch(t){}Array.prototype.findIndex||(Array.prototype.findIndex=e)}}(this)},function(e,t,r){e.exports={"default":r(26),__esModule:!0}},function(e,t,r){"use strict";var n=r(9)["default"],o=r(23)["default"];t["default"]=function(){function e(e,t){var r=[],o=!0,i=!1,u=void 0;try{for(var a,s=n(e);!(o=(a=s.next()).done)&&(r.push(a.value),!t||r.length!==t);o=!0);}catch(c){i=!0,u=c}finally{try{!o&&s["return"]&&s["return"]()}finally{if(i)throw u}}return r}return function(t,r){if(Array.isArray(t))return t;if(o(Object(t)))return e(t,r);throw new TypeError("Invalid attempt to destructure non-iterable instance")}}(),t.__esModule=!0},function(e,t,r){e.exports={"default":r(27),__esModule:!0}},function(e,t,r){e.exports={"default":r(28),__esModule:!0}},function(){/*! http://mths.be/endswith v0.2.0 by @mathias */ | ||
String.prototype.endsWith||!function(){"use strict";var e=function(){try{var e={},t=Object.defineProperty,r=t(e,e,e)&&t}catch(n){}return r}(),t={}.toString,r=function(e){if(null==this)throw TypeError();var r=String(this);if(e&&"[object RegExp]"==t.call(e))throw TypeError();var n=r.length,o=String(e),i=o.length,u=n;if(arguments.length>1){var a=arguments[1];void 0!==a&&(u=a?Number(a):0,u!=u&&(u=0))}var s=Math.min(Math.max(u,0),n),c=s-i;if(0>c)return!1;for(var l=-1;++l<i;)if(r.charCodeAt(c+l)!=o.charCodeAt(l))return!1;return!0};e?e(String.prototype,"endsWith",{value:r,configurable:!0,writable:!0}):String.prototype.endsWith=r}()},function(e,t,r){"use strict";var n=r(32),o=r(33),i=function(e){return"undefined"!=typeof e&&null!==e},u=r(30)(),a=Object,s=o.call(Function.call,Array.prototype.push),c=o.call(Function.call,Object.prototype.propertyIsEnumerable);e.exports=function(e){if(!i(e))throw new TypeError("target must be an object");var t,r,o,l,f,d=a(e);for(t=1;t<arguments.length;++t){if(r=a(arguments[t]),l=n(r),u&&Object.getOwnPropertySymbols)for(f=Object.getOwnPropertySymbols(r),o=0;o<f.length;++o)c(r,f[o])&&s(l,f[o]);for(o=0;o<l.length;++o)d[l[o]]=r[l[o]]}return d}},function(e,t,r){"use strict";var n=r(20),o=function(){if(!Object.assign||!Object.preventExtensions)return!1;var e=Object.preventExtensions({1:2});try{Object.assign(e,"xy")}catch(t){return"y"===e[1]}};e.exports=function(){return!Object.assign||o()?n:Object.assign}},function(e,t,r){"use strict";var n=r(29),o=r(21);e.exports=function(){var e=o();return Object.assign!==e&&n(Object,{assign:e}),e}},function(e,t,r){e.exports={"default":r(31),__esModule:!0}},function(e,t,r){r(36),r(37),r(38),e.exports=r(35).core.getIterator},function(e,t,r){r(34),e.exports=r(35).core.Object.keys},function(e,t,r){var n=r(35);e.exports=function(e,t,r){return n.setDesc(e,t,r)}},function(e,t,r){var n=r(35);r(34),e.exports=function(e,t){return n.getDesc(e,t)}},function(e,t,r){var n=r(35);e.exports=function(e,t){return n.create(e,t)}},function(e,t,r){"use strict";var n=r(32),o=r(39),i="function"==typeof Symbol&&"symbol"==typeof Symbol(),u=Object.prototype.toString,a=function(e){return"function"==typeof e&&"[object Function]"===u.call(e)},s=function(){var e={};try{Object.defineProperty(e,"x",{value:e,enumerable:!1});for(var t in e)return!1;return e.x===e}catch(r){return!1}},c=Object.defineProperty&&s(),l=function(e,t,r,n){(!(t in e)||a(n)&&n())&&(c?Object.defineProperty(e,t,{configurable:!0,enumerable:!1,writable:!0,value:r}):e[t]=r)},f=function(e,t){var r=arguments.length>2?arguments[2]:{},u=n(t);i&&(u=u.concat(Object.getOwnPropertySymbols(t))),o(u,function(n){l(e,n,t[n],r[n])})};f.supportsDescriptors=!!c,e.exports=f},function(e,t,r){"use strict";var n=r(32);e.exports=function(){if("function"!=typeof Symbol||"function"!=typeof Object.getOwnPropertySymbols)return!1;if("symbol"==typeof Symbol.iterator)return!0;var e={},t=Symbol("test");if("string"==typeof t)return!1;if(t instanceof Symbol)return!1;e[t]=42;for(t in e)return!1;if(0!==n(e).length)return!1;if("function"==typeof Object.keys&&0!==Object.keys(e).length)return!1;if("function"==typeof Object.getOwnPropertyNames&&0!==Object.getOwnPropertyNames(e).length)return!1;var r=Object.getOwnPropertySymbols(e);if(1!==r.length||r[0]!==t)return!1;if(!Object.prototype.propertyIsEnumerable.call(e,t))return!1;if("function"==typeof Object.getOwnPropertyDescriptor){var o=Object.getOwnPropertyDescriptor(e,t);if(42!==o.value||o.enumerable!==!0)return!1}return!0}},function(e,t,r){r(36),r(37),r(38),e.exports=r(35).core.isIterable},function(e,t,r){"use strict";var n=Object.prototype.hasOwnProperty,o=Object.prototype.toString,i=Array.prototype.slice,u=r(40),a=!{toString:null}.propertyIsEnumerable("toString"),s=function(){}.propertyIsEnumerable("prototype"),c=["toString","toLocaleString","valueOf","hasOwnProperty","isPrototypeOf","propertyIsEnumerable","constructor"],l=function(e){var t=e.constructor;return t&&t.prototype===e},f={$window:!0,$console:!0,$parent:!0,$self:!0,$frames:!0,$webkitIndexedDB:!0,$webkitStorageInfo:!0},d=function(){if("undefined"==typeof window)return!1;for(var e in window)if(!f["$"+e]&&n.call(window,e)&&null!==window[e]&&"object"==typeof window[e])try{l(window[e])}catch(t){return!0}return!1}(),p=function(e){if("undefined"==typeof window&&!d)return l(e);try{return l(e)}catch(t){return!1}},h=function(e){var t=null!==e&&"object"==typeof e,r="[object Function]"===o.call(e),i=u(e),l=t&&"[object String]"===o.call(e),f=[];if(!t&&!r&&!i)throw new TypeError("Object.keys called on a non-object");var d=s&&r;if(l&&e.length>0&&!n.call(e,0))for(var h=0;h<e.length;++h)f.push(String(h));if(i&&e.length>0)for(var y=0;y<e.length;++y)f.push(String(y));else for(var v in e)d&&"prototype"===v||!n.call(e,v)||f.push(String(v));if(a)for(var g=p(e),b=0;b<c.length;++b)g&&"constructor"===c[b]||!n.call(e,c[b])||f.push(c[b]);return f};h.shim=function(){if(Object.keys){var e=function(){return 2===(Object.keys(arguments)||"").length}(1,2);if(!e){var t=Object.keys;Object.keys=function(e){return t(u(e)?i.call(e):e)}}}else Object.keys=h;return Object.keys||h},e.exports=h},function(e){var t="Function.prototype.bind called on incompatible ",r=Array.prototype.slice,n=Object.prototype.toString,o="[object Function]";e.exports=function(e){var i=this;if("function"!=typeof i||n.call(i)!==o)throw new TypeError(t+i);for(var u=r.call(arguments,1),a=function(){if(this instanceof f){var t=i.apply(this,u.concat(r.call(arguments)));return Object(t)===t?t:this}return i.apply(e,u.concat(r.call(arguments)))},s=Math.max(0,i.length-u.length),c=[],l=0;s>l;l++)c.push("$"+l);var f=Function("binder","return function ("+c.join(",")+"){ return binder.apply(this,arguments); }")(a);if(i.prototype){var d=function(){};d.prototype=i.prototype,f.prototype=new d,d.prototype=null}return f}},function(e,t,r){var n=r(35),o=r(41),i=n.isObject,u=n.toObject;n.each.call("freeze,seal,preventExtensions,isFrozen,isSealed,isExtensible,getOwnPropertyDescriptor,getPrototypeOf,keys,getOwnPropertyNames".split(","),function(e,t){var a=(n.core.Object||{})[e]||Object[e],s=0,c={};c[e]=0==t?function(e){return i(e)?a(e):e}:1==t?function(e){return i(e)?a(e):e}:2==t?function(e){return i(e)?a(e):e}:3==t?function(e){return i(e)?a(e):!0}:4==t?function(e){return i(e)?a(e):!0}:5==t?function(e){return i(e)?a(e):!1}:6==t?function(e,t){return a(u(e),t)}:7==t?function(e){return a(Object(n.assertDefined(e)))}:8==t?function(e){return a(u(e))}:r(42).get;try{a("z")}catch(l){s=1}o(o.S+o.F*s,"Object",c)})},function(e,t,r){"use strict";function n(e){return isNaN(e=+e)?0:(e>0?y:h)(e)}function o(e,t){return{enumerable:!(1&e),configurable:!(2&e),writable:!(4&e),value:t}}function i(e,t,r){return e[t]=r,e}function u(e){return b?function(t,r,n){return O.setDesc(t,r,o(e,n))}:i}function a(e){return null!==e&&("object"==typeof e||"function"==typeof e)}function s(e){return"function"==typeof e}function c(e){if(void 0==e)throw TypeError("Can't call method on "+e);return e}var l="undefined"!=typeof self?self:Function("return this")(),f={},d=Object.defineProperty,p={}.hasOwnProperty,h=Math.ceil,y=Math.floor,v=Math.max,g=Math.min,b=!!function(){try{return 2==d({},"a",{get:function(){return 2}}).a}catch(e){}}(),m=u(1),O=e.exports=r(49)({g:l,core:f,html:l.document&&document.documentElement,isObject:a,isFunction:s,that:function(){return this},toInteger:n,toLength:function(e){return e>0?g(n(e),9007199254740991):0},toIndex:function(e,t){return e=n(e),0>e?v(e+t,0):g(e,t)},has:function(e,t){return p.call(e,t)},create:Object.create,getProto:Object.getPrototypeOf,DESC:b,desc:o,getDesc:Object.getOwnPropertyDescriptor,setDesc:d,setDescs:Object.defineProperties,getKeys:Object.keys,getNames:Object.getOwnPropertyNames,getSymbols:Object.getOwnPropertySymbols,assertDefined:c,ES5Object:Object,toObject:function(e){return O.ES5Object(c(e))},hide:m,def:u(0),set:l.Symbol?i:m,each:[].forEach});"undefined"!=typeof __e&&(__e=f),"undefined"!=typeof __g&&(__g=l)},function(e,t,r){r(43);var n=r(35),o=r(44).Iterators,i=r(45)("iterator"),u=o.Array,a=n.g.NodeList,s=n.g.HTMLCollection,c=a&&a.prototype,l=s&&s.prototype;n.FW&&(!a||i in c||n.hide(c,i,u),!s||i in l||n.hide(l,i,u)),o.NodeList=o.HTMLCollection=u},function(e,t,r){var n=r(35).set,o=r(46)(!0),i=r(47).safe("iter"),u=r(44),a=u.step;r(48)(String,"String",function(e){n(this,i,{o:String(e),i:0})},function(){var e,t=this[i],r=t.o,n=t.i;return n>=r.length?a(1):(e=o(r,n),t.i+=e.length,a(0,e))})},function(e,t,r){var n=r(35).core,o=r(44);n.isIterable=o.is,n.getIterator=o.get},function(e){var t=Object.prototype.hasOwnProperty,r=Object.prototype.toString;e.exports=function(e,n,o){if("[object Function]"!==r.call(n))throw new TypeError("iterator must be a function");var i=e.length;if(i===+i)for(var u=0;i>u;u++)n.call(o,e[u],u,e);else for(var a in e)t.call(e,a)&&n.call(o,e[a],a,e)}},function(e){"use strict";var t=Object.prototype.toString;e.exports=function(e){var r=t.call(e),n="[object Arguments]"===r;return n||(n="[object Array]"!==r&&null!==e&&"object"==typeof e&&"number"==typeof e.length&&e.length>=0&&"[object Function]"===t.call(e.callee)),n}},function(e,t,r){function n(e,t){return function(){return e.apply(t,arguments)}}function o(e,t,r){var i,c,l,f,d=e&o.G,p=e&o.P,h=d?u:e&o.S?u[t]:(u[t]||{}).prototype,y=d?a:a[t]||(a[t]={});d&&(r=t);for(i in r)c=!(e&o.F)&&h&&i in h,c&&i in y||(l=c?h[i]:r[i],d&&!s(h[i])?f=r[i]:e&o.B&&c?f=n(l,u):e&o.W&&h[i]==l?!function(e){f=function(t){return this instanceof e?new e(t):e(t)},f.prototype=e.prototype}(l):f=p&&s(l)?n(Function.call,l):l,y[i]=f,p&&((y.prototype||(y.prototype={}))[i]=l))}var i=r(35),u=i.g,a=i.core,s=i.isFunction;o.F=1,o.G=2,o.S=4,o.P=8,o.B=16,o.W=32,e.exports=o},function(e,t,r){function n(e){try{return u(e)}catch(t){return a.slice()}}var o=r(35),i={}.toString,u=o.getNames,a="object"==typeof window&&Object.getOwnPropertyNames?Object.getOwnPropertyNames(window):[];e.exports.get=function(e){return a&&"[object Window]"==i.call(e)?n(e):u(o.toObject(e))}},function(e,t,r){var n=r(35),o=r(53),i=r(47).safe("iter"),u=r(44),a=u.step,s=u.Iterators;r(48)(Array,"Array",function(e,t){n.set(this,i,{o:n.toObject(e),i:0,k:t})},function(){var e=this[i],t=e.o,r=e.k,n=e.i++;return!t||n>=t.length?(e.o=void 0,a(1)):"keys"==r?a(0,n):"values"==r?a(0,t[n]):a(0,[n,t[n]])},"values"),s.Arguments=s.Array,o("keys"),o("values"),o("entries")},function(e,t,r){"use strict";function n(e,t){o.hide(e,c,t),l in[]&&o.hide(e,l,t)}var o=r(35),i=r(50),u=i.classof,a=r(51),s=a.obj,c=r(45)("iterator"),l="@@iterator",f=r(52)("iterators"),d={};n(d,o.that),e.exports={BUGGY:"keys"in[]&&!("next"in[].keys()),Iterators:f,step:function(e,t){return{value:t,done:!!e}},is:function(e){var t=Object(e),r=o.g.Symbol;return(r&&r.iterator||l)in t||c in t||o.has(f,u(t))},get:function(e){var t,r=o.g.Symbol;return void 0!=e&&(t=e[r&&r.iterator||l]||e[c]||f[u(e)]),a(o.isFunction(t),e," is not iterable!"),s(t.call(e))},set:n,create:function(e,t,r,n){e.prototype=o.create(n||d,{next:o.desc(1,r)}),i.set(e,t+" Iterator")}}},function(e,t,r){var n=r(35).g,o=r(52)("wks");e.exports=function(e){return o[e]||(o[e]=n.Symbol&&n.Symbol[e]||r(47).safe("Symbol."+e))}},function(e,t,r){var n=r(35);e.exports=function(e){return function(t,r){var o,i,u=String(n.assertDefined(t)),a=n.toInteger(r),s=u.length;return 0>a||a>=s?e?"":void 0:(o=u.charCodeAt(a),55296>o||o>56319||a+1===s||(i=u.charCodeAt(a+1))<56320||i>57343?e?u.charAt(a):o:e?u.slice(a,a+2):(o-55296<<10)+(i-56320)+65536)}}},function(e,t,r){function n(e){return"Symbol(".concat(void 0===e?"":e,")_",(++o+Math.random()).toString(36))}var o=0;n.safe=r(35).g.Symbol||n,e.exports=n},function(e,t,r){var n=r(41),o=r(54),i=r(35),u=r(50),a=r(44),s=r(45)("iterator"),c="@@iterator",l="keys",f="values",d=a.Iterators;e.exports=function(e,t,r,p,h,y,v){function g(e){function t(t){return new r(t,e)}switch(e){case l:return function(){return t(this)};case f:return function(){return t(this)}}return function(){return t(this)}}a.create(r,t,p);var b,m,O=t+" Iterator",j=e.prototype,w=j[s]||j[c]||h&&j[h],x=w||g(h);if(w){var E=i.getProto(x.call(new e));u.set(E,O,!0),i.FW&&i.has(j,c)&&a.set(E,i.that)}if((i.FW||v)&&a.set(j,x),d[t]=x,d[O]=i.that,h)if(b={keys:y?x:g(l),values:h==f?x:g(f),entries:h!=f?x:g("entries")},v)for(m in b)m in j||o(j,m,b[m]);else n(n.P+n.F*a.BUGGY,t,b)}},function(e){e.exports=function(e){return e.FW=!1,e.path=e.core,e}},function(e,t,r){function n(e){return u.call(e).slice(8,-1)}var o=r(35),i=r(45)("toStringTag"),u={}.toString;n.classof=function(e){var t,r;return void 0==e?void 0===e?"Undefined":"Null":"string"==typeof(r=(t=Object(e))[i])?r:n(t)},n.set=function(e,t,r){e&&!o.has(e=r?e:e.prototype,i)&&o.hide(e,i,t)},e.exports=n},function(e,t,r){function n(e,t,r){if(!e)throw TypeError(r?t+r:t)}var o=r(35);n.def=o.assertDefined,n.fn=function(e){if(!o.isFunction(e))throw TypeError(e+" is not a function!");return e},n.obj=function(e){if(!o.isObject(e))throw TypeError(e+" is not an object!");return e},n.inst=function(e,t,r){if(!(e instanceof t))throw TypeError(r+": use the 'new' operator!");return e},e.exports=n},function(e,t,r){var n=r(35),o="__core-js_shared__",i=n.g[o]||(n.g[o]={});e.exports=function(e){return i[e]||(i[e]={})}},function(e){e.exports=function(){}},function(e,t,r){e.exports=r(35).hide}])}); |
{ | ||
"name": "fakerest", | ||
"version": "1.1.4", | ||
"version": "1.2.0", | ||
"repository": "https://github.com/marmelab/FakeRest", | ||
@@ -17,2 +17,3 @@ "description": "Patch XMLHttpRequest to fake a REST server based on JSON data. ", | ||
"babel-runtime": "^5.4.3", | ||
"fetch-mock": "^4.6.0", | ||
"karma": "0.12.14", | ||
@@ -19,0 +20,0 @@ "karma-chrome-launcher": "0.1.3", |
# FakeRest [![Build Status](https://travis-ci.org/marmelab/FakeRest.svg?branch=master)](https://travis-ci.org/marmelab/FakeRest) | ||
Intercept XMLHttpRequest to fake a REST server based on JSON data. Use it on top of [Sinon.js](http://sinonjs.org/) to test JavaScript REST clients on the browser side (e.g. single page apps) without a server. | ||
Intercept AJAX calls to fake a REST server based on JSON data. Use it on top of [Sinon.js](http://sinonjs.org/) (for `XMLHTTPRequest`) or [fetch-mock](https://github.com/wheresrhys/fetch-mock) (for `fetch`) to test JavaScript REST clients on the browser side (e.g. single page apps) without a server. | ||
## Usage | ||
### Fake XMLHTTPRequest | ||
```html | ||
@@ -21,3 +23,7 @@ <script src="/path/to/FakeRest.min.js"></script> | ||
{ id: 3, author_id: 1, title: 'Sense and Sensibility' } | ||
] | ||
], | ||
'settings': { | ||
language: 'english', | ||
preferred_format: 'hardback', | ||
} | ||
}; | ||
@@ -27,2 +33,3 @@ // initialize fake REST server | ||
restServer.init(data); | ||
// use sinon.js to monkey-patch XmlHttpRequest | ||
@@ -34,5 +41,31 @@ var server = sinon.fakeServer.create(); | ||
FakeRest will now intercept every XmlHTTPResquest to the REST server. The handled routes are: | ||
### Fake fetch | ||
```js | ||
import fetchMock from 'fetch-mock'; | ||
import FakeRest from 'fakerest'; | ||
var data = { | ||
'authors': [ | ||
{ id: 0, first_name: 'Leo', last_name: 'Tolstoi' }, | ||
{ id: 1, first_name: 'Jane', last_name: 'Austen' } | ||
], | ||
'books': [ | ||
{ id: 0, author_id: 0, title: 'Anna Karenina' }, | ||
{ id: 1, author_id: 0, title: 'War and Peace' }, | ||
{ id: 2, author_id: 1, title: 'Pride and Prejudice' }, | ||
{ id: 3, author_id: 1, title: 'Sense and Sensibility' } | ||
], | ||
'settings': { | ||
language: 'english', | ||
preferred_format: 'hardback', | ||
} | ||
}; | ||
const restServer = new FakeRest.FetchServer('http://localhost:3000'); | ||
restServer.init(data); | ||
fetchMock.mock('^http://localhost:3000', restServer.getHandler()); | ||
``` | ||
FakeRest will now intercept every XmlHTTPResquest to the REST server. The handled routes for collections of items are: | ||
``` | ||
GET /:resource | ||
@@ -46,2 +79,11 @@ POST /:resource | ||
The handled routes for single items are: | ||
``` | ||
GET /:resource | ||
PUT /:resource | ||
PATCH /:resource | ||
``` | ||
Let's see an example: | ||
@@ -67,2 +109,8 @@ | ||
var req = new XMLHttpRequest(); | ||
req.open("GET", "/settings", false); | ||
req.send(null); | ||
console.log(req.responseText); | ||
// {"language:"english","preferred_format":"hardback"} | ||
var req = new XMLHttpRequest(); | ||
req.open("POST", "/books", false); | ||
@@ -218,4 +266,6 @@ req.send(JSON.stringify({ author_id: 1, title: 'Emma' })); | ||
If the REST flavor you want to simulate differs from the one chosen for FakeRest, no problem: request and response interceptors will do the conversion (see below). | ||
If the REST flavor you want to simulate differs from the one chosen for FakeRest, no problem: request and response interceptors will do the conversion (see below). | ||
Note that all of the above apply only to collections. Single objects respond to `GET /bar`, `PUT /bar` and `PATCH /bar` in a manner identical to those operations for `/foo/:id`, including embedding. `POST /bar` and `DELETE /bar` are not enabled. | ||
## Usage and Configuration | ||
@@ -222,0 +272,0 @@ |
import Server from 'Server'; | ||
import FetchServer from 'FetchServer'; | ||
import Collection from 'Collection'; | ||
import Single from 'Single'; | ||
export default { | ||
Server: Server, | ||
Collection: Collection | ||
FetchServer: FetchServer, | ||
Collection: Collection, | ||
Single: Single | ||
} |
import objectAssign from 'object.assign'; | ||
import Collection from 'Collection'; | ||
import Single from 'Single'; | ||
import parseQueryString from 'parseQueryString'; | ||
const assign = objectAssign.getPolyfill(); | ||
function parseQueryString(queryString) { | ||
if (!queryString) { | ||
return {}; | ||
} | ||
let queryObject = {}; | ||
let queryElements = queryString.split('&'); | ||
queryElements.map(function(queryElement) { | ||
if (queryElement.indexOf('=') === -1) { | ||
queryObject[queryElement] = true; | ||
} else { | ||
let [key, value] = queryElement.split('='); | ||
if (value.indexOf('[') === 0 || value.indexOf('{') === 0) { | ||
value = JSON.parse(value); | ||
} | ||
queryObject[key.trim()] = value; | ||
} | ||
}); | ||
return queryObject; | ||
} | ||
export default class Server { | ||
@@ -35,2 +15,3 @@ constructor(baseUrl='') { | ||
this.collections = {}; | ||
this.singles = {}; | ||
this.requestInterceptors = []; | ||
@@ -45,3 +26,7 @@ this.responseInterceptors = []; | ||
for (let name in data) { | ||
this.addCollection(name, new Collection(data[name], 'id')); | ||
if (Array.isArray(data[name])) { | ||
this.addCollection(name, new Collection(data[name], 'id')); | ||
} else { | ||
this.addSingle(name, new Single(data[name])); | ||
} | ||
} | ||
@@ -87,2 +72,16 @@ } | ||
addSingle(name, single) { | ||
this.singles[name] = single; | ||
single.setServer(this); | ||
single.setName(name); | ||
} | ||
getSingle(name) { | ||
return this.singles[name]; | ||
} | ||
getSingleNames() { | ||
return Object.keys(this.singles); | ||
} | ||
addRequestInterceptor(interceptor) { | ||
@@ -128,2 +127,11 @@ this.requestInterceptors.push(interceptor); | ||
getOnly(name, params) { | ||
return this.singles[name].getOnly(); | ||
} | ||
updateOnly(name, item) { | ||
return this.singles[name].updateOnly(item); | ||
} | ||
decode(request) { | ||
@@ -240,2 +248,34 @@ request.queryString = decodeURIComponent(request.url.slice(request.url.indexOf('?') + 1)); | ||
// Handle Single Objects | ||
for (let name of this.getSingleNames()) { | ||
let matches = request.url.match(new RegExp('^' + this.baseUrl + '\\/(' + name + ')(\\/?.*)?$' )); | ||
if (!matches) continue; | ||
if (request.method == 'GET') { | ||
try { | ||
let item = this.getOnly(name); | ||
return this.respond(item, null, request); | ||
} catch (error) { | ||
return request.respond(404); | ||
} | ||
} | ||
if (request.method == 'PUT') { | ||
try { | ||
let item = this.updateOnly(name, request.json); | ||
return this.respond(item, null, request); | ||
} catch (error) { | ||
return request.respond(404); | ||
} | ||
} | ||
if (request.method == 'PATCH') { | ||
try { | ||
let item = this.updateOnly(name, request.json); | ||
return this.respond(item, null, request); | ||
} catch (error) { | ||
return request.respond(404); | ||
} | ||
} | ||
} | ||
// Handle collections | ||
for (let name of this.getCollectionNames()) { | ||
@@ -242,0 +282,0 @@ let matches = request.url.match(new RegExp('^' + this.baseUrl + '\\/(' + name + ')(\\/(\\d+))?(\\?.*)?$' )); |
@@ -7,2 +7,3 @@ /*global describe,it,expect,beforeEach,jasmine*/ | ||
var Server = FakeRest.Server; | ||
var Single = FakeRest.Single; | ||
var Collection = FakeRest.Collection; | ||
@@ -30,6 +31,8 @@ | ||
foo: [{a:1}, {a:2}, {a:3}], | ||
bar: [{b: true}, {b: false}] | ||
bar: [{b: true}, {b: false}], | ||
baz: {name: 'baz'} | ||
}); | ||
expect(server.getAll('foo')).toEqual([{ id: 0, a:1 }, { id: 1, a:2 }, { id: 2, a:3 }]); | ||
expect(server.getAll('bar')).toEqual([{ id: 0, b: true }, { id: 1, b: false }]); | ||
expect(server.getOnly('baz')).toEqual({name: 'baz'}); | ||
}); | ||
@@ -44,7 +47,17 @@ }); | ||
server.addCollection('foo', collection); | ||
collection = server.getCollection('foo'); | ||
expect(collection).toEqual(collection); | ||
var newcollection = server.getCollection('foo'); | ||
expect(newcollection).toEqual(collection); | ||
}); | ||
}); | ||
describe('addSingle', function () { | ||
it('should add a single object and index it by name', function () { | ||
var server = new Server(); | ||
var single = new Single({name: 'foo', description: 'bar'}); | ||
server.addSingle('foo', single); | ||
expect(server.getSingle('foo')).toEqual(single); | ||
}); | ||
}); | ||
describe('getAll', function() { | ||
@@ -100,2 +113,10 @@ | ||
describe('getOnly', function () { | ||
it('should return the single matching the identifier', function () { | ||
var server = new Server(); | ||
server.addSingle('foo', new Single({name: 'foo'})); | ||
expect(server.getOnly('foo')).toEqual({name: 'foo'}); | ||
}); | ||
}); | ||
describe('addRequestInterceptor', function() { | ||
@@ -318,2 +339,36 @@ it('should allow request transformation', function() { | ||
it('should respond to GET /foo/ with single item', function () { | ||
var server = new Server(); | ||
server.addSingle('foo', new Single({name: 'foo'})); | ||
var request = getFakeXMLHTTPRequest('GET', '/foo'); | ||
server.handle(request); | ||
expect(request.status).toEqual(200); | ||
expect(request.responseText).toEqual('{"name":"foo"}'); | ||
expect(request.getResponseHeader('Content-Type')).toEqual('application/json'); | ||
}); | ||
it('should respond to PUT /foo/ by updating the singleton record', function () { | ||
var server = new Server(); | ||
server.addSingle('foo', new Single({name: 'foo'})); | ||
var request = getFakeXMLHTTPRequest('PUT', '/foo/', JSON.stringify({name: 'baz'})); | ||
server.handle(request); | ||
expect(request.status).toEqual(200); | ||
expect(request.responseText).toEqual('{"name":"baz"}'); | ||
expect(request.getResponseHeader('Content-Type')).toEqual('application/json'); | ||
expect(server.getOnly('foo')).toEqual({ name: 'baz'}); | ||
}); | ||
it('should respond to PATCH /foo/ by updating the singleton record', function () { | ||
var server = new Server(); | ||
server.addSingle('foo', new Single({name: 'foo'})); | ||
var request = getFakeXMLHTTPRequest('PATCH', '/foo/', JSON.stringify({name: 'baz'})); | ||
server.handle(request); | ||
expect(request.status).toEqual(200); | ||
expect(request.responseText).toEqual('{"name":"baz"}'); | ||
expect(request.getResponseHeader('Content-Type')).toEqual('application/json'); | ||
expect(server.getOnly('foo')).toEqual({ name: 'baz'}); | ||
}); | ||
}); | ||
@@ -354,6 +409,7 @@ | ||
foo: [{a:1}, {a:2}, {a:3}], | ||
bar: [{b: true}, {b: false}] | ||
bar: [{b: true}, {b: false}], | ||
biz: {name: 'biz'}, | ||
}); | ||
server.setBatchUrl('/batch'); | ||
var request = getFakeXMLHTTPRequest('POST', '/batch', JSON.stringify({foo0: '/foo/0', allbar: '/bar', baz0: '/baz/0'})); | ||
var request = getFakeXMLHTTPRequest('POST', '/batch', JSON.stringify({foo0: '/foo/0', allbar: '/bar', baz0: '/baz/0', biz: '/biz' })); | ||
server.handle(request); | ||
@@ -383,2 +439,12 @@ expect(request.responseText).toEqual(JSON.stringify({ | ||
body: {} | ||
}, | ||
biz: { | ||
code: 200, | ||
headers: [ | ||
{ | ||
name: 'Content-Type', | ||
value: 'application/json', | ||
} | ||
], | ||
body: '{"name":"biz"}' | ||
} | ||
@@ -385,0 +451,0 @@ })); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
207390
27
2627
343
14