From fc1c0a5b6243363cc5b1f12de83a7a0fc0941165 Mon Sep 17 00:00:00 2001 From: Bradley Shellnut Date: Tue, 14 Feb 2023 17:19:54 -0800 Subject: [PATCH] Fixing TS errors and adding a WALLABAG max pages for articles. --- src/routes/api.ts | 13 +++++++++---- src/routes/api/articles/+server.ts | 15 +++++---------- src/routes/articles/[page]/+page.server.ts | 9 ++++++++- src/routes/articles/[page]/+page.svelte | 2 +- 4 files changed, 23 insertions(+), 16 deletions(-) diff --git a/src/routes/api.ts b/src/routes/api.ts index f76040a..62d4729 100644 --- a/src/routes/api.ts +++ b/src/routes/api.ts @@ -4,7 +4,7 @@ import { WALLABAG_USERNAME, WALLABAG_PASSWORD, WALLABAG_URL, - WALLABAG_MAX_ARTICLES, + WALLABAG_MAX_PAGES, PAGE_SIZE } from '$env/static/private'; import intersect from 'just-intersect'; @@ -47,7 +47,12 @@ export async function fetchArticlesApi( tags: 'programming', content: 'metadata' }; - const entriesQueryParams = new URLSearchParams(pageQuery); + const entriesQueryParams = new URLSearchParams({ + ...pageQuery, + perPage: `${pageQuery.perPage}`, + since: `${pageQuery.since}`, + page: `${pageQuery.page}` + }); console.log(`Entries params: ${entriesQueryParams}`); if (lastFetched) { @@ -115,9 +120,9 @@ export async function fetchArticlesApi( return { articles, currentPage: page, - totalPages: pages, + totalPages: pages <= WALLABAG_MAX_PAGES ? pages : WALLABAG_MAX_PAGES, limit, - totalArticles: total, + totalArticles: total > limit * WALLABAG_MAX_PAGES ? limit * WALLABAG_MAX_PAGES : total, cacheControl }; } diff --git a/src/routes/api/articles/+server.ts b/src/routes/api/articles/+server.ts index 4b22943..572118c 100644 --- a/src/routes/api/articles/+server.ts +++ b/src/routes/api/articles/+server.ts @@ -1,9 +1,12 @@ -import { json } from '@sveltejs/kit'; +import { json, error } from '@sveltejs/kit'; import type { RequestHandler, RequestEvent } from './$types'; import { fetchArticlesApi } from '$root/routes/api'; export const GET: RequestHandler = async ({ url, setHeaders }: RequestEvent) => { try { + if (+url?.searchParams?.get('page') > WALLABAG_MAX_PAGES) { + throw new Error('Page does not exist'); + } const response = await fetchArticlesApi('get', `fetchArticles`, { page: url?.searchParams?.get('page') || '1' }); @@ -21,18 +24,10 @@ export const GET: RequestHandler = async ({ url, setHeaders }: RequestEvent) => } } - // const articlesResponse = response.articles; - // console.log(`Found articles ${articlesResponse?.articles?.length}`); - // const articles = []; - - // for (const article of articlesResponse) { - // const { tags, title, url, hashed_url, reading_time, preview_picture } = article; - // articles.push({ tags, title, url, hashed_url, reading_time, preview_picture }); - // } - return json(response); } } catch (error) { console.error(error); + throw error(error); } }; diff --git a/src/routes/articles/[page]/+page.server.ts b/src/routes/articles/[page]/+page.server.ts index 2440a62..57bb73c 100644 --- a/src/routes/articles/[page]/+page.server.ts +++ b/src/routes/articles/[page]/+page.server.ts @@ -1,5 +1,7 @@ +import { error } from '@sveltejs/kit'; import type { PageServerLoad } from './$types'; -import type { Article } from '$root/lib/types/article'; +import { WALLABAG_MAX_PAGES } from '$env/static/private'; +import type { Article } from '$lib/types/article'; export type ArticlePageLoad = { articles: Article[]; @@ -11,6 +13,11 @@ export type ArticlePageLoad = { export const load: PageServerLoad = async ({ fetch, params }) => { const { page } = params; + if (+page > WALLABAG_MAX_PAGES) { + throw error(404, { + message: 'Not found' + }); + } const resp = await fetch(`/api/articles?page=${page}`); const { articles, currentPage, totalPages, limit, totalArticles }: ArticlePageLoad = await resp.json(); diff --git a/src/routes/articles/[page]/+page.svelte b/src/routes/articles/[page]/+page.svelte index 10ee5ac..d8cac72 100644 --- a/src/routes/articles/[page]/+page.svelte +++ b/src/routes/articles/[page]/+page.svelte @@ -33,7 +33,7 @@ {article.title}