mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-04-25 00:36:31 +02:00
format with biome
This commit is contained in:
parent
77d49ca11c
commit
81ee04c2a5
21 changed files with 1602 additions and 1752 deletions
|
|
@ -1,179 +1,163 @@
|
|||
import z from "zod";
|
||||
import {
|
||||
AppError,
|
||||
AuthenticationError,
|
||||
AuthorizationError,
|
||||
ValidationError,
|
||||
} from "../error";
|
||||
import { AppError, AuthenticationError, AuthorizationError, ValidationError } from "../error";
|
||||
|
||||
export type RequestOptions = {
|
||||
method: "GET" | "POST" | "PUT" | "DELETE";
|
||||
headers?: Record<string, string>;
|
||||
contentType?: "application/json" | "application/x-www-form-urlencoded";
|
||||
signal?: AbortSignal;
|
||||
body?: any;
|
||||
// Add more options as needed
|
||||
method: "GET" | "POST" | "PUT" | "DELETE";
|
||||
headers?: Record<string, string>;
|
||||
contentType?: "application/json" | "application/x-www-form-urlencoded";
|
||||
signal?: AbortSignal;
|
||||
body?: any;
|
||||
// Add more options as needed
|
||||
};
|
||||
|
||||
export class BaseApiService {
|
||||
bearerToken: string;
|
||||
baseUrl: string;
|
||||
bearerToken: string;
|
||||
baseUrl: string;
|
||||
|
||||
constructor(bearerToken: string, baseUrl: string) {
|
||||
this.bearerToken = bearerToken;
|
||||
this.baseUrl = baseUrl;
|
||||
}
|
||||
constructor(bearerToken: string, baseUrl: string) {
|
||||
this.bearerToken = bearerToken;
|
||||
this.baseUrl = baseUrl;
|
||||
}
|
||||
|
||||
setBearerToken(bearerToken: string) {
|
||||
this.bearerToken = bearerToken;
|
||||
}
|
||||
setBearerToken(bearerToken: string) {
|
||||
this.bearerToken = bearerToken;
|
||||
}
|
||||
|
||||
async request<T>(
|
||||
url: string,
|
||||
body?: any,
|
||||
responseSchema?: z.ZodSchema<T>,
|
||||
options?: RequestOptions
|
||||
) : Promise<T> {
|
||||
const defaultOptions: RequestOptions = {
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Bearer ${this.bearerToken}`,
|
||||
},
|
||||
method: "GET",
|
||||
};
|
||||
async request<T>(
|
||||
url: string,
|
||||
body?: any,
|
||||
responseSchema?: z.ZodSchema<T>,
|
||||
options?: RequestOptions
|
||||
): Promise<T> {
|
||||
const defaultOptions: RequestOptions = {
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Bearer ${this.bearerToken}`,
|
||||
},
|
||||
method: "GET",
|
||||
};
|
||||
|
||||
const mergedOptions: RequestOptions = {
|
||||
...defaultOptions,
|
||||
...(options ?? {}),
|
||||
headers: {
|
||||
...defaultOptions.headers,
|
||||
...(options?.headers ?? {}),
|
||||
},
|
||||
};
|
||||
const mergedOptions: RequestOptions = {
|
||||
...defaultOptions,
|
||||
...(options ?? {}),
|
||||
headers: {
|
||||
...defaultOptions.headers,
|
||||
...(options?.headers ?? {}),
|
||||
},
|
||||
};
|
||||
|
||||
let requestBody;
|
||||
let requestBody;
|
||||
|
||||
// Serialize body
|
||||
if (body) {
|
||||
if (
|
||||
mergedOptions.headers?.["Content-Type"].toLocaleLowerCase() ===
|
||||
"application/json"
|
||||
) {
|
||||
requestBody = JSON.stringify(body);
|
||||
}
|
||||
// Serialize body
|
||||
if (body) {
|
||||
if (mergedOptions.headers?.["Content-Type"].toLocaleLowerCase() === "application/json") {
|
||||
requestBody = JSON.stringify(body);
|
||||
}
|
||||
|
||||
if (
|
||||
mergedOptions.headers?.["Content-Type"].toLocaleLowerCase() ===
|
||||
"application/x-www-form-urlencoded"
|
||||
) {
|
||||
requestBody = new URLSearchParams(body);
|
||||
}
|
||||
if (
|
||||
mergedOptions.headers?.["Content-Type"].toLocaleLowerCase() ===
|
||||
"application/x-www-form-urlencoded"
|
||||
) {
|
||||
requestBody = new URLSearchParams(body);
|
||||
}
|
||||
|
||||
mergedOptions.body = requestBody;
|
||||
}
|
||||
mergedOptions.body = requestBody;
|
||||
}
|
||||
|
||||
if (!this.baseUrl) {
|
||||
throw new AppError("Base URL is not set.");
|
||||
}
|
||||
if (!this.baseUrl) {
|
||||
throw new AppError("Base URL is not set.");
|
||||
}
|
||||
|
||||
if (!this.bearerToken) {
|
||||
throw new AuthenticationError(
|
||||
"You are not authenticated. Please login again."
|
||||
);
|
||||
}
|
||||
if (!this.bearerToken) {
|
||||
throw new AuthenticationError("You are not authenticated. Please login again.");
|
||||
}
|
||||
|
||||
const fullUrl = new URL(url, this.baseUrl).toString();
|
||||
const fullUrl = new URL(url, this.baseUrl).toString();
|
||||
|
||||
const response = await fetch(fullUrl, mergedOptions);
|
||||
const response = await fetch(fullUrl, mergedOptions);
|
||||
|
||||
if (!response.ok) {
|
||||
if (response.status === 401) {
|
||||
throw new AuthenticationError(
|
||||
"You are not authenticated. Please login again."
|
||||
);
|
||||
}
|
||||
if (!response.ok) {
|
||||
if (response.status === 401) {
|
||||
throw new AuthenticationError("You are not authenticated. Please login again.");
|
||||
}
|
||||
|
||||
if (response.status === 403) {
|
||||
throw new AuthorizationError(
|
||||
"You don't have permission to access this resource."
|
||||
);
|
||||
}
|
||||
if (response.status === 403) {
|
||||
throw new AuthorizationError("You don't have permission to access this resource.");
|
||||
}
|
||||
|
||||
throw new AppError(`API Error: ${response.statusText}`);
|
||||
}
|
||||
throw new AppError(`API Error: ${response.statusText}`);
|
||||
}
|
||||
|
||||
let data;
|
||||
let data;
|
||||
|
||||
try {
|
||||
data = await response.json();
|
||||
} catch (error) {
|
||||
throw new AppError(`Failed to parse response as JSON: ${error}`);
|
||||
}
|
||||
try {
|
||||
data = await response.json();
|
||||
} catch (error) {
|
||||
throw new AppError(`Failed to parse response as JSON: ${error}`);
|
||||
}
|
||||
|
||||
if (!responseSchema) {
|
||||
return data;
|
||||
}
|
||||
if (!responseSchema) {
|
||||
return data;
|
||||
}
|
||||
|
||||
const parsedData = responseSchema.safeParse(data);
|
||||
const parsedData = responseSchema.safeParse(data);
|
||||
|
||||
if (!parsedData.success) {
|
||||
throw new ValidationError(
|
||||
`Invalid response: ${parsedData.error.message}`
|
||||
);
|
||||
}
|
||||
if (!parsedData.success) {
|
||||
throw new ValidationError(`Invalid response: ${parsedData.error.message}`);
|
||||
}
|
||||
|
||||
return parsedData.data;
|
||||
}
|
||||
return parsedData.data;
|
||||
}
|
||||
|
||||
async get<T>(
|
||||
url: string,
|
||||
responseSchema?: z.ZodSchema<T>,
|
||||
options?: Omit<RequestOptions, "method">
|
||||
) {
|
||||
return this.request(url, undefined, responseSchema, {
|
||||
...options,
|
||||
method: "GET",
|
||||
});
|
||||
}
|
||||
async get<T>(
|
||||
url: string,
|
||||
responseSchema?: z.ZodSchema<T>,
|
||||
options?: Omit<RequestOptions, "method">
|
||||
) {
|
||||
return this.request(url, undefined, responseSchema, {
|
||||
...options,
|
||||
method: "GET",
|
||||
});
|
||||
}
|
||||
|
||||
async post<T>(
|
||||
url: string,
|
||||
body?: any,
|
||||
responseSchema?: z.ZodSchema<T>,
|
||||
options?: Omit<RequestOptions, "method">
|
||||
) {
|
||||
return this.request(url, body, responseSchema, {
|
||||
...options,
|
||||
method: "POST",
|
||||
});
|
||||
}
|
||||
async post<T>(
|
||||
url: string,
|
||||
body?: any,
|
||||
responseSchema?: z.ZodSchema<T>,
|
||||
options?: Omit<RequestOptions, "method">
|
||||
) {
|
||||
return this.request(url, body, responseSchema, {
|
||||
...options,
|
||||
method: "POST",
|
||||
});
|
||||
}
|
||||
|
||||
async put<T>(
|
||||
url: string,
|
||||
body?: any,
|
||||
responseSchema?: z.ZodSchema<T>,
|
||||
options?: Omit<RequestOptions, "method">
|
||||
) {
|
||||
return this.request(url, body, responseSchema, {
|
||||
...options,
|
||||
method: "PUT",
|
||||
});
|
||||
}
|
||||
async put<T>(
|
||||
url: string,
|
||||
body?: any,
|
||||
responseSchema?: z.ZodSchema<T>,
|
||||
options?: Omit<RequestOptions, "method">
|
||||
) {
|
||||
return this.request(url, body, responseSchema, {
|
||||
...options,
|
||||
method: "PUT",
|
||||
});
|
||||
}
|
||||
|
||||
async delete<T>(
|
||||
url: string,
|
||||
body?: any,
|
||||
responseSchema?: z.ZodSchema<T>,
|
||||
options?: Omit<RequestOptions, "method">
|
||||
) {
|
||||
return this.request(url, body, responseSchema, {
|
||||
...options,
|
||||
method: "DELETE",
|
||||
});
|
||||
}
|
||||
async delete<T>(
|
||||
url: string,
|
||||
body?: any,
|
||||
responseSchema?: z.ZodSchema<T>,
|
||||
options?: Omit<RequestOptions, "method">
|
||||
) {
|
||||
return this.request(url, body, responseSchema, {
|
||||
...options,
|
||||
method: "DELETE",
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export const baseApiService = new BaseApiService(
|
||||
typeof window !== "undefined" ? localStorage.getItem("surfsense_bearer_token") || "" : "",
|
||||
process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL || ""
|
||||
typeof window !== "undefined" ? localStorage.getItem("surfsense_bearer_token") || "" : "",
|
||||
process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL || ""
|
||||
);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue