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

Loading…
Cancel
Save