UNPKG

ant-design-x-vue

Version:

Craft AI-driven interfaces effortlessly

2 lines (1 loc) 2.11 kB
"use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const m=require("../x-stream/x-stream.js"),R=require("./x-fetch.js"),d=class d{constructor(t){this.create=async(u,e,r)=>{var i,h;const n=new AbortController,o={method:"POST",body:JSON.stringify({model:this.model,...u}),headers:this.defaultHeaders,signal:n.signal};(i=e==null?void 0:e.onStream)==null||i.call(e,n);try{const s=await R.default(this.baseURL,{fetch:this.customOptions.fetch,...o});if(r){await this.customResponseHandler(s,e,r);return}const f=s.headers.get("content-type")||"";switch(f.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: ${f} is not support!`)}}catch(s){const f=s instanceof Error?s:new Error("Unknown error!");throw(h=e==null?void 0:e.onError)==null||h.call(e,f),f}},this.customResponseHandler=async(u,e,r)=>{var o,i;const n=[];for await(const h of m.default({readableStream:u.body,transformStream:r}))n.push(h),(o=e==null?void 0:e.onUpdate)==null||o.call(e,h);(i=e==null?void 0:e.onSuccess)==null||i.call(e,n)},this.sseResponseHandler=async(u,e)=>{var n,o;const r=[];for await(const i of m.default({readableStream:u.body}))r.push(i),(n=e==null?void 0:e.onUpdate)==null||n.call(e,i);(o=e==null?void 0:e.onSuccess)==null||o.call(e,r)},this.jsonResponseHandler=async(u,e)=>{var n,o;const r=await u.json();(n=e==null?void 0:e.onUpdate)==null||n.call(e,r),(o=e==null?void 0:e.onSuccess)==null||o.call(e,[r])};const{baseURL:p,model:g,dangerouslyApiKey:S,...w}=t;this.baseURL=t.baseURL,this.model=t.model,this.defaultHeaders={"Content-Type":"application/json",...t.dangerouslyApiKey&&{Authorization:t.dangerouslyApiKey}},this.customOptions=w}static init(t){if(!t.baseURL||typeof t.baseURL!="string")throw new Error("The baseURL is not valid!");const p=t.fetch||t.baseURL;return d.instanceBuffer.has(p)||d.instanceBuffer.set(p,new d(t)),d.instanceBuffer.get(p)}};d.instanceBuffer=new Map;let y=d;const U=y.init;exports.default=U;