96 lines
3.0 KiB
TypeScript
96 lines
3.0 KiB
TypeScript
import PocketBase, { RecordModel } from "pocketbase";
|
|
|
|
const isServer = typeof window === "undefined";
|
|
|
|
const publicUrl =
|
|
process.env.NEXT_PUBLIC_PB_URL || "https://netrunner-vpn.com/cms-api";
|
|
const internalUrl = process.env.PB_INTERNAL_URL; // Не ставим дефолт здесь
|
|
|
|
export const PB_URL = isServer ? internalUrl || publicUrl : publicUrl;
|
|
|
|
export const pb = new PocketBase(PB_URL);
|
|
|
|
// Отключаем автоотмену на сервере, чтобы избежать AbortError при SSR/ISR
|
|
if (isServer) {
|
|
pb.autoCancellation(false);
|
|
}
|
|
|
|
if (typeof window !== "undefined") {
|
|
// Фильтруем ошибки абортов (CANCELLED), которые генерит PocketBase
|
|
const originalError = console.error;
|
|
console.error = (...args) => {
|
|
if (args[0]?.message?.includes("The request was autocancelled")) return;
|
|
originalError.apply(console, args);
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Базовый интерфейс для записей PocketBase.
|
|
* Наследуемся от RecordModel, чтобы иметь системные поля (id, created, updated, и т.д.)
|
|
*/
|
|
export interface BaseRecord extends RecordModel {
|
|
id: string;
|
|
collectionId: string;
|
|
collectionName: string;
|
|
created: string;
|
|
updated: string;
|
|
}
|
|
|
|
/**
|
|
* Типизация для постов блога
|
|
*/
|
|
export interface PBPost extends BaseRecord {
|
|
title: string;
|
|
slug: string;
|
|
excerpt: string;
|
|
content: string;
|
|
image: string; // имя файла
|
|
lang: "ru" | "en";
|
|
published: boolean;
|
|
}
|
|
|
|
/**
|
|
* Типизация для FAQ
|
|
*/
|
|
export interface PBFaq extends BaseRecord {
|
|
question: string;
|
|
answer: string;
|
|
order: number;
|
|
lang: string;
|
|
}
|
|
|
|
const FILE_BASE_URL =
|
|
process.env.NEXT_PUBLIC_PB_URL || "https://netrunner-vpn.com/cms-api";
|
|
|
|
/**
|
|
* Функция для генерации ссылки на картинку из PB
|
|
* @param record - Объект записи (должен содержать id и collectionId)
|
|
* @param filename - Имя файла из поля записи
|
|
*/
|
|
export function getPbImage(
|
|
record: Pick<BaseRecord, "id" | "collectionId">,
|
|
filename: string | undefined,
|
|
): string {
|
|
if (!filename) return "";
|
|
return `${FILE_BASE_URL}/api/files/${record.collectionId}/${record.id}/${filename}`;
|
|
}
|
|
// lib/pb.ts (добавь в конец файла)
|
|
|
|
export interface PBDownload extends BaseRecord {
|
|
name: string; // Имя (напр. "Windows 11")
|
|
platformId: "windows" | "android" | "linux" | "macos" | "ios"; // Строгий ID для иконок
|
|
version: string; // Строка версии
|
|
status: "active" | "dev"; // Статус разработки
|
|
file: string; // Сам бинарник (файл)
|
|
storeLink: string; // URL на магазин или github
|
|
order: number; // Для сортировки (1, 2, 3...)
|
|
}
|
|
|
|
export function getPbFileUrl(
|
|
record: Pick<BaseRecord, "id" | "collectionId">,
|
|
filename: string | undefined,
|
|
): string {
|
|
if (!filename) return "";
|
|
return `${FILE_BASE_URL}/api/files/${record.collectionId}/${record.id}/${filename}`;
|
|
}
|