mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-05-29 19:35:20 +02:00
extends the base apis to support blob response
This commit is contained in:
parent
8015eb8e94
commit
b2da1aa8e9
1 changed files with 196 additions and 152 deletions
|
|
@ -4,7 +4,6 @@ import {
|
||||||
AuthenticationError,
|
AuthenticationError,
|
||||||
AuthorizationError,
|
AuthorizationError,
|
||||||
NotFoundError,
|
NotFoundError,
|
||||||
ValidationError,
|
|
||||||
} from "../error";
|
} from "../error";
|
||||||
|
|
||||||
export type RequestOptions = {
|
export type RequestOptions = {
|
||||||
|
|
@ -13,14 +12,19 @@ export type RequestOptions = {
|
||||||
contentType?: "application/json" | "application/x-www-form-urlencoded";
|
contentType?: "application/json" | "application/x-www-form-urlencoded";
|
||||||
signal?: AbortSignal;
|
signal?: AbortSignal;
|
||||||
body?: any;
|
body?: any;
|
||||||
|
responseType?: "json" | "text" | "blob" | "arrayBuffer"; // Add more response types as needed
|
||||||
// Add more options as needed
|
// Add more options as needed
|
||||||
};
|
};
|
||||||
|
|
||||||
export class BaseApiService {
|
class BaseApiService {
|
||||||
bearerToken: string;
|
bearerToken: string;
|
||||||
baseUrl: string;
|
baseUrl: string;
|
||||||
|
|
||||||
noAuthEndpoints: string[] = ["/auth/jwt/login", "/auth/register", "/auth/refresh"]; // Add more endpoints as needed
|
noAuthEndpoints: string[] = [
|
||||||
|
"/auth/jwt/login",
|
||||||
|
"/auth/register",
|
||||||
|
"/auth/refresh",
|
||||||
|
]; // Add more endpoints as needed
|
||||||
|
|
||||||
constructor(bearerToken: string, baseUrl: string) {
|
constructor(bearerToken: string, baseUrl: string) {
|
||||||
this.bearerToken = bearerToken;
|
this.bearerToken = bearerToken;
|
||||||
|
|
@ -43,6 +47,7 @@ export class BaseApiService {
|
||||||
Authorization: `Bearer ${this.bearerToken || ""}`,
|
Authorization: `Bearer ${this.bearerToken || ""}`,
|
||||||
},
|
},
|
||||||
method: "GET",
|
method: "GET",
|
||||||
|
responseType: "json",
|
||||||
};
|
};
|
||||||
|
|
||||||
const mergedOptions: RequestOptions = {
|
const mergedOptions: RequestOptions = {
|
||||||
|
|
@ -59,7 +64,9 @@ export class BaseApiService {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!this.bearerToken && !this.noAuthEndpoints.includes(url)) {
|
if (!this.bearerToken && !this.noAuthEndpoints.includes(url)) {
|
||||||
throw new AuthenticationError("You are not authenticated. Please login again.");
|
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();
|
||||||
|
|
@ -75,11 +82,15 @@ export class BaseApiService {
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Failed to parse response as JSON:", error);
|
console.error("Failed to parse response as JSON:", error);
|
||||||
|
|
||||||
throw new AppError("Something went wrong", response.status, response.statusText);
|
throw new AppError(
|
||||||
|
"Something went wrong",
|
||||||
|
response.status,
|
||||||
|
response.statusText
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// for fastapi errors response
|
// for fastapi errors response
|
||||||
if ("detail" in data) {
|
if (typeof data === "object" && "detail" in data) {
|
||||||
throw new AppError(data.detail, response.status, response.statusText);
|
throw new AppError(data.detail, response.status, response.statusText);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -97,28 +108,56 @@ export class BaseApiService {
|
||||||
response.statusText
|
response.statusText
|
||||||
);
|
);
|
||||||
case 404:
|
case 404:
|
||||||
throw new NotFoundError("Resource not found", response.status, response.statusText);
|
throw new NotFoundError(
|
||||||
|
"Resource not found",
|
||||||
|
response.status,
|
||||||
|
response.statusText
|
||||||
|
);
|
||||||
// Add more cases as needed
|
// Add more cases as needed
|
||||||
default:
|
default:
|
||||||
throw new AppError("Something went wrong", response.status, response.statusText);
|
throw new AppError(
|
||||||
|
"Something went wrong",
|
||||||
|
response.status,
|
||||||
|
response.statusText
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// biome-ignore lint/suspicious: Unknown
|
// biome-ignore lint/suspicious: Unknown
|
||||||
let data;
|
let data;
|
||||||
|
const responseType = mergedOptions.responseType || "json";
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
switch (responseType) {
|
||||||
|
case "json":
|
||||||
data = await response.json();
|
data = await response.json();
|
||||||
|
break;
|
||||||
|
case "text":
|
||||||
|
data = await response.text();
|
||||||
|
break;
|
||||||
|
case "blob":
|
||||||
|
data = await response.blob();
|
||||||
|
break;
|
||||||
|
case "arrayBuffer":
|
||||||
|
data = await response.arrayBuffer();
|
||||||
|
break;
|
||||||
|
// Add more cases as needed
|
||||||
|
default:
|
||||||
|
data = await response.text();
|
||||||
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Failed to parse response as JSON:", error);
|
console.error("Failed to parse response as JSON:", error);
|
||||||
|
throw new AppError(
|
||||||
throw new AppError("Something went wrong", response.status, response.statusText);
|
"Failed to parse response",
|
||||||
|
response.status,
|
||||||
|
response.statusText
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (responseType === "json") {
|
||||||
if (!responseSchema) {
|
if (!responseSchema) {
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
const parsedData = responseSchema.safeParse(data);
|
const parsedData = responseSchema.safeParse(data);
|
||||||
|
|
||||||
if (!parsedData.success) {
|
if (!parsedData.success) {
|
||||||
|
|
@ -129,6 +168,9 @@ export class BaseApiService {
|
||||||
console.error("Invalid API response schema:", parsedData.error);
|
console.error("Invalid API response schema:", parsedData.error);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
return data;
|
return data;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Request failed:", error);
|
console.error("Request failed:", error);
|
||||||
|
|
@ -182,6 +224,8 @@ export class BaseApiService {
|
||||||
}
|
}
|
||||||
|
|
||||||
export const baseApiService = new BaseApiService(
|
export const baseApiService = new BaseApiService(
|
||||||
typeof window !== "undefined" ? localStorage.getItem("surfsense_bearer_token") || "" : "",
|
typeof window !== "undefined"
|
||||||
|
? localStorage.getItem("surfsense_bearer_token") || ""
|
||||||
|
: "",
|
||||||
process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL || ""
|
process.env.NEXT_PUBLIC_FASTAPI_BACKEND_URL || ""
|
||||||
);
|
);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue