41 lines
955 B
TypeScript
41 lines
955 B
TypeScript
import { type ClientOptions, fetch } from "@tauri-apps/plugin-http";
|
|
|
|
class FetchApi {
|
|
async sendRequest(
|
|
url: string,
|
|
method: string,
|
|
data?: any,
|
|
config: RequestInit & ClientOptions = {}
|
|
) {
|
|
const result = await fetch(url, {
|
|
method: method,
|
|
body: data ? JSON.stringify(data) : undefined,
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
},
|
|
...config,
|
|
});
|
|
if (result.ok) {
|
|
return {
|
|
success: true,
|
|
data: await result.json(),
|
|
};
|
|
} else {
|
|
return {
|
|
success: false,
|
|
data: await result.text(),
|
|
};
|
|
}
|
|
}
|
|
|
|
async get(url: string, config: RequestInit & ClientOptions = {}) {
|
|
return await this.sendRequest(url, "GET", undefined, config);
|
|
}
|
|
|
|
async post(url: string, data: any, config: RequestInit & ClientOptions = {}) {
|
|
return await this.sendRequest(url, "POST", data, config);
|
|
}
|
|
}
|
|
|
|
export const api = new FetchApi();
|