mirror of
https://github.com/BradNut/personal-website-sveltekit
synced 2025-09-08 23:20:18 +00:00
Fixing TS errors and adding a WALLABAG max pages for articles.
This commit is contained in:
parent
a1e6b637f5
commit
fc1c0a5b62
4 changed files with 23 additions and 16 deletions
|
|
@ -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
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
};
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
|
|
|
|||
|
|
@ -33,7 +33,7 @@
|
|||
<a
|
||||
target="_blank"
|
||||
aria-label={`Link to ${article.title}`}
|
||||
href={article.url}
|
||||
href={article.url.toString()}
|
||||
rel="noreferrer"
|
||||
>
|
||||
{article.title}
|
||||
|
|
|
|||
Loading…
Reference in a new issue