UNPKG

@pollyjs/persister-rest

Version:
3 lines (2 loc) 16.2 kB
import t from"@pollyjs/persister";import{buildUrl as e}from"@pollyjs/utils";var n=Object.freeze({}),r=Math.ceil,o=Math.floor,i=function(t){return isNaN(t=+t)?0:(t>0?o:r)(t)},c=function(t){if(null==t)throw TypeError("Can't call method on "+t);return t};function u(t,e){return t(e={exports:{}},e.exports),e.exports}var a=u((function(t){var e=t.exports="undefined"!=typeof window&&window.Math==Math?window:"undefined"!=typeof self&&self.Math==Math?self:Function("return this")();"number"==typeof __g&&(__g=e)})),s=u((function(t){var e=t.exports={version:"2.6.9"};"number"==typeof __e&&(__e=e)})),f=(s.version,function(t){if("function"!=typeof t)throw TypeError(t+" is not a function!");return t}),l=function(t,e,n){if(f(t),void 0===e)return t;switch(n){case 1:return function(n){return t.call(e,n)};case 2:return function(n,r){return t.call(e,n,r)};case 3:return function(n,r,o){return t.call(e,n,r,o)}}return function(){return t.apply(e,arguments)}},h=function(t){return"object"==typeof t?null!==t:"function"==typeof t},p=function(t){if(!h(t))throw TypeError(t+" is not an object!");return t},v=function(t){try{return!!t()}catch(t){return!0}},d=!v((function(){return 7!=Object.defineProperty({},"a",{get:function(){return 7}}).a})),y=a.document,m=h(y)&&h(y.createElement),_=function(t){return m?y.createElement(t):{}},g=!d&&!v((function(){return 7!=Object.defineProperty(_("div"),"a",{get:function(){return 7}}).a})),w=Object.defineProperty,S={f:d?Object.defineProperty:function(t,e,n){if(p(t),e=function(t,e){if(!h(t))return t;var n,r;if(e&&"function"==typeof(n=t.toString)&&!h(r=n.call(t)))return r;if("function"==typeof(n=t.valueOf)&&!h(r=n.call(t)))return r;if(!e&&"function"==typeof(n=t.toString)&&!h(r=n.call(t)))return r;throw TypeError("Can't convert object to primitive value")}(e,!0),p(n),g)try{return w(t,e,n)}catch(t){}if("get"in n||"set"in n)throw TypeError("Accessors not supported!");return"value"in n&&(t[e]=n.value),t}},b=function(t,e){return{enumerable:!(1&t),configurable:!(2&t),writable:!(4&t),value:e}},j=d?function(t,e,n){return S.f(t,e,b(1,n))}:function(t,e,n){return t[e]=n,t},O={}.hasOwnProperty,P=function(t,e){return O.call(t,e)},T=function(t,e,n){var r,o,i,c=t&T.F,u=t&T.G,f=t&T.S,h=t&T.P,p=t&T.B,v=t&T.W,d=u?s:s[e]||(s[e]={}),y=d.prototype,m=u?a:f?a[e]:(a[e]||{}).prototype;for(r in u&&(n=e),n)(o=!c&&m&&void 0!==m[r])&&P(d,r)||(i=o?m[r]:n[r],d[r]=u&&"function"!=typeof m[r]?n[r]:p&&o?l(i,a):v&&m[r]==i?function(t){var e=function(e,n,r){if(this instanceof t){switch(arguments.length){case 0:return new t;case 1:return new t(e);case 2:return new t(e,n)}return new t(e,n,r)}return t.apply(this,arguments)};return e.prototype=t.prototype,e}(i):h&&"function"==typeof i?l(Function.call,i):i,h&&((d.virtual||(d.virtual={}))[r]=i,t&T.R&&y&&!y[r]&&j(y,r,i)))};T.F=1,T.G=2,T.S=4,T.P=8,T.B=16,T.W=32,T.U=64,T.R=128;var x,L=T,E=j,M={},A={}.toString,R=function(t){return A.call(t).slice(8,-1)},k=Object("z").propertyIsEnumerable(0)?Object:function(t){return"String"==R(t)?t.split(""):Object(t)},C=function(t){return k(c(t))},F=Math.min,I=function(t){return t>0?F(i(t),9007199254740991):0},N=Math.max,D=Math.min,G=u((function(t){var e=a["__core-js_shared__"]||(a["__core-js_shared__"]={});(t.exports=function(t,n){return e[t]||(e[t]=void 0!==n?n:{})})("versions",[]).push({version:s.version,mode:"pure",copyright:"© 2019 Denis Pushkarev (zloirock.ru)"})})),H=0,U=Math.random(),V=function(t){return"Symbol(".concat(void 0===t?"":t,")_",(++H+U).toString(36))},z=G("keys"),B=function(t){return z[t]||(z[t]=V(t))},W=(x=!1,function(t,e,n){var r,o=C(t),c=I(o.length),u=function(t,e){return(t=i(t))<0?N(t+e,0):D(t,e)}(n,c);if(x&&e!=e){for(;c>u;)if((r=o[u++])!=r)return!0}else for(;c>u;u++)if((x||u in o)&&o[u]===e)return x||u||0;return!x&&-1}),q=B("IE_PROTO"),J="constructor,hasOwnProperty,isPrototypeOf,propertyIsEnumerable,toLocaleString,toString,valueOf".split(","),K=Object.keys||function(t){return function(t,e){var n,r=C(t),o=0,i=[];for(n in r)n!=q&&P(r,n)&&i.push(n);for(;e.length>o;)P(r,n=e[o++])&&(~W(i,n)||i.push(n));return i}(t,J)},X=d?Object.defineProperties:function(t,e){p(t);for(var n,r=K(e),o=r.length,i=0;o>i;)S.f(t,n=r[i++],e[n]);return t},Q=a.document,Y=Q&&Q.documentElement,Z=B("IE_PROTO"),$=function(){},tt=function(){var t,e=_("iframe"),n=J.length;for(e.style.display="none",Y.appendChild(e),e.src="javascript:",(t=e.contentWindow.document).open(),t.write("<script>document.F=Object<\/script>"),t.close(),tt=t.F;n--;)delete tt.prototype[J[n]];return tt()},et=Object.create||function(t,e){var n;return null!==t?($.prototype=p(t),n=new $,$.prototype=null,n[Z]=t):n=tt(),void 0===e?n:X(n,e)},nt=u((function(t){var e=G("wks"),n=a.Symbol,r="function"==typeof n;(t.exports=function(t){return e[t]||(e[t]=r&&n[t]||(r?n:V)("Symbol."+t))}).store=e})),rt=S.f,ot=nt("toStringTag"),it=function(t,e,n){t&&!P(t=n?t:t.prototype,ot)&&rt(t,ot,{configurable:!0,value:e})},ct={};j(ct,nt("iterator"),(function(){return this}));var ut,at=function(t,e,n){t.prototype=et(ct,{next:b(1,n)}),it(t,e+" Iterator")},st=B("IE_PROTO"),ft=Object.prototype,lt=Object.getPrototypeOf||function(t){return t=Object(c(t)),P(t,st)?t[st]:"function"==typeof t.constructor&&t instanceof t.constructor?t.constructor.prototype:t instanceof Object?ft:null},ht=nt("iterator"),pt=!([].keys&&"next"in[].keys()),vt=function(){return this},dt=function(t,e,n,r,o,i,c){at(n,e,r);var u,a,s,f=function(t){if(!pt&&t in v)return v[t];switch(t){case"keys":case"values":return function(){return new n(this,t)}}return function(){return new n(this,t)}},l=e+" Iterator",h="values"==o,p=!1,v=t.prototype,d=v[ht]||v["@@iterator"]||o&&v[o],y=d||f(o),m=o?h?f("entries"):y:void 0,_="Array"==e&&v.entries||d;if(_&&(s=lt(_.call(new t)))!==Object.prototype&&s.next&&it(s,l,!0),h&&d&&"values"!==d.name&&(p=!0,y=function(){return d.call(this)}),c&&(pt||p||!v[ht])&&j(v,ht,y),M[e]=y,M[l]=vt,o)if(u={values:h?y:f("values"),keys:i?y:f("keys"),entries:m},c)for(a in u)a in v||E(v,a,u[a]);else L(L.P+L.F*(pt||p),e,u);return u},yt=(ut=!0,function(t,e){var n,r,o=String(c(t)),u=i(e),a=o.length;return u<0||u>=a?ut?"":void 0:(n=o.charCodeAt(u))<55296||n>56319||u+1===a||(r=o.charCodeAt(u+1))<56320||r>57343?ut?o.charAt(u):n:ut?o.slice(u,u+2):r-56320+(n-55296<<10)+65536});dt(String,"String",(function(t){this._t=String(t),this._i=0}),(function(){var t,e=this._t,n=this._i;return n>=e.length?{value:void 0,done:!0}:(t=yt(e,n),this._i+=t.length,{value:t,done:!1})}));var mt=function(t,e){return{value:e,done:!!t}};dt(Array,"Array",(function(t,e){this._t=C(t),this._i=0,this._k=e}),(function(){var t=this._t,e=this._k,n=this._i++;return!t||n>=t.length?(this._t=void 0,mt(1)):mt(0,"keys"==e?n:"values"==e?t[n]:[n,t[n]])}),"values");M.Arguments=M.Array;for(var _t=nt("toStringTag"),gt="CSSRuleList,CSSStyleDeclaration,CSSValueList,ClientRectList,DOMRectList,DOMStringList,DOMTokenList,DataTransferItemList,FileList,HTMLAllCollection,HTMLCollection,HTMLFormElement,HTMLSelectElement,MediaList,MimeTypeArray,NamedNodeMap,NodeList,PaintRequestList,Plugin,PluginArray,SVGLengthList,SVGNumberList,SVGPathSegList,SVGPointList,SVGStringList,SVGTransformList,SourceBufferList,StyleSheetList,TextTrackCueList,TextTrackList,TouchList".split(","),wt=0;wt<gt.length;wt++){var St=gt[wt],bt=a[St],jt=bt&&bt.prototype;jt&&!jt[_t]&&j(jt,_t,St),M[St]=M.Array}var Ot,Pt,Tt,xt=nt("toStringTag"),Lt="Arguments"==R(function(){return arguments}()),Et=function(t){var e,n,r;return void 0===t?"Undefined":null===t?"Null":"string"==typeof(n=function(t,e){try{return t[e]}catch(t){}}(e=Object(t),xt))?n:Lt?R(e):"Object"==(r=R(e))&&"function"==typeof e.callee?"Arguments":r},Mt=function(t,e,n,r){try{return r?e(p(n)[0],n[1]):e(n)}catch(e){var o=t.return;throw void 0!==o&&p(o.call(t)),e}},At=nt("iterator"),Rt=Array.prototype,kt=nt("iterator"),Ct=s.getIteratorMethod=function(t){if(null!=t)return t[kt]||t["@@iterator"]||M[Et(t)]},Ft=u((function(t){var e={},n={},r=t.exports=function(t,r,o,i,c){var u,a,s,f,h,v=c?function(){return t}:Ct(t),d=l(o,i,r?2:1),y=0;if("function"!=typeof v)throw TypeError(t+" is not iterable!");if(void 0===(h=v)||M.Array!==h&&Rt[At]!==h){for(s=v.call(t);!(a=s.next()).done;)if((f=Mt(s,d,a.value,r))===e||f===n)return f}else for(u=I(t.length);u>y;y++)if((f=r?d(p(a=t[y])[0],a[1]):d(t[y]))===e||f===n)return f};r.BREAK=e,r.RETURN=n})),It=nt("species"),Nt=function(t,e){var n,r=p(t).constructor;return void 0===r||null==(n=p(r)[It])?e:f(n)},Dt=function(t,e,n){var r=void 0===n;switch(e.length){case 0:return r?t():t.call(n);case 1:return r?t(e[0]):t.call(n,e[0]);case 2:return r?t(e[0],e[1]):t.call(n,e[0],e[1]);case 3:return r?t(e[0],e[1],e[2]):t.call(n,e[0],e[1],e[2]);case 4:return r?t(e[0],e[1],e[2],e[3]):t.call(n,e[0],e[1],e[2],e[3])}return t.apply(n,e)},Gt=a.process,Ht=a.setImmediate,Ut=a.clearImmediate,Vt=a.MessageChannel,zt=a.Dispatch,Bt=0,Wt={},qt=function(){var t=+this;if(Wt.hasOwnProperty(t)){var e=Wt[t];delete Wt[t],e()}},Jt=function(t){qt.call(t.data)};Ht&&Ut||(Ht=function(t){for(var e=[],n=1;arguments.length>n;)e.push(arguments[n++]);return Wt[++Bt]=function(){Dt("function"==typeof t?t:Function(t),e)},Ot(Bt),Bt},Ut=function(t){delete Wt[t]},"process"==R(Gt)?Ot=function(t){Gt.nextTick(l(qt,t,1))}:zt&&zt.now?Ot=function(t){zt.now(l(qt,t,1))}:Vt?(Tt=(Pt=new Vt).port2,Pt.port1.onmessage=Jt,Ot=l(Tt.postMessage,Tt,1)):a.addEventListener&&"function"==typeof postMessage&&!a.importScripts?(Ot=function(t){a.postMessage(t+"","*")},a.addEventListener("message",Jt,!1)):Ot="onreadystatechange"in _("script")?function(t){Y.appendChild(_("script")).onreadystatechange=function(){Y.removeChild(this),qt.call(t)}}:function(t){setTimeout(l(qt,t,1),0)});var Kt={set:Ht,clear:Ut},Xt=Kt.set,Qt=a.MutationObserver||a.WebKitMutationObserver,Yt=a.process,Zt=a.Promise,$t="process"==R(Yt);function te(t){var e,n;this.promise=new t((function(t,r){if(void 0!==e||void 0!==n)throw TypeError("Bad Promise constructor");e=t,n=r})),this.resolve=f(e),this.reject=f(n)}var ee={f:function(t){return new te(t)}},ne=function(t){try{return{e:!1,v:t()}}catch(t){return{e:!0,v:t}}},re=a.navigator,oe=re&&re.userAgent||"",ie=function(t,e){if(p(t),h(e)&&e.constructor===t)return e;var n=ee.f(t);return(0,n.resolve)(e),n.promise},ce=nt("species"),ue=nt("iterator"),ae=!1;try{var se=[7][ue]();se.return=function(){ae=!0},Array.from(se,(function(){throw 2}))}catch(t){}var fe,le,he,pe,ve,de,ye,me=Kt.set,_e=function(){var t,e,n,r=function(){var r,o;for($t&&(r=Yt.domain)&&r.exit();t;){o=t.fn,t=t.next;try{o()}catch(r){throw t?n():e=void 0,r}}e=void 0,r&&r.enter()};if($t)n=function(){Yt.nextTick(r)};else if(!Qt||a.navigator&&a.navigator.standalone)if(Zt&&Zt.resolve){var o=Zt.resolve(void 0);n=function(){o.then(r)}}else n=function(){Xt.call(a,r)};else{var i=!0,c=document.createTextNode("");new Qt(r).observe(c,{characterData:!0}),n=function(){c.data=i=!i}}return function(r){var o={fn:r,next:void 0};e&&(e.next=o),t||(t=o,n()),e=o}}(),ge=a.TypeError,we=a.process,Se=we&&we.versions,be=Se&&Se.v8||"",je=a.Promise,Oe="process"==Et(we),Pe=function(){},Te=le=ee.f,xe=!!function(){try{var t=je.resolve(1),e=(t.constructor={})[nt("species")]=function(t){t(Pe,Pe)};return(Oe||"function"==typeof PromiseRejectionEvent)&&t.then(Pe)instanceof e&&0!==be.indexOf("6.6")&&-1===oe.indexOf("Chrome/66")}catch(t){}}(),Le=function(t){var e;return!(!h(t)||"function"!=typeof(e=t.then))&&e},Ee=function(t,e){if(!t._n){t._n=!0;var n=t._c;_e((function(){for(var r=t._v,o=1==t._s,i=0,c=function(e){var n,i,c,u=o?e.ok:e.fail,a=e.resolve,s=e.reject,f=e.domain;try{u?(o||(2==t._h&&Re(t),t._h=1),!0===u?n=r:(f&&f.enter(),n=u(r),f&&(f.exit(),c=!0)),n===e.promise?s(ge("Promise-chain cycle")):(i=Le(n))?i.call(n,a,s):a(n)):s(r)}catch(t){f&&!c&&f.exit(),s(t)}};n.length>i;)c(n[i++]);t._c=[],t._n=!1,e&&!t._h&&Me(t)}))}},Me=function(t){me.call(a,(function(){var e,n,r,o=t._v,i=Ae(t);if(i&&(e=ne((function(){Oe?we.emit("unhandledRejection",o,t):(n=a.onunhandledrejection)?n({promise:t,reason:o}):(r=a.console)&&r.error&&r.error("Unhandled promise rejection",o)})),t._h=Oe||Ae(t)?2:1),t._a=void 0,i&&e.e)throw e.v}))},Ae=function(t){return 1!==t._h&&0===(t._a||t._c).length},Re=function(t){me.call(a,(function(){var e;Oe?we.emit("rejectionHandled",t):(e=a.onrejectionhandled)&&e({promise:t,reason:t._v})}))},ke=function(t){var e=this;e._d||(e._d=!0,(e=e._w||e)._v=t,e._s=2,e._a||(e._a=e._c.slice()),Ee(e,!0))},Ce=function(t){var e,n=this;if(!n._d){n._d=!0,n=n._w||n;try{if(n===t)throw ge("Promise can't be resolved itself");(e=Le(t))?_e((function(){var r={_w:n,_d:!1};try{e.call(t,l(Ce,r,1),l(ke,r,1))}catch(t){ke.call(r,t)}})):(n._v=t,n._s=1,Ee(n,!1))}catch(t){ke.call({_w:n,_d:!1},t)}}};xe||(je=function(t){!function(t,e,n,r){if(!(t instanceof e)||void 0!==r&&r in t)throw TypeError(n+": incorrect invocation!")}(this,je,"Promise","_h"),f(t),fe.call(this);try{t(l(Ce,this,1),l(ke,this,1))}catch(t){ke.call(this,t)}},(fe=function(t){this._c=[],this._a=void 0,this._s=0,this._d=!1,this._v=void 0,this._h=0,this._n=!1}).prototype=function(t,e,n){for(var r in e)n&&t[r]?t[r]=e[r]:j(t,r,e[r]);return t}(je.prototype,{then:function(t,e){var n=Te(Nt(this,je));return n.ok="function"!=typeof t||t,n.fail="function"==typeof e&&e,n.domain=Oe?we.domain:void 0,this._c.push(n),this._a&&this._a.push(n),this._s&&Ee(this,!1),n.promise},catch:function(t){return this.then(void 0,t)}}),he=function(){var t=new fe;this.promise=t,this.resolve=l(Ce,t,1),this.reject=l(ke,t,1)},ee.f=Te=function(t){return t===je||t===pe?new he(t):le(t)}),L(L.G+L.W+L.F*!xe,{Promise:je}),it(je,"Promise"),de="function"==typeof s[ve="Promise"]?s[ve]:a[ve],d&&de&&!de[ce]&&S.f(de,ce,{configurable:!0,get:function(){return this}}),pe=s.Promise,L(L.S+L.F*!xe,"Promise",{reject:function(t){var e=Te(this);return(0,e.reject)(t),e.promise}}),L(L.S+!0*L.F,"Promise",{resolve:function(t){return ie(this===pe?je:this,t)}}),L(L.S+L.F*!(xe&&function(t,e){if(!e&&!ae)return!1;var n=!1;try{var r=[7],o=r[ue]();o.next=function(){return{done:n=!0}},r[ue]=function(){return o},t(r)}catch(t){}return n}((function(t){je.all(t).catch(Pe)}))),"Promise",{all:function(t){var e=this,n=Te(e),r=n.resolve,o=n.reject,i=ne((function(){var n=[],i=0,c=1;Ft(t,!1,(function(t){var u=i++,a=!1;n.push(void 0),c++,e.resolve(t).then((function(t){a||(a=!0,n[u]=t,--c||r(n))}),o)})),--c||r(n)}));return i.e&&o(i.v),n.promise},race:function(t){var e=this,n=Te(e),r=n.reject,o=ne((function(){Ft(t,!1,(function(t){e.resolve(t).then(n.resolve,r)}))}));return o.e&&r(o.v),n.promise}}),L(L.P+L.R,"Promise",{finally:function(t){var e=Nt(this,s.Promise||a.Promise),n="function"==typeof t;return this.then(n?function(n){return ie(e,t()).then((function(){return n}))}:t,n?function(n){return ie(e,t()).then((function(){throw n}))}:t)}}),L(L.S,"Promise",{try:function(t){var e=ee.f(this),n=ne(t);return(n.e?e.reject:e.resolve)(n.v),e.promise}}),(ye=n)&&ye.default;var Fe=s.Promise;const{keys:Ie}=Object,Ne=!("navigator"in global)||!/PhantomJS/.test(global.navigator.userAgent),De=global.XMLHttpRequest;function Ge(t,e,n){let r=t.response||t.responseText;if(r&&"string"==typeof r)try{r=JSON.parse(r)}catch(t){t instanceof SyntaxError||console.error(t)}return t.status>=200&&t.status<300?e({body:r,xhr:t}):n(t)}export default class extends t{static get id(){return"rest"}get defaultOptions(){return{host:"http://localhost:3000",apiNamespace:"/polly"}}ajax(t,...n){const{host:r,apiNamespace:o}=this.options;return function(t,e={}){return new Fe((n,r)=>{const o=new De;o.open(e.method||"GET",t,Ne),Ie(e.headers||{}).forEach(t=>o.setRequestHeader(t,e.headers[t])),o.send(e.body),Ne?(o.onreadystatechange=()=>{o.readyState===De.DONE&&Ge(o,n,r)},o.onerror=()=>r(o)):Ge(o,n,r)})}(e(r,o,t),...n)}async onFindRecording(t){const e=await this.ajax("/"+encodeURIComponent(t),{Accept:"application/json; charset=utf-8"});return this._normalize(e)}async onSaveRecording(t,e){await this.ajax("/"+encodeURIComponent(t),{method:"POST",body:this.stringify(e),headers:{"Content-Type":"application/json; charset=utf-8",Accept:"application/json; charset=utf-8"}})}async onDeleteRecording(t){await this.ajax("/"+encodeURIComponent(t),{method:"DELETE"})}_normalize({xhr:t,body:e}){return 204===t.status?null:e}} //# sourceMappingURL=pollyjs-persister-rest.min.js.map