I GOT COMPANY API DATA ON THE PAGE AHHHHHHH

This commit is contained in:
2026-02-13 18:02:35 -06:00
parent 6b176196d3
commit 51db9de171
7 changed files with 600 additions and 9 deletions
+9 -2
View File
@@ -1,9 +1,16 @@
import api from "./axios";
export const company = {
async fetch() {},
async fetch(accessToken: string, id: string) {
const company = await api.get(`/v1/company/${id}`, {
headers: {
Authorization: `Bearer ${accessToken}`,
},
});
return company.data;
},
async fetchMany(accessToken: string, page: number = 1) {
const companies = await api.get("/v1/company/companies", {
const companies = await api.get("/v1/companies", {
params: {
page,
},
+59
View File
@@ -0,0 +1,59 @@
import { error } from "@sveltejs/kit";
export class ApiError extends Error {
constructor(
public statusCode: number,
public message: string,
public details?: unknown,
) {
super(message);
this.name = "ApiError";
}
}
export function handleApiError(err: unknown): never {
console.error("API Error:", err);
if (err instanceof ApiError) {
throw error(err.statusCode, {
message: err.message,
details: err.details,
});
}
if (err instanceof Error) {
const message = err.message || "An unexpected error occurred";
throw error(500, {
message,
details: err.stack,
});
}
throw error(500, {
message: "An unexpected error occurred",
details: String(err),
});
}
export function isNetworkError(err: unknown): boolean {
if (err instanceof Error) {
return (
err.message.includes("Network") ||
err.message.includes("fetch") ||
err.message.includes("ECONNREFUSED")
);
}
return false;
}
export function isUnauthorizedError(err: unknown): boolean {
return err instanceof ApiError && err.statusCode === 401;
}
export function isForbiddenError(err: unknown): boolean {
return err instanceof ApiError && err.statusCode === 403;
}
export function isNotFoundError(err: unknown): boolean {
return err instanceof ApiError && err.statusCode === 404;
}