mirror of
https://github.com/rjNemo/ticket_manager
synced 2026-06-06 08:46:39 +00:00
37 lines
963 B
TypeScript
37 lines
963 B
TypeScript
export interface HttpResponse<T> extends Response {
|
|
parsedBody?: T;
|
|
}
|
|
|
|
export async function http<T>(request: RequestInfo): Promise<HttpResponse<T>> {
|
|
const response: HttpResponse<T> = await fetch(request);
|
|
try {
|
|
response.parsedBody = await response.json();
|
|
} catch (ex) {}
|
|
if (!response.ok) {
|
|
throw new Error(response.statusText);
|
|
}
|
|
return response;
|
|
}
|
|
|
|
export async function get<T>(
|
|
path: string,
|
|
args: RequestInit = { method: "get" }
|
|
): Promise<HttpResponse<T>> {
|
|
return await http<T>(new Request(path, args));
|
|
}
|
|
|
|
export async function post<T>(
|
|
path: string,
|
|
body: any,
|
|
args: RequestInit = { method: "post", body: JSON.stringify(body) }
|
|
): Promise<HttpResponse<T>> {
|
|
return await http<T>(new Request(path, args));
|
|
}
|
|
|
|
export async function put<T>(
|
|
path: string,
|
|
body: any,
|
|
args: RequestInit = { method: "put", body: JSON.stringify(body) }
|
|
): Promise<HttpResponse<T>> {
|
|
return await http<T>(new Request(path, args));
|
|
}
|