import fetch from "node-fetch"; import assert from "assert"; import slugify from "slugify"; const base_host = "http://127.0.0.1:5001"; export const get = async (url) => { assert(url && url[0] === "/", `url must start with / you have ${url}`); const target = `${base_host}${url}`; const res = await fetch(target); if(res.status === 200) { return await res.json(); } else { console.error(`!!! ERROR: Failed getting ${target}`, res.status, await res.text()); return []; } } export const listing = async () => { const listing = await get("/api/livestream"); return listing.sort((a,b) => b.id - a.id); } export const slug = (stream, with_id=true) => { if(with_id) { return slugify(`${stream.id}-${stream.title}`, { lower: true, strict: true, trim: true}); } else { return slugify(`${stream.title}`, { lower: true, strict: true, trim: true}); } } export const stream = async (id) => { const stream = await get(`/api/livestream?livestream_id=${id}`); stream.slug = slug(stream); return stream; } export const load_paths = async() => { const streams = await listing(); const result = []; for(let i of streams) { // stream.slug is setup in rendered/pages/live/index.js result.push(await stream(i.id)); } return result; } export default { listing, stream, load_paths, get, slug }