diff --git a/lib/api/cmFetch.ts b/lib/api/cmFetch.ts new file mode 100644 index 0000000..5e38e74 --- /dev/null +++ b/lib/api/cmFetch.ts @@ -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( + bffUrl: string | URL, + options?: RequestInit, +): Promise { + const response = await fetch(`${FRONTEND_URL}/${bffUrl}`, options); + + if (!response.ok) { + throw new Error("Request failed"); + } + + return response.json(); +} diff --git a/lib/api/helpers/projects.ts b/lib/api/helpers/projects.ts new file mode 100644 index 0000000..e69de29 diff --git a/lib/api/helpers/services.ts b/lib/api/helpers/services.ts new file mode 100644 index 0000000..e718357 --- /dev/null +++ b/lib/api/helpers/services.ts @@ -0,0 +1,26 @@ +import { cmFetch } from "../cmFetch"; +import { Service } from "../types"; + +export async function getServices() { + return cmFetch(`/api/services`); +} + +export async function getService(id: number) { + return cmFetch(`/api/services/${id}`); +} + +export async function createService(service: Service) { + return cmFetch(`/api/services`, { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify(service), + }); +} + +export async function deleteService(id: number) { + return cmFetch(`/api/services/${id}`, { + method: "DELETE", + }); +} diff --git a/lib/api/helpers/users.ts b/lib/api/helpers/users.ts new file mode 100644 index 0000000..3665dd1 --- /dev/null +++ b/lib/api/helpers/users.ts @@ -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(); +} diff --git a/lib/api/types.ts b/lib/api/types.ts new file mode 100644 index 0000000..31589ca --- /dev/null +++ b/lib/api/types.ts @@ -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; +}