From 75345718be70412acd12410ea0b6875b48d25bda Mon Sep 17 00:00:00 2001 From: MillenniumEarl Date: Thu, 4 Mar 2021 11:41:50 +0100 Subject: [PATCH] Clean code --- src/scripts/fetch-data/fetch-latest.ts | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/src/scripts/fetch-data/fetch-latest.ts b/src/scripts/fetch-data/fetch-latest.ts index c4b2626..54d09b7 100644 --- a/src/scripts/fetch-data/fetch-latest.ts +++ b/src/scripts/fetch-data/fetch-latest.ts @@ -17,31 +17,34 @@ import { urls } from "../constants/url.js"; */ export default async function fetchLatestHandiworkURLs(query: LatestSearchQuery, limit: number = 30): Promise { // Local variables + const shallowQuery: LatestSearchQuery = Object.assign(new LatestSearchQuery(), query); const resultURLs = []; let fetchedResults = 0; - let page = 1; let noMorePages = false; do { // Fetch the response (application/json) - const response = await query.execute(); + const response = await shallowQuery.execute(); // Save the URLs if (response.isSuccess()) { - //@ts-ignore - for (const result of response.value.data.msg.data) { + // In-loop variables + const data: [{ thread_id: number}] = response.value.data.msg.data; + const totalPages: number = response.value.data.msg.pagination.total; + + data.map((e, idx) => { if (fetchedResults < limit) { - const gameURL = new URL(result.thread_id, urls.F95_THREADS).href; + + const gameURL = new URL(e.thread_id.toString(), urls.F95_THREADS).href; resultURLs.push(gameURL); + fetchedResults += 1; } - } + }); // Increment page and check for it's existence - page += 1; - - //@ts-ignore - if (page > response.value.data.msg.pagination.total) noMorePages = true; + shallowQuery.page += 1; + noMorePages = shallowQuery.page > totalPages; } else throw response.value; } while (fetchedResults < limit && !noMorePages);