Files
canina/frontend/application/lib/services/videoService.ts
T
parsa aghaei fa1a35c835
Deploy Canina / deploy (push) Successful in 1m34s
E2E Playwright Tests / Run Full E2E & Security Suites (push) Failing after 6s
feat(media): implement nextjs media proxy API routes (/api/uploads & /api/media) with server-side caching and streaming
2026-08-29 12:13:16 +03:30

65 lines
1.8 KiB
TypeScript

import api from './api';
import { getMediaUrl } from '../media';
export interface Video {
id: string;
title: string;
doctor: string;
duration: string;
thumbnail: string;
videoUrl: string;
description: string;
viewsCount?: number;
isFeatured?: boolean;
}
function normalizeVideo(v: any): Video {
return {
id: String(v.id || ''),
title: String(v.title || ''),
doctor: String(v.doctor || 'کادر علمی کنینا'),
duration: String(v.duration || '۳:۰۰'),
thumbnail: getMediaUrl(v.thumbnail || v.coverUrl || '/assets/images/hero-section-image.png'),
videoUrl: getMediaUrl(v.videoUrl || ''),
description: String(v.description || ''),
viewsCount: Number(v.viewsCount) || 0,
isFeatured: Boolean(v.isFeatured),
};
}
export const videoService = {
getVideos: async (params?: { featured?: boolean; limit?: number; search?: string }): Promise<Video[]> => {
try {
const res = await api.get('/videos', { params });
const raw = res.data;
let items: any[] = [];
if (Array.isArray(raw?.videos)) items = raw.videos;
else if (Array.isArray(raw?.data?.videos)) items = raw.data.videos;
else if (Array.isArray(raw?.data)) items = raw.data;
else if (Array.isArray(raw)) items = raw;
return items.map(normalizeVideo);
} catch (error) {
console.error('Failed to fetch videos from API:', error);
return [];
}
},
getVideoById: async (id: string) => {
try {
const res = await api.get(`/videos/${id}`);
return res.data;
} catch (error) {
console.error('Failed to fetch video details:', error);
return null;
}
},
incrementViews: async (id: string) => {
try {
await api.patch(`/videos/${id}/view`);
} catch (error) {
console.warn('Failed to increment video views:', error);
}
}
};