Skip to content

Commit

Permalink
fmt, remove any
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog committed Aug 2, 2023
1 parent 2180102 commit 566d52e
Showing 1 changed file with 22 additions and 17 deletions.
39 changes: 22 additions & 17 deletions plugins/http/guest-js/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}

Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 566d52e

Please sign in to comment.