UNPKG

rxjs-http-client

Version:

A simple to use http client built on ES6 fetch and RxJS

2 lines (1 loc) 3.71 kB
(function(c,t){typeof exports=="object"&&typeof module<"u"?t(exports,require("rxjs")):typeof define=="function"&&define.amd?define(["exports","rxjs"],t):(c=typeof globalThis<"u"?globalThis:c||self,t(c["RxJS HTTP Client"]={},c.rxjs))})(this,function(c,t){"use strict";var T=Object.defineProperty;var y=(c,t,n)=>t in c?T(c,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):c[t]=n;var r=(c,t,n)=>y(c,typeof t!="symbol"?t+"":t,n);var n=(i=>(i.POST="POST",i.GET="GET",i.PUT="PUT",i.PATCH="PATCH",i.DELETE="DELETE",i))(n||{});class f{static for(e,s){return{cache:e.cache,mode:e.mode,credentials:e.credentials,headers:e.headers,redirect:e.redirect,referrer:e.referrer,method:s,body:this.formatBody(e,s)}}static formatBody(e,s){var o;return s===n.GET||!e.body?null:((o=e.body)==null?void 0:o.constructor)===Object?JSON.stringify(e.body):e.body}}function u(){return function(i){return i.pipe(t.mergeMap(e=>e.ok?t.of(e):t.throwError(()=>e)))}}class l{constructor(e){r(this,"_interceptors");this._interceptors=e}execute(e){return this._interceptors.length>0?this._interceptors.reduce((s,o)=>o.intercept(s),e):e}}const m=/https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)?/gi;class a{constructor(e,s){r(this,"url");r(this,"headers");r(this,"mode");r(this,"cache");r(this,"credentials");r(this,"redirect");r(this,"referrer");r(this,"body");this.url=new RegExp(m).test(e)?e:`${window.location.origin}${e}`,this.mode=s.mode||void 0,this.cache=s.cache||void 0,this.credentials=s.credentials||void 0,this.headers=s.headers||{},this.redirect=s.redirect||void 0,this.referrer=s.referrer||void 0,this.body=s.body||void 0}clone(){return new a(this.url,{body:this.body,cache:this.cache,mode:this.mode,credentials:this.credentials,headers:this.headers,redirect:this.redirect,referrer:this.referrer})}}class p{constructor(e){r(this,"headers");r(this,"ok");r(this,"redirected");r(this,"status");r(this,"statusText");r(this,"type");r(this,"url");r(this,"_response");this._response=e,this.headers=e.headers,this.ok=e.ok,this.redirected=e.redirected,this.status=e.status,this.statusText=e.statusText,this.type=e.type,this.url=e.url}arrayBuffer(){return t.from(this._response.arrayBuffer())}blob(){return t.from(this._response.blob())}formData(){return t.from(this._response.formData())}json(){return t.from(this._response.json())}text(){return t.from(this._response.text())}clone(){return new p(this._response.clone())}}class _{constructor(e=[],s=[]){r(this,"_reqInterceptors");r(this,"_resInterceptors");this._reqInterceptors=new l(e),this._resInterceptors=new l(s)}get(e,s={}){const o=this._reqInterceptors.execute(new a(e,s)),h=f.for(o,n.GET);return t.from(fetch(e,h)).pipe(t.map(d=>this._resInterceptors.execute(new p(d))),u())}post(e,s){const o=this._reqInterceptors.execute(new a(e,s)),h=f.for(o,n.POST);return t.from(fetch(e,h)).pipe(t.map(d=>this._resInterceptors.execute(new p(d))),u())}put(e,s){const o=this._reqInterceptors.execute(new a(e,s)),h=f.for(o,n.PUT);return t.from(fetch(e,h)).pipe(t.map(d=>this._resInterceptors.execute(new p(d))),u())}patch(e,s){const o=this._reqInterceptors.execute(new a(e,s)),h=f.for(o,n.PATCH);return t.from(fetch(e,h)).pipe(t.map(d=>this._resInterceptors.execute(new p(d))),u())}delete(e,s){const o=this._reqInterceptors.execute(new a(e,s)),h=f.for(o,n.DELETE);return t.from(fetch(e,h)).pipe(t.map(d=>this._resInterceptors.execute(new p(d))),u())}}class b{constructor(){r(this,"mode");r(this,"cache");r(this,"credentials");r(this,"headers");r(this,"redirect");r(this,"referrer");r(this,"body")}}c.HttpRequest=a,c.HttpRequestConfig=b,c.HttpResponse=p,c.RxJSHttpClient=_,Object.defineProperty(c,Symbol.toStringTag,{value:"Module"})});