|
|
|
@ -33,45 +33,50 @@ declare global {
|
|
|
|
|
async function readBody<T>(rid: number, kind: "blob" | "text"): Promise<T> {
|
|
|
|
|
return await window.__TAURI_INVOKE__("plugin:http|fetch_read_body", {
|
|
|
|
|
rid,
|
|
|
|
|
kind
|
|
|
|
|
kind,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class TauriResponse extends Response {
|
|
|
|
|
_rid: number = 0
|
|
|
|
|
_rid: number = 0;
|
|
|
|
|
|
|
|
|
|
blob(): Promise<Blob> {
|
|
|
|
|
return readBody<Uint8Array>(this._rid, "blob").then(bytes => new Blob([bytes], { type: this.headers.get("content-type") || "application/octet-stream" }))
|
|
|
|
|
return readBody<Uint8Array>(this._rid, "blob").then(
|
|
|
|
|
(bytes) =>
|
|
|
|
|
new Blob([bytes], {
|
|
|
|
|
type: this.headers.get("content-type") || "application/octet-stream",
|
|
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
json(): Promise<any> {
|
|
|
|
|
return readBody<string>(this._rid, "text").then(data => {
|
|
|
|
|
json(): Promise<unknown> {
|
|
|
|
|
return readBody<string>(this._rid, "text").then((data) => {
|
|
|
|
|
try {
|
|
|
|
|
return JSON.parse(data);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
if (this.ok && data === "") {
|
|
|
|
|
return {};
|
|
|
|
|
} else if (this.ok) {
|
|
|
|
|
throw Error(
|
|
|
|
|
`Failed to parse response \`${data}\` as JSON: ${e}`
|
|
|
|
|
);
|
|
|
|
|
throw Error(`Failed to parse response \`${data}\` as JSON: ${e}`);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
formData(): Promise<FormData> {
|
|
|
|
|
return this.json().then((json: Record<string, string | Blob>) => {
|
|
|
|
|
const form = new FormData()
|
|
|
|
|
for (const [key, value] of Object.entries(json)) {
|
|
|
|
|
form.append(key, value)
|
|
|
|
|
return this.json().then((json) => {
|
|
|
|
|
const form = new FormData();
|
|
|
|
|
for (const [key, value] of Object.entries(
|
|
|
|
|
json as Record<string, string | Blob>,
|
|
|
|
|
)) {
|
|
|
|
|
form.append(key, value);
|
|
|
|
|
}
|
|
|
|
|
return form
|
|
|
|
|
})
|
|
|
|
|
return form;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
text(): Promise<string> {
|
|
|
|
|
return readBody(this._rid, "text")
|
|
|
|
|
return readBody(this._rid, "text");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -106,7 +111,7 @@ export interface ClientOptions {
|
|
|
|
|
*/
|
|
|
|
|
export async function fetch(
|
|
|
|
|
input: URL | Request | string,
|
|
|
|
|
init?: RequestInit & ClientOptions
|
|
|
|
|
init?: RequestInit & ClientOptions,
|
|
|
|
|
): Promise<TauriResponse> {
|
|
|
|
|
const maxRedirections = init?.maxRedirections;
|
|
|
|
|
const connectTimeout = init?.maxRedirections;
|
|
|
|
|