owncast/web/pages/utils/apis.ts

59 lines
1.6 KiB
TypeScript
Raw Normal View History

2020-10-08 09:09:42 +03:00
/* eslint-disable prefer-destructuring */
2020-10-08 10:17:40 +03:00
const ADMIN_USERNAME = process.env.NEXT_PUBLIC_ADMIN_USERNAME;
const ADMIN_STREAMKEY = process.env.NEXT_PUBLIC_ADMIN_STREAMKEY;
2020-10-08 09:09:42 +03:00
const NEXT_PUBLIC_API_HOST = process.env.NEXT_PUBLIC_API_HOST;
2020-10-04 06:59:25 +03:00
2020-10-08 09:09:42 +03:00
const API_LOCATION = `${NEXT_PUBLIC_API_HOST}api/admin/`;
2020-10-04 06:59:25 +03:00
2020-10-08 10:17:40 +03:00
export const FETCH_INTERVAL = 15000;
2020-10-04 06:59:25 +03:00
// Current inbound broadcaster info
export const BROADCASTER = `${API_LOCATION}broadcaster`;
// Disconnect inbound stream
export const DISCONNECT = `${API_LOCATION}disconnect`;
// Change the current streaming key in memory
export const STREAMKEY_CHANGE = `${API_LOCATION}changekey`;
// Current server config
export const SERVER_CONFIG = `${API_LOCATION}serverconfig`;
// Get viewer count over time
export const VIEWERS_OVER_TIME = `${API_LOCATION}viewersOverTime`;
2020-10-08 10:26:24 +03:00
// Get currently connected clients
export const CONNECTED_CLIENTS = `${API_LOCATION}clients`;
2020-10-04 06:59:25 +03:00
// Get hardware stats
export const HARDWARE_STATS = `${API_LOCATION}hardwarestats`;
2020-10-26 04:57:23 +03:00
// Current Stream status.
// This is literally the same as /api/status except it supports
// auth.
export const STREAM_STATUS = `${API_LOCATION}status`;
2020-10-04 06:59:25 +03:00
export async function fetchData(url) {
const encoded = btoa(`${ADMIN_USERNAME}:${ADMIN_STREAMKEY}`);
2020-10-04 09:07:37 +03:00
try {
const response = await fetch(url, {
headers: {
'Authorization': `Basic ${encoded}`,
},
mode: 'cors',
credentials: 'include',
});
if (!response.ok) {
const message = `An error has occured: ${response.status}`;
throw new Error(message);
}
const json = await response.json();
return json;
} catch (error) {
console.log(error)
}
2020-10-08 09:09:42 +03:00
return {};
2020-10-04 06:59:25 +03:00
}