@bereasoftware/nexa 1.0.5 → 1.2.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/nexa.umd.js CHANGED
@@ -1 +1,2 @@
1
- (function(e,t){typeof exports==`object`&&typeof module<`u`?t(exports):typeof define==`function`&&define.amd?define([`exports`],t):(e=typeof globalThis<`u`?globalThis:e||self,t(e.Nexa={}))})(this,function(e){Object.defineProperty(e,Symbol.toStringTag,{value:`Module`});var t=e=>({ok:!0,value:e}),n=e=>({ok:!1,error:e});function r(e){return{validate(r){let i=r;for(let[t,r]of Object.entries(e))if(!r(i[t]))return n({message:`Validation failed: field "${t}" is invalid`,code:`VALIDATION_ERROR`});return t(r)}}}function i(e){return{validate(r){let i=r,a=e.filter(e=>!(e in i));return a.length>0?n({message:`Validation failed: missing fields: ${a.join(`, `)}`,code:`VALIDATION_ERROR`}):t(r)}}}var a={validate(e){return Array.isArray(e)?t(e):n({message:`Expected array response`,code:`VALIDATION_ERROR`})}},o={validate(e){return e&&typeof e==`object`&&!Array.isArray(e)?t(e):n({message:`Expected object response`,code:`VALIDATION_ERROR`})}},s={transform(e){return _(e,h)}},c={transform(e){return _(e,g)}},l={transform(e){return v(e)}};function u(e){return{transform(t){return Array.isArray(t)?t.map(t=>y(t,e)):y(t,e)}}}function d(e){return{transform(t){return{[e]:t}}}}var f=class{maxAttempts;constructor(e=5){this.maxAttempts=e}shouldRetry(e){return e<this.maxAttempts}delayMs(e){return e*50}},p=class{retryableStatuses=[408,429,500,502,503,504];maxAttempts;constructor(e=3){this.maxAttempts=e}shouldRetry(e,t){return e>=this.maxAttempts?!1:this.retryableStatuses.includes(t.status??0)||t.code===`TIMEOUT`}delayMs(e){return Math.min(1e3*2**(e-1),1e4)}},m=class{failureCount=0;lastFailureTime=0;maxAttempts;failureThreshold;resetTimeMs;constructor(e=3,t=5,n=6e4){this.maxAttempts=e,this.failureThreshold=t,this.resetTimeMs=n}shouldRetry(e){return e>=this.maxAttempts||(Date.now()-this.lastFailureTime>this.resetTimeMs&&(this.failureCount=0),this.failureCount>=this.failureThreshold)?!1:(this.failureCount++,this.lastFailureTime=Date.now(),!0)}delayMs(e){return 100*2**(e-1)}reset(){this.failureCount=0,this.lastFailureTime=0}};function h(e){return e.replace(/_([a-z])/g,(e,t)=>t.toUpperCase())}function g(e){return e.replace(/[A-Z]/g,e=>`_${e.toLowerCase()}`)}function _(e,t){if(!e||typeof e!=`object`)return e;if(Array.isArray(e))return e.map(e=>_(e,t));let n={};for(let[r,i]of Object.entries(e))n[t(r)]=_(i,t);return n}function v(e,t=``){let n={};if(Array.isArray(e))e.forEach((e,r)=>{let i=t?`${t}[${r}]`:`[${r}]`;Object.assign(n,v(e,i))});else if(e&&typeof e==`object`)for(let[r,i]of Object.entries(e)){let e=t?`${t}.${r}`:r;i&&typeof i==`object`&&!Array.isArray(i)?Object.assign(n,v(i,e)):n[e]=i}return n}function y(e,t){if(!e||typeof e!=`object`)return{};let n=e,r={};for(let e of t)e in n&&(r[e]=n[e]);return r}function b(e){let t=new AbortController,n=setTimeout(()=>t.abort(),e);return t.signal.addEventListener(`abort`,()=>clearTimeout(n),{once:!0}),t}async function x(e,t=3){try{return await e()}catch(n){if(t<=0)throw n;return x(e,t-1)}}var S=class{cache=new Map;get(e){let t=this.cache.get(e);return t?Date.now()-t.timestamp>t.ttlMs?(this.cache.delete(e),null):t.data:null}set(e,t,n=6e4){this.cache.set(e,{data:t,timestamp:Date.now(),ttlMs:n})}clear(){this.cache.clear()}has(e){let t=this.cache.get(e);return t?Date.now()-t.timestamp>t.ttlMs?(this.cache.delete(e),!1):!0:!1}delete(e){this.cache.delete(e)}};function C(e={}){let t=e.cache||new S,n=e.ttlMs||6e4,r=e.cacheableStatuses||[200,304];return async(e,i)=>{let a=(e.request.method||`GET`).toUpperCase(),o=a===`GET`,s=`${a}:${e.request.url}`;if(o&&t.has(s)){let n=t.get(s);if(n){e.response=n,e.state.cacheHit=!0;return}}await i(),o&&e.response&&r.includes(e.response.status)&&(t.set(s,e.response,n),e.state.cacheMiss=!0)}}var ee=C(),w=class{pending=new Map;async execute(e,t){if(this.pending.has(e))return this.pending.get(e);let n=t().finally(()=>{this.pending.delete(e)});return this.pending.set(e,n),n}clear(){this.pending.clear()}};function T(e={}){let t=e.deduplicator||new w,n=e.includeBody??!1,r=e.methods||[`GET`];return async(e,i)=>{let a=(e.request.method||`GET`).toUpperCase();if(!r.includes(a)){await i();return}let o=`${a}:${e.request.url}`;n&&e.request.body&&(o+=`:${JSON.stringify(e.request.body)}`);try{e.response=await t.execute(o,async()=>(await i(),e.response)),e.state.deduped=!0}catch(t){throw e.error=t,t}}}var E=T();function D(e){return async t=>{let n=-1;async function r(i){if(i<=n)throw Error(`next() called multiple times`);n=i;let a=e[i];a&&await a(t,()=>r(i+1))}await r(0)}}var O=class{middlewares=[];use(e){return this.middlewares.push(e),this}async execute(e){if(e&&typeof e==`object`&&`request`in e&&`response`in e){let t=e;return await D(this.middlewares.map(e=>typeof e==`function`&&e.length===2?e:async(t,n)=>{let r=e;t.response.body=await r(t.response.body),await n()}))(t),t.response.body}let t=e;for(let e of this.middlewares)t=await e(t);return t}clear(){this.middlewares=[]}};function k(e,t,n,r={}){return{ok:e>=200&&e<300,data:t,error:n,status:e,headers:r}}function A(e){return async(t,n)=>{let r=e.path,i;switch(e.method){case`GET`:i=await t.get(r);break;case`POST`:i=await t.post(r,n);break;case`PUT`:i=await t.put(r,n);break;case`PATCH`:i=await t.patch(r,n);break;case`DELETE`:i=await t.delete(r);break;default:throw Error(`Unsupported method: ${e.method}`)}return i}}function j(e){return{request:async(t,n,r)=>{let i=e[n];return await A(i)(t,r)}}}var M=class e{subscribers=[];errorSubscribers=[];completeSubscribers=[];subscribe(e,t,n){return e&&this.subscribers.push(e),t&&this.errorSubscribers.push(t),n&&this.completeSubscribers.push(n),{unsubscribe:()=>{this.subscribers=this.subscribers.filter(t=>t!==e),this.errorSubscribers=this.errorSubscribers.filter(e=>e!==t),this.completeSubscribers=this.completeSubscribers.filter(e=>e!==n)}}}next(e){this.subscribers.forEach(t=>t(e))}error(e){this.errorSubscribers.forEach(t=>t(e))}complete(){this.completeSubscribers.forEach(e=>e())}map(t){let n=new e;return this.subscribe(e=>n.next(t(e)),e=>n.error(e),()=>n.complete()),n}filter(t){let n=new e;return this.subscribe(e=>{t(e)&&n.next(e)},e=>n.error(e),()=>n.complete()),n}};function N(e){return t=>{if(!e(t))throw TypeError(`Value does not match expected type`);return t}}function P(e){return e}function F(e){return P(e)}var I=class{_promise;resolveFunc;rejectFunc;constructor(){this._promise=new Promise((e,t)=>{this.resolveFunc=e,this.rejectFunc=t})}resolve(e){this.resolveFunc(e)}reject(e){this.rejectFunc(e)}get promise(){return this._promise}promise_(){return this._promise}};async function L(e,t={}){let n=e.body?.getReader();if(!n)throw Error(`Response body is not readable`);let r=[],i=0,a=parseInt(e.headers.get(`content-length`)||`0`,10);for(;;){let{done:e,value:o}=await n.read();if(e)break;r.push(o),i+=o.length,t.onChunk&&await t.onChunk(o),t.onProgress&&a>0&&t.onProgress(i,a)}return R(r,i)}function R(e,t){let n=new Uint8Array(t),r=0;for(let t of e)n.set(t,r),r+=t.byteLength;return n}async function z(e,t){let n=await L(e);if(typeof window>`u`)await(await import(`fs`).then(e=>e.promises)).writeFile(t,n);else{let e=new Blob([n.buffer],{type:`application/octet-stream`}),r=URL.createObjectURL(e),i=document.createElement(`a`);i.href=r,i.download=t,i.click(),URL.revokeObjectURL(r)}}function B(e={}){return async(t,n)=>{if(await n(),t.response&&t.response.body&&typeof t.response.body==`object`&&`getReader`in t.response.body){let n=t.response.body.getReader(),r=[],i=0,a=parseInt(t.response.headers?.[`content-length`]||`0`,10);try{for(;;){let{done:o,value:s}=await n.read();if(o)break;r.push(s),i+=s.length,e.onChunk&&await e.onChunk(s),e.onProgress&&a>0&&e.onProgress(i,a),t.state.streamedChunks=t.state.streamedChunks||[],t.state.streamedChunks.push(s)}let o=R(r,i);t.response.body=o,t.state.streaming=!0,t.state.streamedBytes=i}finally{n.releaseLock()}}}}var V=B({onProgress:(e,t)=>{if(t>0){let n=Math.round(e/t*100);console.log(`⬇️ Streaming: ${n}% (${e}/${t} bytes)`)}}}),H=class{plugins=[];cache=new S;deduplicator=new w;middlewares=[];listeners=new Map;register(e){return this.plugins.push(e),e.setup(this),this.emit(`plugin:registered`,e.name),this}addMiddleware(e){return this.middlewares.push(e),this}getCache(){return this.cache}getDeduplicator(){return this.deduplicator}getPipeline(){return D(this.middlewares)}async executePipeline(e){await this.getPipeline()(e)}on(e,t){return this.listeners.has(e)||this.listeners.set(e,new Set),this.listeners.get(e)?.add(t),this}emit(e,...t){let n=this.listeners.get(e);if(n)for(let e of n)e(...t)}off(e,t){return this.listeners.get(e)?.delete(t),this}getPlugins(){return[...this.plugins]}clear(){this.plugins=[],this.middlewares=[],this.listeners.clear(),this.cache.clear(),this.deduplicator.clear()}},U={name:`logger`,setup(e){e.on(`request:start`,(...e)=>{let t=e[0];console.log(`📤 Request started: ${t}`)}),e.on(`request:success`,(...e)=>{let t=e[0],n=e[1];console.log(`✅ Request succeeded: ${t} (${n})`)}),e.on(`request:error`,(...e)=>{let t=e[0],n=e[1];console.error(`❌ Request failed: ${t}`,n)})}},W=class{name=`metrics`;metrics={requests:0,errors:0,totalTime:0,avgTime:0};setup(e){e.on(`request:complete`,(...e)=>{let t=e[0],n=e[1];this.metrics.requests++,this.metrics.totalTime+=t,this.metrics.avgTime=this.metrics.totalTime/this.metrics.requests,n||this.metrics.errors++})}getMetrics(){return{...this.metrics}}},G=class{name=`cache`;ttlMs;constructor(e=6e4){this.ttlMs=e}setup(e){e.addMiddleware(C({ttlMs:this.ttlMs}))}},K=class{name=`dedupe`;setup(e){e.addMiddleware(T())}},q=class{store=new S;get(e){return this.store.get(e)}set(e,t,n=6e4){this.store.set(e,t,n)}has(e){return this.store.has(e)}clear(){this.store.clear()}},J=class{maxAttempts;baseDelayMs;constructor(e=3,t=100){this.maxAttempts=e,this.baseDelayMs=t}shouldRetry(e,t){let n=t.status!==void 0&&t.status>=500,r=t.code===`NETWORK_ERROR`;return e<this.maxAttempts&&(n||r||t.code===`TIMEOUT`)}delayMs(e){let t=this.baseDelayMs*2**(e-1),n=Math.random()*t*.1;return Math.min(t+n,3e4)}},Y=class{running=0;queue=[];maxConcurrent;constructor(e){this.maxConcurrent=e}async acquire(){if(this.running<this.maxConcurrent){this.running++;return}return new Promise(e=>{this.queue.push(()=>{this.running++,e()})})}release(){this.running--;let e=this.queue.shift();e&&e()}get pending(){return this.queue.length}get active(){return this.running}};function X(e){return e==null?{serialized:void 0,contentType:null}:typeof FormData<`u`&&e instanceof FormData?{serialized:e,contentType:null}:typeof URLSearchParams<`u`&&e instanceof URLSearchParams?{serialized:e,contentType:`application/x-www-form-urlencoded`}:typeof Blob<`u`&&e instanceof Blob?{serialized:e,contentType:e.type||`application/octet-stream`}:e instanceof ArrayBuffer||ArrayBuffer.isView(e)||typeof ReadableStream<`u`&&e instanceof ReadableStream?{serialized:e,contentType:`application/octet-stream`}:typeof e==`string`?{serialized:e,contentType:`text/plain`}:{serialized:JSON.stringify(e),contentType:`application/json`}}function Z(e,t){return t?e.replace(/:([a-zA-Z_][a-zA-Z0-9_]*)/g,(e,n)=>{let r=t[n];if(r===void 0)throw Error(`Missing path parameter: :${n}`);return encodeURIComponent(String(r))}):e}var Q=class e{requestInterceptors=[];responseInterceptors=[];cache;config;requestQueue;pendingRequests=new Map;constructor(e={}){this.config={baseURL:e.baseURL??``,defaultHeaders:e.defaultHeaders??{"Content-Type":`application/json`},defaultTimeout:e.defaultTimeout??3e4,validateStatus:e.validateStatus??(e=>e>=200&&e<300),cacheStrategy:e.cacheStrategy??new q,maxConcurrent:e.maxConcurrent??0,defaultResponseType:e.defaultResponseType??`auto`,defaultHooks:e.defaultHooks??{}},this.cache=this.config.cacheStrategy,this.requestQueue=this.config.maxConcurrent>0?new Y(this.config.maxConcurrent):null}async request(e){let r={...this.config.defaultHooks,...e.hooks},i=this.getMaxAttempts(e.retry),a=this.getRetryStrategy(e.retry),o=Symbol(`request`);r.onStart?.(this.buildRequest(e)),this.requestQueue&&await this.requestQueue.acquire();try{for(let s=1;s<=i;s++)try{if((e.method===`GET`||!e.method)&&e.cache?.enabled){let n=this.getCacheKey(e),i=this.cache.get(n);if(i){let e=i;return r.onSuccess?.(e),r.onFinally?.(),t(e)}}let n=this.buildRequest(e);for(let e of this.requestInterceptors)n=await e.onRequest(n);let i=new AbortController;this.pendingRequests.set(o,i),e.signal&&e.signal.addEventListener(`abort`,()=>i.abort(),{once:!0});let a=performance.now(),s=await this.fetchWithTimeout(n,e.timeout??this.config.defaultTimeout,i),c=performance.now()-a,l=s;e.onDownloadProgress&&s.body&&(l=this.trackDownloadProgress(s,e.onDownloadProgress));let u=e.responseType??this.config.defaultResponseType,d=await this.parseResponse(l,n,c,u);if(!this.config.validateStatus(d.status))throw{message:`Request failed with status ${d.status}`,status:d.status,statusText:d.statusText,code:`HTTP_ERROR`};if(e.validate){let t=e.validate.validate(d.data);if(!t.ok)return t}e.transform&&(d.data=e.transform.transform(d.data));let f=d;for(let e of this.responseInterceptors)f=await e.onResponse(f);if((e.method===`GET`||!e.method)&&e.cache?.enabled){let t=this.getCacheKey(e);this.cache.set(t,f,e.cache.ttlMs)}return r.onSuccess?.(f),this.pendingRequests.delete(o),t(f)}catch(e){let t=this.isHttpErrorDetails(e)?e:this.normalizeError(e);if(s<i&&a.shouldRetry(s,t)){r.onRetry?.(s,t);let e=a.delayMs(s);await this.delay(e);continue}let c=t;for(let e of this.responseInterceptors)e.onError&&(c=await e.onError(c));return r.onError?.(c),this.pendingRequests.delete(o),n(c)}let s={message:`Max retries exceeded`,code:`MAX_RETRIES`};return r.onError?.(s),n(s)}finally{r.onFinally?.(),this.requestQueue&&this.requestQueue.release()}}get(e,t){return this.request({...t,url:e,method:`GET`})}post(e,t,n){return this.request({...n,url:e,method:`POST`,body:t})}put(e,t,n){return this.request({...n,url:e,method:`PUT`,body:t})}patch(e,t,n){return this.request({...n,url:e,method:`PATCH`,body:t})}delete(e,t){return this.request({...t,url:e,method:`DELETE`})}head(e,t){return this.request({...t,url:e,method:`HEAD`})}options(e,t){return this.request({...t,url:e,method:`OPTIONS`})}addRequestInterceptor(e){return this.requestInterceptors.push(e),()=>{let t=this.requestInterceptors.indexOf(e);t!==-1&&this.requestInterceptors.splice(t,1)}}addResponseInterceptor(e){return this.responseInterceptors.push(e),()=>{let t=this.responseInterceptors.indexOf(e);t!==-1&&this.responseInterceptors.splice(t,1)}}clearInterceptors(){this.requestInterceptors=[],this.responseInterceptors=[]}clearCache(){this.cache.clear()}cancelAll(){for(let e of this.pendingRequests.values())e.abort();this.pendingRequests.clear()}get activeRequests(){return this.pendingRequests.size}get queueStats(){return{active:this.requestQueue?.active??this.pendingRequests.size,pending:this.requestQueue?.pending??0}}extend(t={}){let n=new e({baseURL:t.baseURL??this.config.baseURL,defaultHeaders:{...this.config.defaultHeaders,...t.defaultHeaders},defaultTimeout:t.defaultTimeout??this.config.defaultTimeout,validateStatus:t.validateStatus??this.config.validateStatus,cacheStrategy:t.cacheStrategy??this.cache,maxConcurrent:t.maxConcurrent??this.config.maxConcurrent,defaultResponseType:t.defaultResponseType??this.config.defaultResponseType,defaultHooks:{...this.config.defaultHooks,...t.defaultHooks}});for(let e of this.requestInterceptors)n.addRequestInterceptor(e);for(let e of this.responseInterceptors)n.addResponseInterceptor(e);return n}async*paginate(e,t,n={}){let r={...n};for(;;){let n=await this.get(e,r);if(!n.ok)break;yield t.getItems(n.value.data);let i=t.getNextPage(n.value.data,r);if(!i)break;r=i}}async poll(e,t,r={}){let i=t.maxAttempts??0;for(let n=1;i===0||n<=i;n++){let a=await this.get(e,r);if(!a.ok||(t.onPoll?.(a.value.data,n),t.until(a.value.data)))return a;if(i>0&&n>=i)break;await this.delay(t.intervalMs)}return n({message:`Polling exhausted after ${i} attempts`,code:`POLL_EXHAUSTED`})}buildRequest(e){let t=Z(e.url,e.params);return{url:this.buildUrl(t,e.query),method:e.method??`GET`,headers:{...this.config.defaultHeaders,...e.headers},body:e.body,params:e.params}}buildUrl(e,t){let n=this.config.baseURL+e;if(t&&Object.keys(t).length>0){let e=new URLSearchParams;Object.entries(t).forEach(([t,n])=>{e.append(t,String(n))}),n+=`?${e.toString()}`}return n}getCacheKey(e){let t=Z(e.url,e.params),n=e.query?JSON.stringify(e.query):``;return`${e.method??`GET`}:${t}${n?`:`+n:``}`}fetchWithTimeout(e,t,n){let{serialized:r,contentType:i}=X(e.body),a={...e.headers};return i?a[`Content-Type`]=i:i===null&&r instanceof FormData&&delete a[`Content-Type`],new Promise((i,o)=>{let s=setTimeout(()=>{n.abort();let e=Error(`Request timed out`);e.name=`TimeoutError`,o(e)},t);fetch(e.url,{method:e.method,headers:a,body:r,signal:n.signal}).then(e=>{clearTimeout(s),i(e)},e=>{clearTimeout(s),o(e)})})}trackDownloadProgress(e,t){let n=parseInt(e.headers.get(`content-length`)||`0`,10),r=e.body?.getReader();if(!r)return e;let i=0,a=new ReadableStream({async pull(e){let{done:a,value:o}=await r.read();if(a){e.close();return}i+=o.byteLength,t({loaded:i,total:n,percent:n>0?Math.round(i/n*100):0}),e.enqueue(o)}});return new Response(a,{headers:e.headers,status:e.status,statusText:e.statusText})}async parseResponse(e,t,n,r){let i=await this.parseBody(e,r);return{status:e.status,statusText:e.statusText,headers:e.headers,data:i,request:t,duration:n}}async parseBody(e,t){switch(t){case`json`:return await e.json();case`text`:return await e.text();case`blob`:return await e.blob();case`arrayBuffer`:return await e.arrayBuffer();case`formData`:return await e.formData();case`stream`:return e.body;default:{let t=e.headers.get(`content-type`)??``;if(t.includes(`application/json`))return await e.json();if(t.includes(`text/`))return await e.text();if(t.includes(`multipart/form-data`))return await e.formData();if(t.includes(`application/octet-stream`)||t.includes(`image/`)||t.includes(`audio/`)||t.includes(`video/`))return await e.blob();try{return await e.json()}catch{return await e.text()}}}}normalizeError(e){return e instanceof Error&&e.name===`TimeoutError`?{message:`Request timed out`,code:`TIMEOUT`}:e instanceof DOMException&&e.name===`AbortError`?{message:`Request aborted`,code:`ABORTED`}:e instanceof Error?e.name===`AbortError`||e.message.includes(`abort`)?{message:`Request aborted`,code:`ABORTED`}:{message:e.message,code:e.name===`TypeError`?`NETWORK_ERROR`:`UNKNOWN_ERROR`,originalError:e}:{message:String(e),code:`UNKNOWN_ERROR`,originalError:e}}isHttpErrorDetails(e){return typeof e==`object`&&!!e&&`message`in e&&`code`in e}getMaxAttempts(e){return e?`maxAttempts`in e?e.maxAttempts:100:1}getRetryStrategy(e){return e?`shouldRetry`in e?e:new J(e.maxAttempts,e.backoffMs):{shouldRetry:()=>!1,delayMs:()=>0}}delay(e){return new Promise(t=>setTimeout(t,e))}},$=class extends Error{status;code;response;constructor(e,t,n,r){super(e),this.name=`HttpError`,this.status=t,this.code=n,this.response=r}};function te(e){return e instanceof $}function ne(e){return new Q(e)}e.AggressiveRetry=f,e.CachePlugin=G,e.CacheStore=S,e.CircuitBreakerRetry=m,e.ConservativeRetry=p,e.DedupePlugin=K,e.Defer=I,e.Err=n,e.HttpClient=Q,e.HttpError=$,e.LoggerPlugin=U,e.MetricsPlugin=W,e.MiddlewarePipeline=O,e.Ok=t,e.PluginManager=H,e.RequestDeduplicator=w,e.TypedObservable=M,e.cacheMiddleware=ee,e.createApiUrl=F,e.createCacheMiddleware=C,e.createDedupeMiddleware=T,e.createHttpClient=ne,e.createPipeline=D,e.createProjectionTransformer=u,e.createRequiredFieldsValidator=i,e.createSchemaValidator=r,e.createStreamingMiddleware=B,e.createTypeGuard=N,e.createTypedApiClient=j,e.createTypedRequest=A,e.createTypedResponse=k,e.createUrl=P,e.createWrapperTransformer=d,e.dedupeMiddleware=E,e.handleStream=L,e.isHttpError=te,e.retry=x,e.streamToFile=z,e.streamingMiddleware=V,e.transformCamelToSnake=c,e.transformFlatten=l,e.transformSnakeToCamel=s,e.validatorIsArray=a,e.validatorIsObject=o,e.withTimeout=b});
1
+ (function(e,t){typeof exports==`object`&&typeof module<`u`?t(exports):typeof define==`function`&&define.amd?define([`exports`],t):(e=typeof globalThis<`u`?globalThis:e||self,t(e.Nexa={}))})(this,function(e){Object.defineProperty(e,Symbol.toStringTag,{value:`Module`});var t=Object.defineProperty,n=(e,t)=>()=>(e&&(t=e(e=0)),t),r=(e,n)=>{let r={};for(var i in e)t(r,i,{get:e[i],enumerable:!0});return n||t(r,Symbol.toStringTag,{value:`Module`}),r},i=e=>({ok:!0,value:e}),a=e=>({ok:!1,error:e});function o(e){return{validate(t){let n=t;for(let[t,r]of Object.entries(e))if(!r(n[t]))return a({message:`Validation failed: field "${t}" is invalid`,code:`VALIDATION_ERROR`});return i(t)}}}function s(e){return{validate(t){let n=t,r=e.filter(e=>!(e in n));return r.length>0?a({message:`Validation failed: missing fields: ${r.join(`, `)}`,code:`VALIDATION_ERROR`}):i(t)}}}var c={validate(e){return Array.isArray(e)?i(e):a({message:`Expected array response`,code:`VALIDATION_ERROR`})}},l={validate(e){return e&&typeof e==`object`&&!Array.isArray(e)?i(e):a({message:`Expected object response`,code:`VALIDATION_ERROR`})}},u={transform(e){return v(e,ee)}},d={transform(e){return v(e,te)}},f={transform(e){return y(e)}};function p(e){return{transform(t){return Array.isArray(t)?t.map(t=>b(t,e)):b(t,e)}}}function m(e){return{transform(t){return{[e]:t}}}}var h=class{maxAttempts;constructor(e=5){this.maxAttempts=e}shouldRetry(e){return e<this.maxAttempts}delayMs(e){return e*50}},g=class{retryableStatuses=[408,429,500,502,503,504];maxAttempts;constructor(e=3){this.maxAttempts=e}shouldRetry(e,t){return e>=this.maxAttempts?!1:this.retryableStatuses.includes(t.status??0)||t.code===`TIMEOUT`}delayMs(e){return Math.min(1e3*2**(e-1),1e4)}},_=class{failureCount=0;lastFailureTime=0;maxAttempts;failureThreshold;resetTimeMs;constructor(e=3,t=5,n=6e4){this.maxAttempts=e,this.failureThreshold=t,this.resetTimeMs=n}shouldRetry(e){return e>=this.maxAttempts||(Date.now()-this.lastFailureTime>this.resetTimeMs&&(this.failureCount=0),this.failureCount>=this.failureThreshold)?!1:(this.failureCount++,this.lastFailureTime=Date.now(),!0)}delayMs(e){return 100*2**(e-1)}reset(){this.failureCount=0,this.lastFailureTime=0}};function ee(e){return e.replace(/_([a-z])/g,(e,t)=>t.toUpperCase())}function te(e){return e.replace(/[A-Z]/g,e=>`_${e.toLowerCase()}`)}function v(e,t){if(!e||typeof e!=`object`)return e;if(Array.isArray(e))return e.map(e=>v(e,t));let n={};for(let[r,i]of Object.entries(e))n[t(r)]=v(i,t);return n}function y(e,t=``){let n={};if(Array.isArray(e))e.forEach((e,r)=>{let i=t?`${t}[${r}]`:`[${r}]`;Object.assign(n,y(e,i))});else if(e&&typeof e==`object`)for(let[r,i]of Object.entries(e)){let e=t?`${t}.${r}`:r;i&&typeof i==`object`&&!Array.isArray(i)?Object.assign(n,y(i,e)):n[e]=i}return n}function b(e,t){if(!e||typeof e!=`object`)return{};let n=e,r={};for(let e of t)e in n&&(r[e]=n[e]);return r}function ne(e){let t=new AbortController,n=setTimeout(()=>t.abort(),e);return t.signal.addEventListener(`abort`,()=>clearTimeout(n),{once:!0}),t}async function re(e,t=3){try{return await e()}catch(n){if(t<=0)throw n;return re(e,t-1)}}var x=class{cache=new Map;get(e){let t=this.cache.get(e);return t?Date.now()-t.timestamp>t.ttlMs?(this.cache.delete(e),null):t.data:null}set(e,t,n=6e4){this.cache.set(e,{data:t,timestamp:Date.now(),ttlMs:n})}clear(){this.cache.clear()}has(e){let t=this.cache.get(e);return t?Date.now()-t.timestamp>t.ttlMs?(this.cache.delete(e),!1):!0:!1}delete(e){this.cache.delete(e)}};function S(e={}){let t=e.cache||new x,n=e.ttlMs||6e4,r=e.cacheableStatuses||[200,304];return async(e,i)=>{let a=(e.request.method||`GET`).toUpperCase(),o=a===`GET`,s=`${a}:${e.request.url}`;if(o&&t.has(s)){let n=t.get(s);if(n){e.response=n,e.state.cacheHit=!0;return}}await i(),o&&e.response&&r.includes(e.response.status)&&(t.set(s,e.response,n),e.state.cacheMiss=!0)}}var ie=S(),C=class{pending=new Map;async execute(e,t){if(this.pending.has(e))return this.pending.get(e);let n=t().finally(()=>{this.pending.delete(e)});return this.pending.set(e,n),n}clear(){this.pending.clear()}};function w(e={}){let t=e.deduplicator||new C,n=e.includeBody??!1,r=e.methods||[`GET`];return async(e,i)=>{let a=(e.request.method||`GET`).toUpperCase();if(!r.includes(a)){await i();return}let o=`${a}:${e.request.url}`;n&&e.request.body&&(o+=`:${JSON.stringify(e.request.body)}`);try{e.response=await t.execute(o,async()=>(await i(),e.response)),e.state.deduped=!0}catch(t){throw e.error=t,t}}}var ae=w();function T(e={maxRequests:100,windowMs:6e4}){let t=e.windowMs??6e4,n=e.maxRequests,r=e.keyGenerator??(e=>`${e.request.method}:${e.request.url}`),i=new Map;return async(a,o)=>{let s=r(a),c=Date.now();if(i.size>1e3)for(let[e,t]of i.entries())c>t.resetTime&&i.delete(e);let l=i.get(s);if((!l||c>l.resetTime)&&(l={count:0,resetTime:c+t},i.set(s,l)),l.count>=n){a.response={status:e.errorResponse?.status??429,headers:{"Content-Type":`application/json`},body:e.errorResponse?.body??{error:`Too Many Requests`,message:`Rate limit exceeded`}},a.state.rateLimited=!0;return}l.count++,a.state.rateLimit={limit:n,remaining:n-l.count,reset:l.resetTime},await o()}}var oe=T();function E(e={}){let t=e.failureThreshold??5,n=e.resetTimeout??3e4,r=e.isFailure??(e=>e.response.status>=500),i=e.keyGenerator??(e=>`${e.request.method}:${e.request.url}`),a=new Map;return async(e,o)=>{let s=i(e),c=a.get(s);if(c||(c={state:`closed`,failures:0,lastFailure:0,successCount:0},a.set(s,c)),c.state===`open`)if(Date.now()-c.lastFailure>n)c.state=`half-open`,c.successCount=0;else{e.response={status:503,headers:{"Content-Type":`application/json`},body:{error:`Service Unavailable`,message:`Circuit breaker is open`}},e.state.circuitOpen=!0;return}try{await o(),r(e)?(c.failures++,c.lastFailure=Date.now(),(c.failures>=t||c.state===`half-open`)&&(c.state=`open`)):(c.failures=0,c.state===`half-open`&&(c.successCount++,c.successCount>=3&&(c.state=`closed`,c.successCount=0)))}catch(e){throw c.failures++,c.lastFailure=Date.now(),(c.failures>=t||c.state===`half-open`)&&(c.state=`open`),e}}}var se=E();function D(e){return async t=>{let n=-1;async function r(i){if(i<=n)throw Error(`next() called multiple times`);n=i;let a=e[i];a&&await a(t,()=>r(i+1))}await r(0)}}var ce=class{middlewares=[];use(e){return this.middlewares.push(e),this}async execute(e){if(e&&typeof e==`object`&&`request`in e&&`response`in e){let t=e;return await D(this.middlewares.map(e=>typeof e==`function`&&e.length===2?e:async(t,n)=>{let r=e;t.response.body=await r(t.response.body),await n()}))(t),t.response.body}let t=e;for(let e of this.middlewares)t=await e(t);return t}clear(){this.middlewares=[]}};function le(e,t,n,r={}){return{ok:e>=200&&e<300,data:t,error:n,status:e,headers:r}}function O(e){return async(t,n)=>{let r=e.path,i;switch(e.method){case`GET`:i=await t.get(r);break;case`POST`:i=await t.post(r,n);break;case`PUT`:i=await t.put(r,n);break;case`PATCH`:i=await t.patch(r,n);break;case`DELETE`:i=await t.delete(r);break;default:throw Error(`Unsupported method: ${e.method}`)}return i}}function ue(e){return{request:async(t,n,r)=>{let i=e[n];return await O(i)(t,r)}}}var de=class e{subscribers=[];errorSubscribers=[];completeSubscribers=[];subscribe(e,t,n){return e&&this.subscribers.push(e),t&&this.errorSubscribers.push(t),n&&this.completeSubscribers.push(n),{unsubscribe:()=>{this.subscribers=this.subscribers.filter(t=>t!==e),this.errorSubscribers=this.errorSubscribers.filter(e=>e!==t),this.completeSubscribers=this.completeSubscribers.filter(e=>e!==n)}}}next(e){this.subscribers.forEach(t=>t(e))}error(e){this.errorSubscribers.forEach(t=>t(e))}complete(){this.completeSubscribers.forEach(e=>e())}map(t){let n=new e;return this.subscribe(e=>n.next(t(e)),e=>n.error(e),()=>n.complete()),n}filter(t){let n=new e;return this.subscribe(e=>{t(e)&&n.next(e)},e=>n.error(e),()=>n.complete()),n}};function fe(e){return t=>{if(!e(t))throw TypeError(`Value does not match expected type`);return t}}function k(e){return e}function pe(e){return k(e)}var me=class{_promise;resolveFunc;rejectFunc;constructor(){this._promise=new Promise((e,t)=>{this.resolveFunc=e,this.rejectFunc=t})}resolve(e){this.resolveFunc(e)}reject(e){this.rejectFunc(e)}get promise(){return this._promise}promise_(){return this._promise}};async function A(e,t={}){let n=e.body?.getReader();if(!n)throw Error(`Response body is not readable`);let r=[],i=0,a=parseInt(e.headers.get(`content-length`)||`0`,10);for(;;){let{done:e,value:o}=await n.read();if(e)break;r.push(o),i+=o.length,t.onChunk&&await t.onChunk(o),t.onProgress&&a>0&&t.onProgress(i,a)}return j(r,i)}function j(e,t){let n=new Uint8Array(t),r=0;for(let t of e)n.set(t,r),r+=t.byteLength;return n}async function he(e,t){let n=await A(e);if(typeof window>`u`)await(await import(`fs`).then(e=>e.promises)).writeFile(t,n);else{let e=new Blob([n.buffer],{type:`application/octet-stream`}),r=URL.createObjectURL(e),i=document.createElement(`a`);i.href=r,i.download=t,i.click(),URL.revokeObjectURL(r)}}function M(e={}){return async(t,n)=>{if(await n(),t.response&&t.response.body&&typeof t.response.body==`object`&&`getReader`in t.response.body){let n=t.response.body.getReader(),r=[],i=0,a=parseInt(t.response.headers?.[`content-length`]||`0`,10);try{for(;;){let{done:o,value:s}=await n.read();if(o)break;r.push(s),i+=s.length,e.onChunk&&await e.onChunk(s),e.onProgress&&a>0&&e.onProgress(i,a),t.state.streamedChunks=t.state.streamedChunks||[],t.state.streamedChunks.push(s)}let o=j(r,i);t.response.body=o,t.state.streaming=!0,t.state.streamedBytes=i}finally{n.releaseLock()}}}}var ge=M({onProgress:(e,t)=>{if(t>0){let n=Math.round(e/t*100);console.log(`⬇️ Streaming: ${n}% (${e}/${t} bytes)`)}}}),N=class{plugins=[];cache=new x;deduplicator=new C;middlewares=[];listeners=new Map;register(e){return this.plugins.push(e),e.setup(this),this.emit(`plugin:registered`,e.name),this}addMiddleware(e){return this.middlewares.push(e),this}getCache(){return this.cache}getDeduplicator(){return this.deduplicator}getPipeline(){return D(this.middlewares)}async executePipeline(e){await this.getPipeline()(e)}on(e,t){return this.listeners.has(e)||this.listeners.set(e,new Set),this.listeners.get(e)?.add(t),this}emit(e,...t){let n=this.listeners.get(e);if(n)for(let e of n)e(...t)}off(e,t){return this.listeners.get(e)?.delete(t),this}getPlugins(){return[...this.plugins]}clear(){this.plugins=[],this.middlewares=[],this.listeners.clear(),this.cache.clear(),this.deduplicator.clear()}},_e={name:`logger`,setup(e){e.on(`request:start`,(...e)=>{let t=e[0];console.log(`📤 Request started: ${t}`)}),e.on(`request:success`,(...e)=>{let t=e[0],n=e[1];console.log(`✅ Request succeeded: ${t} (${n})`)}),e.on(`request:error`,(...e)=>{let t=e[0],n=e[1];console.error(`❌ Request failed: ${t}`,n)})}},ve=class{name=`metrics`;metrics={requests:0,errors:0,totalTime:0,avgTime:0};setup(e){e.on(`request:complete`,(...e)=>{let t=e[0],n=e[1];this.metrics.requests++,this.metrics.totalTime+=t,this.metrics.avgTime=this.metrics.totalTime/this.metrics.requests,n||this.metrics.errors++})}getMetrics(){return{...this.metrics}}},ye=class{name=`cache`;ttlMs;constructor(e=6e4){this.ttlMs=e}setup(e){e.addMiddleware(S({ttlMs:this.ttlMs}))}},be=class{name=`dedupe`;setup(e){e.addMiddleware(w())}},xe=class{name=`rate-limit`;options;constructor(e={}){this.options=e}setup(e){e.addMiddleware(T({maxRequests:this.options.maxRequests??100,windowMs:this.options.windowMs??6e4,keyGenerator:this.options.keyGenerator,errorResponse:this.options.errorResponse}))}},Se=class{name=`circuit-breaker`;options;constructor(e={}){this.options=e}setup(e){e.addMiddleware(E({failureThreshold:this.options.failureThreshold??5,resetTimeout:this.options.resetTimeout??3e4,isFailure:this.options.isFailure,keyGenerator:this.options.keyGenerator}))}},P=r({Http2SessionPool:()=>z,closeHttp2SessionPool:()=>ke,getHttp2SessionPoolStats:()=>Ae,nodeHttp2Adapter:()=>Oe,nodeHttpAdapter:()=>De});async function Ce(){return I||(I=await import(`http`),L=await import(`https`)),{http:I,https:L}}async function we(){return R||=await import(`http2`),R}function F(e,t){let n,r,i,a,o;if(typeof e==`string`)n=e,r=t?.method||`GET`,i=t?.headers||{},a=t?.body,o=t?.signal===null?void 0:t?.signal;else{n=e.url,r=t?.method||e.method||`GET`;let s=new Headers(e.headers),c=new Headers(t?.headers),l=new Headers(s);c.forEach((e,t)=>l.set(t,e)),i=Object.fromEntries(l.entries()),a=t?.body??e.body??void 0,o=t?.signal===null?e.signal:t?.signal}return{url:n,method:r,headers:i,body:a,signal:o}}function Te(e,t,n,r,i=!1){if(i)return null;let a=e.startsWith(`https:`)?n.Agent:t.Agent,o={keepAlive:r?.keepAlive??!0,maxSockets:r?.maxSockets??50,maxFreeSockets:r?.maxFreeSockets??10,timeout:r?.timeout??6e4};return r?.maxRequestsPerSocket!==void 0&&(o.maxRequestsPerSocket=r.maxRequestsPerSocket),new a(o)}async function Ee(e){let t=new Headers;for(let[n,r]of Object.entries(e.headers))Array.isArray(r)?r.forEach(e=>t.append(n,e)):r!==void 0&&t.set(n,String(r));let n=[];return e.on(`data`,e=>n.push(e)),new Promise(r=>{e.on(`end`,()=>{let i=Buffer.concat(n);r(new Response(i,{status:e.statusCode||200,statusText:e.statusMessage||`OK`,headers:t}))})})}async function De(e,t,n){let{http:r,https:i}=await Ce(),{url:a,method:o,headers:s,body:c,signal:l}=F(e,t),u=new URL(a),d=u.protocol===`https:`,f=d?i:r,p=Te(a,r,i,n,!1);return new Promise((e,t)=>{let r=f.request({hostname:u.hostname,port:u.port||(d?443:80),path:u.pathname+u.search,method:o,headers:s,agent:p});if(l){if(l.aborted){r.destroy(),t(Error(`Request aborted`));return}let e=()=>{r.destroy(),t(Error(`Request aborted`))};l.addEventListener(`abort`,e);let n=()=>l.removeEventListener(`abort`,e);r.on(`close`,n),r.on(`error`,n)}r.setTimeout(n?.timeout??6e4,()=>{r.destroy(),t(Error(`Request timed out`))}),r.on(`response`,async n=>{try{e(await Ee(n))}catch(e){t(e)}}),r.on(`error`,t),c&&(typeof c==`string`?r.write(c):c instanceof Uint8Array?r.write(Buffer.from(c)):Buffer.isBuffer(c)?r.write(c):typeof c==`object`&&r.write(JSON.stringify(c))),r.end()})}async function Oe(e,t,n){let{url:r,method:i,headers:a,body:o,signal:s}=F(e,t),c=new URL(r),l=c.origin;return new Promise(async(e,t)=>{let r,u,d=!1,f=()=>{d||(d=!0,B.releaseSession(l))},p=e=>{f(),t(e)},m=t=>{f(),e(t)};try{if(r=await B.getSession(l,n),s?.aborted){p(Error(`Request aborted`));return}if(u=r.request({":path":c.pathname+c.search,":method":i,...a}),n?.timeout&&u.setTimeout(n.timeout,()=>{u.close(),p(Error(`Request timed out`))}),s){let e=()=>{u.close(),p(Error(`Request aborted`))};s.addEventListener(`abort`,e);let t=()=>s.removeEventListener(`abort`,e);u.on(`close`,t),u.on(`error`,t)}let e=[];u.on(`data`,t=>e.push(Buffer.isBuffer(t)?t:Buffer.from(t))),u.on(`response`,t=>{let n=Number(t[`:status`])||200,r=new Headers;for(let[e,n]of Object.entries(t))e.startsWith(`:`)||(Array.isArray(n)?n.forEach(t=>r.append(e,t)):n!==void 0&&r.set(e,String(n)));u.on(`end`,()=>{let t=Buffer.concat(e);m(new Response(t,{status:n,statusText:`OK`,headers:r}))})}),u.on(`error`,e=>{p(e)}),o&&(typeof o==`string`||o instanceof Uint8Array||Buffer.isBuffer(o)?u.write(o):typeof o==`object`&&u.write(JSON.stringify(o))),u.end()}catch(e){p(e instanceof Error?e:Error(String(e)))}})}function ke(){B.closeAll()}function Ae(){let e=B.getStats();return{sessionCount:e.sessionCount,origins:e.origins}}var I,L,R,z,B,V=n((()=>{z=class{sessions=new Map;cleanupInterval=null;maxIdleTime=3e4;maxRequestsPerSession=1e3;constructor(){this.startCleanup()}startCleanup(){this.cleanupInterval||=setInterval(()=>this.cleanup(),1e4)}cleanup(){let e=Date.now();for(let t of this.sessions.values())!t.closing&&e-t.lastUsed>this.maxIdleTime&&this.closeSession(t,`idle timeout`),!t.closing&&t.requestCount>=this.maxRequestsPerSession&&this.closeSession(t,`max requests exceeded`)}closeSession(e,t){e.closing=!0,e.session.close(),this.sessions.delete(e.origin)}async getSession(e,t){let n=this.sessions.get(e);if(n&&!n.session.closed&&!n.session.destroyed)return n.lastUsed=Date.now(),n.requestCount++,n.session;let r=(await we()).connect(e,{settings:t?.http2Settings});return r.on(`error`,t=>{this.sessions.delete(e)}),r.on(`close`,()=>{this.sessions.delete(e)}),n={session:r,lastUsed:Date.now(),requestCount:1,origin:e,closing:!1},this.sessions.set(e,n),r}releaseSession(e){let t=this.sessions.get(e);t&&(t.lastUsed=Date.now())}getStats(){return{sessionCount:this.sessions.size,origins:Array.from(this.sessions.keys()),sessions:Array.from(this.sessions.values()).map(e=>({origin:e.origin,requestCount:e.requestCount,lastUsed:e.lastUsed,closing:e.closing,sessionAlive:!e.session.closed&&!e.session.destroyed}))}}closeAll(){for(let e of this.sessions.values())e.closing||e.session.close();this.sessions.clear(),this.cleanupInterval&&=(clearInterval(this.cleanupInterval),null)}},B=new z}));function je(){return typeof window>`u`&&typeof process<`u`&&process.versions?.node!==void 0}function Me(){let e=globalThis;return typeof window>`u`&&e.Deno!==void 0&&e.Deno.version?.deno!==void 0}function Ne(){let e=globalThis;return e.Bun!==void 0&&e.Bun.version!==void 0}function Pe(){let e=globalThis;return e.caches!==void 0&&e.WebSocketPair!==void 0}function Fe(e,t){if(!e||e===`fetch`)return fetch;if(e===`node`||e===`http2`){if(!je())throw Error(`Transport '${e}' is only available in Node.js environment`);return(n,r)=>e===`http2`?Promise.resolve().then(()=>(V(),P)).then(e=>e.nodeHttp2Adapter(n,r,t)):Promise.resolve().then(()=>(V(),P)).then(e=>e.nodeHttpAdapter(n,r,t))}switch(e){case`deno`:if(!Me())throw Error(`Transport 'deno' is only available in Deno environment`);return fetch;case`bun`:if(!Ne())throw Error(`Transport 'bun' is only available in Bun environment`);return fetch;case`cloudflare`:if(!Pe())throw Error(`Transport 'cloudflare' is only available in Cloudflare Workers environment`);return fetch;default:return fetch}}var Ie=class{store=new x;get(e){return this.store.get(e)}set(e,t,n=6e4){this.store.set(e,t,n)}has(e){return this.store.has(e)}clear(){this.store.clear()}},Le=class{maxAttempts;baseDelayMs;constructor(e=3,t=100){this.maxAttempts=e,this.baseDelayMs=t}shouldRetry(e,t){let n=t.status!==void 0&&t.status>=500,r=t.code===`NETWORK_ERROR`;return e<this.maxAttempts&&(n||r||t.code===`TIMEOUT`)}delayMs(e){let t=this.baseDelayMs*2**(e-1),n=Math.random()*t*.1;return Math.min(t+n,3e4)}},Re=class{maxAttempts;baseDelayMs;condition;constructor(e=3,t=100,n){this.maxAttempts=e,this.baseDelayMs=t,this.condition=n}shouldRetry(e,t){if(e>=this.maxAttempts)return!1;if(this.condition)return this.condition(t,e);let n=t.status!==void 0&&t.status>=500,r=t.code===`NETWORK_ERROR`;return n||r||t.code===`TIMEOUT`}delayMs(e){let t=this.baseDelayMs*2**(e-1),n=Math.random()*t*.1;return Math.min(t+n,3e4)}},ze=class{running=0;queue=[];maxConcurrent;constructor(e){this.maxConcurrent=e}async acquire(){if(this.running<this.maxConcurrent){this.running++;return}return new Promise(e=>{this.queue.push(()=>{this.running++,e()})})}release(){this.running--;let e=this.queue.shift();e&&e()}get pending(){return this.queue.length}get active(){return this.running}};function H(e){return e==null?!1:typeof Blob<`u`&&e instanceof Blob||typeof File<`u`&&e instanceof File?!0:Array.isArray(e)?e.some(e=>H(e)):typeof e==`object`?Object.values(e).some(e=>H(e)):!1}function U(e,t,n){let r=t||new FormData;for(let[t,i]of Object.entries(e)){let e=n?`${n}[${t}]`:t;i!=null&&(typeof Blob<`u`&&i instanceof Blob?r.append(e,i):Array.isArray(i)?i.some(e=>H(e))?i.forEach(t=>{H(t)?r.append(e,t):r.append(e,JSON.stringify(t))}):r.append(e,JSON.stringify(i)):typeof i==`object`&&!(i instanceof Blob)?U(i,r,e):r.append(e,String(i)))}return r}function Be(e,t){return!t||e==null||typeof FormData<`u`&&e instanceof FormData||typeof Blob<`u`&&e instanceof Blob||typeof URLSearchParams<`u`&&e instanceof URLSearchParams||e instanceof ArrayBuffer||ArrayBuffer.isView(e)||typeof ReadableStream<`u`&&e instanceof ReadableStream||typeof e==`string`?e:typeof e==`object`&&H(e)?U(e):e}function Ve(e,t=!0){let n=Be(e,t);return n==null?{serialized:void 0,contentType:null}:typeof FormData<`u`&&n instanceof FormData?{serialized:n,contentType:null}:typeof URLSearchParams<`u`&&n instanceof URLSearchParams?{serialized:n,contentType:`application/x-www-form-urlencoded`}:typeof Blob<`u`&&n instanceof Blob?{serialized:n,contentType:n.type||`application/octet-stream`}:n instanceof ArrayBuffer||ArrayBuffer.isView(n)||typeof ReadableStream<`u`&&n instanceof ReadableStream?{serialized:n,contentType:`application/octet-stream`}:typeof n==`string`?{serialized:n,contentType:`text/plain`}:{serialized:JSON.stringify(n),contentType:`application/json`}}function W(e,t){return t?e.replace(/:([a-zA-Z_][a-zA-Z0-9_]*)/g,(e,n)=>{let r=t[n];if(r===void 0)throw Error(`Missing path parameter: :${n}`);return encodeURIComponent(String(r))}):e}function G(e,t){if(e!==void 0)return e;if(t!==void 0)return t?`include`:`same-origin`}function He(e){return typeof e==`number`?{total:e}:typeof e==`object`&&e?{connection:e.connection,response:e.response,total:e.total}:{}}var K=class e{requestInterceptors=[];responseInterceptors=[];cache;config;requestQueue;pendingRequests=new Map;constructor(e={}){this.config={baseURL:e.baseURL??``,defaultHeaders:e.defaultHeaders??{"Content-Type":`application/json`},defaultTimeout:e.defaultTimeout??3e4,validateStatus:e.validateStatus??(e=>e>=200&&e<300),cacheStrategy:e.cacheStrategy??new Ie,maxConcurrent:e.maxConcurrent??0,defaultResponseType:e.defaultResponseType??`auto`,defaultHooks:e.defaultHooks??{},transformRequest:e.transformRequest,credentials:G(e.credentials,e.withCredentials),adapter:e.adapter,autoFormData:e.autoFormData??!0,debug:e.debug,logger:e.logger,transport:e.transport,nodeOptions:e.nodeOptions},this.cache=this.config.cacheStrategy,this.requestQueue=this.config.maxConcurrent>0?new ze(this.config.maxConcurrent):null}logDebug(e,t,n,r,i){let a=e??this.config.debug;if(!a||a===!0&&t===`verbose`)return;let o=`[Nexa HTTP] `+n,s=i??this.config.logger??console.log;r===void 0?s(o):s(o,r)}async request(e){let t={...this.config.defaultHooks,...e.hooks},n=e.debug??this.config.debug,r=e.logger??this.config.logger,o=this.getMaxAttempts(e.retry),s=this.getRetryStrategy(e.retry),c=Symbol(`request`);t.onStart?.(this.buildRequest(e)),this.requestQueue&&await this.requestQueue.acquire();try{for(let l=1;l<=o;l++){let u;try{if((e.method===`GET`||!e.method)&&e.cache?.enabled){let a=this.getCacheKey(e),o=this.cache.get(a);if(o){let a=o;return this.logDebug(n,`info`,`Cache hit for ${e.url}`,a,r),t.onSuccess?.(a),t.onFinally?.(),i(a)}}u=this.buildRequest(e),this.logDebug(n,`info`,`${u.method||`GET`} ${u.url}`,u,r);for(let e of this.requestInterceptors)u=await e.onRequest(u);this.logDebug(n,`verbose`,`Request after interceptors`,u,r),u=this.applyTransformRequestToRequest(u,e),this.logDebug(n,`verbose`,`Request after transformRequest`,u,r);let a=He(e.timeout??this.config.defaultTimeout),s=new AbortController;this.pendingRequests.set(c,s),e.signal&&e.signal.addEventListener(`abort`,()=>s.abort(),{once:!0}),this.logDebug(n,`info`,`Fetching (attempt ${l}/${o})`,{url:u.url,method:u.method},r);let d=performance.now(),f=await this.fetchWithTimeout(u,a,s),p=performance.now()-d;this.logDebug(n,`info`,`Response ${f.status} ${f.statusText}`,{duration:p,status:f.status,attempt:l},r);let m=f;e.onDownloadProgress&&f.body&&(m=this.trackDownloadProgress(f,e.onDownloadProgress));let h=e.responseType??this.config.defaultResponseType,g=await this.parseResponse(m,u,p,h,a.response);if(!this.config.validateStatus(g.status))throw{message:`Request failed with status ${g.status}`,status:g.status,statusText:g.statusText,code:`HTTP_ERROR`,request:u,response:g,config:e};if(e.validate){let t=e.validate.validate(g.data);if(!t.ok)return t}e.transform&&(g.data=e.transform.transform(g.data));let _=g;for(let e of this.responseInterceptors)_=await e.onResponse(_);if((e.method===`GET`||!e.method)&&e.cache?.enabled){let t=this.getCacheKey(e);this.cache.set(t,_,e.cache.ttlMs)}return t.onSuccess?.(_),this.logDebug(n,`verbose`,`Response data`,_.data,r),this.pendingRequests.delete(c),i(_)}catch(i){let d=this.normalizeError(i,u,e);if(this.logDebug(n,`info`,`Error: ${d.message}`,{error:d,attempt:l},r),l<o&&s.shouldRetry(l,d)){t.onRetry?.(l,d);let e=s.delayMs(l);this.logDebug(n,`info`,`Retrying after ${e}ms (attempt ${l+1}/${o})`,{error:d},r),await this.delay(e);continue}let f=d;for(let e of this.responseInterceptors)e.onError&&(f=await e.onError(f));return t.onError?.(f),this.pendingRequests.delete(c),a(f)}}let l={message:`Max retries exceeded`,code:`MAX_RETRIES`};return this.logDebug(n,`info`,`Max retries exceeded`,l,r),t.onError?.(l),a(l)}finally{t.onFinally?.(),this.requestQueue&&this.requestQueue.release()}}get(e,t){return this.request({...t,url:e,method:`GET`})}post(e,t,n){return this.request({...n,url:e,method:`POST`,body:t})}put(e,t,n){return this.request({...n,url:e,method:`PUT`,body:t})}patch(e,t,n){return this.request({...n,url:e,method:`PATCH`,body:t})}delete(e,t){return this.request({...t,url:e,method:`DELETE`})}head(e,t){return this.request({...t,url:e,method:`HEAD`})}options(e,t){return this.request({...t,url:e,method:`OPTIONS`})}addRequestInterceptor(e){return this.requestInterceptors.push(e),()=>{let t=this.requestInterceptors.indexOf(e);t!==-1&&this.requestInterceptors.splice(t,1)}}addResponseInterceptor(e){return this.responseInterceptors.push(e),()=>{let t=this.responseInterceptors.indexOf(e);t!==-1&&this.responseInterceptors.splice(t,1)}}clearInterceptors(){this.requestInterceptors=[],this.responseInterceptors=[]}clearCache(){this.cache.clear()}cancelAll(){for(let e of this.pendingRequests.values())e.abort();this.pendingRequests.clear()}get activeRequests(){return this.pendingRequests.size}get queueStats(){return{active:this.requestQueue?.active??this.pendingRequests.size,pending:this.requestQueue?.pending??0}}extend(t={}){let n=new e({baseURL:t.baseURL??this.config.baseURL,defaultHeaders:{...this.config.defaultHeaders,...t.defaultHeaders},credentials:G(t.credentials,t.withCredentials)??this.config.credentials,defaultTimeout:t.defaultTimeout??this.config.defaultTimeout,validateStatus:t.validateStatus??this.config.validateStatus,cacheStrategy:t.cacheStrategy??this.cache,maxConcurrent:t.maxConcurrent??this.config.maxConcurrent,defaultResponseType:t.defaultResponseType??this.config.defaultResponseType,defaultHooks:{...this.config.defaultHooks,...t.defaultHooks},transformRequest:t.transformRequest??this.config.transformRequest,adapter:t.adapter??this.config.adapter,autoFormData:t.autoFormData??this.config.autoFormData});for(let e of this.requestInterceptors)n.addRequestInterceptor(e);for(let e of this.responseInterceptors)n.addResponseInterceptor(e);return n}async*paginate(e,t,n={}){let r={...n};for(;;){let n=await this.get(e,r);if(!n.ok)break;yield t.getItems(n.value.data);let i=t.getNextPage(n.value.data,r);if(!i)break;r=i}}async poll(e,t,n={}){let r=t.maxAttempts??0;for(let i=1;r===0||i<=r;i++){let a=await this.get(e,n);if(!a.ok||(t.onPoll?.(a.value.data,i),t.until(a.value.data)))return a;if(r>0&&i>=r)break;await this.delay(t.intervalMs)}return a({message:`Polling exhausted after ${r} attempts`,code:`POLL_EXHAUSTED`})}buildRequest(e){let t=W(e.url,e.params),n=this.buildUrl(t,e.query),r=G(e.credentials,e.withCredentials)??this.config.credentials,i=e.transport??this.config.transport,a=e.nodeOptions??this.config.nodeOptions;return{url:n,method:e.method??`GET`,headers:{...this.config.defaultHeaders,...e.headers},body:e.body,query:e.query,params:e.params,timeout:e.timeout,signal:e.signal,credentials:r,adapter:e.adapter,autoFormData:e.autoFormData??this.config.autoFormData,transport:i,nodeOptions:a}}buildUrl(e,t){let n=this.config.baseURL+e;if(t&&Object.keys(t).length>0){let e=new URLSearchParams;Object.entries(t).forEach(([t,n])=>{e.append(t,String(n))}),n+=`?${e.toString()}`}return n}applyTransformRequestToRequest(e,t){let n=this.config.transformRequest,r=t.transformRequest,i=[];if(n&&(Array.isArray(n)?i.push(...n):i.push(n)),r&&(Array.isArray(r)?i.push(...r):i.push(r)),i.length===0)return e;let a=e.body,o=e.headers??{};for(let e of i)a=e(a,o);return{...e,body:a,headers:o}}getCacheKey(e){let t=W(e.url,e.params),n=e.query?JSON.stringify(e.query):``;return`${e.method??`GET`}:${t}${n?`:`+n:``}`}fetchWithTimeout(e,t,n){let{serialized:r,contentType:i}=Ve(e.body,e.autoFormData),a={...e.headers};i?a[`Content-Type`]=i:i===null&&r instanceof FormData&&delete a[`Content-Type`];let o=t.total??t.connection;return new Promise((t,i)=>{let s=null;o!==void 0&&(s=setTimeout(()=>{n.abort();let e=Error(`Request timed out`);e.name=`TimeoutError`,i(e)},o));let c={method:e.method,headers:a,body:r,signal:n.signal};e.credentials!==void 0&&(c.credentials=e.credentials);let l=e.transport??this.config.transport??`fetch`,u=e.nodeOptions??this.config.nodeOptions,d=e.adapter??this.config.adapter;d||=Fe(l,u),d(e.url,c).then(e=>{s&&clearTimeout(s),t(e)},e=>{s&&clearTimeout(s),i(e)})})}trackDownloadProgress(e,t){let n=parseInt(e.headers.get(`content-length`)||`0`,10),r=e.body?.getReader();if(!r)return e;let i=0,a=new ReadableStream({async pull(e){let{done:a,value:o}=await r.read();if(a){e.close();return}i+=o.byteLength,t({loaded:i,total:n,percent:n>0?Math.round(i/n*100):0}),e.enqueue(o)}});return new Response(a,{headers:e.headers,status:e.status,statusText:e.statusText})}withTimeout(e,t,n=`Operation timed out`){return t<=0?e:new Promise((r,i)=>{let a=setTimeout(()=>{let e=Error(n);e.name=`TimeoutError`,i(e)},t);e.then(e=>{clearTimeout(a),r(e)},e=>{clearTimeout(a),i(e)})})}async parseResponse(e,t,n,r,i){let a=await this.parseBody(e,r,i);return{status:e.status,statusText:e.statusText,headers:e.headers,data:a,request:t,duration:n}}async parseBody(e,t,n){let r=async e=>n!==void 0&&n>0?this.withTimeout(e,n,`Response timeout`):e;switch(t){case`json`:return await r(e.json());case`text`:return await r(e.text());case`blob`:return await r(e.blob());case`arrayBuffer`:return await r(e.arrayBuffer());case`formData`:return await r(e.formData());case`stream`:return e.body;default:{let t=e.headers.get(`content-type`)??``;if(t.includes(`application/json`))return await r(e.json());if(t.includes(`text/`))return await r(e.text());if(t.includes(`multipart/form-data`))return await r(e.formData());if(t.includes(`application/octet-stream`)||t.includes(`image/`)||t.includes(`audio/`)||t.includes(`video/`))return await r(e.blob());try{return await r(e.json())}catch{return await r(e.text())}}}}normalizeError(e,t,n){let r=(r,i)=>({message:i,code:r,originalError:e,request:t,config:n});return e instanceof Error&&e.name===`TimeoutError`?e.message.includes(`Response timeout`)?r(`RESPONSE_TIMEOUT`,e.message):r(`TIMEOUT`,`Request timed out`):e instanceof DOMException&&e.name===`AbortError`?r(`ABORTED`,`Request aborted`):e instanceof Error?e.name===`AbortError`||e.message.includes(`abort`)?r(`ABORTED`,`Request aborted`):{message:e.message,code:e.name===`TypeError`?`NETWORK_ERROR`:`UNKNOWN_ERROR`,originalError:e,request:t,config:n}:this.isHttpErrorDetails(e)?{...e,request:e.request??t,config:e.config??n}:{message:String(e),code:`UNKNOWN_ERROR`,originalError:e,request:t,config:n}}isHttpErrorDetails(e){return typeof e==`object`&&!!e&&`message`in e&&`code`in e}getMaxAttempts(e){return e?`shouldRetry`in e?100:e.maxAttempts??3:1}getRetryStrategy(e){if(!e)return{shouldRetry:()=>!1,delayMs:()=>0};if(`shouldRetry`in e)return e;let t=e;return t.on?new Re(t.maxAttempts??3,t.backoffMs??100,t.on):new Le(t.maxAttempts??3,t.backoffMs??100)}delay(e){return new Promise(t=>setTimeout(t,e))}},q=class extends Error{status;code;response;constructor(e,t,n,r){super(e),this.name=`HttpError`,this.status=t,this.code=n,this.response=r}};function Ue(e){return e instanceof q}function We(e){return new K(e)}function J(){return typeof window>`u`&&typeof process<`u`&&process.versions?.node!==void 0}var Ge=class{url;options;pluginManager;status=`closed`;reconnectAttempt=0;reconnectTimer=null;heartbeatTimer=null;stats={messagesSent:0,messagesReceived:0,connectionTime:0,reconnectAttempts:0};connectionStartTime=0;listeners={open:new Set,close:new Set,error:new Set,message:new Set};constructor(e,t={}){this.url=e,this.options=t,this.pluginManager=new N}updateStatus(e){this.status=e,e===`open`?this.connectionStartTime=Date.now():e===`closed`&&this.connectionStartTime>0&&(this.stats.connectionTime+=Date.now()-this.connectionStartTime,this.connectionStartTime=0)}emitOpen(e){this.pluginManager.emit(`websocket:open`,this.url,e);for(let t of this.listeners.open)t(e)}emitClose(e){this.pluginManager.emit(`websocket:close`,this.url,e);for(let t of this.listeners.close)t(e)}emitError(e){let t;e instanceof Error?(t=new Event(`error`),t.error=e):t=e,this.pluginManager.emit(`websocket:error`,this.url,t);for(let e of this.listeners.error)e(t)}emitMessage(e){this.pluginManager.emit(`websocket:message`,this.url,e);for(let t of this.listeners.message)t(e)}onOpen(e){return this.listeners.open.add(e),()=>this.listeners.open.delete(e)}onClose(e){return this.listeners.close.add(e),()=>this.listeners.close.delete(e)}onError(e){return this.listeners.error.add(e),()=>this.listeners.error.delete(e)}onMessage(e){return this.listeners.message.add(e),()=>this.listeners.message.delete(e)}getStatus(){return this.status}getStats(){return{...this.stats,connectionTime:this.stats.connectionTime+(this.connectionStartTime>0?Date.now()-this.connectionStartTime:0)}}scheduleReconnect(){if(this.options.reconnect?.enabled===!1)return;let e=this.options.reconnect?.maxAttempts??1/0;if(this.reconnectAttempt>=e){this.pluginManager.emit(`websocket:reconnect:failed`,this.url,this.reconnectAttempt);return}let t=this.options.reconnect?.baseDelay??1e3,n=this.options.reconnect?.maxDelay??3e4,r=Math.min(n,t*2**this.reconnectAttempt);this.reconnectAttempt++,this.stats.reconnectAttempts=this.reconnectAttempt,this.options.reconnect?.onReconnecting?.(this.reconnectAttempt,r),this.pluginManager.emit(`websocket:reconnecting`,this.url,this.reconnectAttempt,r),this.reconnectTimer=setTimeout(()=>{this.connect().catch(e=>{this.emitError(e instanceof Error?e:Error(String(e)))})},r)}startHeartbeat(){if(!this.options.heartbeat)return;let e=this.options.heartbeat.interval??3e4,t=this.options.heartbeat.pingMessage??`ping`,n=this.options.heartbeat.pongMessage??`pong`,r=!0,i=()=>{if(!r){this.pluginManager.emit(`websocket:heartbeat:timeout`,this.url),this.disconnect();return}r=!1,this.send(t),this.heartbeatTimer=setTimeout(i,e)};this.onMessage(e=>{let t=e.raw;typeof t==`string`&&t===n&&(r=!0)}),this.heartbeatTimer=setTimeout(i,e)}stopHeartbeat(){this.heartbeatTimer&&=(clearTimeout(this.heartbeatTimer),null)}cleanup(){this.reconnectTimer&&=(clearTimeout(this.reconnectTimer),null),this.stopHeartbeat()}},Y=class extends Ge{socket=null;messageTypeListeners=new Map;constructor(e,t={}){super(e,t)}async connect(){if(!(this.status===`connecting`||this.status===`open`))return this.updateStatus(`connecting`),this.pluginManager.emit(`websocket:connect:start`,this.url),new Promise((e,t)=>{let n=this.options.timeout??1e4,r=setTimeout(()=>{this.updateStatus(`closed`),this.socket?.close(),this.socket=null;let e=Error(`WebSocket connection timeout after ${n}ms`);this.emitError(e),t(e)},n);try{this.socket=new WebSocket(this.url,this.options.protocols),this.socket.onopen=t=>{clearTimeout(r),this.updateStatus(`open`),this.reconnectAttempt=0,this.emitOpen(t),this.options.onOpen?.(t),this.startHeartbeat(),this.pluginManager.emit(`websocket:connect:success`,this.url),e()},this.socket.onclose=e=>{clearTimeout(r),this.updateStatus(`closed`),this.emitClose(e),this.options.onClose?.(e),this.stopHeartbeat(),this.pluginManager.emit(`websocket:disconnected`,this.url,e.code,e.reason),e.code!==1e3&&!e.wasClean&&this.scheduleReconnect()},this.socket.onerror=e=>{clearTimeout(r),this.updateStatus(`closed`),this.emitError(e),this.options.onError?.(e),this.pluginManager.emit(`websocket:connect:error`,this.url,e),t(e)},this.socket.onmessage=e=>{this.stats.messagesReceived++;let t={data:this.tryParseData(e.data),raw:e.data,type:`message`,timestamp:Date.now()};this.emitMessage(t),this.pluginManager.emit(`websocket:message:received`,this.url,t)}}catch(e){clearTimeout(r),this.updateStatus(`closed`),this.pluginManager.emit(`websocket:connect:error`,this.url,e),t(e)}})}disconnect(){this.cleanup(),this.socket&&(this.updateStatus(`closing`),this.socket.close(1e3,`Client disconnected`),this.socket=null,this.updateStatus(`closed`))}send(e){if(!this.socket||this.socket.readyState!==WebSocket.OPEN)throw Error(`WebSocket is not connected`);this.socket.send(e),this.stats.messagesSent++,this.pluginManager.emit(`websocket:message:sent`,this.url,e)}sendJson(e){this.send(JSON.stringify(e))}onMessageType(e,t){return this.messageTypeListeners.has(e)||this.messageTypeListeners.set(e,new Set),this.messageTypeListeners.get(e).add(t),()=>{let n=this.messageTypeListeners.get(e);n&&(n.delete(t),n.size===0&&this.messageTypeListeners.delete(e))}}tryParseData(e){if(typeof e==`string`)try{return JSON.parse(e)}catch{return e}return e}},Ke=class extends Y{async connect(){if(J())try{let{default:e}=await import(`ws`);return super.connect()}catch{throw Error(`WebSocket client for Node.js requires the "ws" package. Please install it: npm install ws`)}return super.connect()}};function qe(e,t={}){return J()?new Ke(e,t):new Y(e,t)}function X(){return typeof window>`u`&&typeof process<`u`&&process.versions?.node!==void 0}var Z=class{url;options;_source=null;pluginManager;status=`closed`;reconnectAttempt=0;reconnectTimer=null;stats={messagesSent:0,messagesReceived:0,connectionTime:0,reconnectAttempts:0};connectionStartTime=0;listeners={open:new Set,close:new Set,error:new Set,message:new Set,event:new Map};_lastEventId=null;constructor(e,t={}){this.url=e,this.options=t,this.pluginManager=new N}updateStatus(e){this.status=e,e===`open`?this.connectionStartTime=Date.now():e===`closed`&&this.connectionStartTime>0&&(this.stats.connectionTime+=Date.now()-this.connectionStartTime,this.connectionStartTime=0)}async connect(){if(!(this.status===`connecting`||this.status===`open`))return this.updateStatus(`connecting`),this.pluginManager.emit(`sse:connect:start`,this.url),new Promise((e,t)=>{let n=this.options.timeout??1e4,r=setTimeout(()=>{this.updateStatus(`closed`),this._source?.close(),this._source=null;let e=Error(`SSE connection timeout after ${n}ms`);this.emitError(e),t(e)},n);try{this._source=new EventSource(this.url),this._source.onopen=t=>{clearTimeout(r),this.updateStatus(`open`),this.reconnectAttempt=0,this.emitOpen(t),this.options.onOpen?.(t),this.pluginManager.emit(`sse:connect:success`,this.url),e()},this._source.onerror=e=>{clearTimeout(r),this.updateStatus(`closed`),this.emitError(e),this.options.onError?.(e),this.pluginManager.emit(`sse:connect:error`,this.url,e),this._source?.readyState===EventSource.CLOSED&&(this.emitClose(),this.options.onClose?.(),this.scheduleReconnect()),t(e)},this._source.onmessage=e=>{this.stats.messagesReceived++,this._lastEventId=e.lastEventId||this._lastEventId;let t={data:this.tryParseData(e.data),raw:e.data,type:e.type||`message`,timestamp:Date.now()};this.emitMessage(t),this.pluginManager.emit(`sse:message:received`,this.url,t);let n=e.type||`message`,r=this.listeners.event.get(n);if(r)for(let e of r)e(t.data)},this._source.addEventListener=this._source.addEventListener.bind(this._source)}catch(e){clearTimeout(r),this.updateStatus(`closed`),this.pluginManager.emit(`sse:connect:error`,this.url,e),t(e)}})}disconnect(){this.cleanup(),this._source&&(this.updateStatus(`closing`),this._source.close(),this._source=null,this.updateStatus(`closed`),this.emitClose(),this.options.onClose?.())}send(e){throw Error(`SSE is a receive-only protocol. Use HTTP requests to send data to server.`)}onMessage(e){return this.listeners.message.add(e),()=>this.listeners.message.delete(e)}onOpen(e){return this.listeners.open.add(e),()=>this.listeners.open.delete(e)}onClose(e){return this.listeners.close.add(e),()=>this.listeners.close.delete(e)}onError(e){return this.listeners.error.add(e),()=>this.listeners.error.delete(e)}onEvent(e,t){return this.listeners.event.has(e)||this.listeners.event.set(e,new Set),this.listeners.event.get(e).add(t),this._source&&!this._source[`on${e}`]&&this._source.addEventListener(e,n=>{t({data:this.tryParseData(n.data),raw:n.data,type:e,timestamp:Date.now()}.data)}),()=>{let n=this.listeners.event.get(e);n&&(n.delete(t),n.size===0&&this.listeners.event.delete(e))}}getStatus(){return this.status}getStats(){return{...this.stats,connectionTime:this.stats.connectionTime+(this.connectionStartTime>0?Date.now()-this.connectionStartTime:0)}}get lastEventId(){return this._lastEventId}get source(){return this._source}emitOpen(e){this.pluginManager.emit(`sse:open`,this.url,e);for(let t of this.listeners.open)t(e)}emitClose(){this.pluginManager.emit(`sse:close`,this.url);for(let e of this.listeners.close)e()}emitError(e){let t;e instanceof Error?(t=new Event(`error`),t.error=e):t=e,this.pluginManager.emit(`sse:error`,this.url,t);for(let e of this.listeners.error)e(t)}emitMessage(e){this.pluginManager.emit(`sse:message`,this.url,e);for(let t of this.listeners.message)t(e)}scheduleReconnect(){if(this.options.reconnect?.enabled===!1)return;let e=this.options.reconnect?.maxAttempts??1/0;if(this.reconnectAttempt>=e){this.pluginManager.emit(`sse:reconnect:failed`,this.url,this.reconnectAttempt);return}let t=this.options.reconnect?.baseDelay??1e3,n=this.options.reconnect?.maxDelay??3e4,r=Math.min(n,t*2**this.reconnectAttempt);this.reconnectAttempt++,this.stats.reconnectAttempts=this.reconnectAttempt,this.options.reconnect?.onReconnecting?.(this.reconnectAttempt,r),this.pluginManager.emit(`sse:reconnecting`,this.url,this.reconnectAttempt,r),this.reconnectTimer=setTimeout(()=>{this.connect().catch(e=>{this.emitError(e instanceof Error?e:Error(String(e)))})},r)}cleanup(){this.reconnectTimer&&=(clearTimeout(this.reconnectTimer),null)}tryParseData(e){try{return JSON.parse(e)}catch{return e}}},Je=class extends Z{async connect(){if(X())throw Error(`SSE client for Node.js requires a polyfill or custom implementation. Consider using a library like "eventsource" or implement using fetch with streaming.`);return super.connect()}};function Ye(e,t={}){return X()?new Je(e,t):new Z(e,t)}var Xe=class{name=`realtime`;setup(e){e.on(`websocket:connect:start`,(...t)=>{let n=t[0];e.emit(`realtime:connect:start`,`websocket`,n)}),e.on(`websocket:connect:success`,(...t)=>{let n=t[0];e.emit(`realtime:connect:success`,`websocket`,n)}),e.on(`websocket:message:received`,(...t)=>{let n=t[0],r=t[1];e.emit(`realtime:message`,`websocket`,n,r)}),e.on(`sse:connect:start`,(...t)=>{let n=t[0];e.emit(`realtime:connect:start`,`sse`,n)}),e.on(`sse:connect:success`,(...t)=>{let n=t[0];e.emit(`realtime:connect:success`,`sse`,n)}),e.on(`sse:message:received`,(...t)=>{let n=t[0],r=t[1];e.emit(`realtime:message`,`sse`,n,r)})}};function Ze(){return new Xe}var Q=class{adapter;method;urlPattern;constructor(e,t,n){this.adapter=e,this.method=t,this.urlPattern=n}reply(e,t,n){let r;return r=typeof e==`number`?{status:e,data:t,headers:n}:e,this.adapter.addRoute(this.method,this.urlPattern,r),this.adapter}replyOnce(e,t,n){let r;return r=typeof e==`number`?{status:e,data:t,headers:n}:e,this.adapter.addRoute(this.method,this.urlPattern,r,{times:1}),this.adapter}networkError(e=`Network Error`){return this.adapter.addRoute(this.method,this.urlPattern,{networkError:!0,errorMessage:e}),this.adapter}timeout(){return this.adapter.addRoute(this.method,this.urlPattern,{status:408,statusText:`Request Timeout`}),this.adapter}},$=class{routes=[];originalAdapter;mockClient;options;defaultResponse={status:200,statusText:`OK`,headers:{"content-type":`application/json`}};constructor(e,t={}){this.options=t;let n=this.createAdapter();if(typeof e.extend==`function`)this.mockClient=e.extend({adapter:n});else throw Error(`MockAdapter requires an HttpClient instance with extend() method`)}get client(){return this.mockClient}createAdapter(){return async(e,t)=>{let n=typeof e==`string`?e:e.url,r=t?.method||`GET`,i={url:n,method:r,headers:t?.headers,body:t?.body,signal:t?.signal??void 0},a=this.findMatchingRoute(r,n);if(!a)return this.options.passthrough?(this.originalAdapter||fetch)(e,t):new Response(JSON.stringify({error:`No mock route matched`}),{status:404,statusText:`Not Found`,headers:{"content-type":`application/json`}});a.timesCalled++;let o;if(o=typeof a.response==`function`?await a.response(i):a.response,o.networkError)throw TypeError(o.errorMessage||`Network Error`);let s=Object.fromEntries(Object.entries(o).filter(([e,t])=>t!==void 0)),c={...this.defaultResponse,...s};if(o.headers&&`content-type`in o.headers||(c.data instanceof Uint8Array||ArrayBuffer.isView(c.data)?c.headers={...c.headers,"content-type":`application/octet-stream`}:c.data&&typeof c.data==`object`&&(c.headers={...c.headers,"content-type":`application/json`})),c.delay||this.options.delay){let e=c.delay??this.options.delay;if(e&&e>0){let n=t?.signal;n?(n.throwIfAborted(),await new Promise((t,r)=>{let i=setTimeout(t,e),a=()=>{clearTimeout(i),r(new DOMException(`Aborted`,`AbortError`))};n.addEventListener(`abort`,a,{once:!0}),setTimeout(()=>{n.removeEventListener(`abort`,a)},e)})):await new Promise(t=>setTimeout(t,e))}}let l=new Headers(c.headers),u=c.data;(c.status===204||c.status===205)&&l.delete(`content-type`);let d;if(u==null)d=``;else if(typeof u==`string`)d=u;else if(u instanceof Uint8Array||ArrayBuffer.isView(u)){let e;if(u.buffer instanceof ArrayBuffer)e=u.buffer.slice(u.byteOffset,u.byteOffset+u.byteLength);else{let t=new Uint8Array(u.byteLength);t.set(new Uint8Array(u.buffer,u.byteOffset,u.byteLength)),e=t.buffer}return new Response(e,{status:c.status,statusText:c.statusText,headers:l})}else d=JSON.stringify(u);let f=d;return(c.status===204||c.status===205||f===``)&&(f=null),f===null&&l.delete(`content-type`),new Response(f,{status:c.status,statusText:c.statusText,headers:l})}}onGet(e){return new Q(this,`GET`,e)}onPost(e){return new Q(this,`POST`,e)}onPut(e){return new Q(this,`PUT`,e)}onPatch(e){return new Q(this,`PATCH`,e)}onDelete(e){return new Q(this,`DELETE`,e)}onAny(e){return new Q(this,`ANY`,e)}addRoute(e,t,n,r){return this.routes.push({method:e.toUpperCase(),urlPattern:t,response:n,timesCalled:0,times:r?.times}),this}reset(){this.routes=[]}restore(){}static reply(e,t,n){return{status:e,data:t,headers:n}}static networkError(e=`Network Error`){return{networkError:!0,errorMessage:e}}findMatchingRoute(e,t){let n=t;if(this.options.baseURL&&t.startsWith(this.options.baseURL))n=t.slice(this.options.baseURL.length);else try{if(n.includes(`://`)){let e=new URL(n);n=e.pathname+e.search}}catch{}for(let t of this.routes){if(t.times&&t.timesCalled>=t.times||t.method!==`ANY`&&t.method!==e.toUpperCase())continue;let r=!1;if(typeof t.urlPattern==`string`?r=n===t.urlPattern||n.startsWith(t.urlPattern):t.urlPattern instanceof RegExp&&(r=t.urlPattern.test(n)),r)return t}return null}};function Qe(e,t={}){return new $(e,t)}e.AggressiveRetry=h,e.CachePlugin=ye,e.CacheStore=x,e.CircuitBreakerPlugin=Se,e.CircuitBreakerRetry=_,e.ConservativeRetry=g,e.DedupePlugin=be,e.Defer=me,e.Err=a,e.HttpClient=K,e.HttpError=q,e.LoggerPlugin=_e,e.MetricsPlugin=ve,e.MiddlewarePipeline=ce,e.MockAdapter=$,e.Ok=i,e.PluginManager=N,e.RateLimitPlugin=xe,e.RequestDeduplicator=C,e.TypedObservable=de,e.cacheMiddleware=ie,e.circuitBreakerMiddleware=se,e.createApiUrl=pe,e.createCacheMiddleware=S,e.createCircuitBreakerMiddleware=E,e.createDedupeMiddleware=w,e.createHttpClient=We,e.createMockClient=Qe,e.createPipeline=D,e.createProjectionTransformer=p,e.createRateLimitMiddleware=T,e.createRealtimePlugin=Ze,e.createRequiredFieldsValidator=s,e.createSSEClient=Ye,e.createSchemaValidator=o,e.createStreamingMiddleware=M,e.createTypeGuard=fe,e.createTypedApiClient=ue,e.createTypedRequest=O,e.createTypedResponse=le,e.createUrl=k,e.createWebSocketClient=qe,e.createWrapperTransformer=m,e.dedupeMiddleware=ae,e.handleStream=A,e.isHttpError=Ue,e.rateLimitMiddleware=oe,e.retry=re,e.streamToFile=he,e.streamingMiddleware=ge,e.transformCamelToSnake=d,e.transformFlatten=f,e.transformSnakeToCamel=u,e.validatorIsArray=c,e.validatorIsObject=l,e.withTimeout=ne});
2
+ //# sourceMappingURL=nexa.umd.js.map