Full push for initial version of NullTranslationGroup website.
This commit is contained in:
84
src/lib/api.tsx
Normal file
84
src/lib/api.tsx
Normal file
@@ -0,0 +1,84 @@
|
||||
import { Book, Chapter, Editor } from "./types";
|
||||
|
||||
const API_URL = process.env.NEXT_PUBLIC_API_URL as string;
|
||||
const API_TOKEN = process.env.STRAPI_API_TOKEN as string;
|
||||
|
||||
/**
|
||||
* Centralized API fetch function with TypeScript support.
|
||||
* Handles GET, POST, PUT, DELETE methods and includes headers by default.
|
||||
*/
|
||||
export async function fetchFromAPI<T>(
|
||||
endpoint: string,
|
||||
options: RequestInit = {}
|
||||
): Promise<T> {
|
||||
const url = `${API_URL}${endpoint}`;
|
||||
|
||||
const headers: HeadersInit = {
|
||||
Authorization: `Bearer ${API_TOKEN}`,
|
||||
"Content-Type": "application/json",
|
||||
};
|
||||
|
||||
const config: RequestInit = {
|
||||
method: "GET", // Default method is GET
|
||||
headers,
|
||||
...options,
|
||||
};
|
||||
|
||||
try {
|
||||
const response = await fetch(url, config);
|
||||
if (!response.ok) {
|
||||
const errorData = await response.json();
|
||||
console.error(`Error fetching ${url}:`, errorData);
|
||||
throw new Error(errorData.message || `API fetch error (status: ${response.status})`);
|
||||
}
|
||||
const responseJson = await response.json();
|
||||
console.log(responseJson)
|
||||
return responseJson;
|
||||
} catch (error) {
|
||||
console.error("Fetch error:", error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches all books from the API.
|
||||
* Populates optional fields like Chapters or Editors based on requirements.
|
||||
*/
|
||||
export async function fetchBooks(): Promise<Book[]> {
|
||||
const data = await fetchFromAPI<{ data: Book[] }>("/api/books?populate=*");
|
||||
return data.data;
|
||||
}
|
||||
|
||||
export async function fetchBookChapterLinks(bookId: string): Promise<Book> {
|
||||
const currentDate = new Date().toISOString().split("T")[0];
|
||||
const data = await fetchFromAPI<{ data: Book }>(`/api/books/${bookId}?populate[chapters][filters][ReleaseDate][$lte]=${currentDate}`);
|
||||
return data.data
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches a specific book by ID with its chapters.
|
||||
* Filters chapters by release date to include only valid ones.
|
||||
*/
|
||||
export async function fetchBookById(bookId: string): Promise<Book> {
|
||||
const currentDate = new Date().toISOString().split("T")[0];
|
||||
const data = await fetchFromAPI<{ data: Book }>(
|
||||
`/api/books/${bookId}?populate[chapters][filters][ReleaseDate][$lte]=${currentDate}`
|
||||
);
|
||||
return data.data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches a specific chapter by ID.
|
||||
*/
|
||||
export async function fetchChapterById(chapterId: string): Promise<Chapter> {
|
||||
const data = await fetchFromAPI<{ data: Chapter }>(`/api/chapters/${chapterId}?populate[book][fields]=documentId`);
|
||||
return data.data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches all editors.
|
||||
*/
|
||||
export async function fetchEditors(): Promise<Editor[]> {
|
||||
const data = await fetchFromAPI<{ data: Editor[] }>("/api/editors");
|
||||
return data.data;
|
||||
}
|
||||
65
src/lib/types.tsx
Normal file
65
src/lib/types.tsx
Normal file
@@ -0,0 +1,65 @@
|
||||
// src/lib/types.ts
|
||||
export interface Chapter {
|
||||
id: number;
|
||||
documentId: string;
|
||||
Name: string;
|
||||
Chapter_Number: number;
|
||||
ReleaseDate: string;
|
||||
Content: string;
|
||||
book?: Book;
|
||||
}
|
||||
|
||||
export interface Editor {
|
||||
id: number;
|
||||
name: string;
|
||||
email: string;
|
||||
books: Book[];
|
||||
}
|
||||
|
||||
export interface Glossary {
|
||||
id: number;
|
||||
name: string;
|
||||
entries: string[];
|
||||
}
|
||||
|
||||
export interface Media {
|
||||
id: number;
|
||||
name: string;
|
||||
alternativeText?: string; // Optional field
|
||||
caption?: string; // Optional field
|
||||
width?: number;
|
||||
height?: number;
|
||||
formats?: {
|
||||
thumbnail?: { url: string; width: number; height: number };
|
||||
small?: { url: string; width: number; height: number };
|
||||
medium?: { url: string; width: number; height: number };
|
||||
large?: { url: string; width: number; height: number };
|
||||
};
|
||||
hash: string;
|
||||
ext: string;
|
||||
mime: string;
|
||||
size: number;
|
||||
url: string; // The URL to access the media
|
||||
previewUrl?: string; // Optional preview URL
|
||||
provider: string;
|
||||
provider_metadata?: string; // Metadata specific to the provider
|
||||
createdAt: string;
|
||||
updatedAt: string;
|
||||
}
|
||||
|
||||
export interface Book {
|
||||
id: number;
|
||||
documentId: string;
|
||||
Name: string;
|
||||
ReleaseDate: string;
|
||||
chapters: Chapter[];
|
||||
Cover: Media | null;
|
||||
Author: string;
|
||||
Complete: boolean;
|
||||
editors: Editor[];
|
||||
RawName: string;
|
||||
RawAuthor: string;
|
||||
glossary: Glossary;
|
||||
Description: string;
|
||||
}
|
||||
|
||||
7
src/lib/utils.tsx
Normal file
7
src/lib/utils.tsx
Normal file
@@ -0,0 +1,7 @@
|
||||
export function formatDateToMonthDayYear(date: Date): string {
|
||||
return date.toLocaleDateString("en-US", {
|
||||
month: "long",
|
||||
day: "numeric",
|
||||
year: "numeric",
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user