2024-01-20 16:16:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
import { IVod } from './vods'
|
|
|
|
import { strapiUrl, siteUrl } from './constants';
|
|
|
|
import qs from 'qs';
|
2024-07-15 16:07:04 +00:00
|
|
|
import { IMeta, IVtuber, IVtubersResponse } from '@futureporn/types';
|
2024-01-20 16:16:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
const fetchVtubersOptions = {
|
|
|
|
next: {
|
|
|
|
tags: ['vtubers']
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function getUrl(slug: string): string {
|
|
|
|
return `${siteUrl}/vt/${slug}`
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export async function getVtuberBySlug(slug: string): Promise<IVtuber|null> {
|
|
|
|
const query = qs.stringify(
|
|
|
|
{
|
|
|
|
filters: {
|
|
|
|
slug: {
|
|
|
|
$eq: slug
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
const res = await fetch(`${strapiUrl}/api/vtubers?${query}`);
|
|
|
|
if (!res.ok) {
|
|
|
|
console.error(`error inside getVtuberBySlug-- ${res.statusText}`);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
const vtuber = await res.json();
|
|
|
|
return vtuber.data[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getVtuberById(id: number): Promise<IVtuber|null> {
|
|
|
|
const res = await fetch(`${strapiUrl}/api/vtubers?filters[id][$eq]=${id}`);
|
|
|
|
if (!res.ok) {
|
|
|
|
console.error(`error inside getVtuberById-- ${res.statusText}`);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
const vtuber = await res.json();
|
|
|
|
return vtuber
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getVtubers(): Promise<IVtubersResponse|null> {
|
|
|
|
const res = await fetch(`${strapiUrl}/api/vtubers`);
|
|
|
|
if (!res.ok) {
|
|
|
|
console.error(`error inside getVtubers-- ${res.statusText}`);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
const vtubers = await res.json();
|
|
|
|
return vtubers;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getAllVtubers(): Promise<IVtuber[] | null> {
|
|
|
|
const pageSize = 100;
|
|
|
|
|
|
|
|
const allVtubers: IVtuber[] = [];
|
|
|
|
let currentPage = 1;
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
const query = qs.stringify({
|
|
|
|
pagination: {
|
|
|
|
pageSize,
|
|
|
|
page: currentPage,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
try {
|
2024-02-27 19:53:57 +00:00
|
|
|
console.log(`Getting ${strapiUrl}/api/vtubers page=${currentPage}`);
|
2024-01-20 16:16:14 +00:00
|
|
|
const response = await fetch(`${strapiUrl}/api/vtubers?${query}`, fetchVtubersOptions);
|
|
|
|
|
|
|
|
if (!response.ok) {
|
|
|
|
// Handle non-successful response (e.g., HTTP error)
|
|
|
|
throw new Error(`HTTP error! Status: ${response.status}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
const responseData = await response.json();
|
|
|
|
|
|
|
|
if (!responseData.data || responseData.data.length === 0) {
|
|
|
|
// No more data to fetch
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
allVtubers.push(...responseData.data);
|
|
|
|
currentPage++;
|
|
|
|
} catch (error) {
|
|
|
|
// Handle fetch error
|
|
|
|
if (error instanceof Error) {
|
|
|
|
console.error('Error fetching data:', error.message);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return allVtubers;
|
|
|
|
}
|