async function* pages<T>(path: string): AsyncGenerator<T> {
let cursor: string | null = null;
do {
const url = new URL(`https://inboxapp.com/api/v2${path}`);
url.searchParams.set("limit", "100");
if (cursor) url.searchParams.set("cursor", cursor);
const response = await fetch(url, {
headers: { Authorization: `Bearer ${process.env.INBOX_API_TOKEN}` },
});
if (!response.ok) {
const error = await response.json();
throw new Error(`${error.code}: ${error.message} (${error.requestId})`);
}
const page = await response.json();
yield* page.data;
cursor = page.nextCursor;
} while (cursor);
}
for await (const thread of pages<{ id: string }>("/threads?folder=all")) {
console.log(thread.id);
}