@basmilius/http-client 3.12.1 → 3.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.mjs +1 -1
- package/package.json +4 -4
- package/src/http/RequestBuilder.ts +9 -3
package/dist/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{DateTime as e}from"luxon";import{debounce as t,getPrototypeChain as n,setObjectMethod as r,setObjectValue as i}from"@basmilius/utils";import{customRef as a,markRaw as o,toRaw as s}from"vue";function c(e){return class extends e{constructor(...e){throw Error(`@adapter: cannot create instance of class.`)}}}function ee(){return(e,t)=>{e[t]=e[t].bind(e)}}function te(e){return(n,r,i)=>{i.value=t(i.value,e,n)}}function ne(e,t){return e===t?!0:e===t}const l=Symbol(),u=Symbol(),d=Symbol(),f=Symbol(),p=Symbol(),m=Symbol(),h=Symbol(),g=Symbol(),_=Symbol(),v=Symbol(),re=Symbol();function y(e){return e&&typeof e==`object`&&!!e[p]}function b(e){if(!y(e))throw Error(`@dto assert given object is not a class decorated with @Dto.`)}const ie=Symbol();function x(e,t=0,n){return function(...r){let i=e[ie]??=new WeakMap,a=r[t],o=n===void 0?`self`:r[n];if(typeof a!=`object`||!a)return e.call(this,...r);i.has(a)||i.set(a,[]);let s=i.get(a);if(!s.includes(o)){s.push(o);try{return e.call(this,...r)}finally{let e=s.lastIndexOf(o);e!==-1&&s.splice(e,1),s.length===0&&i.delete(a)}}}}function ae(e){return b(e),e.clone()}function S(e){return b(e),e[f]}const C=x(function(e,t,n,r){let i=e[re];i(e,t,n,r),e[m]&&C(e[m],e[h],e[m][e[h]])},0,1),w=x(function(e){b(e),e[f]&&(e[f]=!1,C(e,f,!1,!0)),!(!e[u]||e[u].length===0)&&e[u].filter(S).forEach(w)});async function oe(e,t){!y(e)||!S(e)||(await t(e),w(e))}function se(e){return b(e),!e[f]}const T=x(function(e,t){b(e),e[f]||(e[f]=!0,C(e,f,!0,!1)),e[m]&&T(e[m],e[h])});function E(e,t,n){t[u]??=[],!t[u].includes(e)&&t[u].push(e),e[m]!==t&&(e[m]=t),e[h]!==n&&(e[h]=n)}function D(e,t,n){if(y(n))E(n,e,t);else if(Array.isArray(n)){for(let r of n)y(r)&&E(r,e,t);n[m]=e,n[h]=t}}function O(e,t){let n=e[v];n(e,t)}function ce(e,t){if(u in t){let n=t[u].indexOf(e);n!==-1&&t[u].splice(n,1)}e[m]=void 0,e[h]=void 0}function le(e,t){if(y(t))ce(t,e);else if(Array.isArray(t)){for(let n of t)y(n)&&ce(n,e);t[m]=void 0,t[h]=void 0}}const k={};var ue={deleteProperty(e,t){if(Reflect.deleteProperty(e,t),A(e,t))return!0;let n=e[m];return n&&C(n,e[h],n[e[h]]),n&&T(n,e[h]),!0},get(e,t,n){if(t===_)return!0;if(A(e,t))return Reflect.get(e,t,n);let r=e[m];return r&&O(r,e[h]),Reflect.get(e,t)},set(e,t,n,r){if(A(e,t))return Reflect.set(e,t,n,r);let i=e[m];return i&&C(i,e[h],i[e[h]]),i&&T(i,e[h]),Reflect.set(e,t,n)}};function A(e,t){return typeof t==`symbol`||typeof e[t]==`function`||t===`length`}var de={get(e,t,n){if(t===_)return!0;if(typeof t==`symbol`)return Reflect.get(e,t,n);let r=e[d][t];if(!r||!r.get)return Reflect.get(e,t,n);let i=r.get.call(e);return O(e,t),D(e,t,i),i},getOwnPropertyDescriptor(e,t){return e[d][t]},ownKeys(e){return e[g]},set(e,t,n,r){if(typeof t==`symbol`)return Reflect.set(e,t,n,r);let i=e[d][t];if(!i||!i.set)return Reflect.set(e,t,n,r);let a=i.get?.call(e)??void 0;return ne(n,a)?!0:(le(e,a),Array.isArray(n)&&!n[_]&&(n=new Proxy(n,ue)),i.set.call(e,n),D(e,t,n),T(e,t),C(e,t,n,a),!0)}},fe={get(e,t,n){return t===`__v_isRef`?!1:t===_?!0:t in e?Reflect.get(e,t,n):Reflect.get(e.value,t)},getOwnPropertyDescriptor(e,t){return Reflect.getOwnPropertyDescriptor(e.value,t)},ownKeys(e){return Reflect.ownKeys(e.value)},set(e,t,n,r){return t in e?Reflect.set(e,t,n,r):Reflect.set(e.value,t,n)}},pe={construct(e,t,n){t=t.map(e=>Array.isArray(e)?new Proxy(e,ue):e);let r=a((r,i)=>{let a=o(Reflect.construct(e,t,n));a[l]=t,a[f]=!1,a[v]=r,a[re]=i;let s=new Proxy(a,de);return{get:()=>(r(),s),set:()=>void 0}});return new Proxy(r,fe)}};function me(){let e=this;b(e);let t=k[e[p]],n=new t(...e[l]);for(let[e,t]of Object.entries(this[d]))t.set&&(n[e]=y(this[e])?this[e].clone():this[e]);return n}function he(e){for(let t in e){let n=this[d][t];y(this[t])&&typeof e[t]==`object`?this[t].fill(e[t]):n&&n.set&&(this[t]=e[t])}}function ge(){let e={};for(let t of this[g]){let n=this[t];y(n)&&(n=n.toJSON()),e[t]=n}return e}function j(e){ve(e);let t=Object.freeze(n(e)),a=Object.keys(t);return i(e.prototype,d,t),i(e.prototype,p,e.name),i(e.prototype,g,a),i(e,Symbol.hasInstance,t=>typeof t==`object`&&t?.[p]===e.name),r(e,`clone`,me),r(e,`fill`,he),r(e,`toJSON`,ge),_e(e)}function _e(e){let t=new Proxy(e,pe);return k[e.name]=t,t}function ve(e){let t=Object.getPrototypeOf(e.prototype);if(p in t)throw Error(`⛔️ @dto ${e.name} cannot extend parent class which is also decorated with @dto ${t[p]}.`)}function M(e){return e.map(F)}function ye([,t]){return e.fromISO(t)}function N(e){return Object.fromEntries(Object.entries(e).map(([e,t])=>[e,F(t)]).filter(([,e])=>e!==void 0))}const P={};function be([,e,t,n,r]){if(!(t in k))throw Error(`Cannot restore @dto. Dto ${t} was not found.`);if(e in P)return P[e];let i=k[t],a=new i(...M(r));return a.fill(N(n)),P[e]=a,a}function xe(e){return Array.isArray(e)&&e[0]===3058}function Se(e){return Array.isArray(e)&&e[0]===3057}function F(e){switch(!0){case e===null:return null;case Array.isArray(e):switch(!0){case xe(e):return ye(e);case Se(e):return be(e);default:return M(e)}case typeof e==`object`:return N(e);default:return e}}function Ce(e){return F(JSON.parse(e))}function I(e){return[3058,e.toISO({extendedZone:!0,includeOffset:!0})]}function L(e){return Object.fromEntries(Object.entries(e).map(([e,t])=>[e,B(t)]).filter(([,e])=>e!==void 0))}function we(){return`10000000-1000-4000-8000-100000000000`.replace(/[018]/g,e=>(e^crypto.getRandomValues(new Uint8Array(1))[0]&15>>e/4).toString(16))}function R(e){e=s(e);let t=e.toJSON();return[3057,we(),e[p],L(t),z(e[l])]}function z(t){switch(!0){case t.every(y):return t.map(R);case t.every(e.isDateTime):return t.map(I);default:return t.map(B)}}function B(t){switch(!0){case t===null:return null;case Array.isArray(t):return z(t);case y(t):return R(t);case e.isDateTime(t):return I(t);case typeof t==`object`:return L(t);case typeof t==`boolean`:case typeof t==`number`:case typeof t==`string`:return t;default:return}}function Te(e){return JSON.stringify(B(e))}function V(e,t,n,r){var i=arguments.length,a=i<3?t:r===null?r=Object.getOwnPropertyDescriptor(t,n):r,o;if(typeof Reflect==`object`&&typeof Reflect.decorate==`function`)a=Reflect.decorate(e,t,n,r);else for(var s=e.length-1;s>=0;s--)(o=e[s])&&(a=(i<3?o(a):i>3?o(t,n,a):o(t,n))||a);return i>3&&a&&Object.defineProperty(t,n,a),a}let H=class{get blob(){return this.#e}get name(){return this.#t}#e;#t;constructor(e,t){this.#e=e,this.#t=t}};H=V([j],H);var Ee=H;let U=class{get items(){return this.#e}get page(){return this.#t}get pageSize(){return this.#n}get pages(){return this.#r}get total(){return this.#i}#e;#t;#n;#r;#i;constructor(e,t,n,r,i){this.#e=e,this.#t=t,this.#n=n,this.#r=r,this.#i=i}};U=V([j],U);var De=U;let W=class{get code(){return this.#e}get error(){return this.#t}get errorDescription(){return this.#n}get statusCode(){return this.#r}#e;#t;#n;#r;constructor(e,t,n,r){this.#e=e,this.#t=t,this.#n=n,this.#r=r}};W=V([j],W);var G=W;let K=class{get code(){return this.#e}get error(){return this.#t}get errorDescription(){return this.#n}get errors(){return this.#r}get params(){return this.#i}#e;#t;#n;#r;#i;constructor(e,t,n,r,i){this.#e=e,this.#t=t,this.#n=n,this.#r=r,this.#i=i}};K=V([j],K);var q=K,J;let Y=J=class{static parsePaginatedAdapter(e,t){return new De(e.items.map(t),e.page,e.page_size,e.pages,e.total)}static parseFileNameFromContentDispositionHeader(t){let n=`download-${e.now().toFormat(`yyyy-MM-dd HH-mm-ss`)}`;if(!t.startsWith(`attachment`))return n;let r=/filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/.exec(t);return(r?.length||0)<2?n:r[1].replaceAll(`'`,``).replaceAll(`"`,``).replaceAll(`/`,`-`).replaceAll(`:`,`-`)}static parseRequestError(e,t){return new G(e.code,e.error,e.error_description,t)}static parseValidationError(e){let t;return e.errors&&(t={},Object.entries(e.errors).forEach(([e,n])=>{t[e]=J.parseValidationError(n)})),new q(e.code,e.error,e.error_description,t,e.params)}};Y=J=V([c],Y);var X=class{get data(){return this.#e}get headers(){return this.#t.headers}get ok(){return this.statusCode>=200&&this.statusCode<300}get response(){return this.#t}get statusCode(){return this.#t.status}#e;#t;constructor(e,t){this.#e=e,this.#t=t}},Oe=class e{get authToken(){return this.#e}set authToken(e){this.#e=e}get baseUrl(){return this.#t}get dataField(){return this.#n}#e;#t;#n;constructor(e,t,n=!1){this.#e=e,this.#t=t,this.#n=n}static get instance(){if(e.#r===null)throw Error(`There is currently no HttpClient instance registered. Register one using the HttpClient.register() function.`);return e.#r}static#r=null;static register(t){e.#r=t}},Z=class extends Error{};const Q={};var $=class t{get client(){return this.#e}get options(){return this.#r}get path(){return this.#n}set path(e){this.#n=e}get query(){return this.#i}set query(e){this.#i=e}#e;#t=null;#n;#r={};#i=null;constructor(e,t){this.#e=t??Oe.instance,this.#r.cache=`no-cache`,this.#r.method=`GET`,this.#n=e}autoCancel(e){return this.#t=e,this}bearerToken(e){return e??=this.#e.authToken,e?this.header(`Authorization`,`Bearer ${e}`):(this.#r.headers&&`Authorization`in this.#r.headers&&delete this.#r.headers.Authorization,this)}body(e,t=`application/octet-stream`){return e instanceof FormData?t=null:(Array.isArray(e)||typeof e==`object`)&&(e=JSON.stringify(e),t=`application/json`),this.#r.body=e,t===null?this:this.header(`Content-Type`,t)}header(e,t){return this.#r.headers=this.#r.headers||{},this.#r.headers[e]=t,this}method(e){return this.#r.method=e.toUpperCase(),this}queryString(e){return this.#i=e,this}signal(e=null){return this.#r.signal=e,this}async fetch(){return this.#a().then(e=>e.json())}async fetchBlob(){let t=await this.#a();if(t.status!==200){let e=await t.json();throw`code`in e&&`error`in e&&`error_description`in e?new G(e.code,e.error,e.error_description,t.status):new G(-1,`failed_without_info`,`Request failed without any information from the backend.`,t.status)}let n=t.headers.has(`content-disposition`)?Y.parseFileNameFromContentDispositionHeader(t.headers.get(`content-disposition`)):`download-${e.now().toFormat(`yyyy-MM-dd HH-mm-ss`)}`;return new Ee(await t.blob(),n)}async run(){let{data:e,response:t}=await this.#o();return new X(e,t)}async runAdapter(e){let{data:t,response:n}=await this.#o();return new X(e(t),n)}async runArrayAdapter(e){return this.runAdapter(t=>t.map(e))}async runEmpty(){return await this.#o()}async runPaginatedAdapter(e){return this.runAdapter(t=>Y.parsePaginatedAdapter(t,e))}async runData(){return await this.#o()}async runDataKey(e){let{data:t,response:n}=await this.#o();return new X(t[e],n)}async runStatusCode(){return(await this.#o()).statusCode}async#a(){if(this.#t!==null){this.#t in Q&&Q[this.#t].abort(new Z);let e=new AbortController;Q[this.#t]=e,this.signal(e.signal)}let e=this.path;return this.query!==null&&(e+=`?${this.query.build()}`),await fetch(this.client.baseUrl+e,this.options)}async#o(){return await this.#a().then(e=>t.#s(e,this.client.dataField))}static async#s(e,t){if(e.status===204)return new X(null,e);if(e.headers.has(`content-type`)&&e.headers.get(`content-type`).startsWith(`application/json`)){let n=await e.json();if(n&&typeof n==`object`&&`code`in n&&`error`in n&&`error_description`in n)throw`errors`in n?Y.parseValidationError(n):Y.parseRequestError(n,e.status);return t&&`data`in n?new X(n.data,e):new X(n,e)}if(e.status===401||e.status===403||(await e.text()).length===0&&e.status>=200&&e.status<300)return new X(null,e);throw new G(-1,`not_a_json_response`,`The response was not a JSON response.`,e.status)}},ke=class{request(e,t){return new $(e,t)}},Ae=class e{#e;constructor(){this.#e=new URLSearchParams}build(){return this.#e.toString()}append(e,t){return this.#t(this.#e.append.bind(this.#e),e,t)}appendArray(e,t){return t?.forEach(t=>this.append(e,t)),this}delete(e){return this.#e.delete(e),this}get(e){return this.#e.get(e)}getAll(e){return this.#e.getAll(e)}has(e){return this.#e.has(e)}set(e,t){return this.#t(this.#e.set.bind(this.#e),e,t)}#t(e,t,n){if(!n&&n!==!1)return this;switch(typeof n){case`boolean`:e(t,n?`true`:`false`);break;case`number`:e(t,n.toString(10));break;case`string`:e(t,n);break}return this}static builder(){return new e}};function je(e){return e instanceof Z}function Me(e){return e instanceof G}function Ne(e){return e instanceof G&&(e=e.statusCode),e===403||e===401}function Pe(e){return e instanceof q}export{X as BaseResponse,ke as BaseService,Ee as BlobResponse,Y as HttpAdapter,Oe as HttpClient,De as Paginated,Ae as QueryString,Z as RequestAbortedError,$ as RequestBuilder,G as RequestError,q as ValidationError,c as adapter,b as assertDto,ee as bound,ae as cloneDto,te as debounce,Ce as deserialize,j as dto,oe as executeIfDtoDirtyAndMarkClean,y as isDto,se as isDtoClean,S as isDtoDirty,je as isRequestAborted,Me as isRequestError,Ne as isUnsanctionedRequest,Pe as isValidationError,w as markDtoClean,T as markDtoDirty,Te as serialize};
|
|
1
|
+
import{DateTime as e}from"luxon";import{debounce as t,getPrototypeChain as n,setObjectMethod as r,setObjectValue as i}from"@basmilius/utils";import{customRef as a,markRaw as o,toRaw as s}from"vue";function c(e){return class extends e{constructor(...e){throw Error(`@adapter: cannot create instance of class.`)}}}function ee(){return(e,t)=>{e[t]=e[t].bind(e)}}function te(e){return(n,r,i)=>{i.value=t(i.value,e,n)}}function ne(e,t){return e===t?!0:e===t}const l=Symbol(),u=Symbol(),d=Symbol(),f=Symbol(),p=Symbol(),m=Symbol(),h=Symbol(),g=Symbol(),_=Symbol(),re=Symbol(),ie=Symbol();function v(e){return e&&typeof e==`object`&&!!e[p]}function y(e){if(!v(e))throw Error(`@dto assert given object is not a class decorated with @Dto.`)}const ae=Symbol();function b(e,t=0,n){return function(...r){let i=e[ae]??=new WeakMap,a=r[t],o=n===void 0?`self`:r[n];if(typeof a!=`object`||!a)return e.call(this,...r);i.has(a)||i.set(a,[]);let s=i.get(a);if(!s.includes(o)){s.push(o);try{return e.call(this,...r)}finally{let e=s.lastIndexOf(o);e!==-1&&s.splice(e,1),s.length===0&&i.delete(a)}}}}function oe(e){return y(e),e.clone()}function x(e){return y(e),e[f]}const S=b(function(e,t,n,r){let i=e[ie];i(e,t,n,r),e[m]&&S(e[m],e[h],e[m][e[h]])},0,1),C=b(function(e){y(e),e[f]&&(e[f]=!1,S(e,f,!1,!0)),!(!e[u]||e[u].length===0)&&e[u].filter(x).forEach(C)});async function se(e,t){!v(e)||!x(e)||(await t(e),C(e))}function ce(e){return y(e),!e[f]}const w=b(function(e,t){y(e),e[f]||(e[f]=!0,S(e,f,!0,!1)),e[m]&&w(e[m],e[h])});function T(e,t,n){t[u]??=[],!t[u].includes(e)&&t[u].push(e),e[m]!==t&&(e[m]=t),e[h]!==n&&(e[h]=n)}function E(e,t,n){if(v(n))T(n,e,t);else if(Array.isArray(n)){for(let r of n)v(r)&&T(r,e,t);n[m]=e,n[h]=t}}function le(e,t){let n=e[re];n(e,t)}function D(e,t){if(u in t){let n=t[u].indexOf(e);n!==-1&&t[u].splice(n,1)}e[m]=void 0,e[h]=void 0}function ue(e,t){if(v(t))D(t,e);else if(Array.isArray(t)){for(let n of t)v(n)&&D(n,e);t[m]=void 0,t[h]=void 0}}const O={};var k={deleteProperty(e,t){if(Reflect.deleteProperty(e,t),A(e,t))return!0;let n=e[m];return n&&S(n,e[h],n[e[h]]),n&&w(n,e[h]),!0},get(e,t,n){if(t===_)return!0;if(A(e,t))return Reflect.get(e,t,n);let r=e[m];return r&&le(r,e[h]),Reflect.get(e,t)},set(e,t,n,r){if(A(e,t))return Reflect.set(e,t,n,r);let i=e[m];return i&&S(i,e[h],i[e[h]]),i&&w(i,e[h]),Reflect.set(e,t,n)}};function A(e,t){return typeof t==`symbol`||typeof e[t]==`function`||t===`length`}var de={get(e,t,n){if(t===_)return!0;if(typeof t==`symbol`)return Reflect.get(e,t,n);let r=e[d][t];if(!r||!r.get)return Reflect.get(e,t,n);let i=r.get.call(e);return le(e,t),E(e,t,i),i},getOwnPropertyDescriptor(e,t){return e[d][t]},ownKeys(e){return e[g]},set(e,t,n,r){if(typeof t==`symbol`)return Reflect.set(e,t,n,r);let i=e[d][t];if(!i||!i.set)return Reflect.set(e,t,n,r);let a=i.get?.call(e)??void 0;return ne(n,a)?!0:(ue(e,a),Array.isArray(n)&&!n[_]&&(n=new Proxy(n,k)),i.set.call(e,n),E(e,t,n),w(e,t),S(e,t,n,a),!0)}},fe={get(e,t,n){return t===`__v_isRef`?!1:t===_?!0:t in e?Reflect.get(e,t,n):Reflect.get(e.value,t)},getOwnPropertyDescriptor(e,t){return Reflect.getOwnPropertyDescriptor(e.value,t)},ownKeys(e){return Reflect.ownKeys(e.value)},set(e,t,n,r){return t in e?Reflect.set(e,t,n,r):Reflect.set(e.value,t,n)}},pe={construct(e,t,n){t=t.map(e=>Array.isArray(e)?new Proxy(e,k):e);let r=a((r,i)=>{let a=o(Reflect.construct(e,t,n));a[l]=t,a[f]=!1,a[re]=r,a[ie]=i;let s=new Proxy(a,de);return{get:()=>(r(),s),set:()=>void 0}});return new Proxy(r,fe)}};function me(){let e=this;y(e);let t=O[e[p]],n=new t(...e[l]);for(let[e,t]of Object.entries(this[d]))t.set&&(n[e]=v(this[e])?this[e].clone():this[e]);return n}function he(e){for(let t in e){let n=this[d][t];v(this[t])&&typeof e[t]==`object`?this[t].fill(e[t]):n&&n.set&&(this[t]=e[t])}}function ge(){let e={};for(let t of this[g]){let n=this[t];v(n)&&(n=n.toJSON()),e[t]=n}return e}function j(e){ve(e);let t=Object.freeze(n(e)),a=Object.keys(t);return i(e.prototype,d,t),i(e.prototype,p,e.name),i(e.prototype,g,a),i(e,Symbol.hasInstance,t=>typeof t==`object`&&t?.[p]===e.name),r(e,`clone`,me),r(e,`fill`,he),r(e,`toJSON`,ge),_e(e)}function _e(e){let t=new Proxy(e,pe);return O[e.name]=t,t}function ve(e){let t=Object.getPrototypeOf(e.prototype);if(p in t)throw Error(`⛔️ @dto ${e.name} cannot extend parent class which is also decorated with @dto ${t[p]}.`)}function M(e){return e.map(F)}function ye([,t]){return e.fromISO(t)}function N(e){return Object.fromEntries(Object.entries(e).map(([e,t])=>[e,F(t)]).filter(([,e])=>e!==void 0))}const P={};function be([,e,t,n,r]){if(!(t in O))throw Error(`Cannot restore @dto. Dto ${t} was not found.`);if(e in P)return P[e];let i=O[t],a=new i(...M(r));return a.fill(N(n)),P[e]=a,a}function xe(e){return Array.isArray(e)&&e[0]===3058}function Se(e){return Array.isArray(e)&&e[0]===3057}function F(e){switch(!0){case e===null:return null;case Array.isArray(e):switch(!0){case xe(e):return ye(e);case Se(e):return be(e);default:return M(e)}case typeof e==`object`:return N(e);default:return e}}function Ce(e){return F(JSON.parse(e))}function we(e){return[3058,e.toISO({extendedZone:!0,includeOffset:!0})]}function I(e){return Object.fromEntries(Object.entries(e).map(([e,t])=>[e,z(t)]).filter(([,e])=>e!==void 0))}function Te(){return`10000000-1000-4000-8000-100000000000`.replace(/[018]/g,e=>(e^crypto.getRandomValues(new Uint8Array(1))[0]&15>>e/4).toString(16))}function L(e){e=s(e);let t=e.toJSON();return[3057,Te(),e[p],I(t),R(e[l])]}function R(t){switch(!0){case t.every(v):return t.map(L);case t.every(e.isDateTime):return t.map(we);default:return t.map(z)}}function z(t){switch(!0){case t===null:return null;case Array.isArray(t):return R(t);case v(t):return L(t);case e.isDateTime(t):return we(t);case typeof t==`object`:return I(t);case typeof t==`boolean`:case typeof t==`number`:case typeof t==`string`:return t;default:return}}function Ee(e){return JSON.stringify(z(e))}function B(e,t,n,r){var i=arguments.length,a=i<3?t:r===null?r=Object.getOwnPropertyDescriptor(t,n):r,o;if(typeof Reflect==`object`&&typeof Reflect.decorate==`function`)a=Reflect.decorate(e,t,n,r);else for(var s=e.length-1;s>=0;s--)(o=e[s])&&(a=(i<3?o(a):i>3?o(t,n,a):o(t,n))||a);return i>3&&a&&Object.defineProperty(t,n,a),a}let V=class{get blob(){return this.#e}get name(){return this.#t}#e;#t;constructor(e,t){this.#e=e,this.#t=t}};V=B([j],V);var H=V;let U=class{get items(){return this.#e}get page(){return this.#t}get pageSize(){return this.#n}get pages(){return this.#r}get total(){return this.#i}#e;#t;#n;#r;#i;constructor(e,t,n,r,i){this.#e=e,this.#t=t,this.#n=n,this.#r=r,this.#i=i}};U=B([j],U);var W=U;let G=class{get code(){return this.#e}get error(){return this.#t}get errorDescription(){return this.#n}get statusCode(){return this.#r}#e;#t;#n;#r;constructor(e,t,n,r){this.#e=e,this.#t=t,this.#n=n,this.#r=r}};G=B([j],G);var K=G;let q=class{get code(){return this.#e}get error(){return this.#t}get errorDescription(){return this.#n}get errors(){return this.#r}get params(){return this.#i}#e;#t;#n;#r;#i;constructor(e,t,n,r,i){this.#e=e,this.#t=t,this.#n=n,this.#r=r,this.#i=i}};q=B([j],q);var J=q,Y;let X=Y=class{static parsePaginatedAdapter(e,t){return new W(e.items.map(t),e.page,e.page_size,e.pages,e.total)}static parseFileNameFromContentDispositionHeader(t){let n=`download-${e.now().toFormat(`yyyy-MM-dd HH-mm-ss`)}`;if(!t.startsWith(`attachment`))return n;let r=/filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/.exec(t);return(r?.length||0)<2?n:r[1].replaceAll(`'`,``).replaceAll(`"`,``).replaceAll(`/`,`-`).replaceAll(`:`,`-`)}static parseRequestError(e,t){return new K(e.code,e.error,e.error_description,t)}static parseValidationError(e){let t;return e.errors&&(t={},Object.entries(e.errors).forEach(([e,n])=>{t[e]=Y.parseValidationError(n)})),new J(e.code,e.error,e.error_description,t,e.params)}};X=Y=B([c],X);var Z=class{get data(){return this.#e}get headers(){return this.#t.headers}get ok(){return this.statusCode>=200&&this.statusCode<300}get response(){return this.#t}get statusCode(){return this.#t.status}#e;#t;constructor(e,t){this.#e=e,this.#t=t}},De=class e{get authToken(){return this.#e}set authToken(e){this.#e=e}get baseUrl(){return this.#t}get dataField(){return this.#n}#e;#t;#n;constructor(e,t,n=!1){this.#e=e,this.#t=t,this.#n=n}static get instance(){if(e.#r===null)throw Error(`There is currently no HttpClient instance registered. Register one using the HttpClient.register() function.`);return e.#r}static#r=null;static register(t){e.#r=t}},Q=class extends Error{};const $={};var Oe=class t{get client(){return this.#e}get options(){return this.#r}get path(){return this.#n}set path(e){this.#n=e}get query(){return this.#i}set query(e){this.#i=e}#e;#t=null;#n;#r={};#i=null;constructor(e,t){this.#e=t??De.instance,this.#r.cache=`no-cache`,this.#r.method=`GET`,this.#n=e}autoCancel(e){return this.#t=e,this}bearerToken(e){return e??=this.#e.authToken,e?this.header(`Authorization`,`Bearer ${e}`):(this.#r.headers&&`Authorization`in this.#r.headers&&delete this.#r.headers.Authorization,this)}body(e,t=`application/octet-stream`){return e instanceof FormData?t=null:(Array.isArray(e)||typeof e==`object`)&&(e=JSON.stringify(e),t=`application/json`),this.#r.body=e,t===null?this:this.header(`Content-Type`,t)}header(e,t){return this.#r.headers=this.#r.headers||{},this.#r.headers[e]=t,this}method(e){return this.#r.method=e.toUpperCase(),this}queryString(e){return this.#i=e,this}signal(e=null){return this.#r.signal=e,this}async fetch(){return this.#a().then(e=>e.json())}async fetchBlob(){let t=await this.#a();if(t.status!==200){let e=await t.json();throw`code`in e&&`error`in e&&`error_description`in e?new K(e.code,e.error,e.error_description,t.status):new K(-1,`failed_without_info`,`Request failed without any information from the backend.`,t.status)}let n=t.headers.has(`content-disposition`)?X.parseFileNameFromContentDispositionHeader(t.headers.get(`content-disposition`)??``):`download-${e.now().toFormat(`yyyy-MM-dd HH-mm-ss`)}`;return new H(await t.blob(),n)}async run(){let{data:e,response:t}=await this.#o();return new Z(e,t)}async runAdapter(e){let{data:t,response:n}=await this.#o();return new Z(e(t),n)}async runArrayAdapter(e){return this.runAdapter(t=>t.map(e))}async runEmpty(){return await this.#o()}async runPaginatedAdapter(e){return this.runAdapter(t=>X.parsePaginatedAdapter(t,e))}async runData(){return await this.#o()}async runDataKey(e){let{data:t,response:n}=await this.#o();return new Z(t[e],n)}async runStatusCode(){return(await this.#o()).statusCode}async#a(){if(this.#t!==null){this.#t in $&&$[this.#t].abort(new Q);let e=new AbortController;$[this.#t]=e,this.signal(e.signal)}let e=this.path;this.query!==null&&(e+=`?${this.query.build()}`);let t=await fetch(this.client.baseUrl+e,this.options);return this.#t!==null&&delete $[this.#t],t}async#o(){return await this.#a().then(e=>t.#s(e,this.client.dataField))}static async#s(e,t){if(e.status===204)return new Z(null,e);if(e.headers.has(`content-type`)&&e.headers.get(`content-type`)?.startsWith(`application/json`)){let n=await e.json();if(n&&typeof n==`object`&&`code`in n&&`error`in n&&`error_description`in n)throw`errors`in n?X.parseValidationError(n):X.parseRequestError(n,e.status);return t&&`data`in n?new Z(n.data,e):new Z(n,e)}if(e.status===401||e.status===403||(await e.text()).length===0&&e.status>=200&&e.status<300)return new Z(null,e);throw new K(-1,`not_a_json_response`,`The response was not a JSON response.`,e.status)}},ke=class{request(e,t){return new Oe(e,t)}},Ae=class e{#e;constructor(){this.#e=new URLSearchParams}build(){return this.#e.toString()}append(e,t){return this.#t(this.#e.append.bind(this.#e),e,t)}appendArray(e,t){return t?.forEach(t=>this.append(e,t)),this}delete(e){return this.#e.delete(e),this}get(e){return this.#e.get(e)}getAll(e){return this.#e.getAll(e)}has(e){return this.#e.has(e)}set(e,t){return this.#t(this.#e.set.bind(this.#e),e,t)}#t(e,t,n){if(!n&&n!==!1)return this;switch(typeof n){case`boolean`:e(t,n?`true`:`false`);break;case`number`:e(t,n.toString(10));break;case`string`:e(t,n);break}return this}static builder(){return new e}};function je(e){return e instanceof Q}function Me(e){return e instanceof K}function Ne(e){return e instanceof K&&(e=e.statusCode),e===403||e===401}function Pe(e){return e instanceof J}export{Z as BaseResponse,ke as BaseService,H as BlobResponse,X as HttpAdapter,De as HttpClient,W as Paginated,Ae as QueryString,Q as RequestAbortedError,Oe as RequestBuilder,K as RequestError,J as ValidationError,c as adapter,y as assertDto,ee as bound,oe as cloneDto,te as debounce,Ce as deserialize,j as dto,se as executeIfDtoDirtyAndMarkClean,v as isDto,ce as isDtoClean,x as isDtoDirty,je as isRequestAborted,Me as isRequestError,Ne as isUnsanctionedRequest,Pe as isValidationError,C as markDtoClean,w as markDtoDirty,Ee as serialize};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@basmilius/http-client",
|
|
3
3
|
"description": "Http client for Vue projects.",
|
|
4
|
-
"version": "3.
|
|
4
|
+
"version": "3.13.0",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"funding": "https://github.com/sponsors/basmilius",
|
|
7
7
|
"homepage": "https://github.com/basmilius/packages",
|
|
@@ -46,12 +46,12 @@
|
|
|
46
46
|
"provenance": true
|
|
47
47
|
},
|
|
48
48
|
"dependencies": {
|
|
49
|
-
"@basmilius/utils": "3.
|
|
49
|
+
"@basmilius/utils": "3.13.0",
|
|
50
50
|
"luxon": "^3.7.2",
|
|
51
|
-
"vue": "^3.6.0-beta.
|
|
51
|
+
"vue": "^3.6.0-beta.8"
|
|
52
52
|
},
|
|
53
53
|
"devDependencies": {
|
|
54
54
|
"@types/luxon": "^3.7.1",
|
|
55
|
-
"tsdown": "^0.21.
|
|
55
|
+
"tsdown": "^0.21.4"
|
|
56
56
|
}
|
|
57
57
|
}
|
|
@@ -129,7 +129,7 @@ export default class RequestBuilder {
|
|
|
129
129
|
}
|
|
130
130
|
|
|
131
131
|
let filename = response.headers.has('content-disposition')
|
|
132
|
-
? HttpAdapter.parseFileNameFromContentDispositionHeader(response.headers.get('content-disposition'))
|
|
132
|
+
? HttpAdapter.parseFileNameFromContentDispositionHeader(response.headers.get('content-disposition') ?? '')
|
|
133
133
|
: `download-${DateTime.now().toFormat('yyyy-MM-dd HH-mm-ss')}`;
|
|
134
134
|
|
|
135
135
|
return new BlobResponse(
|
|
@@ -196,7 +196,13 @@ export default class RequestBuilder {
|
|
|
196
196
|
path += `?${this.query.build()}`;
|
|
197
197
|
}
|
|
198
198
|
|
|
199
|
-
|
|
199
|
+
const response = await fetch(this.client.baseUrl + path, this.options);
|
|
200
|
+
|
|
201
|
+
if (this.#autoCancelIdentifier !== null) {
|
|
202
|
+
delete abortControllers[this.#autoCancelIdentifier];
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
return response;
|
|
200
206
|
}
|
|
201
207
|
|
|
202
208
|
async #executeSafe<TResult>(): Promise<BaseResponse<TResult>> {
|
|
@@ -210,7 +216,7 @@ export default class RequestBuilder {
|
|
|
210
216
|
return new BaseResponse(null, response);
|
|
211
217
|
}
|
|
212
218
|
|
|
213
|
-
if (response.headers.has('content-type') && response.headers.get('content-type')
|
|
219
|
+
if (response.headers.has('content-type') && response.headers.get('content-type')?.startsWith('application/json')) {
|
|
214
220
|
const data = await response.json();
|
|
215
221
|
|
|
216
222
|
if (data && typeof data === 'object' && 'code' in data && 'error' in data && 'error_description' in data) {
|