ant-design-x-vue
Version:
Craft AI-driven interfaces effortlessly
2 lines (1 loc) • 2.03 kB
JavaScript
;Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const y=require("../x-stream/x-stream.js"),R=require("./x-fetch.js"),m=require("vue");class p{constructor(r){this.create=async(u,e,n)=>{var i,d;const t=new AbortController,o={method:"POST",body:JSON.stringify({model:this.model,...u}),headers:this.defaultHeaders,signal:t.signal};(i=e==null?void 0:e.onStream)==null||i.call(e,t);try{const s=await R.default(this.baseURL,{fetch:this.customOptions.fetch,...o});if(n){await this.customResponseHandler(s,e,n);return}const h=s.headers.get("content-type")||"";switch(h.split(";")[0].trim()){case"text/event-stream":await this.sseResponseHandler(s,e);break;case"application/json":await this.jsonResponseHandler(s,e);break;default:throw new Error(`The response content-type: ${h} is not support!`)}}catch(s){const h=s instanceof Error?s:new Error("Unknown error!");throw(d=e==null?void 0:e.onError)==null||d.call(e,h),h}},this.customResponseHandler=async(u,e,n)=>{var o,i;const t=[];for await(const d of y.default({readableStream:u.body,transformStream:n}))t.push(d),(o=e==null?void 0:e.onUpdate)==null||o.call(e,d);(i=e==null?void 0:e.onSuccess)==null||i.call(e,t)},this.sseResponseHandler=async(u,e)=>{var t,o;const n=[];for await(const i of y.default({readableStream:u.body}))n.push(i),(t=e==null?void 0:e.onUpdate)==null||t.call(e,i);(o=e==null?void 0:e.onSuccess)==null||o.call(e,n)},this.jsonResponseHandler=async(u,e)=>{var t,o;const n=await u.json();(t=e==null?void 0:e.onUpdate)==null||t.call(e,n),(o=e==null?void 0:e.onSuccess)==null||o.call(e,[n])};const{baseURL:U,model:g,dangerouslyApiKey:H,...w}=r;this.baseURL=r.baseURL,this.model=r.model,this.defaultHeaders={"Content-Type":"application/json",...r.dangerouslyApiKey&&{Authorization:r.dangerouslyApiKey}},this.customOptions=w}static init(r){if(!r.baseURL||typeof r.baseURL!="string")throw new Error("The baseURL is not valid!");return new p(r)}}const S=f=>m.computed(()=>p.init(m.toValue(f)));exports.default=S;