This repository has been archived on 2023-10-27. You can view files and clone it, but cannot push or open issues or pull requests.
Files
scoresaber-reloaded/src/network/queues/twitch/api-queue.js

112 lines
2.6 KiB
JavaScript
Raw Normal View History

2023-10-17 23:38:18 +01:00
import { default as createQueue, PRIORITY } from "../http-queue";
import ssrConfig from "../../../ssr-config";
import { substituteVars } from "../../../utils/format";
2023-10-17 21:42:37 +01:00
2023-10-17 23:38:18 +01:00
const CLIENT_ID = "u0swxz56n4iumc634at1osoqdk31qt";
2023-10-17 21:42:37 +01:00
2023-10-17 23:38:18 +01:00
const TWITCH_AUTH_URL = "https://id.twitch.tv/oauth2";
const AUTHORIZATION_URL =
`${TWITCH_AUTH_URL}/authorize?client_id=${CLIENT_ID}&redirect_uri=${encodeURIComponent(
ssrConfig.domain + "/twitch",
)}&response_type=token` + "&scope=${scopes}&state=${state}";
const VALIDATE_URL = `${TWITCH_AUTH_URL}/validate`;
2023-10-17 21:42:37 +01:00
2023-10-17 23:38:18 +01:00
const TWITCH_API_URL = "https://api.twitch.tv/helix";
const PROFILE_URL = TWITCH_API_URL + "/users?login=${login}";
const VIDEOS_URL =
TWITCH_API_URL + "/videos?user_id=${userId}&type=${type}&first=100";
const STREAMS_URL = TWITCH_API_URL + "/streams?user_id=${userId}";
2023-10-17 21:42:37 +01:00
export default (options = {}) => {
const queue = createQueue(options);
2023-10-17 23:38:18 +01:00
const { fetchJson, fetchHtml, ...queueToReturn } = queue;
2023-10-17 21:42:37 +01:00
2023-10-17 23:38:18 +01:00
const fetchApi = (
2023-10-17 21:42:37 +01:00
url,
2023-10-17 23:38:18 +01:00
accessToken,
priority = PRIORITY.FG_LOW,
options = {},
) =>
fetchJson(
url,
{
...options,
headers: {
"Client-ID": CLIENT_ID,
Authorization: `Bearer ${accessToken}`,
},
},
priority,
);
2023-10-17 21:42:37 +01:00
2023-10-17 23:38:18 +01:00
const getAuthUrl = (state = "", scopes = "") =>
substituteVars(AUTHORIZATION_URL, {
state: encodeURIComponent(state),
scopes: encodeURIComponent(scopes),
});
2023-10-17 21:42:37 +01:00
2023-10-17 23:38:18 +01:00
const validateToken = async (
accessToken,
priority = PRIORITY.FG_LOW,
options = {},
) =>
fetchJson(
VALIDATE_URL,
{ ...options, headers: { Authorization: `OAuth ${accessToken}` } },
priority,
);
2023-10-17 21:42:37 +01:00
2023-10-17 23:38:18 +01:00
const profile = async (
accessToken,
login,
priority = PRIORITY.FG_LOW,
options = {},
) =>
fetchApi(
substituteVars(PROFILE_URL, { login: encodeURIComponent(login) }),
accessToken,
priority,
options,
);
2023-10-17 21:42:37 +01:00
2023-10-17 23:38:18 +01:00
const videos = async (
accessToken,
userId,
type = "archive",
priority = PRIORITY.FG_LOW,
options = {},
) =>
fetchApi(
substituteVars(VIDEOS_URL, {
userId: encodeURIComponent(userId),
type: encodeURIComponent(type),
}),
accessToken,
priority,
options,
);
2023-10-17 21:42:37 +01:00
2023-10-17 23:38:18 +01:00
const streams = async (
accessToken,
userId,
priority = PRIORITY.FG_LOW,
options = {},
) =>
fetchApi(
substituteVars(STREAMS_URL, { userId: encodeURIComponent(userId) }),
accessToken,
priority,
options,
);
2023-10-17 21:42:37 +01:00
return {
getAuthUrl,
validateToken,
profile,
videos,
streams,
...queueToReturn,
2023-10-17 23:38:18 +01:00
};
};