hungry-fetch
Version:
A hungry fetch mock.
3 lines (2 loc) • 2.56 kB
JavaScript
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e||self).hungryFetch={})}(this,function(e){function t(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);t&&(r=r.filter(function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable})),n.push.apply(n,r)}return n}function n(e){for(var n=1;n<arguments.length;n++){var o=null!=arguments[n]?arguments[n]:{};n%2?t(Object(o),!0).forEach(function(t){r(e,t,o[t])}):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(o)):t(Object(o)).forEach(function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(o,t))})}return e}function r(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}var o=/*#__PURE__*/function(){function e(e,t,n){void 0===t&&(t={}),this.url=void 0,this.additionalArgs=void 0,this.request=void 0,this.request=t,this.url=e,this.additionalArgs=n}var t=e.prototype;return t.json=function(){if("string"!=typeof this.request.body)throw Error("Can't parse json from "+typeof this.request.body);return JSON.parse(this.request.body)},t.body=function(){return this.request.body},e}(),i=[],u=[];function s(e,t){return e===t?1:"*"===t?.1:function(e,t){var n=e.split("/"),r=t.split("/");return n.length===r.length&&r.every(function(e,t){return"*"===e||n[t]===e})}(e,t)?.5:0}function c(){if(0===i.length)throw new Error("fetch(…) has not been called!");return i[i.length-1]}("undefined"!=typeof window?window:global).fetch=function(e,t){var r=arguments;return new Promise(function(c,l){i.push(new o(e,t,[].slice.call(r,2)));var f=function(e){return 0===u.length?null:u.reduce(function(t,n){var r=s(e,n.pattern);return 0===r?t:null===t||r>s(e,t.pattern)?n:t},null)}(e);if(f){var a=function(e){var t,r;"object"==typeof e.body?(t=JSON.stringify(e.body),r="application/json"):(t=e.body,r="text/plain");var o=e.config;return new Response(t,{headers:n({"Content-Type":o.contentType||r},o.headers||{}),status:o.status||200})}(f);f.resolve?c(a):l(new Error("Failed to fetch"))}else c()})},e.calls=function(){return i},e.clear=function(){i=[],u=[]},e.lastCall=c,e.mockResponse=function(e,t,n,r){void 0===n&&(n={}),void 0===r&&(r=!0),u.push({pattern:e,body:t,resolve:r,config:n})},e.singleCall=function(){if(1!==i.length)throw new Error("fetch(…) not called exactly one time! It was called "+i.length+" times.");return c()}});
//# sourceMappingURL=hungry-fetch.umd.js.map