Add helpers and types for BFF API

This commit is contained in:
Hannah dagemark 2026-08-14 22:37:42 +02:00
commit 9dbd93a430
5 changed files with 89 additions and 0 deletions

16
lib/api/cmFetch.ts Normal file
View file

@ -0,0 +1,16 @@
import { RequestInit } from "next/dist/server/web/spec-extension/request";
const FRONTEND_URL = process.env.FRONTEND_URL;
export async function cmFetch<T>(
bffUrl: string | URL,
options?: RequestInit,
): Promise<T> {
const response = await fetch(`${FRONTEND_URL}/${bffUrl}`, options);
if (!response.ok) {
throw new Error("Request failed");
}
return response.json();
}

View file

View file

@ -0,0 +1,26 @@
import { cmFetch } from "../cmFetch";
import { Service } from "../types";
export async function getServices() {
return cmFetch<Service[]>(`/api/services`);
}
export async function getService(id: number) {
return cmFetch<Service>(`/api/services/${id}`);
}
export async function createService(service: Service) {
return cmFetch<Service>(`/api/services`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(service),
});
}
export async function deleteService(id: number) {
return cmFetch<Service>(`/api/services/${id}`, {
method: "DELETE",
});
}

18
lib/api/helpers/users.ts Normal file
View file

@ -0,0 +1,18 @@
export async function getUsers() {
const response = await fetch("/api/users");
if (!response.ok) {
throw new Error(`Response status: ${response.status}`);
}
return response.json();
}
export async function getUser(id: number) {
const response = await fetch(`/api/users/${id}`);
if (!response.ok) {
throw new Error(`Response status: ${response.status}`);
}
return response.json();
}

29
lib/api/types.ts Normal file
View file

@ -0,0 +1,29 @@
export interface User {
id: number | null;
username: string;
authority: number;
}
export interface Service {
id: number | null;
title: string;
description: string | null;
priority: number;
link: string;
}
export interface Project {
id: number | null;
parent_id: number | null;
title: string;
content: string | null;
}