SurfSense/surfsense_web/lib/apis/base-api.service.ts

327 lines
8.7 KiB
TypeScript
Raw Normal View History

2025-12-20 05:10:47 -08:00
import type { ZodType } from "zod";
import { getBearerToken, handleUnauthorized } from "../auth-utils";
2025-11-18 11:35:06 +02:00
import { AppError, AuthenticationError, AuthorizationError, NotFoundError } from "../error";
2025-11-13 23:59:14 +02:00
2025-11-18 11:33:57 +02:00
enum ResponseType {
2025-11-18 11:35:06 +02:00
JSON = "json",
TEXT = "text",
BLOB = "blob",
ARRAY_BUFFER = "arrayBuffer",
// Add more response types as needed
2025-11-18 11:33:57 +02:00
}
2025-11-13 23:59:14 +02:00
export type RequestOptions = {
2026-01-13 00:17:12 -08:00
method: "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
2025-11-18 11:35:06 +02:00
headers?: Record<string, string>;
contentType?: "application/json" | "application/x-www-form-urlencoded";
signal?: AbortSignal;
body?: any;
responseType?: ResponseType;
// Add more options as needed
2025-11-13 23:59:14 +02:00
};
class BaseApiService {
2025-11-18 11:35:06 +02:00
baseUrl: string;
noAuthEndpoints: string[] = ["/auth/jwt/login", "/auth/register", "/auth/refresh"];
// Prefixes that don't require auth (checked with startsWith)
noAuthPrefixes: string[] = ["/api/v1/public/", "/api/v1/podcasts/"];
2025-11-18 11:35:06 +02:00
// Use a getter to always read fresh token from localStorage
// This ensures the token is always up-to-date after login/logout
get bearerToken(): string {
return typeof window !== "undefined" ? getBearerToken() || "" : "";
}
constructor(baseUrl: string) {
2025-11-18 11:35:06 +02:00
this.baseUrl = baseUrl;
}
// Keep for backward compatibility, but token is now always read from localStorage
setBearerToken(_bearerToken: string) {
// No-op: token is now always read fresh from localStorage via the getter
2025-11-18 11:35:06 +02:00
}
async request<T, R extends ResponseType = ResponseType.JSON>(
url: string,
2025-12-20 05:10:47 -08:00
responseSchema?: ZodType<T>,
2025-11-18 11:35:06 +02:00
options?: RequestOptions & { responseType?: R }
): Promise<
R extends ResponseType.JSON
? T
: R extends ResponseType.TEXT
? string
: R extends ResponseType.BLOB
? Blob
: R extends ResponseType.ARRAY_BUFFER
? ArrayBuffer
: unknown
> {
try {
2025-11-18 18:35:48 +02:00
/**
* ----------
* REQUEST
* ----------
*/
2025-11-18 11:35:06 +02:00
const defaultOptions: RequestOptions = {
headers: {
Authorization: `Bearer ${this.bearerToken || ""}`,
},
method: "GET",
responseType: ResponseType.JSON,
};
const mergedOptions: RequestOptions = {
...defaultOptions,
...(options ?? {}),
headers: {
...defaultOptions.headers,
...(options?.headers ?? {}),
},
};
2025-11-18 18:35:48 +02:00
// Validate the base URL
2025-11-18 11:35:06 +02:00
if (!this.baseUrl) {
throw new AppError("Base URL is not set.");
}
2025-11-18 18:35:48 +02:00
// Validate the bearer token
const isNoAuthEndpoint =
this.noAuthEndpoints.includes(url) ||
this.noAuthPrefixes.some((prefix) => url.startsWith(prefix));
if (!this.bearerToken && !isNoAuthEndpoint) {
2025-11-18 11:35:06 +02:00
throw new AuthenticationError("You are not authenticated. Please login again.");
}
2025-11-18 18:35:48 +02:00
// Construct the full URL
2025-11-18 11:35:06 +02:00
const fullUrl = new URL(url, this.baseUrl).toString();
2025-11-18 18:35:48 +02:00
// Prepare fetch options
const fetchOptions: RequestInit = {
method: mergedOptions.method,
headers: mergedOptions.headers,
signal: mergedOptions.signal,
};
// Automatically stringify body if Content-Type is application/json and body is an object
if (mergedOptions.body !== undefined) {
const contentType = mergedOptions.headers?.["Content-Type"];
if (contentType === "application/json" && typeof mergedOptions.body === "object") {
fetchOptions.body = JSON.stringify(mergedOptions.body);
} else {
// Pass body as-is for other content types (e.g., form data, already stringified)
fetchOptions.body = mergedOptions.body;
}
}
const response = await fetch(fullUrl, fetchOptions);
2025-11-18 11:35:06 +02:00
2025-11-18 18:35:48 +02:00
/**
* ----------
* RESPONSE
* ----------
*/
// Handle errors
2025-11-18 11:35:06 +02:00
if (!response.ok) {
// biome-ignore lint/suspicious: Unknown
let data;
try {
data = await response.json();
} catch (error) {
2025-11-18 18:35:48 +02:00
console.error("Failed to parse response as JSON: ", JSON.stringify(error));
throw new AppError("Failed to parse response", response.status, response.statusText);
2025-11-18 11:35:06 +02:00
}
// Handle 401 first before other error handling - ensures token is cleared and user redirected
if (response.status === 401) {
handleUnauthorized();
throw new AuthenticationError(
typeof data === "object" && "detail" in data
? data.detail
: "You are not authenticated. Please login again.",
response.status,
response.statusText
);
}
2025-11-18 18:35:48 +02:00
// For fastapi errors response
2025-11-18 11:35:06 +02:00
if (typeof data === "object" && "detail" in data) {
throw new AppError(data.detail, response.status, response.statusText);
}
switch (response.status) {
case 403:
throw new AuthorizationError(
"You don't have permission to access this resource.",
response.status,
response.statusText
);
case 404:
throw new NotFoundError("Resource not found", response.status, response.statusText);
// Add more cases as needed
default:
throw new AppError("Something went wrong", response.status, response.statusText);
}
}
2026-01-12 14:17:15 -08:00
// biome-ignore lint/suspicious: Unknown
let data;
const responseType = mergedOptions.responseType;
try {
switch (responseType) {
case ResponseType.JSON:
data = await response.json();
break;
case ResponseType.TEXT:
data = await response.text();
break;
case ResponseType.BLOB:
data = await response.blob();
break;
case ResponseType.ARRAY_BUFFER:
data = await response.arrayBuffer();
break;
// Add more cases as needed
default:
data = await response.json();
}
} catch (error) {
console.error("Failed to parse response as JSON:", error);
throw new AppError("Failed to parse response", response.status, response.statusText);
}
2025-11-18 11:35:06 +02:00
2026-01-12 14:17:15 -08:00
// Validate response
if (responseType === ResponseType.JSON) {
if (!responseSchema) {
return data;
}
const parsedData = responseSchema.safeParse(data);
if (!parsedData.success) {
/** The request was successful, but the response data does not match the expected schema.
* This is a client side error, and should be fixed by updating the responseSchema to keep things typed.
* This error should not be shown to the user , it is for dev only.
*/
console.error(`Invalid API response schema - ${url} :`, JSON.stringify(parsedData.error));
}
2025-11-18 11:35:06 +02:00
return data;
}
return data;
} catch (error) {
2025-11-18 18:35:48 +02:00
console.error("Request failed:", JSON.stringify(error));
2025-11-18 11:35:06 +02:00
throw error;
}
}
async get<T>(
url: string,
2025-12-20 05:10:47 -08:00
responseSchema?: ZodType<T>,
2025-11-18 11:35:06 +02:00
options?: Omit<RequestOptions, "method" | "responseType">
) {
return this.request(url, responseSchema, {
method: "GET",
headers: {
"Content-Type": "application/json",
},
...options,
2025-11-18 11:35:06 +02:00
responseType: ResponseType.JSON,
});
}
async post<T>(
url: string,
2025-12-20 05:10:47 -08:00
responseSchema?: ZodType<T>,
2025-11-18 11:35:06 +02:00
options?: Omit<RequestOptions, "method" | "responseType">
) {
return this.request(url, responseSchema, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
2025-11-18 11:35:06 +02:00
...options,
responseType: ResponseType.JSON,
});
}
async put<T>(
url: string,
2025-12-20 05:10:47 -08:00
responseSchema?: ZodType<T>,
2025-11-18 11:35:06 +02:00
options?: Omit<RequestOptions, "method" | "responseType">
) {
return this.request(url, responseSchema, {
method: "PUT",
headers: {
"Content-Type": "application/json",
},
2025-11-18 11:35:06 +02:00
...options,
responseType: ResponseType.JSON,
});
}
async delete<T>(
url: string,
2025-12-20 05:10:47 -08:00
responseSchema?: ZodType<T>,
2025-11-18 11:35:06 +02:00
options?: Omit<RequestOptions, "method" | "responseType">
) {
return this.request(url, responseSchema, {
method: "DELETE",
headers: {
"Content-Type": "application/json",
2026-01-13 00:17:12 -08:00
},
...options,
responseType: ResponseType.JSON,
});
}
async patch<T>(
url: string,
responseSchema?: ZodType<T>,
options?: Omit<RequestOptions, "method" | "responseType">
) {
return this.request(url, responseSchema, {
method: "PATCH",
headers: {
"Content-Type": "application/json",
},
2025-11-18 11:35:06 +02:00
...options,
responseType: ResponseType.JSON,
});
}
async getBlob(url: string, options?: Omit<RequestOptions, "method" | "responseType">) {
return this.request(url, undefined, {
...options,
method: "GET",
responseType: ResponseType.BLOB,
});
}
2025-11-20 11:00:42 +02:00
async postFormData<T>(
url: string,
2025-12-20 05:10:47 -08:00
responseSchema?: ZodType<T>,
2025-11-20 11:00:42 +02:00
options?: Omit<RequestOptions, "method" | "responseType" | "body"> & { body: FormData }
) {
// Remove Content-Type from options headers if present
const { "Content-Type": _, ...headersWithoutContentType } = options?.headers ?? {};
return this.request(url, responseSchema, {
method: "POST",
...options,
headers: {
// Don't set Content-Type - let browser set it with multipart boundary
Authorization: `Bearer ${this.bearerToken}`,
...headersWithoutContentType,
},
responseType: ResponseType.JSON,
});
}
2025-11-13 23:59:14 +02:00
}
2025-11-14 00:25:08 +02:00
2025-12-27 00:03:03 +05:30
export const baseApiService = new BaseApiService(process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL || "");