Files
indiekit-endpoint-youtube/lib/controllers/videos.js
Ricardo 2b8de8027b Initial commit: YouTube channel endpoint for Indiekit
Features:
- Display latest videos from any YouTube channel
- Live streaming status with animated badge
- Upcoming stream detection
- Admin dashboard with video grid
- Public JSON API for Eleventy integration
- Quota-efficient API usage (playlist method)
- Smart caching (5min videos, 1min live status)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-23 23:02:13 +01:00

50 lines
1.2 KiB
JavaScript

import { YouTubeClient } from "../youtube-client.js";
/**
* Videos controller
*/
export const videosController = {
/**
* Get latest videos (JSON API)
* @type {import("express").RequestHandler}
*/
async api(request, response) {
try {
const { youtubeConfig } = request.app.locals.application;
if (!youtubeConfig) {
return response.status(500).json({ error: "Not configured" });
}
const { apiKey, channelId, channelHandle, cacheTtl, limits } = youtubeConfig;
if (!apiKey || (!channelId && !channelHandle)) {
return response.status(500).json({ error: "Invalid configuration" });
}
const client = new YouTubeClient({
apiKey,
channelId,
channelHandle,
cacheTtl,
});
const maxResults = Math.min(
parseInt(request.query.limit, 10) || limits?.videos || 10,
50
);
const videos = await client.getLatestVideos(maxResults);
response.json({
videos,
count: videos.length,
cached: true,
});
} catch (error) {
console.error("[YouTube] Videos API error:", error);
response.status(500).json({ error: error.message });
}
},
};