fmt, remove any

pull/428/head
Lucas Nogueira 2 years ago
parent 21801022f6
commit 566d52e0ab
No known key found for this signature in database
GPG Key ID: FFEA6C72E73482F1

@ -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;

Loading…
Cancel
Save