Skip to content

Commit

Permalink
no need to separate these
Browse files Browse the repository at this point in the history
  • Loading branch information
markflorkowski committed Mar 7, 2024
1 parent 6b7790d commit a8d9a78
Showing 1 changed file with 28 additions and 39 deletions.
67 changes: 28 additions & 39 deletions src/utils/twitch-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,36 +55,6 @@ const getValidTokenForCreator = async (creatorName: string) => {
return await getTwitchTokenFromClerk(creatorFoundInClerk.id);
};

const fetchPaginatedMarkers = async (
url: string,
init?: RequestInit | undefined
): Promise<any[]> => {
type ApiRes = {
data: any[];
pagination: { cursor: string };
};

let {
data,
pagination: { cursor },
} = (await fetch(url, init).then((response) => response.json())) as ApiRes;

const extractMarkers = (result: ApiRes) =>
result.data?.[0]?.videos?.[0]?.markers ?? [];

let markers = extractMarkers({ data, pagination: { cursor } });

while (cursor) {
const next = (await fetch(`${url}&after=${cursor}`, init).then((response) =>
response.json()
)) as ApiRes;
markers = [...markers, ...extractMarkers(next)];
cursor = next.pagination.cursor;
}

return markers;
};

export const getVodWithMarkers = async (vodId: string, token: string) => {
const vodResponse = await fetch(
`https://api.twitch.tv/helix/videos?id=${vodId}`,
Expand All @@ -106,18 +76,37 @@ export const getVodWithMarkers = async (vodId: string, token: string) => {

const tokenForMarkers = await getValidTokenForCreator(creatorName);

const markersData = await fetchPaginatedMarkers(
const opts = {
method: "GET",
headers: generateTwitchRequestHeaders(tokenForMarkers),
next: { revalidate: 60 },
};

let {
data,
pagination: { cursor },
} = await fetch(
`https://api.twitch.tv/helix/streams/markers?video_id=${vodId}&first=100`,
{
method: "GET",
headers: generateTwitchRequestHeaders(tokenForMarkers),
next: { revalidate: 60 },
}
);
opts
).then((response) => response.json());

const extractMarkers = (result: { data: any }) =>
result.data?.[0]?.videos?.[0]?.markers ?? [];

let markers = extractMarkers({ data });

while (cursor) {
const next = await fetch(
`https://api.twitch.tv/helix/streams/markers?video_id=${vodId}&first=100&after=${cursor}`,
opts
).then((response) => response.json());
markers = [...markers, ...extractMarkers(next)];
cursor = next.pagination.cursor;
}

console.log("MARKERS", markersData);
console.log("MARKERS", markers);

return { ...vodData?.data?.[0], markers: markersData } as VOD;
return { ...vodData?.data?.[0], markers } as VOD;
};

export const getTwitchTokenFromClerk = async (clerkUserId: string) => {
Expand Down

0 comments on commit a8d9a78

Please sign in to comment.