ant-design-x-vue-next
Version:
Ant Design X for Vue — community continuation aligned with @ant-design/x
216 lines (215 loc) • 8.04 kB
JavaScript
import H from "../x-stream/x-stream.mjs";
import j from "./x-fetch.mjs";
import { computed as x, toValue as U } from "vue";
const o = {
headers: {
"Content-Type": "application/json"
}
};
function G(p) {
Object.assign(o, p);
}
function P() {
return o;
}
function F() {
for (const p of Object.keys(o))
delete o[p];
o.headers = { "Content-Type": "application/json" };
}
const L = "Last-Event-ID";
class E {
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 j(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 w;
this._isStreamTimeout = !0, (w = this.abortController) == null || w.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 = H({
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 = H({
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: O,
streamTimeout: R,
retryInterval: _,
retryTimes: g,
streamSeparator: C,
partSeparator: I,
kvSeparator: q
} = 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: O ?? o.timeout,
streamTimeout: R ?? o.streamTimeout,
retryInterval: _ ?? o.retryInterval,
retryTimes: g ?? o.retryTimes,
streamSeparator: C ?? o.streamSeparator,
partSeparator: I ?? o.partSeparator,
kvSeparator: q ?? o.kvSeparator
};
}
static init(m) {
if (!m.baseURL || typeof m.baseURL != "string")
throw new Error("The baseURL is not valid!");
return new E(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[L] = this.lastEventId), this.executeCreate(
this.lastParams,
this.lastCallbacks,
this.lastTransformStream,
v
);
}, h));
}
}
const M = (p) => x(() => E.init(U(p)));
export {
M as default,
P as getXRequestGlobalOptions,
F as resetXRequestGlobalOptions,
G as setXRequestGlobalOptions
};