mirror of https://github.com/VERT-sh/VERT.git
fix: handle envelope response
This commit is contained in:
parent
2e34c36ac5
commit
bbc7768f79
|
|
@ -71,18 +71,36 @@ export const vertdFetch: {
|
||||||
const res = await fetch(domain + url, options);
|
const res = await fetch(domain + url, options);
|
||||||
|
|
||||||
const text = await res.text();
|
const text = await res.text();
|
||||||
let json = null;
|
const normalizedText = text.replace(/^\uFEFF/, "").trim();
|
||||||
|
|
||||||
|
if (!normalizedText.length) {
|
||||||
|
if (!res.ok) throw new Error(`vertd request failed (${res.status})`);
|
||||||
|
return undefined as RouteResponseMap[typeof url];
|
||||||
|
}
|
||||||
|
|
||||||
|
let json: unknown = null;
|
||||||
try {
|
try {
|
||||||
json = JSON.parse(text);
|
json = JSON.parse(normalizedText);
|
||||||
} catch {
|
} catch {
|
||||||
throw new Error(text);
|
throw new Error(normalizedText);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (json.type === "error") {
|
if (json && typeof json === "object" && "type" in json && "data" in json) {
|
||||||
throw new Error(json.data);
|
const envelope = json as { type: string; data: unknown };
|
||||||
|
|
||||||
|
if (envelope.type === "error") {
|
||||||
|
if (typeof envelope.data === "string")
|
||||||
|
throw new Error(envelope.data);
|
||||||
|
throw new Error(JSON.stringify(envelope.data));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (envelope.type === "success")
|
||||||
|
return envelope.data as RouteResponseMap[typeof url];
|
||||||
}
|
}
|
||||||
|
|
||||||
return json.data;
|
if (!res.ok) throw new Error(normalizedText);
|
||||||
|
|
||||||
|
return json as RouteResponseMap[typeof url];
|
||||||
};
|
};
|
||||||
|
|
||||||
// ws types
|
// ws types
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue