Single part at the root, COMPOSE_FILE in .env
This commit is contained in:
1 parent
ade2cebf63
commit
ecd38ee28b
31 files changed
+70
-28
No files matched your search
@@ -0,0 +1,56 @@
|
||||
import { env } from "$env/dynamic/public";
|
||||
|
||||
const BASE = env.PUBLIC_API_BASE_URL ?? "/api";
|
||||
|
||||
export class ApiError extends Error {
|
||||
status: number;
|
||||
|
||||
constructor(status: number, message: string) {
|
||||
super(message);
|
||||
this.status = status;
|
||||
this.name = "ApiError";
|
||||
}
|
||||
}
|
||||
|
||||
type Json = Record<string, unknown> | unknown[];
|
||||
|
||||
async function handle<T>(res: Response): Promise<T> {
|
||||
if (res.status === 204) {
|
||||
return undefined as T;
|
||||
}
|
||||
if (res.ok) {
|
||||
return (await res.json()) as T;
|
||||
}
|
||||
let detail = res.statusText;
|
||||
try {
|
||||
const { detail: parsed } = (await res.json()) as { detail?: string };
|
||||
if (parsed) {
|
||||
detail = parsed;
|
||||
}
|
||||
} catch {
|
||||
detail = res.statusText;
|
||||
}
|
||||
throw new ApiError(res.status, detail);
|
||||
}
|
||||
|
||||
async function request<T>(
|
||||
method: string,
|
||||
path: string,
|
||||
body?: Json
|
||||
): Promise<T> {
|
||||
const init: RequestInit = { credentials: "include", method };
|
||||
if (body !== undefined) {
|
||||
init.headers = { "Content-Type": "application/json" };
|
||||
init.body = JSON.stringify(body);
|
||||
}
|
||||
const res = await fetch(BASE + path, init);
|
||||
return handle<T>(res);
|
||||
}
|
||||
|
||||
export const api = {
|
||||
del: <T>(path: string) => request<T>("DELETE", path),
|
||||
get: <T>(path: string) => request<T>("GET", path),
|
||||
patch: <T>(path: string, body?: Json) => request<T>("PATCH", path, body),
|
||||
post: <T>(path: string, body?: Json) => request<T>("POST", path, body),
|
||||
put: <T>(path: string, body?: Json) => request<T>("PUT", path, body),
|
||||
};
|
||||
@@ -0,0 +1,2 @@
|
||||
// place files you want to import through the `$lib` alias in this folder.
|
||||
// biome-ignore-all lint/suspicious/noEmptySource: temporary
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
import { type ClassValue, clsx } from "clsx";
|
||||
import { twMerge } from "tailwind-merge";
|
||||
|
||||
export function cn(...inputs: ClassValue[]) {
|
||||
return twMerge(clsx(inputs));
|
||||
}
|
||||
|
||||
export type WithoutChild<T> = T extends { child?: any } ? Omit<T, "child"> : T;
|
||||
export type WithoutChildren<T> = T extends { children?: any }
|
||||
? Omit<T, "children">
|
||||
: T;
|
||||
export type WithoutChildrenOrChild<T> = WithoutChildren<WithoutChild<T>>;
|
||||
export type WithElementRef<T, U extends HTMLElement = HTMLElement> = T & {
|
||||
ref?: U | null;
|
||||
};
|
||||
Reference in new issue
Block a user