import { user } from './user'; export interface Thread { id: number; title: string; createdAt: number; } export interface ThreadPage { data: { Page: { threads: Thread[]; pageInfo: { hasNextPage: boolean; currentPage: number; }; }; }; } const threadPage = async (page: number, userId: number): Promise => await ( await fetch('https://graphql.anilist.co', { method: 'POST', headers: { 'Content-Type': 'application/json', Accept: 'application/json' }, body: JSON.stringify({ query: `{ Page(perPage: 50, page: ${page}) { threads(userId: ${userId}) { id title createdAt } pageInfo { hasNextPage } } }` }) }) ).json(); export const threads = async (username: string): Promise => { const allThreads = []; const userId = (await user(username)).id; let page = 1; let currentPage = await threadPage(page, userId); for (const thread of currentPage.data.Page.threads) { allThreads.push(thread); } while (currentPage.data.Page.pageInfo.hasNextPage) { page += 1; currentPage = await threadPage(page, userId); for (const thread of currentPage.data.Page.threads) { allThreads.push(thread); } } return allThreads; };