init
This commit is contained in:
76
k-notes-frontend/src/lib/api.ts
Normal file
76
k-notes-frontend/src/lib/api.ts
Normal file
@@ -0,0 +1,76 @@
|
||||
const getApiUrl = () => {
|
||||
const stored = localStorage.getItem("k_notes_api_url");
|
||||
return stored ? `${stored}/api/v1` : "http://localhost:3000/api/v1";
|
||||
};
|
||||
|
||||
export const getBaseUrl = () => {
|
||||
const stored = localStorage.getItem("k_notes_api_url");
|
||||
return stored ? stored : "http://localhost:3000";
|
||||
}
|
||||
|
||||
export class ApiError extends Error {
|
||||
public status: number;
|
||||
|
||||
constructor(status: number, message: string) {
|
||||
super(message);
|
||||
this.status = status;
|
||||
this.name = "ApiError";
|
||||
}
|
||||
}
|
||||
|
||||
async function fetchWithAuth(endpoint: string, options: RequestInit = {}) {
|
||||
const url = `${getApiUrl()}${endpoint}`;
|
||||
|
||||
const headers = {
|
||||
"Content-Type": "application/json",
|
||||
...options.headers,
|
||||
};
|
||||
|
||||
const config: RequestInit = {
|
||||
...options,
|
||||
headers,
|
||||
credentials: "include", // Important for cookies!
|
||||
};
|
||||
|
||||
const response = await fetch(url, config);
|
||||
|
||||
if (!response.ok) {
|
||||
// Try to parse error message
|
||||
let errorMessage = "An error occurred";
|
||||
try {
|
||||
const errorData = await response.json();
|
||||
errorMessage = errorData.error?.message || errorData.message || errorMessage;
|
||||
} catch {
|
||||
// failed to parse json
|
||||
}
|
||||
|
||||
throw new ApiError(response.status, errorMessage);
|
||||
}
|
||||
|
||||
// For 204 No Content or empty responses
|
||||
if (response.status === 204) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Try to parse JSON
|
||||
try {
|
||||
return await response.json();
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export const api = {
|
||||
get: (endpoint: string) => fetchWithAuth(endpoint, { method: "GET" }),
|
||||
post: (endpoint: string, body: any) =>
|
||||
fetchWithAuth(endpoint, {
|
||||
method: "POST",
|
||||
body: JSON.stringify(body),
|
||||
}),
|
||||
patch: (endpoint: string, body: any) =>
|
||||
fetchWithAuth(endpoint, {
|
||||
method: "PATCH",
|
||||
body: JSON.stringify(body),
|
||||
}),
|
||||
delete: (endpoint: string) => fetchWithAuth(endpoint, { method: "DELETE" }),
|
||||
};
|
||||
17
k-notes-frontend/src/lib/constants.ts
Normal file
17
k-notes-frontend/src/lib/constants.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
const NOTE_COLORS = [
|
||||
{ name: "DEFAULT", value: "bg-background border-border", label: "Default" },
|
||||
{ name: "RED", value: "bg-red-50 border-red-200 dark:bg-red-950/20 dark:border-red-900", label: "Red" },
|
||||
{ name: "ORANGE", value: "bg-orange-50 border-orange-200 dark:bg-orange-950/20 dark:border-orange-900", label: "Orange" },
|
||||
{ name: "YELLOW", value: "bg-yellow-50 border-yellow-200 dark:bg-yellow-950/20 dark:border-yellow-900", label: "Yellow" },
|
||||
{ name: "GREEN", value: "bg-green-50 border-green-200 dark:bg-green-950/20 dark:border-green-900", label: "Green" },
|
||||
{ name: "TEAL", value: "bg-teal-50 border-teal-200 dark:bg-teal-950/20 dark:border-teal-900", label: "Teal" },
|
||||
{ name: "BLUE", value: "bg-blue-50 border-blue-200 dark:bg-blue-950/20 dark:border-blue-900", label: "Blue" },
|
||||
{ name: "INDIGO", value: "bg-indigo-50 border-indigo-200 dark:bg-indigo-950/20 dark:border-indigo-900", label: "Indigo" },
|
||||
];
|
||||
|
||||
export function getNoteColor(colorName: string | undefined): string {
|
||||
const color = NOTE_COLORS.find(c => c.name === colorName);
|
||||
return color ? color.value : NOTE_COLORS[0].value;
|
||||
}
|
||||
|
||||
export { NOTE_COLORS };
|
||||
6
k-notes-frontend/src/lib/utils.ts
Normal file
6
k-notes-frontend/src/lib/utils.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
import { clsx, type ClassValue } from "clsx"
|
||||
import { twMerge } from "tailwind-merge"
|
||||
|
||||
export function cn(...inputs: ClassValue[]) {
|
||||
return twMerge(clsx(inputs))
|
||||
}
|
||||
Reference in New Issue
Block a user