ant-design-x-vue-next
Version:
Ant Design X for Vue — community continuation aligned with @ant-design/x
2 lines (1 loc) • 6.02 kB
JavaScript
"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const _=require("../x-stream/x-stream.js"),x=require("./x-fetch.js"),w=require("vue"),o={headers:{"Content-Type":"application/json"}};function j(p){Object.assign(o,p)}function U(){return o}function L(){for(const p of Object.keys(o))delete o[p];o.headers={"Content-Type":"application/json"}}const X="Last-Event-ID";class R{constructor(m){this._isTimeout=!1,this._isStreamTimeout=!1,this._isRequesting=!1,this.retryTimes=0,this.abort=()=>{var r;this.clearTimers(),(r=this.abortController)==null||r.abort(),this._isRequesting=!1},this.create=async(r,t,i)=>{this.resetRetryState(),this.lastParams=r,this.lastCallbacks=t,this.lastTransformStream=i,await this.executeCreate(r,t,i)},this.executeCreate=async(r,t,i,a)=>{var f;this.abortController=new AbortController,this._isTimeout=!1,this._isStreamTimeout=!1,this._isRequesting=!0;const u={method:"POST",body:JSON.stringify({model:this.model,...r}),headers:{...this.defaultHeaders,...a||{}},signal:this.abortController.signal};(f=t==null?void 0:t.onStream)==null||f.call(t,this.abortController);const d=this.customOptions.timeout;d&&d>0&&(this.timeoutHandler=setTimeout(()=>{var s;this._isTimeout=!0,(s=this.abortController)==null||s.abort();const e=new Error("TimeoutError");e.name="TimeoutError",this.handleError(e,t)},d));try{const e=await x.default(this.baseURL,{fetch:this.customOptions.fetch,middlewares:this.customOptions.middlewares,...u});if(this.timeoutHandler&&(clearTimeout(this.timeoutHandler),this.timeoutHandler=void 0),this._isTimeout)return;if(i){await this.customResponseHandler(e,t,i);return}const s=e.headers.get("content-type")||"";switch(s.split(";")[0].trim()){case"text/event-stream":await this.sseResponseHandler(e,t);break;case"application/json":await this.jsonResponseHandler(e,t);break;default:throw new Error(`The response content-type: ${s} is not support!`)}}catch(e){if(this.timeoutHandler&&(clearTimeout(this.timeoutHandler),this.timeoutHandler=void 0),this._isTimeout||this._isStreamTimeout)return;const s=e instanceof Error||typeof DOMException<"u"&&e instanceof DOMException?e:new Error("Unknown error!");if(this.handleError(s,t),this.retryTimer||s.name==="AbortError")return;throw s}},this.processStream=async(r,t)=>{var u,d,f;const i=[],a=this.customOptions.streamTimeout;try{for(;;){a&&a>0&&(this.streamTimeoutHandler=setTimeout(()=>{var O;this._isStreamTimeout=!0,(O=this.abortController)==null||O.abort();const s=new Error("StreamTimeoutError");s.name="StreamTimeoutError",this.handleError(s,t)},a));let e;try{e=await r.next()}catch{if(this._isStreamTimeout||this._isTimeout)return;throw new Error("Stream read failed")}if(this.streamTimeoutHandler&&(clearTimeout(this.streamTimeoutHandler),this.streamTimeoutHandler=void 0),this._isStreamTimeout)return;if(e.done)break;if(e.value){i.push(e.value),(u=t==null?void 0:t.onUpdate)==null||u.call(t,e.value);const s=(d=e.value)==null?void 0:d.id;typeof s<"u"&&(this.lastEventId=String(s))}}this._isRequesting=!1,(f=t==null?void 0:t.onSuccess)==null||f.call(t,i)}finally{this.streamTimeoutHandler&&(clearTimeout(this.streamTimeoutHandler),this.streamTimeoutHandler=void 0)}},this.customResponseHandler=async(r,t,i)=>{const a=_.default({readableStream:r.body,transformStream:i,streamSeparator:this.customOptions.streamSeparator,partSeparator:this.customOptions.partSeparator,kvSeparator:this.customOptions.kvSeparator});await this.processStream(a[Symbol.asyncIterator](),t)},this.sseResponseHandler=async(r,t)=>{const i=_.default({readableStream:r.body,streamSeparator:this.customOptions.streamSeparator,partSeparator:this.customOptions.partSeparator,kvSeparator:this.customOptions.kvSeparator});await this.processStream(i[Symbol.asyncIterator](),t)},this.jsonResponseHandler=async(r,t)=>{var a,u;const i=await r.json();(a=t==null?void 0:t.onUpdate)==null||a.call(t,i),this._isRequesting=!1,(u=t==null?void 0:t.onSuccess)==null||u.call(t,[i])};const{baseURL:n,model:y,dangerouslyApiKey:h,headers:T,fetch:S,middlewares:v,timeout:E,streamTimeout:H,retryInterval:g,retryTimes:q,streamSeparator:C,partSeparator:I,kvSeparator:l}=m;this.baseURL=n,this.model=y,this.defaultHeaders={...o.headers||{},"Content-Type":"application/json",...T||{},...h&&{Authorization:h}},this.customOptions={headers:T,fetch:S??o.fetch,middlewares:v??o.middlewares,timeout:E??o.timeout,streamTimeout:H??o.streamTimeout,retryInterval:g??o.retryInterval,retryTimes:q??o.retryTimes,streamSeparator:C??o.streamSeparator,partSeparator:I??o.partSeparator,kvSeparator:l??o.kvSeparator}}static init(m){if(!m.baseURL||typeof m.baseURL!="string")throw new Error("The baseURL is not valid!");return new R(m)}get isTimeout(){return this._isTimeout}get isStreamTimeout(){return this._isStreamTimeout}get isRequesting(){return this._isRequesting}resetRetryState(){this.clearTimers(),this.retryTimes=0,this.lastEventId=void 0,this._isTimeout=!1,this._isStreamTimeout=!1}clearTimers(){this.timeoutHandler&&clearTimeout(this.timeoutHandler),this.streamTimeoutHandler&&clearTimeout(this.streamTimeoutHandler),this.retryTimer&&clearTimeout(this.retryTimer),this.timeoutHandler=void 0,this.streamTimeoutHandler=void 0,this.retryTimer=void 0}handleError(m,n){var T,S;if(this._isRequesting=!1,m.name==="AbortError"&&!this._isTimeout&&!this._isStreamTimeout){(T=n==null?void 0:n.onError)==null||T.call(n,m);return}const y=(S=n==null?void 0:n.onError)==null?void 0:S.call(n,m),h=typeof y=="number"?y:this.customOptions.retryInterval;m.name!=="AbortError"&&h&&h>0&&(typeof this.customOptions.retryTimes!="number"||this.retryTimes<this.customOptions.retryTimes)&&(this.retryTimes+=1,this.retryTimer=setTimeout(()=>{const v={};typeof this.lastEventId<"u"&&(v[X]=this.lastEventId),this.executeCreate(this.lastParams,this.lastCallbacks,this.lastTransformStream,v)},h))}}const A=p=>w.computed(()=>R.init(w.toValue(p)));exports.default=A;exports.getXRequestGlobalOptions=U;exports.resetXRequestGlobalOptions=L;exports.setXRequestGlobalOptions=j;