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)}
<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>
{/each}

View file

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