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_USERNAME,
|
||||||
WALLABAG_PASSWORD,
|
WALLABAG_PASSWORD,
|
||||||
WALLABAG_URL,
|
WALLABAG_URL,
|
||||||
WALLABAG_MAX_ARTICLES,
|
WALLABAG_MAX_PAGES,
|
||||||
PAGE_SIZE
|
PAGE_SIZE
|
||||||
} from '$env/static/private';
|
} from '$env/static/private';
|
||||||
import intersect from 'just-intersect';
|
import intersect from 'just-intersect';
|
||||||
|
|
@ -47,7 +47,12 @@ export async function fetchArticlesApi(
|
||||||
tags: 'programming',
|
tags: 'programming',
|
||||||
content: 'metadata'
|
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}`);
|
console.log(`Entries params: ${entriesQueryParams}`);
|
||||||
|
|
||||||
if (lastFetched) {
|
if (lastFetched) {
|
||||||
|
|
@ -115,9 +120,9 @@ export async function fetchArticlesApi(
|
||||||
return {
|
return {
|
||||||
articles,
|
articles,
|
||||||
currentPage: page,
|
currentPage: page,
|
||||||
totalPages: pages,
|
totalPages: pages <= WALLABAG_MAX_PAGES ? pages : WALLABAG_MAX_PAGES,
|
||||||
limit,
|
limit,
|
||||||
totalArticles: total,
|
totalArticles: total > limit * WALLABAG_MAX_PAGES ? limit * WALLABAG_MAX_PAGES : total,
|
||||||
cacheControl
|
cacheControl
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,12 @@
|
||||||
import { json } from '@sveltejs/kit';
|
import { json, error } from '@sveltejs/kit';
|
||||||
import type { RequestHandler, RequestEvent } from './$types';
|
import type { RequestHandler, RequestEvent } from './$types';
|
||||||
import { fetchArticlesApi } from '$root/routes/api';
|
import { fetchArticlesApi } from '$root/routes/api';
|
||||||
|
|
||||||
export const GET: RequestHandler = async ({ url, setHeaders }: RequestEvent) => {
|
export const GET: RequestHandler = async ({ url, setHeaders }: RequestEvent) => {
|
||||||
try {
|
try {
|
||||||
|
if (+url?.searchParams?.get('page') > WALLABAG_MAX_PAGES) {
|
||||||
|
throw new Error('Page does not exist');
|
||||||
|
}
|
||||||
const response = await fetchArticlesApi('get', `fetchArticles`, {
|
const response = await fetchArticlesApi('get', `fetchArticles`, {
|
||||||
page: url?.searchParams?.get('page') || '1'
|
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);
|
return json(response);
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
|
throw error(error);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,7 @@
|
||||||
|
import { error } from '@sveltejs/kit';
|
||||||
import type { PageServerLoad } from './$types';
|
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 = {
|
export type ArticlePageLoad = {
|
||||||
articles: Article[];
|
articles: Article[];
|
||||||
|
|
@ -11,6 +13,11 @@ export type ArticlePageLoad = {
|
||||||
|
|
||||||
export const load: PageServerLoad = async ({ fetch, params }) => {
|
export const load: PageServerLoad = async ({ fetch, params }) => {
|
||||||
const { page } = params;
|
const { page } = params;
|
||||||
|
if (+page > WALLABAG_MAX_PAGES) {
|
||||||
|
throw error(404, {
|
||||||
|
message: 'Not found'
|
||||||
|
});
|
||||||
|
}
|
||||||
const resp = await fetch(`/api/articles?page=${page}`);
|
const resp = await fetch(`/api/articles?page=${page}`);
|
||||||
const { articles, currentPage, totalPages, limit, totalArticles }: ArticlePageLoad =
|
const { articles, currentPage, totalPages, limit, totalArticles }: ArticlePageLoad =
|
||||||
await resp.json();
|
await resp.json();
|
||||||
|
|
|
||||||
|
|
@ -33,7 +33,7 @@
|
||||||
<a
|
<a
|
||||||
target="_blank"
|
target="_blank"
|
||||||
aria-label={`Link to ${article.title}`}
|
aria-label={`Link to ${article.title}`}
|
||||||
href={article.url}
|
href={article.url.toString()}
|
||||||
rel="noreferrer"
|
rel="noreferrer"
|
||||||
>
|
>
|
||||||
{article.title}
|
{article.title}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue