36 lines
1.2 KiB
TypeScript
36 lines
1.2 KiB
TypeScript
/**
|
|
* Serves the titles and lengths of a YouTube playlist's entries, which the
|
|
* global player lists beside the embed. On any failure it shows the embed
|
|
* without a tracklist.
|
|
*
|
|
* - `GET /api/youtube/playlist?list=` — public. Answers the playlist's
|
|
* tracklist.
|
|
*
|
|
* @module
|
|
*/
|
|
|
|
import { Router } from "@oak/oak";
|
|
import type { APIResponse } from "../../shared/api.ts";
|
|
import { resolveYouTubePlaylist } from "../remote/youtube-playlist.ts";
|
|
import type { YouTubePlaylist } from "../../shared/player.ts";
|
|
|
|
/**
|
|
* Mounted by `api/main.ts` behind the error, Open Graph and CORS middleware.
|
|
* An `APIException` thrown by a handler becomes the JSON error response there.
|
|
*/
|
|
const youtubeRouter: Router = new Router();
|
|
|
|
// Public, like /api/bandcamp/tracks: it only reads a public page.
|
|
youtubeRouter.get("/api/youtube/playlist", async (ctx) => {
|
|
const listId = ctx.request.url.searchParams.get("list") ?? "";
|
|
const responseBody: APIResponse<YouTubePlaylist> = {
|
|
success: true,
|
|
data: await resolveYouTubePlaylist(listId),
|
|
};
|
|
|
|
ctx.response.headers.set("Cache-Control", "public, max-age=3600");
|
|
ctx.response.body = responseBody;
|
|
});
|
|
|
|
export default youtubeRouter;
|