@signumjs/http
Version:
SignumJS Generic Http Module
3 lines (2 loc) • 4.22 kB
JavaScript
;var sig$http=(()=>{var g=Object.defineProperty;var R=Object.getOwnPropertyDescriptor;var w=Object.getOwnPropertyNames;var v=Object.prototype.hasOwnProperty;var F=(i,t)=>{for(var e in t)g(i,e,{get:t[e],enumerable:!0})},O=(i,t,e,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let n of w(t))!v.call(i,n)&&n!==e&&g(i,n,{get:()=>t[n],enumerable:!(r=R(t,n))||r.enumerable});return i};var E=i=>O(g({},"__esModule",{value:!0}),i);var T={};F(T,{HttpClientFactory:()=>d,HttpError:()=>o,HttpMockBuilder:()=>h,HttpResponse:()=>u});var u=class{constructor(t,e){this.status=t;this.response=e}};var o=class extends Error{constructor(e,r,n,s){super(n);this.requestUrl=e;this.status=r;this.message=n;this.data=s}timestamp=Date.now()};var p=class i{static ForAll="__all";constructor(){this.reset()}_replyFunctions=i.initialReplyFunctions();static initialReplyFunctions(){return{get:{},post:{},put:{},delete:{}}}createReplyFn(t,e){return()=>Promise.resolve(new u(t,e))}createErrorFn(t,e,r,n=null){return()=>{throw new o(t,e,r,n)}}reset(){this._replyFunctions=i.initialReplyFunctions()}registerResponse(t,e,r,n){this._replyFunctions[t][e]=this.createReplyFn(r,n)}registerError(t,e,r,n,s){this._replyFunctions[t][e]=this.createErrorFn(e,r,n,s)}get(t){return this.request("get",t)}delete(t){return this.request("delete",t)}post(t,e){return this.request("post",t)}put(t,e){return this.request("put",t)}request(t,e){let r=this._replyFunctions[t][e]||this._replyFunctions[t][i.ForAll];if(!r)throw new Error(`Could not find any mocked function for method ${t.toUpperCase()} url ${e}`);return r()}},h=class i{_httpMock;constructor(){this._httpMock=new p}static create(){return new i}onReply(t,e,r,n=p.ForAll){return this._httpMock.registerResponse(t,n,e,r),this}onThrowError(t,e,r,n,s=p.ForAll){return this._httpMock.registerError(t,s,e,r,n),this}onGetReply(t,e,r){return this.onReply("get",t,e,r)}onGetThrowError(t,e,r=null,n=p.ForAll){return this.onThrowError("get",t,e,r,n)}onPostReply(t,e,r){return this.onReply("post",t,e,r)}onPostThrowError(t,e,r,n=p.ForAll){return this.onThrowError("post",t,e,r,n)}onPutReply(t,e,r){return this.onReply("put",t,e,r)}onPutThrowError(t,e,r,n=p.ForAll){return this.onThrowError("put",t,e,r,n)}onDeleteReply(t,e,r){return this.onReply("delete",t,e,r)}onDeleteThrowError(t,e,r,n=p.ForAll){return this.onThrowError("delete",t,e,r,n)}build(){return this._httpMock}};var P=i=>i>=200&&i<300,y=class{_baseURL;_defaults;constructor(t,e={}){this._baseURL=t.replace(/\/+$/,""),this._defaults=e}async get(t,e){return this.request("GET",t,void 0,e)}async post(t,e,r){return this.request("POST",t,e,r)}async put(t,e,r){return this.request("PUT",t,e,r)}async delete(t,e){return this.request("DELETE",t,void 0,e)}buildUrl(t){return/^https?:\/\//i.test(t)||!this._baseURL?t:t.startsWith("/")?`${this._baseURL}${t}`:`${this._baseURL}/${t}`}resolveOptions(t){let e={...this._defaults,...t,headers:{...this._defaults.headers||{},...t?.headers||{}},fetchOptions:{...this._defaults.fetchOptions||{},...t?.fetchOptions||{}}};return{...e,validateStatus:e.validateStatus||P}}async request(t,e,r,n){let s=this.resolveOptions(n),H=this.buildUrl(e),c={Accept:"application/json",...s.headers||{}},m;r!=null&&(typeof r=="string"||r instanceof ArrayBuffer||r instanceof Uint8Array||typeof FormData<"u"&&r instanceof FormData?m=r:(m=JSON.stringify(r),!c["Content-Type"]&&!c["content-type"]&&(c["Content-Type"]="application/json")));let f={...s.fetchOptions||{},method:t,headers:c,body:m};s.timeout&&s.timeout>0&&(f.signal=AbortSignal.timeout(s.timeout));let l;try{l=await fetch(H,f)}catch(a){throw a?.name==="TimeoutError"||a?.name==="AbortError"?new o(e,0,"Request timed out",a?.message||null):new o(e,0,"Request failed",a?.message||String(a))}let b=await this.parseBody(l);if(!s.validateStatus(l.status))throw new o(e,l.status,l.statusText||"Request failed",b);return new u(l.status,b)}async parseBody(t){if(t.status===204||t.status===205)return null;if((t.headers.get("content-type")||"").includes("application/json")){let r=await t.text();return r?JSON.parse(r):null}return t.text()}};var d=class{static createHttpClient(t,e){return new y(t,e)}};return E(T);})();
//# sourceMappingURL=signumjs.http.min.js.map