File uploading
This commit is contained in:
+10
-6
@@ -3,14 +3,10 @@ export type PostJsonResult<T> =
|
||||
// status is 0 when the request never reached the server
|
||||
| { ok: false; status: number; error: string };
|
||||
|
||||
export async function post_json<T>(url: string, body: unknown, method = "POST"): Promise<PostJsonResult<T>> {
|
||||
export async function fetch_json<T>(url: string, init: RequestInit): Promise<PostJsonResult<T>> {
|
||||
let response: Response;
|
||||
try {
|
||||
response = await fetch(url, {
|
||||
method,
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(body),
|
||||
});
|
||||
response = await fetch(url, init);
|
||||
} catch (err) {
|
||||
return { ok: false, status: 0, error: err instanceof Error ? err.message : String(err) };
|
||||
}
|
||||
@@ -25,3 +21,11 @@ export async function post_json<T>(url: string, body: unknown, method = "POST"):
|
||||
}
|
||||
return { ok: true, data: data as T };
|
||||
}
|
||||
|
||||
export function post_json<T>(url: string, body: unknown, method = "POST"): Promise<PostJsonResult<T>> {
|
||||
return fetch_json<T>(url, {
|
||||
method,
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(body),
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user