update to pt-api v2

This commit is contained in:
Niki Wix Skaarup 2025-04-06 02:41:18 +02:00
parent 0d06e26d89
commit 350b01800a
Signed by: nikiskaarup
GPG key ID: FC2F1B116F6E788C
3 changed files with 23 additions and 32 deletions

View file

@ -83,7 +83,7 @@
> >
{@render done(entry.finished)} {@render done(entry.finished)}
<div class="hidden max-w-24 px-1 py-1 text-xs text-gray-200 @xl:block"> <div class="hidden max-w-24 px-1 py-1 text-xs text-gray-200 @xl:block">
{formatter.format(entry.updated_at)} {formatter.format(Date.parse(entry.updated_at))}
</div> </div>
</div> </div>
{/each} {/each}

View file

@ -1,16 +1,15 @@
import { env } from 'bun'; import { env } from 'bun';
type SelectBookmark = { export type SelectEntry = {
id: number; id: number;
name: string; name: string;
href: string; href: string;
finished: boolean; finished: boolean;
created_at: number; updated_at: string;
updated_at: number;
}; };
const baseUrl = env.API_SERVICE_URL; const baseUrl = env.API_SERVICE_URL;
const bookmarksUrl = `${baseUrl}/bookmarks`; const entriesUrl = `${baseUrl}/api/entries`;
const noBodyHeaders = new Headers({ const noBodyHeaders = new Headers({
Authorization: `Bearer ${env.BEARER_TOKEN}`, Authorization: `Bearer ${env.BEARER_TOKEN}`,
@ -21,50 +20,50 @@ const bodyHeaders = new Headers({
Authorization: `Bearer ${env.BEARER_TOKEN}`, Authorization: `Bearer ${env.BEARER_TOKEN}`,
}); });
async function query(): Promise<Array<SelectBookmark>> { async function query(): Promise<Array<SelectEntry>> {
try { try {
const response = await fetch(bookmarksUrl, { const response = await fetch(entriesUrl, {
method: 'GET', method: 'GET',
signal: AbortSignal.timeout(50000), signal: AbortSignal.timeout(50000),
headers: noBodyHeaders, headers: noBodyHeaders,
}); });
if (!response.ok) { if (!response.ok) {
throw new Error('Failed to fetch bookmarks'); throw new Error('Failed to fetch entries');
} }
const data = await response.json(); const data = await response.json();
if (!Array.isArray(data)) { if (!Array.isArray(data)) {
throw new Error('Invalid bookmarks data'); throw new Error('Invalid entries data');
} }
return data; return data;
} catch (err) { } catch (err) {
console.error(err); console.error(err);
throw new Error('Failed to fetch bookmarks'); throw new Error('Failed to fetch entries');
} }
} }
async function remove(id: number) { async function remove(id: number) {
try { try {
const response = await fetch(`${bookmarksUrl}/${id}`, { const response = await fetch(`${entriesUrl}/${id}`, {
method: 'DELETE', method: 'DELETE',
signal: AbortSignal.timeout(5000), signal: AbortSignal.timeout(5000),
headers: noBodyHeaders, headers: noBodyHeaders,
}); });
if (!response.ok) { if (!response.ok) {
throw new Error('Failed to remove bookmark'); throw new Error('Failed to remove entry');
} }
} catch (err) { } catch (err) {
console.error(err); console.error(err);
throw new Error('Failed to remove bookmark'); throw new Error('Failed to remove entry');
} }
} }
async function update(id: number, body: { name: string; href: string }) { async function update(id: number, body: { name: string; href: string }) {
try { try {
const response = await fetch(`${bookmarksUrl}/${id}`, { const response = await fetch(`${entriesUrl}/${id}`, {
method: 'PUT', method: 'PUT',
body: JSON.stringify(body), body: JSON.stringify(body),
signal: AbortSignal.timeout(5000), signal: AbortSignal.timeout(5000),
@ -72,45 +71,45 @@ async function update(id: number, body: { name: string; href: string }) {
}); });
if (!response.ok) { if (!response.ok) {
throw new Error('Failed to update bookmark'); throw new Error('Failed to update entry');
} }
} catch (err) { } catch (err) {
console.error(err); console.error(err);
throw new Error('Failed to update bookmark'); throw new Error('Failed to update entry');
} }
} }
async function check(id: number, finished: boolean) { async function check(id: number, finished: boolean) {
try { try {
const response = await fetch(`${bookmarksUrl}/${id}/check/${finished}`, { const response = await fetch(`${entriesUrl}/${id}/check/${finished}`, {
method: 'PUT', method: 'PUT',
signal: AbortSignal.timeout(5000), signal: AbortSignal.timeout(5000),
headers: noBodyHeaders, headers: noBodyHeaders,
}); });
if (!response.ok) { if (!response.ok) {
throw new Error('Failed to check bookmark'); throw new Error('Failed to check entry');
} }
} catch (err) { } catch (err) {
console.error(err); console.error(err);
throw new Error('Failed to check bookmark'); throw new Error('Failed to check entry');
} }
} }
async function create(body: { name: string; href: string }) { async function create(body: { name: string; href: string }) {
try { try {
const response = await fetch(`${bookmarksUrl}`, { const response = await fetch(`${entriesUrl}`, {
method: 'POST', method: 'POST',
body: JSON.stringify(body), body: JSON.stringify(body),
signal: AbortSignal.timeout(5000), signal: AbortSignal.timeout(5000),
headers: bodyHeaders, headers: bodyHeaders,
}); });
if (!response.ok) { if (!response.ok) {
throw new Error('Failed to create bookmark'); throw new Error('Failed to create entry');
} }
} catch (err) { } catch (err) {
if (err) { if (err) {
console.error(err); console.error(err);
throw new Error('Failed to create bookmark'); throw new Error('Failed to create entry');
} }
} }
} }

View file

@ -1,15 +1,7 @@
import { userstate } from './shared.svelte'; import { userstate } from './shared.svelte';
import type { SelectEntry } from './server/pt-api';
type SelectBookmark = { export async function fetchEntries(): Promise<SelectEntry[]> {
id: number;
name: string;
href: string;
finished: boolean;
created_at: number;
updated_at: number;
};
export async function fetchEntries(): Promise<SelectBookmark[]> {
try { try {
const response = await fetch('/api/entries', { const response = await fetch('/api/entries', {
method: 'GET', method: 'GET',