aboutsummaryrefslogtreecommitdiff
path: root/src/lib/fetch.ts
diff options
context:
space:
mode:
authorFuwn <[email protected]>2026-01-24 13:09:50 +0000
committerFuwn <[email protected]>2026-01-24 13:09:50 +0000
commit396acf3bbbe00a192cb0ea0a9ccf91b1d8d2850b (patch)
treeb9df4ca6a70db45cfffbae6fdd7252e20fb8e93c /src/lib/fetch.ts
downloadumami-main.tar.xz
umami-main.zip
Initial commitHEADmain
Created from https://vercel.com/new
Diffstat (limited to 'src/lib/fetch.ts')
-rw-r--r--src/lib/fetch.ts58
1 files changed, 58 insertions, 0 deletions
diff --git a/src/lib/fetch.ts b/src/lib/fetch.ts
new file mode 100644
index 0000000..1086973
--- /dev/null
+++ b/src/lib/fetch.ts
@@ -0,0 +1,58 @@
+import { buildPath } from '@/lib/url';
+
+export interface ErrorResponse {
+ error: {
+ status: number;
+ message: string;
+ code?: string;
+ };
+}
+
+export interface FetchResponse {
+ ok: boolean;
+ status: number;
+ data?: any;
+ error?: ErrorResponse;
+}
+
+export async function request(
+ method: string,
+ url: string,
+ body?: string,
+ headers: object = {},
+): Promise<FetchResponse> {
+ return fetch(url, {
+ method,
+ cache: 'no-cache',
+ headers: {
+ Accept: 'application/json',
+ 'Content-Type': 'application/json',
+ ...headers,
+ },
+ body,
+ }).then(async res => {
+ const data = await res.json();
+
+ return {
+ ok: res.ok,
+ status: res.status,
+ data,
+ };
+ });
+}
+
+export async function httpGet(path: string, params: object = {}, headers: object = {}) {
+ return request('GET', buildPath(path, params), undefined, headers);
+}
+
+export async function httpDelete(path: string, params: object = {}, headers: object = {}) {
+ return request('DELETE', buildPath(path, params), undefined, headers);
+}
+
+export async function httpPost(path: string, params: object = {}, headers: object = {}) {
+ return request('POST', path, JSON.stringify(params), headers);
+}
+
+export async function httpPut(path: string, params: object = {}, headers: object = {}) {
+ return request('PUT', path, JSON.stringify(params), headers);
+}