adds svelte API

This commit is contained in:
Christian Beutel
2024-03-09 16:46:02 +01:00
parent 33c3b273ab
commit 1b493f576e
114 changed files with 1574 additions and 2528 deletions

View File

@@ -1,5 +1,6 @@
import { regenerateInstance } from "$lib/meilisearch";
import { pb } from "$lib/pocketbase";
import { ClientResponseError } from "pocketbase";
import { writable, type Writable } from "svelte/store";
export type User = {
@@ -10,20 +11,38 @@ export type User = {
avatar?: string;
unit?: "metric" | "imperial";
language?: "en" | "de";
location?: {name: string, lat: number, lon: number}
location?: { name: string, lat: number, lon: number }
}
export const currentUser: Writable<User | null> = writable<User | null>()
export async function users_create(user: User) {
const model = await pb.collection('users').create({ ...user, passwordConfirm: user.password });
const r = await fetch('/api/v1/user', {
method: 'PUT',
body: JSON.stringify({ ...user, passwordConfirm: user.password })
})
if (r.ok) {
return await r.json();
} else {
throw new ClientResponseError(await r.json())
}
return model;
}
export async function login(user: User) {
const authData = await pb.collection('users').authWithPassword(user.email ?? user.username!, user.password);
regenerateInstance()
const r = await fetch('/api/v1/auth/login', {
method: 'POST',
body: JSON.stringify(user),
})
if (r.ok) {
pb.authStore.loadFromCookie(document.cookie)
regenerateInstance()
} else {
throw new ClientResponseError(await r.json())
}
}
export async function logout() {
@@ -32,15 +51,27 @@ export async function logout() {
}
export async function users_update(id: string, user: User | { [K in keyof User]?: User[K] } | FormData) {
let model = await pb
.collection("users")
.update<User>(id, user);
const r = await fetch('/api/v1/user', {
method: 'POST',
body: JSON.stringify({ id: id, user: user })
})
currentUser.set(model);
if (r.ok) {
const model = await r.json();
currentUser.set(model);
} else {
throw new ClientResponseError(await r.json())
}
}
export async function users_delete(user: User) {
let success = await pb
.collection("users")
.delete(user.id);
const r = await fetch('/api/v1/user', {
method: 'DELETE',
body: JSON.stringify({ id: user.id })
})
if (!r.ok) {
throw new ClientResponseError(await r.json())
}
}