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